Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require 'core/loader.rb'
config = BeEF::Core::Configuration.new('config.yaml')
$home_dir = Dir.pwd
$root_dir = Dir.pwd
require 'core/bootstrap.rb'
require 'rack/test'
require 'curb'
require 'rest-client'
require 'yaml'
require 'selenium-webdriver'
require 'browserstack/local'
require 'byebug'
Dir['spec/support/*.rb'].each do |f|
require f
end
ENV['RACK_ENV'] ||= 'test'
ARGV.clear
class Capybara::Selenium::Driver < Capybara::Driver::Base
def reset!
@browser.navigate.to('about:blank') if @browser
end
end
TASK_ID = (ENV['TASK_ID'] || 0).to_i
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'] || ''
CONFIG['key'] = ENV['BROWSERSTACK_ACCESS_KEY'] || ''
ActiveRecord::Base.logger = nil
OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: ':memory:')
if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2')
OTR::ActiveRecord.establish_connection!
end
ActiveRecord::Schema.verbose = false
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
if context.needs_migration?
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
end
RSpec.configure do |config|
config.disable_monkey_patching!
config.bisect_runner = :shell
config.order = :random
Kernel.srand config.seed
config.include Rack::Test::Methods
config.expect_with :rspec do |c|
c.syntax = :expect
end
config.around do |example|
ActiveRecord::Base.transaction do
example.run
raise ActiveRecord::Rollback
end
end
def server_teardown(webdriver, server_pid, server_pids)
webdriver.quit
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.backtrace}"
exit 0
ensure
print_info 'Shutting down server'
Process.kill('KILL', server_pid)
Process.kill('KILL', server_pids)
end
def reset_beef_db
begin
db_file = BeEF::Core::Configuration.instance.get('beef.database.file')
File.delete(db_file) if File.exist?(db_file)
rescue => e
print_error("Could not remove '#{db_file}' database file: #{e.message}")
end
end
require 'socket'
def port_available?
socket = TCPSocket.new(@host, @port)
socket.close
false
rescue Errno::ECONNREFUSED
true
rescue Errno::EADDRNOTAVAIL
true
end
def configure_beef
@config = BeEF::Core::Configuration.instance
@config.set('beef.credentials.user', "beef")
@config.set('beef.credentials.passwd', "beef")
@config.set('beef.http.https.enable', false)
end
def load_beef_extensions_and_modules
BeEF::Extensions.load
BeEF::Modules.load if @config.get('beef.module').nil?
end
def start_beef_server
configure_beef
@port = @config.get('beef.http.port')
@host = @config.get('beef.http.host')
@host = '127.0.0.1'
unless port_available?
print_error "Port #{@port} is already in use. Exiting."
exit
end
load_beef_extensions_and_modules
db_file = @config.get('beef.database.file')
if BeEF::Core::Console::CommandLine.parse[:resetdb]
File.delete(db_file) if File.exist?(db_file)
end
ActiveRecord::Base.logger = nil
OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file)
if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2')
OTR::ActiveRecord.establish_connection!
end
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
if context.needs_migration?
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
end
BeEF::Core::Migration.instance.update_db!
http_hook_server = BeEF::Core::Server.instance
http_hook_server.prepare
BeEF::Core::Crypto::api_token
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)
begin
uri = URI.parse(uri_str)
response = Net::HTTP.get_response(uri)
response.is_a?(Net::HTTPSuccess)
rescue Errno::ECONNREFUSED
return false
rescue StandardError => e
return false
end
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
puts "Starting BeEF server"
pid = start_beef_server
puts "BeEF server started with PID: #{pid}"
if wait_for_beef_server_to_start('http://localhost:3000', timeout: SERVER_START_TIMEOUT)
else
print_error "Server failed to start within timeout."
end
pid
end
def stop_beef_server(pid)
exit if pid.nil?
Process.kill("KILL", pid) unless pid.nil?
Process.wait(pid) unless pid.nil?
pid = nil
end
end