From d2f3c7fbe2b4a9c987ce73455f891cad583e253b Mon Sep 17 00:00:00 2001 From: Haoxi Tan Date: Wed, 15 Jan 2020 13:47:46 +1000 Subject: [PATCH] added tests for browser details handler --- Gemfile | 2 +- .../handlers/browser_details_handler_spec.rb | 93 +++++++++++++++++++ spec/support/beef_test.rb | 4 + spec/support/simple_rest_client.rb | 4 +- 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 spec/beef/core/main/handlers/browser_details_handler_spec.rb diff --git a/Gemfile b/Gemfile index 9672d0f72..35ef1f1f9 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ # Browser Exploitation Framework (BeEF) - http://beefproject.com # See the file 'doc/COPYING' for copying permission # - +gem 'simplecov', require: false, group: :test gem 'eventmachine' gem 'thin' gem 'sinatra' diff --git a/spec/beef/core/main/handlers/browser_details_handler_spec.rb b/spec/beef/core/main/handlers/browser_details_handler_spec.rb new file mode 100644 index 000000000..6c9647a15 --- /dev/null +++ b/spec/beef/core/main/handlers/browser_details_handler_spec.rb @@ -0,0 +1,93 @@ +RSpec.describe 'Browser details handler' 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") + + #generate api token + BeEF::Core::Crypto::api_token + + # load up DB + # Connect to DB + ActiveRecord::Base.logger = nil + OTR::ActiveRecord.migrations_paths = [File.join('core', 'main', 'ar-migrations')] + OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database:'beef.db') + # Migrate (if required) + context = ActiveRecord::Migration.new.migration_context + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration).migrate + end + + + http_hook_server = BeEF::Core::Server.instance + http_hook_server.prepare + @pids = fork do + if ENV['RAILS_ENV'] == 'test' or ENV["COVERAGE"] + require 'simplecov' + # Give our new forked process a unique command name, to prevent problems + # when merging coverage results. + puts 'starting simplecov in fork..' + SimpleCov.command_name SecureRandom.uuid + SimpleCov.start + end + BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server) + end + @pid = fork do + if ENV['RAILS_ENV'] == 'test' or ENV["COVERAGE"] + puts 'starting simplecov in fork..' + require 'simplecov' + # Give our new forked process a unique command name, to prevent problems + # when merging coverage results. + SimpleCov.command_name SecureRandom.uuid + SimpleCov.start + end + + http_hook_server.start + end + # wait for server to start + sleep 1 + end + # wait for server to start + + after(:all) do + + Process.kill("INT",@pid) + Process.kill("INT",@pids) + + end + + it 'browser details handler working' do + + api = BeefRestClient.new('http', ATTACK_DOMAIN, '3000', BEEF_USER, BEEF_PASSWD) + + response = api.auth() + + @token = response[:token] + + puts "authenticated. api token: #{@token}" + + puts 'hooking a new victim, waiting a few seconds...' + + victim = BeefTest.new_victim + sleep 3.0 + + response = RestClient.get "#{RESTAPI_HOOKS}", {:params => {:token => @token}} + + j = JSON.parse(response.body) + + response = RestClient.get "#{RESTAPI_HOOKS}/#{j['hooked-browsers']['offline']['0']['session']}" , {:params => {:token => @token}} + puts "getting browser details:" + + details = JSON.parse(response.body) + + + # require 'byebug';byebug + expect(victim.driver.browser.browser.to_s.downcase).to eql (details["browser.name.friendly"].downcase) + + + end + +end diff --git a/spec/support/beef_test.rb b/spec/support/beef_test.rb index 2b8b7c38d..c13e846e9 100644 --- a/spec/support/beef_test.rb +++ b/spec/support/beef_test.rb @@ -42,6 +42,10 @@ class BeefTest end def self.new_victim + if ENV['RAILS_ENV'] == 'test' or ENV["COVERAGE"] + puts 'starting simplecov in fork..' + require 'simplecov' + end victim = Capybara::Session.new(:selenium_headless) victim.visit(VICTIM_URL) victim diff --git a/spec/support/simple_rest_client.rb b/spec/support/simple_rest_client.rb index 4d75d2e8a..a4b47f91b 100644 --- a/spec/support/simple_rest_client.rb +++ b/spec/support/simple_rest_client.rb @@ -27,9 +27,9 @@ class BeefRestClient :accept => :json result = JSON.parse(response.body) @token = result['token'] - {:success => result['success'], :payload => result} + {:success => result['success'], :payload => result, :token => @token} rescue => e - {:success => false, :payload => e.message } + {:success => false, :payload => e.message} end end