added tests for browser details handler

This commit is contained in:
Haoxi Tan
2020-01-15 13:47:46 +10:00
parent a46a2fe2c1
commit d2f3c7fbe2
4 changed files with 100 additions and 3 deletions

View File

@@ -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'

View File

@@ -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

View File

@@ -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

View File

@@ -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