From 32543c6f33a2cd9ed83d49d138ceb4c5b8109988 Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 20 Mar 2024 04:11:39 +1000 Subject: [PATCH 01/35] refactor starting beef server --- spec/beef/api/auth_rate_spec.rb | 250 ++++++++++++++++---------------- 1 file changed, 127 insertions(+), 123 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index ac878b08d..896b47996 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -1,139 +1,143 @@ -# # -# # Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net -# # Browser Exploitation Framework (BeEF) - https://beefproject.com -# # See the file 'doc/COPYING' for copying permission -# # +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# -# RSpec.describe 'BeEF API Rate Limit' do +require 'net/http' +require 'uri' -# before(:all) do -# @config = BeEF::Core::Configuration.instance -# @config.set('beef.credentials.user', "beef") -# @config.set('beef.credentials.passwd', "beef") -# @username = @config.get('beef.credentials.user') -# @password = @config.get('beef.credentials.passwd') +RSpec.describe 'BeEF API Rate Limit' do + + def start_beef_server + @config = BeEF::Core::Configuration.instance + @config.set('beef.credentials.user', "beef") + @config.set('beef.credentials.passwd', "beef") + @username = @config.get('beef.credentials.user') + @password = @config.get('beef.credentials.passwd') -# # Load BeEF extensions and modules -# # Always load Extensions, as previous changes to the config from other tests may affect -# # whether or not this test passes. -# print_info "Loading in BeEF::Extensions" -# BeEF::Extensions.load -# sleep 2 + # Load BeEF extensions + # Always load Extensions, as previous changes to the config from other tests may affect + # whether or not this test passes. + BeEF::Extensions.load -# # Check if modules already loaded. No need to reload. -# if @config.get('beef.module').nil? -# print_info "Loading in BeEF::Modules" -# BeEF::Modules.load + # Load BeEF modules + # Check if modules already loaded. No need to reload. + if @config.get('beef.module').nil? + BeEF::Modules.load + else + print_info "Modules already loaded" + end -# sleep 2 -# else -# print_info "Modules already loaded" -# end + # Grab DB file and regenerate if requested + db_file = @config.get('beef.database.file') -# # Grab DB file and regenerate if requested -# print_info "Loading database" -# db_file = @config.get('beef.database.file') + if BeEF::Core::Console::CommandLine.parse[:resetdb] + File.delete(db_file) if File.exist?(db_file) + end -# if BeEF::Core::Console::CommandLine.parse[:resetdb] -# print_info 'Resetting the database for BeEF.' -# File.delete(db_file) if File.exist?(db_file) -# end + # Load up DB and migrate if necessary + ActiveRecord::Base.logger = nil + OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] + OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file) + # otr-activerecord require you to manually establish the connection with the following line + #Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems. + if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2') + OTR::ActiveRecord.establish_connection! + end -# # Load up DB and migrate if necessary -# ActiveRecord::Base.logger = nil -# OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] -# OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file) -# # otr-activerecord require you to manually establish the connection with the following line -# #Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems. -# if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2') -# OTR::ActiveRecord.establish_connection! -# end + # Migrate (if required) + ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages + context = ActiveRecord::Migration.new.migration_context + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate + end + BeEF::Core::Migration.instance.update_db! -# # Migrate (if required) -# ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages -# context = ActiveRecord::Migration.new.migration_context -# if context.needs_migration? -# ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate -# end + # Spawn HTTP Server + # print_info "Starting HTTP Hook Server" + http_hook_server = BeEF::Core::Server.instance + http_hook_server.prepare -# sleep 2 + # Generate a token for the server to respond with + BeEF::Core::Crypto::api_token -# BeEF::Core::Migration.instance.update_db! + # Initiate server start-up + BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server) + pid = fork do + http_hook_server.start + end -# # Spawn HTTP Server -# print_info "Starting HTTP Hook Server" -# http_hook_server = BeEF::Core::Server.instance -# http_hook_server.prepare + return pid + end -# # Generate a token for the server to respond with -# BeEF::Core::Crypto::api_token + def beef_server_running?(uri_str) + uri = URI.parse(uri_str) + response = Net::HTTP.get_response(uri) + response.is_a?(Net::HTTPSuccess) + rescue + false + end + + def wait_for_beef_server_to_start(uri_str, timeout: 30) + start_time = Time.now + + until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do + sleep 0.1 + end + + beef_server_running?(uri_str) + end + + def start_beef_server_and_wait + pid = start_beef_server + + if wait_for_beef_server_to_start('http://localhost:3000', timeout: 30) + # print_info "Server started successfully." + else + print_info "Server failed to start within timeout." + end + + pid + end + + before(:all) do + @pid = start_beef_server_and_wait + end -# # Initiate server start-up -# @pids = fork do -# BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server) -# end -# @pid = fork do -# http_hook_server.start -# end - -# # Give the server time to start-up -# sleep 3 - -# # Try to connect 3 times -# (0..2).each do |again| -# # Authenticate to REST API & pull the token from the response -# if @response.nil? -# print_info "Try to connect: " + again.to_s -# begin -# creds = { 'username': "#{@username}", 'password': "#{@password}" }.to_json -# @response = RestClient.post "#{RESTAPI_ADMIN}/login", creds, :content_type => :json -# rescue RestClient::ServerBrokeConnection, Errno::ECONNREFUSED # likely to be starting up still -# rescue => error -# print_error error.message -# end -# print_info "Rescue: sleep for 10 and try to connect again" -# sleep 10 -# end -# end -# expect(@response) .to be_truthy # confirm the test has connected to the server -# print_info "Connection with server was successful" -# @token = JSON.parse(@response)['token'] -# end - -# after(:all) do -# print_info "Shutting down server" -# Process.kill("KILL",@pid) unless @pid.nil? -# Process.kill("KILL",@pids) unless @pid.nil? -# end + after(:all) do + print_info "Shutting down server" + Process.kill("KILL",@pid) unless @pid.nil? + end -# xit 'adheres to auth rate limits' do -# passwds = (1..9).map { |i| "broken_pass"} -# passwds.push BEEF_PASSWD -# apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } -# l = apis.length -# (0..2).each do |again| # multiple sets of auth attempts -# # first pass -- apis in order, valid passwd on 9th attempt -# # subsequent passes apis shuffled -# print_info "Starting authentication attempt sequence #{again + 1}. The valid password is placed randomly among failed attempts." -# (0..50).each do |i| -# test_api = apis[i%l] -# expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) -# end -# # again with more time between calls -- there should be success (1st iteration) -# print_info "Initiating delayed authentication requests to test successful authentication with correct credentials." -# print_info "Delayed requests are made to simulate more realistic login attempts and verify rate limiting." -# (0..(l*2)).each do |i| -# test_api = apis[i%l] -# if (test_api.is_pass?(BEEF_PASSWD)) -# expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed -# else -# expect(test_api.auth()[:payload]).to eql("401 Unauthorized") -# end -# sleep(0.5) -# end -# apis.shuffle! # new order for next iteration -# apis = apis.reverse if (apis[0].is_pass?(BEEF_PASSWD)) # prevent the first from having valid passwd -# end # multiple sets of auth attempts -# end + it 'adheres to auth rate limits' do + passwds = (1..9).map { |i| "broken_pass"} + passwds.push BEEF_PASSWD + apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } + l = apis.length + (0..2).each do |again| # multiple sets of auth attempts + # first pass -- apis in order, valid passwd on 9th attempt + # subsequent passes apis shuffled + print_info "Starting authentication attempt sequence #{again + 1}. The valid password is placed randomly among failed attempts." + (0..50).each do |i| + test_api = apis[i%l] + expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) + end + # again with more time between calls -- there should be success (1st iteration) + print_info "Initiating delayed authentication requests to test successful authentication with correct credentials." + print_info "Delayed requests are made to simulate more realistic login attempts and verify rate limiting." + (0..(l*2)).each do |i| + test_api = apis[i%l] + if (test_api.is_pass?(BEEF_PASSWD)) + expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed + else + expect(test_api.auth()[:payload]).to eql("401 Unauthorized") + end + sleep(0.5) + end + apis.shuffle! # new order for next iteration + apis = apis.reverse if (apis[0].is_pass?(BEEF_PASSWD)) # prevent the first from having valid passwd + end # multiple sets of auth attempts + end -# end +end From 0f216f4e202893905c30b0b27ddcd02b651c4eb0 Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 20 Mar 2024 12:24:23 +1000 Subject: [PATCH 02/35] refactor test cases --- spec/beef/api/auth_rate_spec.rb | 127 +++++++++++++++++++------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index 896b47996..f2d33c3a8 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -9,27 +9,29 @@ require 'uri' RSpec.describe 'BeEF API Rate Limit' do + def configure_beef + @config = BeEF::Core::Configuration.instance + + @config.set('beef.credentials.user', "beef") + @config.set('beef.credentials.passwd', "beef") + + @username = @config.get('beef.credentials.user') + @password = @config.get('beef.credentials.passwd') + end + + def load_beef_extensions_and_modules + # Load BeEF extensions + BeEF::Extensions.load + + # Load BeEF modules only if they are not already loaded + BeEF::Modules.load if @config.get('beef.module').nil? + end + def start_beef_server - @config = BeEF::Core::Configuration.instance - @config.set('beef.credentials.user', "beef") - @config.set('beef.credentials.passwd', "beef") - @username = @config.get('beef.credentials.user') - @password = @config.get('beef.credentials.passwd') - - # Load BeEF extensions - # Always load Extensions, as previous changes to the config from other tests may affect - # whether or not this test passes. - BeEF::Extensions.load + configure_beef + load_beef_extensions_and_modules - # Load BeEF modules - # Check if modules already loaded. No need to reload. - if @config.get('beef.module').nil? - BeEF::Modules.load - else - print_info "Modules already loaded" - end - - # Grab DB file and regenerate if requested + # Grab DB file and regenerate if requested db_file = @config.get('beef.database.file') if BeEF::Core::Console::CommandLine.parse[:resetdb] @@ -79,7 +81,7 @@ RSpec.describe 'BeEF API Rate Limit' do false end - def wait_for_beef_server_to_start(uri_str, timeout: 30) + def wait_for_beef_server_to_start(uri_str, timeout: 5) start_time = Time.now until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do @@ -92,7 +94,7 @@ RSpec.describe 'BeEF API Rate Limit' do def start_beef_server_and_wait pid = start_beef_server - if wait_for_beef_server_to_start('http://localhost:3000', timeout: 30) + if wait_for_beef_server_to_start('http://localhost:3000', timeout: 5) # print_info "Server started successfully." else print_info "Server failed to start within timeout." @@ -106,38 +108,59 @@ RSpec.describe 'BeEF API Rate Limit' do end after(:all) do - print_info "Shutting down server" - Process.kill("KILL",@pid) unless @pid.nil? + # Shutting down server + Process.kill("KILL", @pid) unless @pid.nil? + Process.wait(@pid) # Ensure the process has exited and the port is released + @pid = nil end - - it 'adheres to auth rate limits' do - passwds = (1..9).map { |i| "broken_pass"} - passwds.push BEEF_PASSWD + + it 'confirm correct creds are successful' do + + sleep 0.5 + test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, BEEF_PASSWD) + expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed + + end + + it 'confirm incorrect creds are unsuccessful' do + + sleep 0.5 + test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, "wrong_passowrd") + expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) + end + + it 'adheres to 9 bad passwords then 1 correct auth rate limits' do + + # create api structures with bad passwords and one good + passwds = (1..9).map { |i| "bad_password"} # incorrect password + passwds.push BEEF_PASSWD # correct password apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } - l = apis.length - (0..2).each do |again| # multiple sets of auth attempts - # first pass -- apis in order, valid passwd on 9th attempt - # subsequent passes apis shuffled - print_info "Starting authentication attempt sequence #{again + 1}. The valid password is placed randomly among failed attempts." - (0..50).each do |i| - test_api = apis[i%l] - expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) - end - # again with more time between calls -- there should be success (1st iteration) - print_info "Initiating delayed authentication requests to test successful authentication with correct credentials." - print_info "Delayed requests are made to simulate more realistic login attempts and verify rate limiting." - (0..(l*2)).each do |i| - test_api = apis[i%l] - if (test_api.is_pass?(BEEF_PASSWD)) - expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed - else - expect(test_api.auth()[:payload]).to eql("401 Unauthorized") - end - sleep(0.5) - end - apis.shuffle! # new order for next iteration - apis = apis.reverse if (apis[0].is_pass?(BEEF_PASSWD)) # prevent the first from having valid passwd - end # multiple sets of auth attempts - end + + (0..apis.length-1).each do |i| + test_api = apis[i] + expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) + end + end + + it 'adheres to random bad passords and 1 correct auth rate limits' do + + # create api structures with bad passwords and one good + passwds = (1..9).map { |i| "bad_password"} # incorrect password + passwds.push BEEF_PASSWD # correct password + apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } + + apis.shuffle! # random order for next iteration + apis = apis.reverse if (apis[0].is_pass?(BEEF_PASSWD)) # prevent the first from having valid passwd + + (0..apis.length-1).each do |i| + test_api = apis[i] + if (test_api.is_pass?(BEEF_PASSWD)) + sleep(0.5) + expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed + else + expect(test_api.auth()[:payload]).to eql("401 Unauthorized") + end + end + end end From 55632fe2d6feeffbb31421783913bbcbe777d19e Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 20 Mar 2024 16:43:13 +1000 Subject: [PATCH 03/35] move the proxy banner to banners.rb --- core/main/console/banners.rb | 9 +++++++++ extensions/proxy/api.rb | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/core/main/console/banners.rb b/core/main/console/banners.rb index d7749e163..cbbae5663 100644 --- a/core/main/console/banners.rb +++ b/core/main/console/banners.rb @@ -134,6 +134,15 @@ module BeEF print_info "Starting WebSocketSecure server on wss://[#{config.beef_host}:#{config.get('beef.http.websocket.secure_port').to_i} [timer: #{ws_poll_timeout}]" end end + + # + # Print WebSocket servers + # + def print_http_proxy + config = BeEF::Core::Configuration.instance + print_info "HTTP Proxy: http://#{config.get('beef.extension.proxy.address')}:#{config.get('beef.extension.proxy.port')}" + end + end end end diff --git a/extensions/proxy/api.rb b/extensions/proxy/api.rb index 3f8f5e620..a5f95d7d0 100644 --- a/extensions/proxy/api.rb +++ b/extensions/proxy/api.rb @@ -18,7 +18,6 @@ module BeEF BeEF::Extension::Proxy::Proxy.new end end - print_info "HTTP Proxy: http://#{config.get('beef.extension.proxy.address')}:#{config.get('beef.extension.proxy.port')}" end def self.mount_handler(beef_server) From 9077123d1e6b3463193afb2a54ee49ceabeb2904 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 05:04:12 +1000 Subject: [PATCH 04/35] change to Configuration.instance instead of calling .new --- spec/beef/core/main/command_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/beef/core/main/command_spec.rb b/spec/beef/core/main/command_spec.rb index c060a26b0..0b7d8bc2b 100644 --- a/spec/beef/core/main/command_spec.rb +++ b/spec/beef/core/main/command_spec.rb @@ -1,8 +1,7 @@ RSpec.describe 'BeEF Command class testing' do before(:each) do # Reset or re-initialise the configuration to a default state - config = File.expand_path('../../../support/assets/config_old.yaml', __dir__) - @config_instance = BeEF::Core::Configuration.new(config) + @config_instance = BeEF::Core::Configuration.instance end it 'should return a beef configuration variable' do From a67c791731d92c602ffd2f6048c68d8e8cd941b4 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 05:36:17 +1000 Subject: [PATCH 05/35] clean up QR output --- spec/beef/extensions/qrcode_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/beef/extensions/qrcode_spec.rb b/spec/beef/extensions/qrcode_spec.rb index d06453c60..426e732ab 100644 --- a/spec/beef/extensions/qrcode_spec.rb +++ b/spec/beef/extensions/qrcode_spec.rb @@ -7,7 +7,7 @@ RSpec.describe 'BeEF Extension QRCode' do @config.load_extensions_config end - it 'loads configuration' do + xit 'loads configuration' do config = @config.get('beef.extension.qrcode') expect(config).to have_key('enable') expect(config).to have_key('targets') From 50d03b561959b963a5a1f1a4d8a91ef9f7e97ab0 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 05:36:59 +1000 Subject: [PATCH 06/35] clean up QR output --- extensions/qrcode/qrcode.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/extensions/qrcode/qrcode.rb b/extensions/qrcode/qrcode.rb index 1d66e0be2..e55ccd3d1 100644 --- a/extensions/qrcode/qrcode.rb +++ b/extensions/qrcode/qrcode.rb @@ -32,11 +32,7 @@ module BeEF # Retrieve the list of network interfaces from BeEF::Core::Console::Banners interfaces = BeEF::Core::Console::Banners.interfaces - # Check if the interfaces variable is nil, indicating that network interfaces are not available - if interfaces.nil? - print_error "[QR] Error: Network interfaces information is unavailable." - print_error "[QR] Error: This will be acceptable during testing." - else + if not interfaces.nil? and not interfaces.empty? # If interfaces are available, iterate over each network interface # If interfaces are available, iterate over each network interface interfaces.each do |int| # Skip the loop iteration if the interface address is '0.0.0.0' (which generally represents all IPv4 addresses on the local machine) From 5392867e72b2a4a062ae822854b7bedb5ef44a5c Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 05:44:43 +1000 Subject: [PATCH 07/35] enable QR tests --- spec/beef/extensions/qrcode_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/beef/extensions/qrcode_spec.rb b/spec/beef/extensions/qrcode_spec.rb index 426e732ab..d06453c60 100644 --- a/spec/beef/extensions/qrcode_spec.rb +++ b/spec/beef/extensions/qrcode_spec.rb @@ -7,7 +7,7 @@ RSpec.describe 'BeEF Extension QRCode' do @config.load_extensions_config end - xit 'loads configuration' do + it 'loads configuration' do config = @config.get('beef.extension.qrcode') expect(config).to have_key('enable') expect(config).to have_key('targets') From abdd89eaccf093870cf045f3dacfc0bdd62470c3 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 10:32:31 +1000 Subject: [PATCH 08/35] stop multiple dns servers from starting and add a stop function --- extensions/dns/dns.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extensions/dns/dns.rb b/extensions/dns/dns.rb index c25574028..7a9cabbeb 100644 --- a/extensions/dns/dns.rb +++ b/extensions/dns/dns.rb @@ -118,6 +118,7 @@ module BeEF @lock.synchronize do Thread.new do EventMachine.next_tick do + next if @server_started # Check if the server was already started upstream = options[:upstream] || nil listen = options[:listen] || nil @@ -132,6 +133,7 @@ module BeEF begin # super(:listen => listen) Thread.new { super() } + @server_started = true # Set the server started flag rescue RuntimeError => e if e.message =~ /no datagram socket/ || e.message =~ /no acceptor/ # the port is in use print_error "[DNS] Another process is already listening on port #{options[:listen]}" @@ -146,6 +148,14 @@ module BeEF end end + def stop + return unless @server_started # Check if the server was started + + # Logic to stop the Async::DNS server + puts EventMachine.stop if EventMachine.reactor_running? + @server_started = false # Reset the server started flag + end + # Entry point for processing incoming DNS requests. Attempts to find a matching rule and # sends back its associated response. # From 8e67dab667fa05b65b9f003d86a604443ff4ae06 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 10:48:33 +1000 Subject: [PATCH 09/35] set dns logger level to ERROR only --- extensions/dns/dns.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/dns/dns.rb b/extensions/dns/dns.rb index 7a9cabbeb..9693d0a12 100644 --- a/extensions/dns/dns.rb +++ b/extensions/dns/dns.rb @@ -15,6 +15,7 @@ module BeEF def initialize super() + logger.level = Logger::ERROR @lock = Mutex.new @database = BeEF::Core::Models::Dns::Rule @data_chunks = {} From 86cf8bcfe5aee224a5b0eba72145f38a044a7eb6 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 11:02:10 +1000 Subject: [PATCH 10/35] added shutdown for the dns server --- spec/beef/extensions/dns_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/beef/extensions/dns_spec.rb b/spec/beef/extensions/dns_spec.rb index 0881b673d..91bb1634e 100644 --- a/spec/beef/extensions/dns_spec.rb +++ b/spec/beef/extensions/dns_spec.rb @@ -11,6 +11,11 @@ RSpec.describe 'BeEF Extension DNS' do @dns = BeEF::Extension::Dns::Server.instance end + after(:all) do + # Stop the DNS server after each test case + BeEF::Extension::Dns::Server.instance.stop # this might not be needed? + end + it 'loaded configuration' do config = @config.get('beef.extension.dns') expect(config).to have_key('protocol') From 2d54cde54d2c72fffc677935a2425ce34398bcf0 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 11:33:27 +1000 Subject: [PATCH 11/35] basic format change --- spec/support/simple_rest_client.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/support/simple_rest_client.rb b/spec/support/simple_rest_client.rb index 3d2f723ff..d09b3b6f8 100644 --- a/spec/support/simple_rest_client.rb +++ b/spec/support/simple_rest_client.rb @@ -28,6 +28,7 @@ class BeefRestClient rescue StandardError => e { success: false, payload: e.message } end + def version return { success: false, payload: 'no token' } if @token.nil? From f978b5fc4fd0e2df035c69304469c6987edb0b75 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 11:47:45 +1000 Subject: [PATCH 12/35] moved testing server functions to spec_helper.rb --- spec/beef/api/auth_rate_spec.rb | 138 +++++++------------------------- spec/spec_helper.rb | 118 ++++++++++++++++++++++++++- 2 files changed, 147 insertions(+), 109 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index f2d33c3a8..06f36a745 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -8,124 +8,43 @@ require 'net/http' require 'uri' RSpec.describe 'BeEF API Rate Limit' do - - def configure_beef - @config = BeEF::Core::Configuration.instance - - @config.set('beef.credentials.user', "beef") - @config.set('beef.credentials.passwd', "beef") - + + before(:each) do + port = 3000 + expect(`lsof -i :#{port}`).to be_empty + @pid = start_beef_server_and_wait @username = @config.get('beef.credentials.user') @password = @config.get('beef.credentials.passwd') - end - - def load_beef_extensions_and_modules - # Load BeEF extensions - BeEF::Extensions.load - - # Load BeEF modules only if they are not already loaded - BeEF::Modules.load if @config.get('beef.module').nil? - end - - def start_beef_server - configure_beef - load_beef_extensions_and_modules - - # Grab DB file and regenerate if requested - db_file = @config.get('beef.database.file') - - if BeEF::Core::Console::CommandLine.parse[:resetdb] - File.delete(db_file) if File.exist?(db_file) - end - - # Load up DB and migrate if necessary - ActiveRecord::Base.logger = nil - OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] - OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file) - # otr-activerecord require you to manually establish the connection with the following line - #Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems. - if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2') - OTR::ActiveRecord.establish_connection! - end - - # Migrate (if required) - ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages - context = ActiveRecord::Migration.new.migration_context - if context.needs_migration? - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate - end - BeEF::Core::Migration.instance.update_db! - - # Spawn HTTP Server - # print_info "Starting HTTP Hook Server" - http_hook_server = BeEF::Core::Server.instance - http_hook_server.prepare - - # Generate a token for the server to respond with - BeEF::Core::Crypto::api_token - - # Initiate server start-up - BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server) - pid = fork do - http_hook_server.start - end - - return pid - end - - def beef_server_running?(uri_str) - uri = URI.parse(uri_str) - response = Net::HTTP.get_response(uri) - response.is_a?(Net::HTTPSuccess) - rescue - false - end - - def wait_for_beef_server_to_start(uri_str, timeout: 5) - start_time = Time.now - - until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do - sleep 0.1 - end - - beef_server_running?(uri_str) - end - - def start_beef_server_and_wait - pid = start_beef_server - - if wait_for_beef_server_to_start('http://localhost:3000', timeout: 5) - # print_info "Server started successfully." - else - print_info "Server failed to start within timeout." - end - - pid - end - - before(:all) do - @pid = start_beef_server_and_wait end - after(:all) do + after(:each) do + # Stop the DNS server after each test case + BeEF::Extension::Dns::Server.instance.stop # Shutting down server Process.kill("KILL", @pid) unless @pid.nil? - Process.wait(@pid) # Ensure the process has exited and the port is released + Process.wait(@pid) unless @pid.nil? # Ensure the process has exited and the port is released @pid = nil end it 'confirm correct creds are successful' do - sleep 0.5 - test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, BEEF_PASSWD) + # sleep 1 + # uri = URI.parse("http://#{ATTACK_DOMAIN}:3000") + # response = Net::HTTP.get_response(uri) + # expect(response).to be_a(Net::HTTPSuccess) # HTTP request is successful + + # sleep 60 + test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', @username, @password) + expect(@config.get('beef.credentials.user')).to eq('beef') + expect(@config.get('beef.credentials.passwd')).to eq('beef') + expect(test_api.auth()[:payload]).not_to eql("401 Unauthorized") expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed end it 'confirm incorrect creds are unsuccessful' do - sleep 0.5 - test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, "wrong_passowrd") + test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', @username, "wrong_passowrd") expect(test_api.auth()[:payload]).to eql("401 Unauthorized") # all (unless the valid is first 1 in 10 chance) end @@ -133,8 +52,8 @@ RSpec.describe 'BeEF API Rate Limit' do # create api structures with bad passwords and one good passwds = (1..9).map { |i| "bad_password"} # incorrect password - passwds.push BEEF_PASSWD # correct password - apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } + passwds.push @password # correct password + apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', @username, pswd) } (0..apis.length-1).each do |i| test_api = apis[i] @@ -146,16 +65,19 @@ RSpec.describe 'BeEF API Rate Limit' do # create api structures with bad passwords and one good passwds = (1..9).map { |i| "bad_password"} # incorrect password - passwds.push BEEF_PASSWD # correct password - apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, pswd) } + passwds.push @password # correct password + apis = passwds.map { |pswd| BeefRestClient.new('http', ATTACK_DOMAIN, '3000', @username, pswd) } apis.shuffle! # random order for next iteration - apis = apis.reverse if (apis[0].is_pass?(BEEF_PASSWD)) # prevent the first from having valid passwd + apis = apis.reverse if (apis[0].is_pass?(@password)) # prevent the first from having valid passwd (0..apis.length-1).each do |i| test_api = apis[i] - if (test_api.is_pass?(BEEF_PASSWD)) - sleep(0.5) + if (test_api.is_pass?(@password)) + sleep 0.5 + expect(@config.get('beef.credentials.user')).to eq('beef') + expect(@config.get('beef.credentials.passwd')).to eq('beef') + expect(test_api.auth()[:payload]).not_to eql("401 Unauthorized") expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed else expect(test_api.auth()[:payload]).to eql("401 Unauthorized") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2216e6f87..320587ed8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,7 +21,7 @@ Dir['spec/support/*.rb'].each do |f| require f end -ENV['RACK_ENV'] ||= 'test' +ENV['RACK_ENV'] ||= 'test' # Set the environment to test ARGV.clear ## BrowserStack config @@ -89,4 +89,120 @@ RSpec.configure do |config| Process.kill('KILL', server_pid) Process.kill('KILL', server_pids) end + + + +######################################## + +require 'socket' + + def port_available? + socket = TCPSocket.new('localhost', 3000) + socket.close + false # If a connection is made, the port is in use, so it's not available. + rescue Errno::ECONNREFUSED + true # If the connection is refused, the port is not in use, so it's available. + rescue Errno::EADDRNOTAVAIL + true # If the connection is refused, the port is not in use, so it's available. + end + + + def configure_beef + + # Reset or re-initialise the configuration to a default state + @config = BeEF::Core::Configuration.instance + + @config.set('beef.credentials.user', "beef") + @config.set('beef.credentials.passwd', "beef") + + @username = @config.get('beef.credentials.user') + @password = @config.get('beef.credentials.passwd') + end + + # Load the server + def load_beef_extensions_and_modules + # Load BeEF extensions + BeEF::Extensions.load + + # Load BeEF modules only if they are not already loaded + BeEF::Modules.load if @config.get('beef.module').nil? + end + + def start_beef_server + exit unless port_available? + configure_beef + load_beef_extensions_and_modules + + # Grab DB file and regenerate if requested + db_file = @config.get('beef.database.file') + + if BeEF::Core::Console::CommandLine.parse[:resetdb] + File.delete(db_file) if File.exist?(db_file) + end + + # Load up DB and migrate if necessary + ActiveRecord::Base.logger = nil + OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] + OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file) + # otr-activerecord require you to manually establish the connection with the following line + #Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems. + if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2') + OTR::ActiveRecord.establish_connection! + end + + # Migrate (if required) + ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages + context = ActiveRecord::Migration.new.migration_context + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate + end + BeEF::Core::Migration.instance.update_db! + + # Spawn HTTP Server + # print_info "Starting HTTP Hook Server" + http_hook_server = BeEF::Core::Server.instance + http_hook_server.prepare + + # Generate a token for the server to respond with + BeEF::Core::Crypto::api_token + + # Initiate server start-up + BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server) + pid = fork do + http_hook_server.start + end + + return pid + end + + def beef_server_running?(uri_str) + uri = URI.parse(uri_str) + response = Net::HTTP.get_response(uri) + response.is_a?(Net::HTTPSuccess) + rescue + false + end + + def wait_for_beef_server_to_start(uri_str, timeout: 5) + start_time = Time.now + + until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do + sleep 0.1 + end + + beef_server_running?(uri_str) + end + + def start_beef_server_and_wait + pid = start_beef_server + + if wait_for_beef_server_to_start('http://localhost:3000', timeout: 5) + # print_info "Server started successfully." + else + print_error "Server failed to start within timeout." + end + + pid + end + end From a41e2bb637e8f9c9c230f4495645c9854bcb850b Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 22 Mar 2024 15:19:20 +1000 Subject: [PATCH 13/35] fix typo --- core/main/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/main/configuration.rb b/core/main/configuration.rb index 7a3f73735..36b021991 100644 --- a/core/main/configuration.rb +++ b/core/main/configuration.rb @@ -156,7 +156,7 @@ module BeEF "#{beef_proto}://#{beef_host}:#{beef_port}" end - # Returns the hool path value stored in the config file + # Returns the hook path value stored in the config file # # @return [String] hook file path def hook_file_path From 52abaa32f28abd7cc7a0259d483465e35866153e Mon Sep 17 00:00:00 2001 From: Stephen Date: Sun, 24 Mar 2024 16:59:38 +1000 Subject: [PATCH 14/35] fixed @server_started file init --- extensions/dns/dns.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/dns/dns.rb b/extensions/dns/dns.rb index 9693d0a12..2f460a84b 100644 --- a/extensions/dns/dns.rb +++ b/extensions/dns/dns.rb @@ -19,6 +19,7 @@ module BeEF @lock = Mutex.new @database = BeEF::Core::Models::Dns::Rule @data_chunks = {} + @server_started = false end # Adds a new DNS rule. If the rule already exists, its current ID is returned. From 88f383baaec9375679b2f25416c60f491e23bcd1 Mon Sep 17 00:00:00 2001 From: Stephen Date: Sun, 24 Mar 2024 18:14:19 +1000 Subject: [PATCH 15/35] removed old tests --- spec/beef/core/main/configuration_spec.rb | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/spec/beef/core/main/configuration_spec.rb b/spec/beef/core/main/configuration_spec.rb index 315bb3740..19a07f237 100644 --- a/spec/beef/core/main/configuration_spec.rb +++ b/spec/beef/core/main/configuration_spec.rb @@ -2,26 +2,10 @@ RSpec.configure do |config| end RSpec.describe 'BeEF Configuration' do - before(:context, :type => :old ) do - config = File.expand_path('../../../support/assets/config_old.yaml', __dir__) - @config_instance = BeEF::Core::Configuration.new(config) - end before(:context) do @config_instance = BeEF::Core::Configuration.instance end - context 'configuration validation', :type => :old do - it 'should error when using hold public config' do - @config_instance.set('beef.http.public', 'example.com') - expect(@config_instance.validate).to eq(nil) - end - - it 'should error when using old public_port config' do - @config_instance.set('beef.http.public_port', 443) - expect(@config_instance.validate).to eq(nil) - end - - end context 'http local host configuration values' do it 'should set the local host value to 0.0.0.0' do From 85e550e44cfd37d6b6a5f64b8f706792667a340b Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 05:38:27 +1000 Subject: [PATCH 16/35] add test clean up of config settings --- spec/beef/core/main/configuration_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/beef/core/main/configuration_spec.rb b/spec/beef/core/main/configuration_spec.rb index 19a07f237..caafdc88b 100644 --- a/spec/beef/core/main/configuration_spec.rb +++ b/spec/beef/core/main/configuration_spec.rb @@ -5,6 +5,26 @@ RSpec.describe 'BeEF Configuration' do before(:context) do @config_instance = BeEF::Core::Configuration.instance + + @original_http_host = @config_instance.get('beef.http.host') + @original_http_port = @config_instance.get('beef.http.port') + @original_http_https = @config_instance.get('beef.http.https.enable') + @original_http_public_host = @config_instance.get('beef.http.public.host') + @original_http_public_port = @config_instance.get('beef.http.public.port') + @original_http_public_https = @config_instance.get('beef.http.public.https') + @original_http_hook_file = @config_instance.get('beef.http.hook_file') + end + + after(:context) do + # Reset the configuration values + # This is important as the tests may change the configuration values + @config_instance.set('beef.http.host', @original_http_host) + @config_instance.set('beef.http.port', @original_http_port) + @config_instance.set('beef.http.https.enable', @original_http_https) + @config_instance.set('beef.http.public.host', @original_http_public_host) + @config_instance.set('beef.http.public.port', @original_http_public_port) + @config_instance.set('beef.http.public.https', @original_http_public_https) + @config_instance.set('beef.http.hook_file', @original_http_hook_file) end context 'http local host configuration values' do From 27ae165f9288b0e303c7627002e860152074081d Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 07:17:38 +1000 Subject: [PATCH 17/35] removed incorrect comment --- core/main/configuration.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/core/main/configuration.rb b/core/main/configuration.rb index 36b021991..45a9f2d66 100644 --- a/core/main/configuration.rb +++ b/core/main/configuration.rb @@ -26,7 +26,6 @@ module BeEF begin # open base config @config = load(config) - # set default value if key? does not exist @config.default = nil @@config = config rescue StandardError => e From 3e56a945730accc2999d6a7e7087936ea70a5c49 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 07:26:54 +1000 Subject: [PATCH 18/35] tidy up auth_rate_spec --- spec/beef/api/auth_rate_spec.rb | 19 +-------------- spec/spec_helper.rb | 42 ++++++++++++++++----------------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index 06f36a745..4a7589882 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -4,22 +4,15 @@ # See the file 'doc/COPYING' for copying permission # -require 'net/http' -require 'uri' - RSpec.describe 'BeEF API Rate Limit' do - + before(:each) do - port = 3000 - expect(`lsof -i :#{port}`).to be_empty @pid = start_beef_server_and_wait @username = @config.get('beef.credentials.user') @password = @config.get('beef.credentials.passwd') end after(:each) do - # Stop the DNS server after each test case - BeEF::Extension::Dns::Server.instance.stop # Shutting down server Process.kill("KILL", @pid) unless @pid.nil? Process.wait(@pid) unless @pid.nil? # Ensure the process has exited and the port is released @@ -27,19 +20,11 @@ RSpec.describe 'BeEF API Rate Limit' do end it 'confirm correct creds are successful' do - - # sleep 1 - # uri = URI.parse("http://#{ATTACK_DOMAIN}:3000") - # response = Net::HTTP.get_response(uri) - # expect(response).to be_a(Net::HTTPSuccess) # HTTP request is successful - - # sleep 60 test_api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', @username, @password) expect(@config.get('beef.credentials.user')).to eq('beef') expect(@config.get('beef.credentials.passwd')).to eq('beef') expect(test_api.auth()[:payload]).not_to eql("401 Unauthorized") expect(test_api.auth()[:payload]["success"]).to be(true) # valid pass should succeed - end it 'confirm incorrect creds are unsuccessful' do @@ -49,7 +34,6 @@ RSpec.describe 'BeEF API Rate Limit' do end it 'adheres to 9 bad passwords then 1 correct auth rate limits' do - # create api structures with bad passwords and one good passwds = (1..9).map { |i| "bad_password"} # incorrect password passwds.push @password # correct password @@ -62,7 +46,6 @@ RSpec.describe 'BeEF API Rate Limit' do end it 'adheres to random bad passords and 1 correct auth rate limits' do - # create api structures with bad passwords and one good passwds = (1..9).map { |i| "bad_password"} # incorrect password passwds.push @password # correct password diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 320587ed8..7c0a240fd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -90,14 +90,12 @@ RSpec.configure do |config| Process.kill('KILL', server_pids) end - - ######################################## require 'socket' def port_available? - socket = TCPSocket.new('localhost', 3000) + socket = TCPSocket.new(@host, @port) socket.close false # If a connection is made, the port is in use, so it's not available. rescue Errno::ECONNREFUSED @@ -106,17 +104,13 @@ require 'socket' true # If the connection is refused, the port is not in use, so it's available. end - def configure_beef - # Reset or re-initialise the configuration to a default state @config = BeEF::Core::Configuration.instance @config.set('beef.credentials.user', "beef") @config.set('beef.credentials.passwd', "beef") - - @username = @config.get('beef.credentials.user') - @password = @config.get('beef.credentials.passwd') + @config.set('beef.http.https.enable', false) end # Load the server @@ -129,10 +123,14 @@ require 'socket' end def start_beef_server - exit unless port_available? configure_beef - load_beef_extensions_and_modules + @port = @config.get('beef.http.port') + @host = @config.get('beef.http.host') + @host = '127.0.0.1' + exit unless port_available? + load_beef_extensions_and_modules + # Grab DB file and regenerate if requested db_file = @config.get('beef.database.file') @@ -176,27 +174,29 @@ require 'socket' end def beef_server_running?(uri_str) - uri = URI.parse(uri_str) - response = Net::HTTP.get_response(uri) - response.is_a?(Net::HTTPSuccess) - rescue - false + begin + uri = URI.parse(uri_str) + response = Net::HTTP.get_response(uri) + response.is_a?(Net::HTTPSuccess) + rescue Errno::ECONNREFUSED + return false # Connection refused means the server is not running + rescue StandardError => e + return false # Any other error means the server is not running + end end def wait_for_beef_server_to_start(uri_str, timeout: 5) - start_time = Time.now - + start_time = Time.now # Record the time we started until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do - sleep 0.1 + sleep 0.1 # Wait a bit before checking again end - - beef_server_running?(uri_str) + beef_server_running?(uri_str) # Return the result of the check end def start_beef_server_and_wait pid = start_beef_server - if wait_for_beef_server_to_start('http://localhost:3000', timeout: 5) + if wait_for_beef_server_to_start('http://localhost:3000', timeout: 3) # print_info "Server started successfully." else print_error "Server failed to start within timeout." From c4b0b63e9028e7fd36b9472defc50d004932055d Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 11:44:33 +1000 Subject: [PATCH 19/35] move dns banner output into to banners.rb - still had a bug --- core/main/console/banners.rb | 16 +++++++++++++++- extensions/dns/api.rb | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/main/console/banners.rb b/core/main/console/banners.rb index cbbae5663..d4d703928 100644 --- a/core/main/console/banners.rb +++ b/core/main/console/banners.rb @@ -135,7 +135,6 @@ module BeEF end end - # # Print WebSocket servers # def print_http_proxy @@ -143,6 +142,21 @@ module BeEF print_info "HTTP Proxy: http://#{config.get('beef.extension.proxy.address')}:#{config.get('beef.extension.proxy.port')}" end + def print_dns + address = nil + port = nil + protocol = nil + + # TODO: fix the following reference - extensions/dns/api.rb + # servers, interfaces, address, port, protocol, upstream_servers = get_dns_config # get the DNS configuration + + # Print the DNS server information + unless address.nil? || port.nil? || protocol.nil? + print_info "DNS Server: #{address}:#{port} (#{protocol})" + print_more upstream_servers unless upstream_servers.empty? + end + end + end end end diff --git a/extensions/dns/api.rb b/extensions/dns/api.rb index 9722a416e..f34de49c8 100644 --- a/extensions/dns/api.rb +++ b/extensions/dns/api.rb @@ -24,8 +24,23 @@ module BeEF # # @param http_hook_server [BeEF::Core::Server] HTTP server instance def self.pre_http_start(_http_hook_server) - dns_config = BeEF::Core::Configuration.instance.get('beef.extension.dns') + servers, interfaces, address, port, protocol, upstream_servers = get_dns_config # get the DNS configuration + + # Start the DNS server dns = BeEF::Extension::Dns::Server.instance + dns.run(upstream: servers, listen: interfaces) + end + + def self.print_dns_info + servers, interfaces, address, port, protocol, upstream_servers = get_dns_config # get the DNS configuration + + # Print the DNS server information + print_info "DNS Server: #{address}:#{port} (#{protocol})" + print_more upstream_servers unless upstream_servers.empty? + end + + def self.get_dns_config + dns_config = BeEF::Core::Configuration.instance.get('beef.extension.dns') protocol = begin dns_config['protocol'].to_sym @@ -52,10 +67,7 @@ module BeEF end end - dns.run(upstream: servers, listen: interfaces) - - print_info "DNS Server: #{address}:#{port} (#{protocol})" - print_more upstream_servers unless upstream_servers.empty? + return servers, interfaces, address, port, protocol, upstream_servers end # Mounts the handler for processing DNS RESTful API requests. From 95037f7c5aada26d31e1bb87318cabd783025fe7 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 12:00:12 +1000 Subject: [PATCH 20/35] added the print dns function for banner info --- beef | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beef b/beef index d7790fc99..7d8dd55b9 100755 --- a/beef +++ b/beef @@ -243,6 +243,8 @@ BeEF::Core::Console::Banners.print_loaded_extensions BeEF::Core::Console::Banners.print_loaded_modules BeEF::Core::Console::Banners.print_network_interfaces_count BeEF::Core::Console::Banners.print_network_interfaces_routes +BeEF::Core::Console::Banners.print_http_proxy +BeEF::Core::Console::Banners.print_dns # # @note Prints the API key needed to use the RESTful API From 140129a91a49f2225d7ec7b6962873b8249dabe4 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 12:16:33 +1000 Subject: [PATCH 21/35] fixed module loading bug --- spec/beef/core/main/command_spec.rb | 6 ++++-- spec/beef/core/modules_spec.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/beef/core/main/command_spec.rb b/spec/beef/core/main/command_spec.rb index 0b7d8bc2b..e03b4336a 100644 --- a/spec/beef/core/main/command_spec.rb +++ b/spec/beef/core/main/command_spec.rb @@ -1,11 +1,13 @@ RSpec.describe 'BeEF Command class testing' do before(:each) do # Reset or re-initialise the configuration to a default state - @config_instance = BeEF::Core::Configuration.instance + # @config_instance = BeEF::Core::Configuration.instance end it 'should return a beef configuration variable' do - BeEF::Modules.load + expect { + BeEF::Modules.load if BeEF::Core::Configuration.instance.get('beef.module').nil? + }.to_not raise_error command_mock = BeEF::Core::Command.new('test_get_variable') expect(command_mock.config.beef_host).to eq('0.0.0.0') diff --git a/spec/beef/core/modules_spec.rb b/spec/beef/core/modules_spec.rb index f02b7a805..f9cb31937 100644 --- a/spec/beef/core/modules_spec.rb +++ b/spec/beef/core/modules_spec.rb @@ -2,7 +2,7 @@ RSpec.describe 'BeEF Modules' do it 'loaded successfully' do expect { - BeEF::Modules.load + BeEF::Modules.load if BeEF::Core::Configuration.instance.get('beef.module').nil? }.to_not raise_error modules = BeEF::Core::Configuration.instance.get('beef.module').select do |k,v| From 822c6616de79bf0eb43827f28a928bd57910ffd5 Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 12:18:13 +1000 Subject: [PATCH 22/35] removed excess debug code --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7c0a240fd..6858b3bb4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,7 +34,6 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base end TASK_ID = (ENV['TASK_ID'] || 0).to_i -print_info ENV['CONFIG_FILE'] CONFIG_FILE = ENV['CONFIG_FILE'] || 'windows/win10/win10_chrome_81.config.yml' CONFIG = YAML.safe_load(File.read("./spec/support/browserstack/#{CONFIG_FILE}")) CONFIG['user'] = ENV['BROWSERSTACK_USERNAME'] || '' From 92e03ba62b3e4dde2b01b82cb2c89c76ea0c868d Mon Sep 17 00:00:00 2001 From: Stephen Date: Mon, 25 Mar 2024 17:12:19 +1000 Subject: [PATCH 23/35] remove excess comments --- spec/beef/core/main/command_spec.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/beef/core/main/command_spec.rb b/spec/beef/core/main/command_spec.rb index e03b4336a..ad68e9a65 100644 --- a/spec/beef/core/main/command_spec.rb +++ b/spec/beef/core/main/command_spec.rb @@ -1,9 +1,4 @@ RSpec.describe 'BeEF Command class testing' do - before(:each) do - # Reset or re-initialise the configuration to a default state - # @config_instance = BeEF::Core::Configuration.instance - end - it 'should return a beef configuration variable' do expect { BeEF::Modules.load if BeEF::Core::Configuration.instance.get('beef.module').nil? From 0fb4805c44be724e88d1697c1e18d0f88ee0bc82 Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 26 Mar 2024 06:53:33 +1000 Subject: [PATCH 24/35] fixed login helper function --- spec/support/beef_test.rb | 48 ++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/spec/support/beef_test.rb b/spec/support/beef_test.rb index ad41558ea..a3cfe78fe 100644 --- a/spec/support/beef_test.rb +++ b/spec/support/beef_test.rb @@ -3,29 +3,51 @@ # Browser Exploitation Framework (BeEF) - https://beefproject.com # See the file 'doc/COPYING' for copying permission # -require 'test/unit' - +require 'selenium-webdriver' +require 'spec_helper' require 'capybara' require 'capybara/rspec' Capybara.run_server = false # we need to run our own BeEF server -require 'selenium-webdriver' - class BeefTest - def self.save_screenshot(session) - Dir.mkdir(BEEF_TEST_DIR) unless File.directory?(BEEF_TEST_DIR) - session.driver.browser.save_screenshot(BEEF_TEST_DIR + Time.now.strftime('%Y-%m-%d--%H-%M-%S-%N') + '.png') + def self.save_screenshot(session, dir = nil) + outputDir = dir || BEEF_TEST_DIR + Dir.mkdir(outputDir) unless File.directory?(outputDir) + session.driver.browser.save_screenshot(outputDir + Time.now.strftime('%Y-%m-%d--%H-%M-%S-%N') + '.png') end def self.login(session = nil) session = Capybara::Session.new(:selenium_headless) if session.nil? session.visit(ATTACK_URL) - sleep 2.0 - session.has_content?('BeEF Authentication') - session.fill_in 'user', with: BEEF_USER - session.fill_in 'pass', with: BEEF_PASSWD - session.click_button('Login') - sleep 10.0 + + session.has_content?('Authentication', wait: 10) + save_screenshot(session) + + # enter the credentials + session.execute_script("document.getElementById('pass').value = '#{CGI.escapeHTML(BEEF_PASSWD)}'\;") + session.execute_script("document.getElementById('user').value = '#{CGI.escapeHTML(BEEF_USER)}'\;") + + # due to using JS there seems to be a race condition - this is a workaround + session.has_content?('beef', wait: 10) + + # click the login button + login_script = <<-JAVASCRIPT + var loginButton; + var buttons = document.getElementsByTagName('button'); + for (var i = 0; i < buttons.length; i++) { + if (buttons[i].textContent === 'Login') { + loginButton = buttons[i]; + break; + } + } + if (loginButton) { + loginButton.click(); + } + JAVASCRIPT + session.execute_script(login_script) + + session.has_content?('Hooked Browsers', wait: 10) + save_screenshot(session) session end From c51c0fca6d29f5b3a4104772ff070b5b076552ed Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 26 Mar 2024 19:03:50 +1000 Subject: [PATCH 25/35] added boiler plate --- spec/spec_helper.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6858b3bb4..898c10fbd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'core/loader.rb' # @note We need to load variables that 'beef' usually does for us From 712201d9099e9a970ff068778966eb5be84b5e3b Mon Sep 17 00:00:00 2001 From: Stephen Date: Tue, 26 Mar 2024 19:04:58 +1000 Subject: [PATCH 26/35] fixed logout helper and cleaned up code --- spec/requests/beef_test_spec.rb | 81 +++++++++++++++++++++++++++++++++ spec/support/beef_test.rb | 5 +- 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 spec/requests/beef_test_spec.rb diff --git a/spec/requests/beef_test_spec.rb b/spec/requests/beef_test_spec.rb new file mode 100644 index 000000000..6626bcd3a --- /dev/null +++ b/spec/requests/beef_test_spec.rb @@ -0,0 +1,81 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# +require 'spec_helper' +require 'capybara/rspec' +require_relative '../support/beef_test' + +RSpec.describe BeefTest do + let(:session) { Capybara::Session.new(:selenium) } + let(:victim) { Capybara::Session.new(:selenium) } + + before(:each) do + @pid = start_beef_server_and_wait + end + + after(:each) do + # Shutting down server + Process.kill("KILL", @pid) unless @pid.nil? + Process.wait(@pid) unless @pid.nil? # Ensure the process has exited and the port is released + @pid = nil + end + + describe '.login' do + it 'logs in successfully' do + expect(port_available?) # Check if the tcp port is open + session = BeefTest.login() + expect(session).not_to be_nil + expect(session.has_content?('Hooked Browsers', wait: 10)) + end + end + + describe '.logout' do + before(:each) do + expect(port_available?) # # Check if the tcp port is open + @session = BeefTest.login() # Ensure login before each '.logout' test + expect(@session.has_content?('Hooked Browsers', wait: 10)) + end + + it 'logs out successfully' do + expect(port_available?) # # Check if the tcp port is open + expect(@session.has_content?('Hooked Browsers', wait: 10)) + + # Log out of the session + @sessoin = BeefTest.logout(@session) + expect(@session.has_no_content?('Hooked Browsers', wait: 10)) + expect(@session.has_content?('Authentication', wait: 10)) + end + end + + describe '.save_screenshot' do + it 'saves a screenshot' do + session = Capybara::Session.new(:selenium_headless) if session.nil? + + # Ensure the new directory does not exist + outputDir = '/tmp' + directory = "#{outputDir}/#{SecureRandom.hex}/" + expect(File.directory?(directory)).to be false + + # Save the screenshot + BeefTest.save_screenshot(session, directory) + + # Ensure the screenshot was saved + expect(File.directory?(directory)).to be true + screenshot_files = Dir.glob("#{directory}/*.png") + expect(screenshot_files.empty?).to be false + + # Ensure the screenshot file is not empty and clean up + screenshot_files.each do |file| + expect(File.size(file)).to be > 0 + File.delete(file) + end + expect(Dir.glob("#{directory}/*.png").empty?).to be true + + # Remove the directory + Dir.delete(directory) + expect(File.directory?(directory)).to be false + end + end +end diff --git a/spec/support/beef_test.rb b/spec/support/beef_test.rb index a3cfe78fe..7db058326 100644 --- a/spec/support/beef_test.rb +++ b/spec/support/beef_test.rb @@ -21,7 +21,6 @@ class BeefTest session.visit(ATTACK_URL) session.has_content?('Authentication', wait: 10) - save_screenshot(session) # enter the credentials session.execute_script("document.getElementById('pass').value = '#{CGI.escapeHTML(BEEF_PASSWD)}'\;") @@ -47,13 +46,13 @@ class BeefTest session.execute_script(login_script) session.has_content?('Hooked Browsers', wait: 10) - save_screenshot(session) session end def self.logout(session) - session.click_link('Logout') + session.click_on('Logout') + session.has_content?('Authentication', wait: 10) session end From f0bb5ed2d63230ba9021cd0aaed20ce3dfbab7a0 Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 27 Mar 2024 18:50:13 +1000 Subject: [PATCH 27/35] added boiler plate --- spec/beef/extensions/adminui_spec.rb | 9 +++++---- spec/beef/extensions/dns_spec.rb | 5 +++++ spec/beef/extensions/network_spec.rb | 5 +++++ spec/beef/extensions/proxy_spec.rb | 5 +++++ spec/beef/extensions/qrcode_spec.rb | 5 +++++ spec/beef/extensions/requester_spec.rb | 5 +++++ spec/beef/extensions/social_engineering_spec.rb | 5 +++++ spec/beef/extensions/webrtc_spec.rb | 5 +++++ spec/beef/extensions/websocket_hooked_browser_spec.rb | 1 - spec/beef/extensions/websocket_spec.rb | 5 +++++ spec/beef/extensions/xssrays_spec.rb | 6 ++++++ 11 files changed, 51 insertions(+), 5 deletions(-) diff --git a/spec/beef/extensions/adminui_spec.rb b/spec/beef/extensions/adminui_spec.rb index d8f954c1b..bddbc8c09 100644 --- a/spec/beef/extensions/adminui_spec.rb +++ b/spec/beef/extensions/adminui_spec.rb @@ -1,7 +1,8 @@ -# -# Tests for handling access to the Admin UI -# - +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/admin_ui/classes/httpcontroller' require 'extensions/admin_ui/classes/session' require 'extensions/admin_ui/controllers/authentication/authentication' diff --git a/spec/beef/extensions/dns_spec.rb b/spec/beef/extensions/dns_spec.rb index 91bb1634e..ffec46bb7 100644 --- a/spec/beef/extensions/dns_spec.rb +++ b/spec/beef/extensions/dns_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'resolv' require 'extensions/dns/extension.rb' diff --git a/spec/beef/extensions/network_spec.rb b/spec/beef/extensions/network_spec.rb index 1d5029db3..a85d0c57b 100644 --- a/spec/beef/extensions/network_spec.rb +++ b/spec/beef/extensions/network_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/network/models/network_service' require 'extensions/network/models/network_host' diff --git a/spec/beef/extensions/proxy_spec.rb b/spec/beef/extensions/proxy_spec.rb index 3f4f690b8..4f2cae338 100644 --- a/spec/beef/extensions/proxy_spec.rb +++ b/spec/beef/extensions/proxy_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/proxy/extension' RSpec.describe 'BeEF Extension Proxy' do diff --git a/spec/beef/extensions/qrcode_spec.rb b/spec/beef/extensions/qrcode_spec.rb index d06453c60..c1086d4c6 100644 --- a/spec/beef/extensions/qrcode_spec.rb +++ b/spec/beef/extensions/qrcode_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/qrcode/extension' RSpec.describe 'BeEF Extension QRCode' do diff --git a/spec/beef/extensions/requester_spec.rb b/spec/beef/extensions/requester_spec.rb index 8d2a15135..6a9298224 100644 --- a/spec/beef/extensions/requester_spec.rb +++ b/spec/beef/extensions/requester_spec.rb @@ -1,5 +1,10 @@ require 'extensions/requester/extension' +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# RSpec.describe 'BeEF Extension Requester' do before(:all) do @config = BeEF::Core::Configuration.instance diff --git a/spec/beef/extensions/social_engineering_spec.rb b/spec/beef/extensions/social_engineering_spec.rb index d664e40e1..8ccc50026 100644 --- a/spec/beef/extensions/social_engineering_spec.rb +++ b/spec/beef/extensions/social_engineering_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/social_engineering/models/web_cloner' require 'extensions/social_engineering/web_cloner/web_cloner' require 'extensions/social_engineering/web_cloner/interceptor' diff --git a/spec/beef/extensions/webrtc_spec.rb b/spec/beef/extensions/webrtc_spec.rb index 610863825..f42f2d09e 100644 --- a/spec/beef/extensions/webrtc_spec.rb +++ b/spec/beef/extensions/webrtc_spec.rb @@ -1,5 +1,10 @@ require 'rest-client' +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# RSpec.describe 'BeEF Extension WebRTC' do before(:all) do @config = BeEF::Core::Configuration.instance diff --git a/spec/beef/extensions/websocket_hooked_browser_spec.rb b/spec/beef/extensions/websocket_hooked_browser_spec.rb index 8ed4e9a97..774cf1447 100644 --- a/spec/beef/extensions/websocket_hooked_browser_spec.rb +++ b/spec/beef/extensions/websocket_hooked_browser_spec.rb @@ -3,7 +3,6 @@ # Browser Exploitation Framework (BeEF) - https://beefproject.com # See the file 'doc/COPYING' for copying permission # - require 'rest-client' require 'json' require_relative '../../spec_helper' diff --git a/spec/beef/extensions/websocket_spec.rb b/spec/beef/extensions/websocket_spec.rb index b701496c9..ffab4bfa8 100644 --- a/spec/beef/extensions/websocket_spec.rb +++ b/spec/beef/extensions/websocket_spec.rb @@ -1,3 +1,8 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'rest-client' require 'core/main/network_stack/websocket/websocket' require 'websocket-client-simple' diff --git a/spec/beef/extensions/xssrays_spec.rb b/spec/beef/extensions/xssrays_spec.rb index 114bc7339..4715f890b 100644 --- a/spec/beef/extensions/xssrays_spec.rb +++ b/spec/beef/extensions/xssrays_spec.rb @@ -1,3 +1,9 @@ + +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# require 'extensions/xssrays/extension' RSpec.describe 'BeEF Extension XSSRays' do From 24a25d6e6ac0c5855bfaa4eaa2fc352263a5838d Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 27 Mar 2024 18:53:56 +1000 Subject: [PATCH 28/35] tidy BeefTest and add more test cases --- spec/requests/beef_test_spec.rb | 38 +++++++++++++++++++++++++++++++-- spec/support/beef_test.rb | 11 +++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/spec/requests/beef_test_spec.rb b/spec/requests/beef_test_spec.rb index 6626bcd3a..56c18adaa 100644 --- a/spec/requests/beef_test_spec.rb +++ b/spec/requests/beef_test_spec.rb @@ -8,8 +8,6 @@ require 'capybara/rspec' require_relative '../support/beef_test' RSpec.describe BeefTest do - let(:session) { Capybara::Session.new(:selenium) } - let(:victim) { Capybara::Session.new(:selenium) } before(:each) do @pid = start_beef_server_and_wait @@ -20,6 +18,7 @@ RSpec.describe BeefTest do Process.kill("KILL", @pid) unless @pid.nil? Process.wait(@pid) unless @pid.nil? # Ensure the process has exited and the port is released @pid = nil + end describe '.login' do @@ -46,6 +45,7 @@ RSpec.describe BeefTest do @sessoin = BeefTest.logout(@session) expect(@session.has_no_content?('Hooked Browsers', wait: 10)) expect(@session.has_content?('Authentication', wait: 10)) + @session.reset_session! end end @@ -78,4 +78,38 @@ RSpec.describe BeefTest do expect(File.directory?(directory)).to be false end end + + let(:session) { Capybara::Session.new(:selenium_headless) } + let(:victim) { Capybara::Session.new(:selenium_headless) } + + describe '.new_attacker' do + it 'creates a new attacker session' do + # # Test setup + expect(session).not_to be_nil + + result = BeefTest.new_attacker(session) + + # Test assertions + expect(result).to eq(session) + expect(session.has_no_content?('Authentication', wait: 10)) + expect(session.has_content?('Hooked Browsers', wait: 10)) + session.reset_session! + end + end + + describe '.new_victim' do + it 'creates a new victim session' do + # Test setup + allow(victim).to receive(:visit) + expect(victim).not_to be_nil + + # Test execution + result = BeefTest.new_victim(victim) + + # Test assertions + expect(victim).to have_received(:visit).with(VICTIM_URL) + expect(result).to eq(victim) + victim.reset_session! + end + end end diff --git a/spec/support/beef_test.rb b/spec/support/beef_test.rb index 7db058326..094b38af8 100644 --- a/spec/support/beef_test.rb +++ b/spec/support/beef_test.rb @@ -13,7 +13,8 @@ class BeefTest def self.save_screenshot(session, dir = nil) outputDir = dir || BEEF_TEST_DIR Dir.mkdir(outputDir) unless File.directory?(outputDir) - session.driver.browser.save_screenshot(outputDir + Time.now.strftime('%Y-%m-%d--%H-%M-%S-%N') + '.png') + filename = outputDir + Time.now.strftime('%Y-%m-%d--%H-%M-%S-%N') + '.png' + session.driver.browser.save_screenshot(filename) end def self.login(session = nil) @@ -57,12 +58,12 @@ class BeefTest session end - def self.new_attacker - self.login + def self.new_attacker(session = nil) + self.login(session) end - def self.new_victim - victim = Capybara::Session.new(:selenium_headless) + def self.new_victim(victim = nil) + victim = Capybara::Session.new(:selenium_headless) if victim.nil? victim.visit(VICTIM_URL) victim end From aaf4c582350fed30c62e44df9bb76fdcbc8041e1 Mon Sep 17 00:00:00 2001 From: Stephen Date: Wed, 27 Mar 2024 18:57:28 +1000 Subject: [PATCH 29/35] tidy unused file --- test/thirdparty/msf/unit/check_environment.rb | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 test/thirdparty/msf/unit/check_environment.rb diff --git a/test/thirdparty/msf/unit/check_environment.rb b/test/thirdparty/msf/unit/check_environment.rb deleted file mode 100644 index ff1a9b7b2..000000000 --- a/test/thirdparty/msf/unit/check_environment.rb +++ /dev/null @@ -1,15 +0,0 @@ -# -# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net -# Browser Exploitation Framework (BeEF) - https://beefproject.com -# See the file 'doc/COPYING' for copying permission -# -require 'test/unit' - -class TC_CheckEnvironment < Test::Unit::TestCase - - def test_check_env - # Add environment checks here - - end - -end From 5f7d5e822a59609bc89feec4356e15c2d3fdab85 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:25:00 +0000 Subject: [PATCH 30/35] Bump net-smtp from 0.4.0.1 to 0.5.0 Bumps [net-smtp](https://github.com/ruby/net-smtp) from 0.4.0.1 to 0.5.0. - [Release notes](https://github.com/ruby/net-smtp/releases) - [Changelog](https://github.com/ruby/net-smtp/blob/master/NEWS.md) - [Commits](https://github.com/ruby/net-smtp/compare/v0.4.0.1...v0.5.0) --- updated-dependencies: - dependency-name: net-smtp dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index ff30c7b54..90160c138 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -101,7 +101,7 @@ GEM mutex_m (0.2.0) net-protocol (0.2.2) timeout - net-smtp (0.4.0.1) + net-smtp (0.5.0) net-protocol netrc (0.11.0) nio4r (2.7.0) From d713e375062b795712fe28b29b2438186511d262 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:25:25 +0000 Subject: [PATCH 31/35] Bump async from 1.31.0 to 1.32.0 Bumps [async](https://github.com/socketry/async) from 1.31.0 to 1.32.0. - [Release notes](https://github.com/socketry/async/releases) - [Changelog](https://github.com/socketry/async/blob/main/release.cert) - [Commits](https://github.com/socketry/async/compare/v1.31.0...v1.32.0) --- updated-dependencies: - dependency-name: async dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Gemfile | 2 +- Gemfile.lock | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index d0922ba2b..8bddabe77 100644 --- a/Gemfile +++ b/Gemfile @@ -50,7 +50,7 @@ end # DNS extension group :ext_dns do gem 'async-dns', '~> 1.3' - gem 'async', '~> 1.31' + gem 'async', '~> 1.32' end # QRcode extension diff --git a/Gemfile.lock b/Gemfile.lock index ff30c7b54..fa5a10e85 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,7 +23,7 @@ GEM archive-zip (0.12.0) io-like (~> 0.3.0) ast (2.4.2) - async (1.31.0) + async (1.32.0) console (~> 1.10) nio4r (~> 2.3) timers (~> 4.1) @@ -47,9 +47,10 @@ GEM coderay (1.1.3) concurrent-ruby (1.2.2) connection_pool (2.4.1) - console (1.23.3) + console (1.23.6) fiber-annotation fiber-local + json curb (1.0.5) daemons (1.4.1) diff-lcs (1.5.1) @@ -104,7 +105,7 @@ GEM net-smtp (0.4.0.1) net-protocol netrc (0.11.0) - nio4r (2.7.0) + nio4r (2.7.1) nokogiri (1.16.2-arm64-darwin) racc (~> 1.4) nokogiri (1.16.2-x86_64-linux) @@ -249,7 +250,7 @@ PLATFORMS DEPENDENCIES ansi (~> 1.5) - async (~> 1.31) + async (~> 1.32) async-dns (~> 1.3) browserstack-local (~> 1.4) capybara (~> 3.40) From 83015d0a40f549cf5c65ed87a71e9cc0ed617e44 Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 28 Mar 2024 03:07:43 +1000 Subject: [PATCH 32/35] tidy unused --- test/integration/check_environment.rb | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 test/integration/check_environment.rb diff --git a/test/integration/check_environment.rb b/test/integration/check_environment.rb deleted file mode 100644 index ff1a9b7b2..000000000 --- a/test/integration/check_environment.rb +++ /dev/null @@ -1,15 +0,0 @@ -# -# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net -# Browser Exploitation Framework (BeEF) - https://beefproject.com -# See the file 'doc/COPYING' for copying permission -# -require 'test/unit' - -class TC_CheckEnvironment < Test::Unit::TestCase - - def test_check_env - # Add environment checks here - - end - -end From dba210c109af99483568189ec53f1dce5f98e3a2 Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 28 Mar 2024 03:08:32 +1000 Subject: [PATCH 33/35] tidy login_spec file --- spec/requests/login_spec.rb | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 spec/requests/login_spec.rb diff --git a/spec/requests/login_spec.rb b/spec/requests/login_spec.rb new file mode 100644 index 000000000..79f00cab3 --- /dev/null +++ b/spec/requests/login_spec.rb @@ -0,0 +1,78 @@ +# +# Copyright (c) 2006-2024 Wade Alcorn - wade@bindshell.net +# Browser Exploitation Framework (BeEF) - https://beefproject.com +# See the file 'doc/COPYING' for copying permission +# +require 'rspec' +require 'spec/support/constants.rb' +# require '../common/beef_test' + +RSpec.describe 'Beef Login' do + let(:session) { Capybara::Session.new(:selenium_headless) } + + before(:each) do + session.visit(ATTACK_URL) + sleep 2.0 + end + + after(:each) do + BeefTest.save_screenshot(session) + session.driver.browser.close + end + + it 'logs in successfully' do + session.fill_in 'user', with: BEEF_USER + session.fill_in 'pass', with: BEEF_PASSWD + session.click_button('Login') + sleep 10.0 + expect(session).to have_content('Logout') + end + + # it 'logs out successfully' do + # session.fill_in 'user', with: BEEF_USER + # session.fill_in 'pass', with: BEEF_PASSWD + # session.click_button('Login') + # sleep 2.0 + # session.click_link('Logout') + # sleep 2.0 + # expect(session).to have_title('BeEF Authentication') + # end + + # it 'displays logs tab' do + # session.fill_in 'user', with: BEEF_USER + # session.fill_in 'pass', with: BEEF_PASSWD + # session.click_button('Login') + # sleep 2.0 + # session.click_on('Logs') + # expect(session).to have_content('Logout') + # expect(session).to have_content('Hooked Browsers') + # expect(session).to have_content('Type') + # expect(session).to have_content('Event') + # expect(session).to have_content('Date') + # expect(session).to have_content('Page') + # expect(session).to have_content('User with ip 127.0.0.1 has successfully authenticated in the application') + # end + + # it 'hooks a browser successfully' do + # attacker = BeefTest.new_attacker + # victim = BeefTest.new_victim + + # sleep 5.0 + + # expect(attacker).to have_content(VICTIM_DOMAIN) + # expect(attacker).to have_content('127.0.0.1') + # attacker.click_on("127.0.0.1", match: :first) + + # sleep 1.0 + + # expect(attacker).to have_content('Details') + # expect(attacker).to have_content('Commands') + + # BeefTest.save_screenshot(attacker) + # BeefTest.save_screenshot(victim) + + # BeefTest.logout(attacker) + # attacker.driver.browser.close + # victim.driver.browser.close + # end +end \ No newline at end of file From 60cc069a3b1f0e79d4efa679ff22a6ee7f08bae6 Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 28 Mar 2024 03:10:31 +1000 Subject: [PATCH 34/35] remove excess new line --- spec/requests/beef_test_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/requests/beef_test_spec.rb b/spec/requests/beef_test_spec.rb index 56c18adaa..1978a7abe 100644 --- a/spec/requests/beef_test_spec.rb +++ b/spec/requests/beef_test_spec.rb @@ -17,8 +17,7 @@ RSpec.describe BeefTest do # Shutting down server Process.kill("KILL", @pid) unless @pid.nil? Process.wait(@pid) unless @pid.nil? # Ensure the process has exited and the port is released - @pid = nil - + @pid = nil end describe '.login' do From 6c960ea8aa8d8a477843dd9db72fff45f18c0e66 Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 28 Mar 2024 03:12:08 +1000 Subject: [PATCH 35/35] stage login tests --- spec/requests/login_spec.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/requests/login_spec.rb b/spec/requests/login_spec.rb index 79f00cab3..d8458fe15 100644 --- a/spec/requests/login_spec.rb +++ b/spec/requests/login_spec.rb @@ -8,25 +8,25 @@ require 'spec/support/constants.rb' # require '../common/beef_test' RSpec.describe 'Beef Login' do - let(:session) { Capybara::Session.new(:selenium_headless) } + # let(:session) { Capybara::Session.new(:selenium_headless) } - before(:each) do - session.visit(ATTACK_URL) - sleep 2.0 - end + # before(:each) do + # session.visit(ATTACK_URL) + # sleep 2.0 + # end - after(:each) do - BeefTest.save_screenshot(session) - session.driver.browser.close - end + # after(:each) do + # BeefTest.save_screenshot(session) + # session.driver.browser.close + # end - it 'logs in successfully' do - session.fill_in 'user', with: BEEF_USER - session.fill_in 'pass', with: BEEF_PASSWD - session.click_button('Login') - sleep 10.0 - expect(session).to have_content('Logout') - end + # it 'logs in successfully' do + # session.fill_in 'user', with: BEEF_USER + # session.fill_in 'pass', with: BEEF_PASSWD + # session.click_button('Login') + # sleep 10.0 + # expect(session).to have_content('Logout') + # end # it 'logs out successfully' do # session.fill_in 'user', with: BEEF_USER