Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/spec/spec_helper.rb
1146 views
1
#
2
# Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
# Browser Exploitation Framework (BeEF) - https://beefproject.com
4
# See the file 'doc/COPYING' for copying permission
5
#
6
# Set external and internal character encodings to UTF-8
7
Encoding.default_external = Encoding::UTF_8
8
Encoding.default_internal = Encoding::UTF_8
9
10
require 'core/loader.rb'
11
12
# @note We need to load variables that 'beef' usually does for us
13
14
# @todo review this config (this isn't used or is shadowed by the monkey patching, needs a further look to fix properly)
15
config = BeEF::Core::Configuration.new('config.yaml')
16
$home_dir = Dir.pwd
17
$root_dir = Dir.pwd
18
19
require 'core/bootstrap.rb'
20
require 'rack/test'
21
require 'curb'
22
require 'rest-client'
23
require 'yaml'
24
require 'selenium-webdriver'
25
require 'browserstack/local'
26
require 'byebug'
27
28
# Require supports
29
Dir['spec/support/*.rb'].each do |f|
30
require f
31
end
32
33
ENV['RACK_ENV'] ||= 'test' # Set the environment to test
34
ARGV.clear
35
36
## BrowserStack config
37
38
# Monkey patch to avoid reset sessions
39
class Capybara::Selenium::Driver < Capybara::Driver::Base
40
def reset!
41
@browser.navigate.to('about:blank') if @browser
42
end
43
end
44
45
TASK_ID = (ENV['TASK_ID'] || 0).to_i
46
CONFIG_FILE = ENV['CONFIG_FILE'] || 'windows/win10/win10_chrome_81.config.yml'
47
CONFIG = YAML.safe_load(File.read("./spec/support/browserstack/#{CONFIG_FILE}"))
48
CONFIG['user'] = ENV['BROWSERSTACK_USERNAME'] || ''
49
CONFIG['key'] = ENV['BROWSERSTACK_ACCESS_KEY'] || ''
50
51
## DB config
52
ActiveRecord::Base.logger = nil
53
OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: ':memory:')
54
55
# otr-activerecord requires manually establishing the connection with the following line
56
# Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems.
57
if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2')
58
OTR::ActiveRecord.establish_connection!
59
end
60
ActiveRecord::Schema.verbose = false
61
62
# Migrate (if required)
63
ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages
64
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
65
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
66
if context.needs_migration?
67
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
68
end
69
70
RSpec.configure do |config|
71
config.disable_monkey_patching!
72
config.bisect_runner = :shell
73
config.order = :random
74
Kernel.srand config.seed
75
config.include Rack::Test::Methods
76
config.expect_with :rspec do |c|
77
c.syntax = :expect
78
end
79
config.around do |example|
80
ActiveRecord::Base.transaction do
81
# byebug
82
example.run
83
raise ActiveRecord::Rollback
84
end
85
end
86
87
def server_teardown(webdriver, server_pid, server_pids)
88
webdriver.quit
89
rescue StandardError => e
90
print_info "Exception: #{e}"
91
print_info "Exception Class: #{e.class}"
92
print_info "Exception Message: #{e.message}"
93
print_info "Exception Stack Trace: #{e.backtrace}"
94
exit 0
95
ensure
96
print_info 'Shutting down server'
97
Process.kill('KILL', server_pid)
98
Process.kill('KILL', server_pids)
99
end
100
101
########################################
102
103
def reset_beef_db
104
begin
105
db_file = BeEF::Core::Configuration.instance.get('beef.database.file')
106
File.delete(db_file) if File.exist?(db_file)
107
rescue => e
108
print_error("Could not remove '#{db_file}' database file: #{e.message}")
109
end
110
end
111
112
require 'socket'
113
114
def port_available?
115
socket = TCPSocket.new(@host, @port)
116
socket.close
117
false # If a connection is made, the port is in use, so it's not available.
118
rescue Errno::ECONNREFUSED
119
true # If the connection is refused, the port is not in use, so it's available.
120
rescue Errno::EADDRNOTAVAIL
121
true # If the connection is refused, the port is not in use, so it's available.
122
end
123
124
def configure_beef
125
# Reset or re-initialise the configuration to a default state
126
@config = BeEF::Core::Configuration.instance
127
128
@config.set('beef.credentials.user', "beef")
129
@config.set('beef.credentials.passwd', "beef")
130
@config.set('beef.http.https.enable', false)
131
end
132
133
# Load the server
134
def load_beef_extensions_and_modules
135
# Load BeEF extensions
136
BeEF::Extensions.load
137
138
# Load BeEF modules only if they are not already loaded
139
BeEF::Modules.load if @config.get('beef.module').nil?
140
end
141
142
def start_beef_server
143
configure_beef
144
@port = @config.get('beef.http.port')
145
@host = @config.get('beef.http.host')
146
@host = '127.0.0.1'
147
148
unless port_available?
149
print_error "Port #{@port} is already in use. Exiting."
150
exit
151
end
152
load_beef_extensions_and_modules
153
154
# Grab DB file and regenerate if requested
155
db_file = @config.get('beef.database.file')
156
157
if BeEF::Core::Console::CommandLine.parse[:resetdb]
158
File.delete(db_file) if File.exist?(db_file)
159
end
160
161
# Load up DB and migrate if necessary
162
ActiveRecord::Base.logger = nil
163
OTR::ActiveRecord.configure_from_hash!(adapter:'sqlite3', database: db_file)
164
# otr-activerecord require you to manually establish the connection with the following line
165
#Also a check to confirm that the correct Gem version is installed to require it, likely easier for old systems.
166
if Gem.loaded_specs['otr-activerecord'].version > Gem::Version.create('1.4.2')
167
OTR::ActiveRecord.establish_connection!
168
end
169
170
# Migrate (if required)
171
ActiveRecord::Migration.verbose = false # silence activerecord migration stdout messages
172
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
173
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
174
if context.needs_migration?
175
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
176
end
177
178
BeEF::Core::Migration.instance.update_db!
179
180
# Spawn HTTP Server
181
# print_info "Starting HTTP Hook Server"
182
http_hook_server = BeEF::Core::Server.instance
183
http_hook_server.prepare
184
185
# Generate a token for the server to respond with
186
BeEF::Core::Crypto::api_token
187
188
# Initiate server start-up
189
BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server)
190
pid = fork do
191
http_hook_server.start
192
end
193
194
return pid
195
end
196
197
def beef_server_running?(uri_str)
198
begin
199
uri = URI.parse(uri_str)
200
response = Net::HTTP.get_response(uri)
201
response.is_a?(Net::HTTPSuccess)
202
rescue Errno::ECONNREFUSED
203
return false # Connection refused means the server is not running
204
rescue StandardError => e
205
return false # Any other error means the server is not running
206
end
207
end
208
209
def wait_for_beef_server_to_start(uri_str, timeout: 5)
210
start_time = Time.now # Record the time we started
211
until beef_server_running?(uri_str) || (Time.now - start_time) > timeout do
212
sleep 0.1 # Wait a bit before checking again
213
end
214
beef_server_running?(uri_str) # Return the result of the check
215
end
216
217
def start_beef_server_and_wait
218
puts "Starting BeEF server"
219
pid = start_beef_server
220
puts "BeEF server started with PID: #{pid}"
221
222
if wait_for_beef_server_to_start('http://localhost:3000', timeout: SERVER_START_TIMEOUT)
223
# print_info "Server started successfully."
224
else
225
print_error "Server failed to start within timeout."
226
end
227
228
pid
229
end
230
231
def stop_beef_server(pid)
232
exit if pid.nil?
233
# Shutting down server
234
Process.kill("KILL", pid) unless pid.nil?
235
Process.wait(pid) unless pid.nil? # Ensure the process has exited and the port is released
236
pid = nil
237
end
238
239
end
240
241