CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/base/serializer/json.rb
Views: 1904
1
# -*- coding: binary -*-
2
# rubocop:disable Metrics/AbcSize
3
# rubocop:disable Metrics/ClassLength
4
# rubocop:disable Metrics/CyclomaticComplexity
5
module Msf
6
module Serializer
7
#
8
# This class formats information in a json format that
9
# is meant to be displayed on a console or some other non-GUI
10
# medium.
11
class Json
12
#
13
# Returns a formatted string that contains information about
14
# the supplied module instance.
15
#
16
# @param mod [Msf::Module] the module to dump information for.
17
# @param _indent [String] the indentation to use.
18
# @return [String] formatted text output of the dump.
19
def self.dump_module(mod, _indent = "")
20
case mod.type
21
when Msf::MODULE_PAYLOAD
22
return dump_payload_module(mod)
23
when Msf::MODULE_NOP
24
return dump_basic_module(mod)
25
when Msf::MODULE_ENCODER
26
return dump_basic_module(mod)
27
when Msf::MODULE_EXPLOIT
28
return dump_exploit_module(mod)
29
when Msf::MODULE_AUX
30
return dump_auxiliary_module(mod)
31
when Msf::MODULE_POST
32
return dump_post_module(mod)
33
else
34
return dump_basic_module(mod)
35
end
36
end
37
38
# Dumps an exploit's targets.
39
#
40
# @param mod [Msf::Exploit] the exploit module to dump targets
41
# for.
42
# @return [Array] the exploit targets
43
def self.dump_exploit_targets(mod)
44
list = []
45
46
mod.targets.each { |target| list.push(target.name || 'All') }
47
48
list
49
end
50
51
# Dumps a module's actions
52
#
53
# @param mod [Msf::Module] the module.
54
# @return [Array] the module actions
55
def self.dump_module_actions(mod)
56
list = []
57
58
mod.actions.each do |target|
59
list.push('name' => (target.name || 'All'),
60
'description' => (target.description || ''))
61
end
62
63
list
64
end
65
66
# Dumps the module's selected action
67
#
68
# @param mod [Msf::Module] the module.
69
# @return [Array] the module options
70
def self.dump_module_action(mod)
71
list = []
72
73
list.push('name' => (mod.action.name || 'All'),
74
'description' => (mod.action.description || ''))
75
76
list
77
end
78
79
# Dumps information common to all modules
80
def self.dump_common_module_info(mod)
81
{
82
'name' => mod.name,
83
'fullname' => mod.fullname,
84
'authors' => dump_authors(mod),
85
'rank' => mod.rank_to_s.capitalize,
86
'description' => Rex::Text.compress(mod.description),
87
'options' => dump_options(mod)
88
}
89
end
90
91
# Dumps information about an exploit module.
92
#
93
# @param mod [Msf::Exploit] the exploit module.
94
# @return [String] the json string form of the information.
95
def self.dump_exploit_module(mod)
96
# Return a json dump of exploit module data
97
{
98
'platform' => mod.platform_to_s,
99
'privileged' => (mod.privileged? ? "Yes" : "No"),
100
'license' => mod.license,
101
'disclosure_date' => (mod.disclosure_date if mod.disclosure_date),
102
'payload' => {
103
'space' => (mod.payload_space.to_s if mod.payload_space),
104
'badchars' => (mod.payload_badchars.length.to_s if mod.payload_badchars)
105
},
106
'references' => dump_references(mod)
107
}.merge(dump_common_module_info(mod)).to_json
108
end
109
110
# Dumps information about an auxiliary module.
111
#
112
# @param mod [Msf::Auxiliary] the auxiliary module.
113
# @return [String] the string form of the information.
114
def self.dump_auxiliary_module(mod)
115
# Return a json dump of auxiliary module data
116
{
117
'license' => mod.license,
118
'disclosure_date' => (mod.disclosure_date if mod.disclosure_date),
119
'actions' => dump_module_actions(mod),
120
'references' => dump_references(mod)
121
}.merge(dump_common_module_info(mod)).to_json
122
end
123
124
# Dumps information about a post module.
125
#
126
# @param mod [Msf::Post] the post module.
127
# @return [String] the string form of the information.
128
def self.dump_post_module(mod)
129
# Return a json dump of post module data
130
{
131
'platform' => mod.platform_to_s,
132
'arch' => mod.arch_to_s,
133
'disclosure_date' => (mod.disclosure_date if mod.disclosure_date),
134
'actions' => dump_module_actions(mod),
135
'references' => dump_references(mod)
136
}.merge(dump_common_module_info(mod)).to_json
137
end
138
139
# Dumps information about a payload module.
140
#
141
# @param mod [Msf::Payload] the payload module.
142
# @return [String] the string form of the information.
143
def self.dump_payload_module(mod)
144
# Return a json dump of post module data
145
{
146
'platform' => mod.platform_to_s,
147
'arch' => mod.arch_to_s,
148
'privileged' => (mod.privileged? ? "true" : "false"),
149
'size' => mod.size
150
}.merge(dump_common_module_info(mod)).to_json
151
end
152
153
# Returns an array of all authors
154
#
155
# @param mod [Msf::Module]
156
# @return [Array] an array of all authors
157
def self.dump_authors(mod)
158
# Authors
159
authors = []
160
mod.each_author { |author| authors.push(author.to_s) }
161
authors
162
end
163
164
# Dumps information about a module, just the basics.
165
#
166
# @param mod [Msf::Module] the module.
167
# @return [String] the string form of the information.
168
def self.dump_basic_module(mod)
169
{
170
'platform' => mod.platform_to_s,
171
'arch' => mod.arch_to_s,
172
'references' => dump_references(mod)
173
}.merge(dump_common_module_info(mod)).to_json
174
end
175
176
# Dumps the list of options associated with the
177
# supplied module.
178
#
179
# @param mod [Msf::Module] the module.
180
# @return [Array] the array of the information.
181
def self.dump_options(mod)
182
list = []
183
mod.options.sorted.each do |entry|
184
name, opt = entry
185
val = mod.datastore[name] || opt.default
186
187
next if opt.advanced? || opt.evasion?
188
189
list.push('name' => name,
190
'display_value' => opt.display_value(val),
191
'required' => opt.required? ? 'true' : 'false',
192
'description' => opt.desc.strip)
193
end
194
195
list
196
end
197
198
# Dumps the references associated with the supplied module.
199
#
200
# @param mod [Msf::Module] the module.
201
# @return [Array] the array of the information.
202
def self.dump_references(mod)
203
if (mod.respond_to? :references) && mod.references && (mod.references.length > 0)
204
refs = []
205
mod.references.each { |ref| refs.push(ref.to_s) }
206
end
207
208
refs
209
end
210
end
211
end
212
end
213
214