Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/requester/handler.rb
1154 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
module BeEF
7
module Extension
8
module Requester
9
#
10
# The http handler that manages the Requester.
11
#
12
class Handler
13
H = BeEF::Core::Models::Http
14
Z = BeEF::Core::Models::HookedBrowser
15
16
def initialize(data)
17
@data = data
18
setup
19
end
20
21
def setup
22
# validates the hook token
23
beef_hook = @data['beefhook'] || nil
24
if beef_hook.nil?
25
print_error 'beefhook is null'
26
return
27
end
28
29
# validates the request id
30
request_id = @data['cid'].to_s
31
if request_id == ''
32
print_error 'Original request id (command id) is null'
33
return
34
end
35
36
unless BeEF::Filters.nums_only?(request_id)
37
print_error 'Original request id (command id) is invalid'
38
return
39
end
40
41
# validates that a hooked browser with the beef_hook token exists in the db
42
zombie_db = Z.where(session: beef_hook).first || nil
43
if zombie_db.nil?
44
(print_error 'Invalid beefhook id: the hooked browser cannot be found in the database'
45
return)
46
end
47
48
# validates that we have such a http request saved in the db
49
http_db = H.where(id: request_id.to_i, hooked_browser_id: zombie_db.session).first || nil
50
if http_db.nil?
51
print_error 'Invalid http_db: no such request found in the database'
52
return
53
end
54
55
# validates that the http request has not been run before
56
if http_db.has_ran.eql? 'complete'
57
(print_error 'This http request has been saved before'
58
return)
59
end
60
61
# validates the response code
62
response_code = @data['results']['response_status_code'] || nil
63
if response_code.nil?
64
(print_error 'Http response code is null'
65
return)
66
end
67
68
# save the results in the database
69
http_db.response_headers = @data['results']['response_headers']
70
http_db.response_status_code = @data['results']['response_status_code']
71
http_db.response_status_text = @data['results']['response_status_text']
72
http_db.response_port_status = @data['results']['response_port_status']
73
http_db.response_data = @data['results']['response_data']
74
http_db.response_date = Time.now
75
http_db.has_ran = 'complete'
76
77
# Store images as binary
78
# see issue https://github.com/beefproject/beef/issues/449
79
http_db.response_data = http_db.response_data.unpack('a*') if http_db.response_headers =~ /Content-Type: image/
80
81
http_db.save
82
end
83
end
84
end
85
end
86
end
87
88