diff --git a/core/main/client/net.js b/core/main/client/net.js index fd03fc885..57bb44fac 100644 --- a/core/main/client/net.js +++ b/core/main/client/net.js @@ -514,6 +514,35 @@ beef.net = { return false; }, + /** + * Checks if the specified port is valid + */ + is_valid_port: function (port) { + if (isNaN(port)) return false; + if (port > 65535 || port < 0) return false; + return true; + }, + + /** + * Checks if the specified IP address is valid + */ + is_valid_ip: function (ip) { + if (ip == null) return false; + var ip_match = ip.match('^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$'); + if (ip_match == null) return false; + return true; + }, + + /** + * Checks if the specified IP address range is valid + */ + is_valid_ip_range: function (ip_range) { + if (ip_range == null) return false; + var range_match = ip_range.match('^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\-([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$'); + if (range_match == null || range_match[1] == null) return false; + return true; + }, + /** * Sends back browser details to framework, calling beef.browser.getDetails() */ diff --git a/modules/exploits/extract_cmd_exec/command.js b/modules/exploits/extract_cmd_exec/command.js index d7482b3c9..101596259 100644 --- a/modules/exploits/extract_cmd_exec/command.js +++ b/modules/exploits/extract_cmd_exec/command.js @@ -20,11 +20,11 @@ beef.execute(function() { } // validate target details - if (!rport || !rhost || isNaN(rport)) { + if (!rport || !rhost) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed remote host or remote port'); return; } - if (rport > 65535 || rport < 0) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid remote port'); return; } diff --git a/modules/exploits/groovyshell_server_cmd_exec/command.js b/modules/exploits/groovyshell_server_cmd_exec/command.js index 750875a47..e29105928 100644 --- a/modules/exploits/groovyshell_server_cmd_exec/command.js +++ b/modules/exploits/groovyshell_server_cmd_exec/command.js @@ -20,11 +20,11 @@ beef.execute(function() { } // validate target details - if (!rport || !rhost || isNaN(rport)) { + if (!rport || !rhost) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed remote host or remote port'); return; } - if (rport > 65535 || rport < 0) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid remote port'); return; } diff --git a/modules/exploits/qnx_qconn_command_execution/command.js b/modules/exploits/qnx_qconn_command_execution/command.js index a221b4d68..b32dbd888 100644 --- a/modules/exploits/qnx_qconn_command_execution/command.js +++ b/modules/exploits/qnx_qconn_command_execution/command.js @@ -20,11 +20,11 @@ beef.execute(function() { } // validate target details - if (!rport || !rhost || isNaN(rport)) { + if (!rport || !rhost) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed remote host or remote port'); return; } - if (rport > 65535 || rport < 0) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid remote port'); return; } diff --git a/modules/exploits/ruby_nntpd_cmd_exec/command.js b/modules/exploits/ruby_nntpd_cmd_exec/command.js index 770fc0c9c..8207804cd 100644 --- a/modules/exploits/ruby_nntpd_cmd_exec/command.js +++ b/modules/exploits/ruby_nntpd_cmd_exec/command.js @@ -20,11 +20,11 @@ beef.execute(function() { } // validate target details - if (!rport || !rhost || isNaN(rport)) { + if (!rport || !rhost) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed remote host or remote port'); return; } - if (rport > 65535 || rport < 0) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid remote port'); return; } diff --git a/modules/ipec/cross_site_printing/command.js b/modules/ipec/cross_site_printing/command.js index ccd1c5a37..662ff76a6 100644 --- a/modules/ipec/cross_site_printing/command.js +++ b/modules/ipec/cross_site_printing/command.js @@ -46,9 +46,9 @@ beef.execute(function() { } // validate target - if (!target_port || !target_ip || isNaN(target_port)) { + if (!target_port || !target_ip) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed target host or target port'); - } else if (target_port > 65535 || target_port < 0) { + } else if (!beef.net.is_valid_port(target_port)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid target port'); // send request and wait for reply } else { diff --git a/modules/ipec/inter_protocol_posix_bindshell/command.js b/modules/ipec/inter_protocol_posix_bindshell/command.js index 721624107..9277025e6 100644 --- a/modules/ipec/inter_protocol_posix_bindshell/command.js +++ b/modules/ipec/inter_protocol_posix_bindshell/command.js @@ -81,11 +81,10 @@ beef.execute(function() { } // validate target - if (!target_port || !target_ip || isNaN(target_port)) { + if (!target_port || !target_ip) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=malformed target host or target port'); - } else if (target_port > 65535 || target_port < 0) { + } else if (!beef.net.is_valid_port(target_port)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid target port'); - // send request and wait for reply } else { send_cmds(target_ip, target_port, cmd,result_size); diff --git a/modules/ipec/inter_protocol_redis/command.js b/modules/ipec/inter_protocol_redis/command.js index 00bab0dfa..13ed3a204 100644 --- a/modules/ipec/inter_protocol_redis/command.js +++ b/modules/ipec/inter_protocol_redis/command.js @@ -23,7 +23,7 @@ beef.execute(function() { // validate target port var rport = "<%= @rport %>"; - if (!rport || rport > 65535 || rport < 0 || isNaN(rport)) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid target port'); return; } diff --git a/modules/ipec/inter_protocol_win_bindshell/command.js b/modules/ipec/inter_protocol_win_bindshell/command.js index df02b4d27..6da33138a 100644 --- a/modules/ipec/inter_protocol_win_bindshell/command.js +++ b/modules/ipec/inter_protocol_win_bindshell/command.js @@ -23,7 +23,7 @@ beef.execute(function() { // validate target port var rport = "<%= @rport %>"; - if (!rport || rport > 65535 || rport < 0 || isNaN(rport)) { + if (!beef.net.is_valid_port(rport)) { beef.net.send('<%= @command_url %>', <%= @command_id %>, 'fail=invalid target port'); return; } diff --git a/modules/network/get_http_servers/command.js b/modules/network/get_http_servers/command.js index 88acb660d..4c2ef2c79 100644 --- a/modules/network/get_http_servers/command.js +++ b/modules/network/get_http_servers/command.js @@ -31,16 +31,11 @@ beef.execute(function() { } // set target ports - var is_valid_port = function(port) { - if (isNaN(port)) return false; - if (port > 65535 || port < 0) return false; - return true; - } ports = ports.split(','); var target_ports = new Array(); for (var i=0; i