diff --git a/core/main/handlers/browserdetails.rb b/core/main/handlers/browserdetails.rb index fab4d2ea20..a74ddbc452 100644 --- a/core/main/handlers/browserdetails.rb +++ b/core/main/handlers/browserdetails.rb @@ -180,8 +180,7 @@ def setup() if config.get("beef.extension.network.enable") == true if proxy_server =~ /^([\d\.]+):([\d]+)$/ print_debug("Hooked browser [id:#{zombie.id}] is using a proxy [ip: #{$1}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => $1, :type => 'Proxy', :cid => 'init') - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => $1, :type => 'Proxy', :cid => 'init') end end end @@ -354,8 +353,7 @@ def setup() # add localhost as network host if config.get('beef.extension.network.enable') print_debug("Hooked browser has network interface 127.0.0.1") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => '127.0.0.1', :hostname => 'localhost', :os => BeEF::Core::Models::BrowserDetails.get(session_id, 'OsName'), :cid => 'init') - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => '127.0.0.1', :hostname => 'localhost', :os => BeEF::Core::Models::BrowserDetails.get(session_id, 'OsName'), :cid => 'init') end # Call autorun modules diff --git a/modules/exploits/router/asus_rt_series_get_info/module.rb b/modules/exploits/router/asus_rt_series_get_info/module.rb index 5b729ce069..69c7b97c25 100644 --- a/modules/exploits/router/asus_rt_series_get_info/module.rb +++ b/modules/exploits/router/asus_rt_series_get_info/module.rb @@ -31,10 +31,8 @@ def post_execute if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found Asus RT series router [ip: #{ip}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :type => 'Asus Router', :cid => cid) - r.save - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => 'http', :ip => ip, :port => 80, :type => 'HTTP Server', :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :type => 'Asus Router', :cid => cid) + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => 'http', :ip => ip, :port => 80, :type => 'HTTP Server', :cid => cid) end clients.scan(/([\d\.]+,[:\dA-F]{17})/).flatten.each do |client| next if client.nil? @@ -43,22 +41,19 @@ def post_execute mac = $2 if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found router client [ip: #{ip}, mac: #{mac}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :mac => mac, :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :mac => mac, :cid => cid) end end end if !gateway.nil? && BeEF::Filters.is_valid_ip?(gateway) print_debug("Hooked browser found WAN gateway server [ip: #{gateway}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => gateway, :type => 'WAN Gateway', :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => gateway, :type => 'WAN Gateway', :cid => cid) end if !dns_servers.nil? && dns_servers =~ /^([\d\. ]+)$/ dns_servers.split(/ /).uniq.each do |dns| if BeEF::Filters.is_valid_ip?(dns) print_debug("Hooked browser found DNS server [ip: #{dns}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => dns, :type => 'DNS Server', :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => dns, :type => 'DNS Server', :cid => cid) end end end diff --git a/modules/host/detect_cups/module.rb b/modules/host/detect_cups/module.rb index 1a0398a435..6aa6f6d540 100644 --- a/modules/host/detect_cups/module.rb +++ b/modules/host/detect_cups/module.rb @@ -25,14 +25,9 @@ def post_execute session_id = @datastore['beefhook'] type = 'CUPS' cid = @datastore['cid'].to_i - if BeEF::Filters.is_valid_ip?(ip) && BeEF::Core::Models::NetworkService.all(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type).empty? + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found 'CUPS' [proto: #{proto}, ip: #{ip}, port: #{port}]") - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type, :cid => cid) - r.save - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type, :cid => cid) end end end diff --git a/modules/host/get_internal_ip/module.rb b/modules/host/get_internal_ip/module.rb index 85462c2475..928e22e4f5 100755 --- a/modules/host/get_internal_ip/module.rb +++ b/modules/host/get_internal_ip/module.rb @@ -30,10 +30,9 @@ def post_execute # save the network host if @datastore['results'] =~ /^([\d\.]+)$/ ip = $1 - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? # prevent duplicates + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser has network interface #{ip}") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :cid => cid) end end end diff --git a/modules/host/get_internal_ip_webrtc/module.rb b/modules/host/get_internal_ip_webrtc/module.rb index cf6e7f009a..a9fe64659c 100755 --- a/modules/host/get_internal_ip_webrtc/module.rb +++ b/modules/host/get_internal_ip_webrtc/module.rb @@ -12,10 +12,8 @@ def post_execute configuration = BeEF::Core::Configuration.instance if configuration.get("beef.extension.network.enable") == true - session_id = @datastore['beefhook'] cid = @datastore['cid'].to_i - # save the network host if @datastore['results'] =~ /IP is ([\d\.,]+)/ ips = $1.to_s.split(/,/) @@ -25,16 +23,12 @@ def post_execute next unless ip =~ /^[\d\.]+$/ next if ip =~ /^0\.0\.0\.0$/ next unless BeEF::Filters.is_valid_ip?(ip) - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? # prevent duplicates - print_debug("Hooked browser has network interface #{ip}") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :os => os, :cid => cid) - r.save - end + print_debug("Hooked browser has network interface #{ip}") + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :os => os, :cid => cid) end end end end - end end diff --git a/modules/network/cross_origin_scanner/module.rb b/modules/network/cross_origin_scanner/module.rb index e9c6216eb7..91992ec2d3 100644 --- a/modules/network/cross_origin_scanner/module.rb +++ b/modules/network/cross_origin_scanner/module.rb @@ -22,14 +22,9 @@ def post_execute port = $2 proto = 'http' type = 'HTTP Server (CORS)' - print_debug("Hooked browser found HTTP server #{ip}:#{port}") - if !ip.nil? && !port.nil? && BeEF::Filters.is_valid_ip?(ip) && BeEF::Core::Models::NetworkService.all(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type).empty? - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type, :cid => cid) - r.save - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + if BeEF::Filters.is_valid_ip?(ip) + print_debug("Hooked browser found HTTP server #{ip}:#{port}") + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => type, :cid => cid) end end end diff --git a/modules/network/get_http_servers/module.rb b/modules/network/get_http_servers/module.rb index 2b0a6fd2d7..ff0cb4b692 100644 --- a/modules/network/get_http_servers/module.rb +++ b/modules/network/get_http_servers/module.rb @@ -35,12 +35,7 @@ def post_execute cid = @datastore['cid'].to_i if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found HTTP Server [proto: #{proto}, ip: #{ip}, port: #{port}]") - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => "HTTP Server", :cid => cid) - r.save - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => "HTTP Server", :cid => cid) end end diff --git a/modules/network/identify_lan_subnets/module.rb b/modules/network/identify_lan_subnets/module.rb index 95e1994bcb..a089d668ce 100644 --- a/modules/network/identify_lan_subnets/module.rb +++ b/modules/network/identify_lan_subnets/module.rb @@ -37,11 +37,8 @@ def post_execute next if ip.nil? next unless ip.to_s =~ /^([\d\.]+)$/ next unless BeEF::Filters.is_valid_ip?(ip) - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? # prevent duplicates - print_debug("Hooked browser found host #{ip}") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + print_debug("Hooked browser found host #{ip}") + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :cid => cid) end end end diff --git a/modules/network/internal_network_fingerprinting/module.rb b/modules/network/internal_network_fingerprinting/module.rb index ff1d2fb3d5..acd9e5aa1f 100644 --- a/modules/network/internal_network_fingerprinting/module.rb +++ b/modules/network/internal_network_fingerprinting/module.rb @@ -35,14 +35,9 @@ def post_execute url = $5 session_id = @datastore['beefhook'] cid = @datastore['cid'].to_i - if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found '#{discovered}' [ip: #{ip}]") - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => discovered, :cid => cid) - r.save - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => discovered, :cid => cid) end end diff --git a/modules/network/jslanscanner/module.rb b/modules/network/jslanscanner/module.rb index 7b1c5dd288..42c424d2f8 100644 --- a/modules/network/jslanscanner/module.rb +++ b/modules/network/jslanscanner/module.rb @@ -25,20 +25,18 @@ def post_execute service = $4 session_id = @datastore['beefhook'] cid = @datastore['cid'].to_i - if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) && BeEF::Core::Models::NetworkService.all(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service).empty? + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found network service " + service + " [proto: #{proto}, ip: #{ip}, port: #{port}]") - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service, :cid => cid) - r.save + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service, :cid => cid) end elsif @datastore['results'] =~ /^ip=(.+)&device=(.+)/ ip = $1 device = $2 session_id = @datastore['beefhook'] cid = @datastore['cid'].to_i - if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) && BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip, :type => device).empty? + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found network device " + device + " [ip: #{ip}]") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :type => device, :cid => cid) - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :type => device, :cid => cid) end end end diff --git a/modules/network/ping_sweep/module.rb b/modules/network/ping_sweep/module.rb index 3754e5fb28..1879df51d2 100644 --- a/modules/network/ping_sweep/module.rb +++ b/modules/network/ping_sweep/module.rb @@ -37,11 +37,8 @@ def post_execute if @datastore['results'] =~ /host=([\d\.]+) is alive/ ip = $1 if BeEF::Filters.is_valid_ip?(ip) - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? # prevent duplicates - print_debug("Hooked browser has network interface #{ip}") - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + print_debug("Hooked browser has network interface #{ip}") + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => session_id, :ip => ip, :cid => cid) end end end diff --git a/modules/network/port_scanner/module.rb b/modules/network/port_scanner/module.rb index 654b532e69..c3817109c8 100644 --- a/modules/network/port_scanner/module.rb +++ b/modules/network/port_scanner/module.rb @@ -39,14 +39,9 @@ def post_execute session_id = @datastore['beefhook'] proto = 'http' cid = @datastore['cid'].to_i - if !ip.nil? && BeEF::Filters.is_valid_ip?(ip) && BeEF::Core::Models::NetworkService.all(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service).empty? + if BeEF::Filters.is_valid_ip?(ip) print_debug("Hooked browser found network service [ip: #{ip}, port: #{port}]") - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service, :cid => cid) - r.save - if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => session_id, :ip => ip).empty? - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => session_id, :ip => ip, :cid => cid) - r.save - end + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => session_id, :proto => proto, :ip => ip, :port => port, :type => service, :cid => cid) end end diff --git a/test/unit/extensions/tc_network.rb b/test/unit/extensions/tc_network.rb index 0416bb5187..21473722fc 100644 --- a/test/unit/extensions/tc_network.rb +++ b/test/unit/extensions/tc_network.rb @@ -28,8 +28,7 @@ def test_01_database # Tests procedure for properly adding new host def test_02_add_host_good assert_nothing_raised do - r = BeEF::Core::Models::NetworkHost.new(:hooked_browser_id => '1234', :ip => '127.0.0.1') - r.save + BeEF::Core::Models::NetworkHost.add(:hooked_browser_id => '1234', :ip => '127.0.0.1') raise "Adding network host failed" if BeEF::Core::Models::NetworkHost.all(:hooked_browser_id => '1234', :ip => '127.0.0.1').empty? end end @@ -37,8 +36,7 @@ def test_02_add_host_good # Tests procedure for properly adding new service def test_03_add_service_good assert_nothing_raised do - r = BeEF::Core::Models::NetworkService.new(:hooked_browser_id => '1234', :proto => 'http', :ip => '127.0.0.1', :port => 80, :type => 'Apache', :cid => 1) - r.save + BeEF::Core::Models::NetworkService.add(:hooked_browser_id => '1234', :proto => 'http', :ip => '127.0.0.1', :port => 80, :type => 'Apache', :cid => 1) raise "Adding network service failed" if BeEF::Core::Models::NetworkService.all(:hooked_browser_id => '1234', :ip => '127.0.0.1').empty? end end