Skip to content

Commit c7665fb

Browse files
authored
Merge pull request #3386 from beefproject/red/dev
Fix forked SQLite connection handling to improve test accuracy (#3249)
2 parents 2300a24 + 31decb8 commit c7665fb

10 files changed

Lines changed: 232 additions & 65 deletions

File tree

.github/workflows/github_actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ jobs:
5252
- name: 'BrowserStackLocal Stop' # Terminating the BrowserStackLocal tunnel connection
5353
uses: browserstack/github-actions/setup-local@master
5454
with:
55-
local-testing: stop
55+
local-testing: stop

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,6 @@ node_modules/
131131
# Generated files
132132
out/
133133
doc/rdoc/
134+
135+
# Secrets for testing github actions locally
136+
.secrets

Gemfile.lock

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ GEM
2222
addressable (2.8.7)
2323
public_suffix (>= 2.0.2, < 7.0)
2424
ansi (1.5.0)
25-
archive-zip (0.12.0)
26-
io-like (~> 0.3.0)
25+
archive-zip (0.13.1)
26+
io-like (~> 0.4.0)
2727
ast (2.4.3)
2828
async (1.32.1)
2929
console (~> 1.10)
@@ -49,7 +49,7 @@ GEM
4949
coderay (1.1.3)
5050
concurrent-ruby (1.3.5)
5151
connection_pool (2.5.3)
52-
console (1.29.2)
52+
console (1.33.0)
5353
fiber-annotation
5454
fiber-local (~> 1.1)
5555
json
@@ -71,7 +71,7 @@ GEM
7171
fiber-annotation (0.2.0)
7272
fiber-local (1.1.0)
7373
fiber-storage
74-
fiber-storage (1.0.0)
74+
fiber-storage (1.0.1)
7575
geckodriver-helper (0.24.0)
7676
archive-zip (~> 0.7)
7777
http-accept (1.7.0)
@@ -80,9 +80,9 @@ GEM
8080
http_parser.rb (0.8.0)
8181
i18n (1.14.7)
8282
concurrent-ruby (~> 1.0)
83-
io-console (0.8.0)
84-
io-endpoint (0.15.1)
85-
io-like (0.3.1)
83+
io-console (0.8.1)
84+
io-endpoint (0.15.2)
85+
io-like (0.4.0)
8686
irb (1.15.2)
8787
pp (>= 0.6.0)
8888
rdoc (>= 4.0.0)
@@ -91,29 +91,43 @@ GEM
9191
language_server-protocol (3.17.0.5)
9292
lint_roller (1.1.0)
9393
logger (1.7.0)
94-
matrix (0.4.2)
94+
matrix (0.4.3)
9595
maxmind-db (1.3.2)
9696
method_source (1.1.0)
9797
mime-types (3.7.0)
9898
logger
9999
mime-types-data (~> 3.2025, >= 3.2025.0507)
100-
mime-types-data (3.2025.0520)
100+
mime-types-data (3.2025.0819)
101101
mini_mime (1.1.5)
102102
minitest (5.25.5)
103103
mojo_magick (0.6.8)
104104
msfrpc-client (1.1.2)
105105
msgpack (~> 1)
106-
msgpack (1.7.5)
107-
mustermann (3.0.3)
106+
msgpack (1.8.0)
107+
mustermann (3.0.4)
108108
ruby2_keywords (~> 0.0.1)
109109
net-protocol (0.2.2)
110110
timeout
111111
net-smtp (0.5.1)
112112
net-protocol
113113
netrc (0.11.0)
114114
nio4r (2.7.4)
115+
nokogiri (1.18.9-aarch64-linux-gnu)
116+
racc (~> 1.4)
117+
nokogiri (1.18.9-aarch64-linux-musl)
118+
racc (~> 1.4)
119+
nokogiri (1.18.9-arm-linux-gnu)
120+
racc (~> 1.4)
121+
nokogiri (1.18.9-arm-linux-musl)
122+
racc (~> 1.4)
123+
nokogiri (1.18.9-arm64-darwin)
124+
racc (~> 1.4)
125+
nokogiri (1.18.9-x86_64-darwin)
126+
racc (~> 1.4)
115127
nokogiri (1.18.9-x86_64-linux-gnu)
116128
racc (~> 1.4)
129+
nokogiri (1.18.9-x86_64-linux-musl)
130+
racc (~> 1.4)
117131
otr-activerecord (2.5.0)
118132
activerecord (>= 6.0, < 8.1)
119133
parallel (1.27.0)
@@ -135,7 +149,7 @@ GEM
135149
psych (5.2.6)
136150
date
137151
stringio
138-
public_suffix (6.0.1)
152+
public_suffix (6.0.2)
139153
qr4r (0.6.2)
140154
mojo_magick (~> 0.6.5)
141155
rqrcode_core (~> 1.0)
@@ -152,7 +166,7 @@ GEM
152166
erb
153167
psych (>= 4.0.0)
154168
regexp_parser (2.11.2)
155-
reline (0.6.0)
169+
reline (0.6.2)
156170
io-console (~> 0.5)
157171
rest-client (2.1.0)
158172
http-accept (>= 1.7.0, < 2.0)
@@ -161,20 +175,20 @@ GEM
161175
netrc (~> 0.8)
162176
rexml (3.4.1)
163177
rqrcode_core (1.2.0)
164-
rr (3.1.1)
178+
rr (3.1.2)
165179
rspec (3.13.1)
166180
rspec-core (~> 3.13.0)
167181
rspec-expectations (~> 3.13.0)
168182
rspec-mocks (~> 3.13.0)
169-
rspec-core (3.13.4)
183+
rspec-core (3.13.5)
170184
rspec-support (~> 3.13.0)
171185
rspec-expectations (3.13.5)
172186
diff-lcs (>= 1.2.0, < 2.0)
173187
rspec-support (~> 3.13.0)
174188
rspec-mocks (3.13.5)
175189
diff-lcs (>= 1.2.0, < 2.0)
176190
rspec-support (~> 3.13.0)
177-
rspec-support (3.13.4)
191+
rspec-support (3.13.5)
178192
rubocop (1.80.0)
179193
json (~> 2.3)
180194
language_server-protocol (~> 3.17.0.2)
@@ -208,12 +222,19 @@ GEM
208222
rack-protection (= 3.2.0)
209223
tilt (~> 2.0)
210224
slack-notifier (2.4.0)
225+
sqlite3 (2.7.3-aarch64-linux-gnu)
226+
sqlite3 (2.7.3-aarch64-linux-musl)
227+
sqlite3 (2.7.3-arm-linux-gnu)
228+
sqlite3 (2.7.3-arm-linux-musl)
229+
sqlite3 (2.7.3-arm64-darwin)
230+
sqlite3 (2.7.3-x86_64-darwin)
211231
sqlite3 (2.7.3-x86_64-linux-gnu)
232+
sqlite3 (2.7.3-x86_64-linux-musl)
212233
stringio (3.1.7)
213234
sync (0.5.0)
214235
term-ansicolor (1.11.2)
215236
tins (~> 1.0)
216-
test-unit (3.6.7)
237+
test-unit (3.7.0)
217238
power_assert
218239
test-unit-context (0.5.1)
219240
test-unit (>= 2.4.0)
@@ -234,10 +255,10 @@ GEM
234255
daemons (~> 1.0, >= 1.0.9)
235256
eventmachine (~> 1.0, >= 1.0.4)
236257
rack (>= 1, < 3)
237-
tilt (2.5.0)
258+
tilt (2.6.1)
238259
timeout (0.4.3)
239260
timers (4.4.0)
240-
tins (1.37.1)
261+
tins (1.42.0)
241262
bigdecimal
242263
sync
243264
tzinfo (2.0.6)
@@ -258,7 +279,14 @@ GEM
258279
nokogiri (~> 1.8)
259280

260281
PLATFORMS
261-
x86_64-linux
282+
aarch64-linux-gnu
283+
aarch64-linux-musl
284+
arm-linux-gnu
285+
arm-linux-musl
286+
arm64-darwin
287+
x86_64-darwin
288+
x86_64-linux-gnu
289+
x86_64-linux-musl
262290

263291
DEPENDENCIES
264292
activerecord (~> 7.2)
@@ -305,4 +333,4 @@ DEPENDENCIES
305333
xmlrpc (~> 0.3.3)
306334

307335
BUNDLED WITH
308-
2.4.10
336+
2.5.18

spec/beef/core/main/autorun_engine/autorun_engine_spec.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
print_info 'Loading database'
1919
db_file = @config.get('beef.database.file')
2020
print_info 'Resetting the database for BeEF.'
21-
File.delete(db_file) if File.exist?(db_file)
21+
22+
if ENV['RESET_DB']
23+
File.delete(db_file) if File.exist?(db_file)
24+
end
2225

2326
@config.set('beef.credentials.user', 'beef')
2427
@config.set('beef.credentials.passwd', 'beef')
@@ -49,9 +52,13 @@
4952
OTR::ActiveRecord.establish_connection!
5053
end
5154
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
52-
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
53-
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate if context.needs_migration?
54-
55+
MUTEX.synchronize do
56+
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
57+
if context.needs_migration?
58+
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
59+
end
60+
end
61+
5562
BeEF::Core::Migration.instance.update_db!
5663

5764
# add AutoRunEngine rule
@@ -63,16 +70,17 @@
6370
# Spawn HTTP Server
6471
print_info 'Starting HTTP Hook Server'
6572
http_hook_server = BeEF::Core::Server.instance
66-
http_hook_server.prepare
6773

6874
# Generate a token for the server to respond with
6975
@token = BeEF::Core::Crypto.api_token
7076

77+
# ***** IMPORTANT: close any and all AR/OTR connections before forking *****
78+
disconnect_all_active_record!
79+
7180
# Initiate server start-up
72-
@pids = fork do
73-
BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server)
74-
end
7581
@pid = fork do
82+
http_hook_server.prepare
83+
BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server)
7684
http_hook_server.start
7785
end
7886

