From b645a730fa7db8a695ed01cf69ea8a64d5fa7c39 Mon Sep 17 00:00:00 2001 From: Jack Walker Date: Mon, 20 Apr 2020 11:26:09 +1000 Subject: [PATCH] Fixed extensions loading issue causing tests to run with previously loaded config. --- spec/beef/api/auth_rate_spec.rb | 69 +++++++++++++++++-- .../autorun_engine/autorun_engine_spec.rb | 14 ++-- .../handlers/browser_details_handler_spec.rb | 13 ++-- .../modules/debug/test_beef_debugs_spec.rb | 30 ++++---- 4 files changed, 87 insertions(+), 39 deletions(-) diff --git a/spec/beef/api/auth_rate_spec.rb b/spec/beef/api/auth_rate_spec.rb index 4740c334d..c9d3fe9ac 100644 --- a/spec/beef/api/auth_rate_spec.rb +++ b/spec/beef/api/auth_rate_spec.rb @@ -7,28 +7,87 @@ RSpec.describe 'BeEF API Rate Limit' do before(:all) do - # Note: rake spec passes --patterns which causes BeEF to pickup this argument via optparse. I can't see a better way at the moment to filter this out. Therefore ARGV=[] for this test. - ARGV = [] @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. + BeEF::Extensions.load + sleep 2 + + # Check if modules already loaded. No need to reload. + if @config.get('beef.module').nil? + print_info "Loading in BeEF::Modules" + BeEF::Modules.load + + sleep 2 + else + print_info "Modules already loaded" + end + + # 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] + print_info 'Resetting the database for BeEF.' + File.delete(db_file) if File.exists?(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) + + context = ActiveRecord::Migration.new.migration_context + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate + end + + sleep 2 + + BeEF::Core::Migration.instance.update_db! + + # add AutoRunEngine rule + test_rule = {"name"=>"Display an alert", "author"=>"mgeeky", "browser"=>"ALL", "browser_version"=>"ALL", "os"=>"ALL", "os_version"=>"ALL", "modules"=>[{"name"=>"alert_dialog", "condition"=>nil, "options"=>{"text"=>"You've been BeEFed ;>"}}], "execution_order"=>[0], "execution_delay"=>[0], "chain_mode"=>"sequential"} + + BeEF::Core::AutorunEngine::RuleLoader.instance.load_directory + # are_engine.R + + # 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 @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 - # wait for server to start + + # Give the server time to start-up sleep 1 + + # Authenticate to REST API & pull the token from the response + @response = RestClient.post "#{RESTAPI_ADMIN}/login", { 'username': "#{@username}", 'password': "#{@password}" }.to_json, :content_type => :json + @token = JSON.parse(@response)['token'] end after(:all) do + print_info "Shutting down server" Process.kill("KILL",@pid) Process.kill("KILL",@pids) - end - + end + it 'adheres to auth rate limits' do passwds = (1..9).map { |i| "broken_pass"} passwds.push BEEF_PASSWD diff --git a/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb b/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb index cd5836691..e908d24f9 100644 --- a/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb +++ b/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb @@ -12,7 +12,6 @@ require_relative '../../../../support/beef_test' RSpec.describe 'AutoRunEngine test' do before(:all) do - # Note: rake spec passes --patterns which causes BeEF to pickup this argument via optparse. I can't see a better way at the moment to filter this out. Therefore ARGV=[] for this test. @config = BeEF::Core::Configuration.instance @config.set('beef.credentials.user', "beef") @config.set('beef.credentials.passwd', "beef") @@ -20,15 +19,12 @@ RSpec.describe 'AutoRunEngine test' do @password = @config.get('beef.credentials.passwd') # Load BeEF extensions and modules - if @config.get('beef.extension').keys.length == 10 - print_info "Loading in BeEF::Extensions" - BeEF::Extensions.load - - sleep 2 - else - print_info "Extensions already loaded" - end + # Always load Extensions, as previous changes to the config from other tests may affect + # whether or not this test passes. + BeEF::Extensions.load + sleep 2 + # Check if modules already loaded. No need to reload. if @config.get('beef.module').nil? print_info "Loading in BeEF::Modules" BeEF::Modules.load diff --git a/spec/beef/core/main/handlers/browser_details_handler_spec.rb b/spec/beef/core/main/handlers/browser_details_handler_spec.rb index a1b5a5740..f545f3c36 100644 --- a/spec/beef/core/main/handlers/browser_details_handler_spec.rb +++ b/spec/beef/core/main/handlers/browser_details_handler_spec.rb @@ -18,15 +18,12 @@ RSpec.describe 'Browser details handler' do @password = @config.get('beef.credentials.passwd') # Load BeEF extensions and modules - if @config.get('beef.extension').keys.length == 10 - print_info "Loading in BeEF::Extensions" - BeEF::Extensions.load - - sleep 2 - else - print_info "Extensions already loaded" - end + # Always load Extensions, as previous changes to the config from other tests may affect + # whether or not this test passes. + BeEF::Extensions.load + sleep 2 + # Check if modules already loaded. No need to reload. if @config.get('beef.module').nil? print_info "Loading in BeEF::Modules" BeEF::Modules.load diff --git a/spec/beef/modules/debug/test_beef_debugs_spec.rb b/spec/beef/modules/debug/test_beef_debugs_spec.rb index 41cf0bd5c..f3b7569b4 100644 --- a/spec/beef/modules/debug/test_beef_debugs_spec.rb +++ b/spec/beef/modules/debug/test_beef_debugs_spec.rb @@ -17,25 +17,21 @@ RSpec.describe 'BeEF Debug Command Modules:' do @username = @config.get('beef.credentials.user') @password = @config.get('beef.credentials.passwd') - # Load BeEF extensions and modules - if @config.get('beef.extension').keys.length == 10 - print_info "Loading in BeEF::Extensions" - BeEF::Extensions.load + # 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. + BeEF::Extensions.load + sleep 2 - sleep 2 - else - print_info "Extensions already loaded" - end + # Check if modules already loaded. No need to reload. + if @config.get('beef.module').nil? + print_info "Loading in BeEF::Modules" + BeEF::Modules.load - - if @config.get('beef.module').nil? - BeEF::Modules.load - print_info "Loading in BeEF::Modules" - - sleep 2 - else - print_info "Modules already loaded" - end + sleep 2 + else + print_info "Modules already loaded" + end # Grab DB file and regenerate if requested print_info "Loading database"