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 7fcdcbec4..f67e74928 100644 --- a/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb +++ b/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb @@ -10,131 +10,128 @@ require_relative '../../../../spec_helper' require_relative '../../../../support/constants' require_relative '../../../../support/beef_test' -RSpec.describe 'AutoRunEngine Test', :run_on_browserstack => true do - 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') - - # 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 +RSpec.describe 'AutoRunEngine Test', run_on_browserstack: true do + 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') - # 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 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 - sleep 2 - else - print_info "Modules 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 - # Grab DB file and regenerate if requested - print_info "Loading database" - db_file = @config.get('beef.database.file') + sleep 2 + else + print_info 'Modules already loaded' + end - if BeEF::Core::Console::CommandLine.parse[:resetdb] - print_info 'Resetting the database for BeEF.' - File.delete(db_file) if File.exists?(db_file) - end + # Grab DB file and regenerate if requested + print_info 'Loading database' + db_file = @config.get('beef.database.file') - # 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) + if BeEF::Core::Console::CommandLine.parse[:resetdb] + print_info 'Resetting the database for BeEF.' + File.delete(db_file) if File.exist?(db_file) + end - context = ActiveRecord::Migration.new.migration_context - if context.needs_migration? - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate - 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) - sleep 2 + context = ActiveRecord::Migration.new.migration_context + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate if context.needs_migration? - BeEF::Core::Migration.instance.update_db! + sleep 2 - # 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::Migration.instance.update_db! - BeEF::Core::AutorunEngine::RuleLoader.instance.load_directory - # are_engine.R + # 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' } - # Spawn HTTP Server - print_info "Starting HTTP Hook Server" - http_hook_server = BeEF::Core::Server.instance - http_hook_server.prepare + BeEF::Core::AutorunEngine::RuleLoader.instance.load_directory + # are_engine.R - # Generate a token for the server to respond with - @token = BeEF::Core::Crypto::api_token + # Spawn HTTP Server + print_info 'Starting HTTP Hook Server' + http_hook_server = BeEF::Core::Server.instance + http_hook_server.prepare - # 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 + # Generate a token for the server to respond with + @token = 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 sleep 1 begin - @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) - @caps["name"] = self.class.description || ENV['name'] || 'no-name' - @caps["browserstack.local"] = true - @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] + @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) + @caps['name'] = self.class.description || ENV['name'] || 'no-name' + @caps['browserstack.local'] = true + @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] - @driver = Selenium::WebDriver.for(:remote, - :url => "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", - :desired_capabilities => @caps) - # Hook new victim - print_info 'Hooking a new victim, waiting a few seconds...' - wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds + @driver = Selenium::WebDriver.for(:remote, + url: "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", + desired_capabilities: @caps) + # Hook new victim + print_info 'Hooking a new victim, waiting a few seconds...' + wait = Selenium::WebDriver::Wait.new(timeout: 30) # seconds - @driver.navigate.to "#{VICTIM_URL}" - - # Give time for browser hook to occur - sleep 3 + @driver.navigate.to VICTIM_URL.to_s - sleep 1 until wait.until { @driver.execute_script("return window.beef.session.get_hook_session_id().length") > 0} + # Give time for browser hook to occur + sleep 3 - @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" - @hooks = JSON.parse(@hook_request) - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError + sleep 1 until wait.until { @driver.execute_script('return window.beef.session.get_hook_session_id().length') > 0 } + + @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" + @hooks = JSON.parse(@hook_request) + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError exit 1 else exit 0 end - end - end + end + end after(:all) do server_teardown(@driver, @pid, @pids) - end + end - it 'AutoRunEngine is working' do - begin - expect(@hooks['hooked-browsers']['online']).not_to be_empty - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script("return window.beef.session.get_hook_session_id()"))).to eq true - end + it 'AutoRunEngine is working' do + expect(@hooks['hooked-browsers']['online']).not_to be_empty + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script('return window.beef.session.get_hook_session_id()'))).to eq true end - end - + end end 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 8ecf73303..4fff9d6df 100644 --- a/spec/beef/core/main/handlers/browser_details_handler_spec.rb +++ b/spec/beef/core/main/handlers/browser_details_handler_spec.rb @@ -10,148 +10,143 @@ require_relative '../../../../spec_helper' require_relative '../../../../support/constants' require_relative '../../../../support/beef_test' -RSpec.describe 'Browser Details Handler', :run_on_browserstack => true do - 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') - - # 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 +RSpec.describe 'Browser Details Handler', run_on_browserstack: true do + 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') - # 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 + # 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 - # Grab DB file and regenerate if requested - print_info "Loading database" - db_file = @config.get('beef.database.file') + sleep 2 - if BeEF::Core::Console::CommandLine.parse[:resetdb] - print_info 'Resetting the database for BeEF.' - File.delete(db_file) if File.exists?(db_file) - 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 + sleep 2 + else + print_info 'Modules already loaded' + 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) + # Grab DB file and regenerate if requested + print_info 'Loading database' + db_file = @config.get('beef.database.file') - context = ActiveRecord::Migration.new.migration_context - if context.needs_migration? - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate - 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 - sleep 2 + # 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) - BeEF::Core::Migration.instance.update_db! + context = ActiveRecord::Migration.new.migration_context + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate if context.needs_migration? - # 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 - @token = BeEF::Core::Crypto::api_token + BeEF::Core::Migration.instance.update_db! - # 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 + # Spawn HTTP Server + print_info 'Starting HTTP Hook Server' + http_hook_server = BeEF::Core::Server.instance + http_hook_server.prepare - # Give the server time to start-up + # Generate a token for the server to respond with + @token = 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 + + # Give the server time to start-up sleep 1 - + begin @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) - @caps["name"] = self.class.description || ENV['name'] || 'no-name' - @caps["browserstack.local"] = true + @caps['name'] = self.class.description || ENV['name'] || 'no-name' + @caps['browserstack.local'] = true @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] @driver = Selenium::WebDriver.for(:remote, - :url => "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", - :desired_capabilities => @caps) + url: "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", + desired_capabilities: @caps) # Hook new victim print_info 'Hooking a new victim, waiting a few seconds...' - wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds + wait = Selenium::WebDriver::Wait.new(timeout: 30) # seconds + + @driver.navigate.to VICTIM_URL.to_s - @driver.navigate.to "#{VICTIM_URL}" - # Give time for browser hook to occur sleep 3 - sleep 1 until wait.until { @driver.execute_script("return window.beef.session.get_hook_session_id().length") > 0} + sleep 1 until wait.until { @driver.execute_script('return window.beef.session.get_hook_session_id().length') > 0 } @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" @hooks = JSON.parse(@hook_request) @session = @hooks['hooked-browsers']['online'][0]['session'] - - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError exit 1 else exit 0 - end - end - end - - after(:all) do - server_teardown(@driver, @pid, @pids) - end - - it 'can successfully hook a browser' do - begin - expect(@hooks['hooked-browsers']['online']).not_to be_empty - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script("return window.beef.session.get_hook_session_id()"))).to eq true end end end - it 'browser details handler working' do - begin - print_info "Getting browser details" - response = RestClient.get "#{RESTAPI_HOOKS}/#{@session}?token=#{@token}" - details = JSON.parse(response.body) + after(:all) do + server_teardown(@driver, @pid, @pids) + end - if details['browser.name.friendly'].downcase == 'internet explorer' - browser_name = 'internet_explorer' - else - browser_name = details['browser.name.friendly'].downcase - end - - expect(@driver.browser.to_s.downcase).to eq(browser_name) - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - exit 0 + it 'can successfully hook a browser' do + expect(@hooks['hooked-browsers']['online']).not_to be_empty + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script('return window.beef.session.get_hook_session_id()'))).to eq true end - end + end + + it 'browser details handler working' do + print_info 'Getting browser details' + response = RestClient.get "#{RESTAPI_HOOKS}/#{@session}?token=#{@token}" + details = JSON.parse(response.body) + + browser_name = if details['browser.name.friendly'].downcase == 'internet explorer' + 'internet_explorer' + else + details['browser.name.friendly'].downcase + end + + expect(@driver.browser.to_s.downcase).to eq(browser_name) + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + exit 0 + end end diff --git a/spec/beef/extensions/websocket_hooked_browser_spec.rb b/spec/beef/extensions/websocket_hooked_browser_spec.rb index 2cdbdfb54..210579356 100644 --- a/spec/beef/extensions/websocket_hooked_browser_spec.rb +++ b/spec/beef/extensions/websocket_hooked_browser_spec.rb @@ -12,101 +12,100 @@ require_relative '../../support/beef_test' require 'core/main/network_stack/websocket/websocket' require 'websocket-client-simple' -RSpec.describe 'Browser hooking with Websockets', :run_on_browserstack => true do - before(:all) do - @config = BeEF::Core::Configuration.instance - @config.set('beef.credentials.user', "beef") - @config.set('beef.credentials.passwd', "beef") +RSpec.describe 'Browser hooking with Websockets', run_on_browserstack: true do + before(:all) do + @config = BeEF::Core::Configuration.instance + @config.set('beef.credentials.user', 'beef') + @config.set('beef.credentials.passwd', 'beef') @config.set('beef.http.websocket.secure', false) @config.set('beef.http.websocket.enable', true) @ws = BeEF::Core::Websocket::Websocket.instance - @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 + @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 - # 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" + # 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! - # 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 - @token = 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 - # Give the server time to start-up + # 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.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) + context = ActiveRecord::Migration.new.migration_context + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate if context.needs_migration? + sleep 2 + 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 + @token = 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 + # Give the server time to start-up sleep 1 begin - @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) - @caps["name"] = self.class.description || ENV['name'] || 'no-name' - @caps["browserstack.local"] = true - @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] + @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) + @caps['name'] = self.class.description || ENV['name'] || 'no-name' + @caps['browserstack.local'] = true + @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] - @driver = Selenium::WebDriver.for(:remote, - :url => "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", - :desired_capabilities => @caps) - # Hook new victim - print_info 'Hooking a new victim, waiting a few seconds...' - wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds + @driver = Selenium::WebDriver.for(:remote, + url: "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", + desired_capabilities: @caps) + # Hook new victim + print_info 'Hooking a new victim, waiting a few seconds...' + wait = Selenium::WebDriver::Wait.new(timeout: 30) # seconds - @driver.navigate.to "#{VICTIM_URL}" - - # Give time for browser hook to occur - sleep 3 + @driver.navigate.to VICTIM_URL.to_s - sleep 1 until wait.until { @driver.execute_script("return window.beef.session.get_hook_session_id().length") > 0} + # Give time for browser hook to occur + sleep 3 - @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" - @hooks = JSON.parse(@hook_request) - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError + sleep 1 until wait.until { @driver.execute_script('return window.beef.session.get_hook_session_id().length') > 0 } + + @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" + @hooks = JSON.parse(@hook_request) + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError exit 1 else exit 0 end - end - end + end + end after(:all) do server_teardown(@driver, @pid, @pids) - end + end it 'confirms a websocket server has been started' do expect(@ws).to be_a_kind_of(BeEF::Core::Websocket::Websocket) @@ -119,18 +118,17 @@ RSpec.describe 'Browser hooking with Websockets', :run_on_browserstack => true d end it 'can successfully hook a browser' do - begin - expect(@hooks['hooked-browsers']['online']).not_to be_empty - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script("return window.beef.session.get_hook_session_id()"))).to eq true - end + expect(@hooks['hooked-browsers']['online']).not_to be_empty + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + expect(BeEF::Filters.is_valid_hook_session_id?(@driver.execute_script('return window.beef.session.get_hook_session_id()'))).to eq true end - end + end end diff --git a/spec/beef/modules/debug/test_beef_debugs_spec.rb b/spec/beef/modules/debug/test_beef_debugs_spec.rb index 569aac970..fe5c2169b 100644 --- a/spec/beef/modules/debug/test_beef_debugs_spec.rb +++ b/spec/beef/modules/debug/test_beef_debugs_spec.rb @@ -10,301 +10,292 @@ require_relative '../../../spec_helper' require_relative '../../../support/constants' require_relative '../../../support/beef_test' -RSpec.describe 'BeEF Debug Command Modules:', :run_on_browserstack => true do - before(:all) do - # Grab config and set creds in variables for ease of access - @config = BeEF::Core::Configuration.instance - @username = @config.get('beef.credentials.user') - @password = @config.get('beef.credentials.passwd') +RSpec.describe 'BeEF Debug Command Modules:', run_on_browserstack: true do + before(:all) do + # Grab config and set creds in variables for ease of access + @config = BeEF::Core::Configuration.instance + @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 + + # 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 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 + else + print_info 'Modules 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 + # Grab DB file and regenerate if requested + print_info 'Loading database' + db_file = @config.get('beef.database.file') - sleep 2 + 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) + + context = ActiveRecord::Migration.new.migration_context + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate if context.needs_migration? + + sleep 2 + + 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 + @token = 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 + + # Give the server time to start-up + sleep 1 + + begin + @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) + @caps['name'] = self.class.description || ENV['name'] || 'no-name' + @caps['browserstack.local'] = true + @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] + + @driver = Selenium::WebDriver.for(:remote, + url: "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", + desired_capabilities: @caps) + # Hook new victim + print_info 'Hooking a new victim, waiting a few seconds...' + wait = Selenium::WebDriver::Wait.new(timeout: 30) # seconds + + @driver.navigate.to VICTIM_URL.to_s + + # Give time for browser hook to occur + sleep 3 + + sleep 1 until wait.until { @driver.execute_script('return window.beef.session.get_hook_session_id().length') > 0 } + + @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" + @hooks = JSON.parse(@hook_request) + + @session = @driver.execute_script('return window.beef.session.get_hook_session_id()') + + # Grab Command Module IDs as they can differ from machine to machine + @debug_mod_ids = JSON.parse(RestClient.get("#{RESTAPI_MODULES}?token=#{@token}")) + @debug_mod_names_ids = {} + @debug_mods = @debug_mod_ids.to_a.select { |cmd_mod| cmd_mod[1]['category'] == 'Debug' } + .map do |debug_mod| + @debug_mod_names_ids[debug_mod[1]['class']] = debug_mod[1]['id'] + end + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 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! - - # 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 - @token = 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 - - # Give the server time to start-up - sleep 1 - - begin - @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID]) - @caps["name"] = self.class.description || ENV['name'] || 'no-name' - @caps["browserstack.local"] = true - @caps['browserstack.localIdentifier'] = ENV['BROWSERSTACK_LOCAL_IDENTIFIER'] - - @driver = Selenium::WebDriver.for(:remote, - :url => "http://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub", - :desired_capabilities => @caps) - # Hook new victim - print_info 'Hooking a new victim, waiting a few seconds...' - wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds - - @driver.navigate.to "#{VICTIM_URL}" - - # Give time for browser hook to occur - sleep 3 - - sleep 1 until wait.until { @driver.execute_script("return window.beef.session.get_hook_session_id().length") > 0} - - @hook_request = RestClient.get "#{RESTAPI_HOOKS}?token=#{@token}" - @hooks = JSON.parse(@hook_request) - - @session = @driver.execute_script("return window.beef.session.get_hook_session_id()") - - # Grab Command Module IDs as they can differ from machine to machine - @debug_mod_ids = JSON.parse(RestClient.get "#{RESTAPI_MODULES}?token=#{@token}") - @debug_mod_names_ids = {} - @debug_mods = @debug_mod_ids.to_a.select { |cmd_mod| cmd_mod[1]['category'] == 'Debug' } - .map do |debug_mod| - @debug_mod_names_ids[debug_mod[1]['class']] = debug_mod[1]['id'] - end - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end + exit 0 end end + end - after(:all) do - server_teardown(@driver, @pid, @pids) + after(:all) do + server_teardown(@driver, @pid, @pids) + end + + it 'The Test_beef.debug() command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_beef_debug'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + { "msg": 'test' }.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Test_beef.debug() command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_beef_debug'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { "msg": "test" }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Return ASCII Characters command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_return_ascii_chars'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + {}.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Return ASCII Characters command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_return_ascii_chars'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Return Image command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_return_image'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + {}.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Return Image command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_return_image'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Test HTTP Redirect command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_http_redirect'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + {}.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - - it 'The Test HTTP Redirect command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_http_redirect'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Test Returning Results/Long String command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_return_long_string'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + { "repeat": 20, + "repeat_string": 'beef' }.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + print_info "Exception Message: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Test Returning Results/Long String command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_return_long_string'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { "repeat": 20, - "repeat_string": "beef" }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Test Network Request command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_network_request'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + { "scheme": 'http', + "method": 'GET', + "domain": ATTACK_DOMAIN.to_s, + "port": @config.get('beef.http.port').to_s, + "path": '/hook.js', + "anchor": 'anchor', + "data": 'query=testquerydata', + "timeout": '10', + "dataType": 'script' }.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Test Network Request command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_network_request'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { "scheme": "http", - "method": "GET", - "domain": "#{ATTACK_DOMAIN}", - "port": "#{@config.get('beef.http.port')}", - "path": "/hook.js", - "anchor": "anchor", - "data": "query=testquerydata", - "timeout": "10", - "dataType": "script" }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Test DNS Tunnel command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_dns_tunnel_client'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + { "domain": 'example.com', + "data": 'Lorem ipsum' }.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end - it 'The Test DNS Tunnel command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_dns_tunnel_client'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { "domain": "example.com", - "data": "Lorem ipsum" }.to_json, - :content_type => :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end - end - - it 'The Test CORS Request command module successfully executes' do - begin - cmd_mod_id = @debug_mod_names_ids['Test_cors_request'] - response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", - { "method": "GET", - "url": "example.com", - "data": { - "test": "data" - }}.to_json, - content_type: :json - result_data = JSON.parse(response.body) - expect(result_data['success']).to eq "true" - rescue => exception - print_info "Exception: #{exception}" - print_info "Exception Class: #{exception.class}" - print_info "Exception Message: #{exception.message}" - if @driver.execute_script("return window.beef.session.get_hook_session_id().length").nil? && - exception.class == NoMethodError - exit 1 - else - exit 0 - end - end + it 'The Test CORS Request command module successfully executes' do + cmd_mod_id = @debug_mod_names_ids['Test_cors_request'] + response = RestClient.post "#{RESTAPI_MODULES}/#{@session}/#{cmd_mod_id}?token=#{@token}", + { "method": 'GET', + "url": 'example.com', + "data": { + "test": 'data' + } }.to_json, + content_type: :json + result_data = JSON.parse(response.body) + expect(result_data['success']).to eq 'true' + rescue StandardError => e + print_info "Exception: #{e}" + print_info "Exception Class: #{e.class}" + print_info "Exception Message: #{e.message}" + print_info "Exception Stack Trace: #{e.stacktrace}" + if @driver.execute_script('return window.beef.session.get_hook_session_id().length').nil? && + e.class == NoMethodError + exit 1 + else + exit 0 end + end end