@@ -101,6 +109,7 @@
101109

102110
after(:all) do
103111
server_teardown(@driver, @pid, @pids)
112+
disconnect_all_active_record!
104113
end
105114

106115
it 'AutoRunEngine is working' do

spec/beef/core/main/handlers/browser_details_handler_spec.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
@config = BeEF::Core::Configuration.instance
1717
db_file = @config.get('beef.database.file')
1818
print_info 'Resetting the database for BeEF.'
19-
File.delete(db_file) if File.exist?(db_file)
19+
20+
if ENV['RESET_DB']
21+
File.delete(db_file) if File.exist?(db_file)
22+
end
23+
2024
@config.set('beef.credentials.user', 'beef')
2125
@config.set('beef.credentials.passwd', 'beef')
2226
@username = @config.get('beef.credentials.user')
@@ -50,24 +54,28 @@
5054

5155
# Migrate (if required)
5256
ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')]
53-
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
54-
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate if context.needs_migration?
55-
57+
MUTEX.synchronize do
58+
context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
59+
if context.needs_migration?
60+
ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate
61+
end
62+
end
5663
BeEF::Core::Migration.instance.update_db!
5764

5865
# Spawn HTTP Server
5966
print_info 'Starting HTTP Hook Server'
6067
http_hook_server = BeEF::Core::Server.instance
61-
http_hook_server.prepare
6268

