Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/models/command.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
7
module BeEF
8
module Core
9
module Models
10
# @note Table stores the commands that have been sent to the Hooked Browsers.
11
class Command < BeEF::Core::Model
12
has_many :results
13
has_one :command_module
14
has_one :hooked_browser
15
16
#
17
# Save results and flag that the command has been run on the hooked browser
18
#
19
# @param [String] hook_session_id The session_id.
20
# @param [String] command_id The command_id.
21
# @param [String] command_friendly_name The command friendly name.
22
# @param [String] result The result of the command module.
23
#
24
def self.save_result(hook_session_id, command_id, command_friendly_name, result, status)
25
# @note argument type checking
26
raise TypeError, '"hook_session_id" needs to be a string' unless hook_session_id.is_a?(String)
27
raise TypeError, '"command_id" needs to be an integer' unless command_id.is_a?(Integer)
28
raise TypeError, '"command_friendly_name" needs to be a string' unless command_friendly_name.is_a?(String)
29
raise TypeError, '"result" needs to be a hash' unless result.is_a?(Hash)
30
raise TypeError, '"status" needs to be an integer' unless status.is_a?(Integer)
31
32
# @note get the hooked browser structure and id from the database
33
hooked_browser = BeEF::Core::Models::HookedBrowser.where(session: hook_session_id).first || nil
34
raise TypeError, 'hooked_browser is nil' if hooked_browser.nil?
35
raise TypeError, 'hooked_browser.id is nil' if hooked_browser.id.nil?
36
37
# @note get the command module data structure from the database
38
command = where(id: command_id, hooked_browser_id: hooked_browser.id).first || nil
39
raise TypeError, 'command is nil' if command.nil?
40
41
# @note create the entry for the results
42
BeEF::Core::Models::Result.create(
43
hooked_browser_id: hooked_browser.id,
44
command_id: command.id,
45
data: result.to_json,
46
status: status,
47
date: Time.now.to_i
48
)
49
50
s = show_status(status)
51
log = "Hooked browser [id:#{hooked_browser.id}, ip:#{hooked_browser.ip}]"
52
log += " has executed instructions (status: #{s}) from command module [cid:#{command_id},"
53
log += " mod: #{command.command_module_id}, name:'#{command_friendly_name}']"
54
BeEF::Core::Logger.instance.register('Command', log, hooked_browser.id)
55
print_info log
56
57
true
58
end
59
60
# @note show status
61
def self.show_status(status)
62
case status
63
when -1
64
'ERROR'
65
when 1
66
'SUCCESS'
67
else
68
'UNKNOWN'
69
end
70
end
71
end
72
end
73
end
74
end
75
76