6369
# Generate a token for the server to respond with
6470
@token = BeEF::Core::Crypto.api_token
6571

72+
# ***** IMPORTANT: close any and all AR/OTR connections before forking *****
73+
disconnect_all_active_record!
74+
6675
# Initiate server start-up
67-
@pids = fork do
68-
BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server)
69-
end
7076
@pid = fork do
77+
http_hook_server.prepare
78+
BeEF::API::Registrar.instance.fire(BeEF::API::Server, 'pre_http_start', http_hook_server)
7179
http_hook_server.start
7280
end
7381

@@ -97,6 +105,7 @@
97105

98106
after(:all) do
99107
server_teardown(@driver, @pid, @pids)
108+
disconnect_all_active_record!
100109
end
101110

102111
it 'can successfully hook a browser' do

spec/beef/core/main/network_stack/handlers/dynamic_reconstruction_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
@server = Thin::Server.new('127.0.0.1', @port.to_s, @rackApp)
1313
trap("INT") { @server.stop }
1414
trap("TERM") { @server.stop }
15+
16+
# ***** IMPORTANT: close any and all AR/OTR connections before forking *****
17+
disconnect_all_active_record!
18+
1519
@pid = fork do
1620
@server.start!
1721
end

spec/beef/core/main/network_stack/handlers/redirector_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
@server = Thin::Server.new('127.0.0.1', @port.to_s, @rackApp)
1313
trap("INT") { @server.stop }
1414
trap("TERM") { @server.stop }
15+
16+
17+
# ***** IMPORTANT: close any and all AR/OTR connections before forking *****
18+
disconnect_all_active_record!
19+
1520
@pid = fork do
1621
@server.start!
1722
end

0 commit comments

Comments
 (0)