Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf_autoload.rb
25316 views
1
require 'zeitwerk'
2
3
###
4
# TODO:
5
# Apply zeitwerk to the rest of framework
6
# Namespacing wmap/events (when we're able to make potentially breaking changes)
7
# Correct namespacing to remove the custom inflector (or reduce it's complexity)
8
# Correct namespacing to cut down on inflector overrides
9
# Make the necessary changes to reduce/remove the ignored/collapsed files and folders
10
#
11
# I don't know why these are needed in `lib/msf/util/dot_net_deserialization/types.rb`
12
# require 'msf/util/dot_net_deserialization/types/primitives'
13
# require 'msf/util/dot_net_deserialization/types/general'
14
# require 'msf/util/dot_net_deserialization/types/record_values'
15
###
16
17
class MsfAutoload
18
include Singleton
19
20
def initialize
21
@loader = create_loader
22
configure(@loader)
23
finalize_loader(@loader)
24
end
25
26
class TempInflector < Zeitwerk::Inflector
27
def camelize(basename, abspath)
28
if basename == 'osx' && abspath.end_with?("#{__dir__}/msf/core/payload/osx", "#{__dir__}/msf/core/payload/osx.rb")
29
'Osx'
30
elsif basename == 'exe' && abspath.end_with?("#{__dir__}/msf/core/exe", "#{__dir__}/msf/core/exe.rb")
31
'Exe'
32
elsif basename == 'json' && abspath.end_with?("#{__dir__}/msf/base/serializer/json.rb")
33
'Json'
34
elsif basename == 'powershell' && abspath.end_with?("#{__dir__}/msf/base/sessions/powershell.rb")
35
'PowerShell'
36
elsif basename == 'ui' && abspath.end_with?("#{__dir__}/msf/core/module/ui", "#{__dir__}/msf/core/module/ui.rb", "#{__dir__}/rex/post/ui", "#{__dir__}/rex/post/ui.rb", "#{__dir__}/rex/post/meterpreter/extensions/stdapi/ui.rb")
37
'UI'
38
elsif basename == 'mysql' && abspath.end_with?("#{__dir__}/msf/core/exploit/remote/mysql.rb")
39
'MYSQL'
40
elsif basename == 'ssh' && abspath.end_with?("#{__dir__}/rex/proto/ssh")
41
'Ssh'
42
elsif basename == 'http' && abspath.end_with?("#{__dir__}/rex/proto/http")
43
'Http'
44
elsif basename == 'rftransceiver' && abspath.end_with?("#{__dir__}/rex/post/hwbridge/ui/console/command_dispatcher/rftransceiver.rb")
45
'RFtransceiver'
46
else
47
super
48
end
49
end
50
end
51
52
private
53
54
def ignore_list
55
[
56
"#{__dir__}/msf/core/modules/external/go/pkg",
57
"#{__dir__}/msf/core/constants.rb",
58
"#{__dir__}/msf/core/cert_provider.rb",
59
"#{__dir__}/msf/core/rpc/json/",
60
"#{__dir__}/msf/core/modules/external/ruby/metasploit.rb",
61
"#{__dir__}/msf/core/rpc/v10/constants.rb",
62
"#{__dir__}/msf/core.rb",
63
"#{__dir__}/msf/base.rb",
64
"#{__dir__}/rex/post/",
65
"#{__dir__}/rex/post.rb",
66
"#{__dir__}/rex/proto/ssh/hrr_rb_ssh.rb",
67
"#{__dir__}/rex/proto/ssh/connection.rb",
68
"#{__dir__}/rex/proto/kerberos/pac/krb5_pac.rb"
69
]
70
end
71
72
def collapse_list
73
[
74
"#{__dir__}/msf/core",
75
"#{__dir__}/msf/core/rpc/v10",
76
"#{__dir__}/msf/core/payload/osx/x64",
77
"#{__dir__}/msf/core/payload/windows/x64",
78
# "#{__dir__}/msf/core/payload/linux/x64",
79
"#{__dir__}/msf/core/web_services/servlet",
80
"#{__dir__}/msf/base",
81
"#{__dir__}/rex/parser/fs"
82
]
83
end
84
85
def custom_inflections
86
{
87
'uuid' => 'UUID',
88
'db_manager' => 'DBManager',
89
'ci' => 'CI',
90
'fusion_vm' => 'FusionVM',
91
'gpp' => 'GPP',
92
'ip360' => 'IP360',
93
'aspl' => 'ASPL',
94
'ip_list' => 'IPList',
95
'mbsa' => 'MBSA',
96
'xml' => 'XML',
97
'nbe' => 'NBE',
98
'open_vas' => 'OpenVAS',
99
'ip_address' => 'IPAddress',
100
'wmap' => 'WMAP',
101
'reflective_dll_loader' => 'ReflectiveDLLLoader',
102
'ssl' => 'SSL',
103
'reverse_tcp_double_ssl' => 'ReverseTcpDoubleSSL',
104
'rpc' => 'RPC',
105
'db_import_error' => 'DBImportError',
106
'db_export' => 'DBExport',
107
'extapi' => 'ExtAPI',
108
'nonalpha' => 'NonAlpha',
109
'nonupper' => 'NonUpper',
110
'natpmp' => 'NATPMP',
111
'udp_scanner' => 'UDPScanner',
112
'epmp' => 'EPMP',
113
'cnpilot' => 'CNPILOT',
114
'rservices' => 'RServices',
115
'ntp' => 'NTP',
116
'mqtt' => 'MQTT',
117
'iax2' => 'IAX2',
118
'pii' => 'PII',
119
'mdns' => 'MDNS',
120
'crand' => 'CRand',
121
'llmnr' => 'LLMNR',
122
'drdos' => 'DRDoS',
123
'jsp' => 'JSP',
124
'macho' => 'MachO',
125
'nodejs' => 'NodeJS',
126
'jsobfu' => 'JSObfu',
127
'vbsobfuscate' => 'VBSObfuscate',
128
'osx' => 'OSX',
129
'webrtc' => 'WebRTC',
130
'json' => 'JSON',
131
'sip' => 'SIP',
132
'ntlm' => 'NTLM',
133
'mssql_commands' => 'MSSQL_COMMANDS',
134
'mssql' => 'MSSQL',
135
'pdf' => 'PDF',
136
'fileformat' => 'FILEFORMAT',
137
'http' => 'HTTP',
138
'html' => 'HTML',
139
'pdf_parse' => 'PDF_Parse',
140
'vim_soap' => 'VIMSoap',
141
'ndmp' => 'NDMP',
142
'ndmp_socket' => 'NDMPSocket',
143
'dcerpc' => 'DCERPC',
144
'dcerpc_mgmt' => 'DCERPC_MGMT',
145
'dcerpc_epm' => 'DCERPC_EPM',
146
'dcerpc_lsa' => 'DCERPC_LSA',
147
'wdbrpc_client' => 'WDBRPC_Client',
148
'sunrpc' => 'SunRPC',
149
'mysql' => 'MySQL',
150
'ldap' => 'LDAP',
151
'sqli' => 'SQLi',
152
'dhcp_server' => 'DHCPServer',
153
'tns' => 'TNS',
154
'oracle' => 'ORACLE',
155
'dect_coa' => 'DECT_COA',
156
'wdbrpc' => 'WDBRPC',
157
'exe' => 'EXE',
158
'php_exe' => 'PhpEXE',
159
'mssql_sqli' => 'MSSQL_SQLI',
160
'snmp_client' => 'SNMPClient',
161
'afp' => 'AFP',
162
'zeromq' => 'ZeroMQ',
163
'tftp_server' => 'TFTPServer',
164
'db2' => 'DB2',
165
'rdp' => 'RDP',
166
'riff' => 'RIFF',
167
'dns' => 'DNS',
168
'smtp_deliver' => 'SMTPDeliver',
169
'send_uuid' => 'SendUUID',
170
'exec_x64' => 'Exec_x64',
171
'reflective_dll_injection' => 'ReflectiveDLLInjection',
172
'reflective_pe_loader' => 'ReflectivePELoader',
173
'pe_inject' => 'PEInject',
174
'payload_db_conf' => 'PayloadDBConf',
175
'reverse_tcp_x86' => 'ReverseTcp_x86',
176
'reverse_tcp_aarch64' => 'ReverseTcp_Aarch64',
177
'ruby_dl' => 'RubyDL',
178
'wmic' => 'WMIC',
179
'net_api' => 'NetAPI',
180
'rpc_base' => 'RPC_Base',
181
'rpc_plugin' => 'RPC_Plugin',
182
'rpc_db' => 'RPC_Db',
183
'rpc_console' => 'RPC_Console',
184
'rpc_session' => 'RPC_Session',
185
'rpc_auth' => 'RPC_Auth',
186
'rpc_job' => 'RPC_Job',
187
'rpc_core' => 'RPC_Core',
188
'rpc_health' => 'RPC_Health',
189
'rpc_module' => 'RPC_Module',
190
'cli' => 'CLI',
191
'sqlitei' => 'SQLitei',
192
'mysqli' => 'MySQLi',
193
'postgresql' => 'PostgreSQL',
194
'postgresqli' => 'PostgreSQLi',
195
'ssh' => 'SSH',
196
'winrm' => 'WinRM',
197
'smb' => 'SMB',
198
'uris' => 'URIs',
199
'jboss' => 'JBoss',
200
'send_uuid_x64' => 'SendUUID_x64',
201
'reverse_tcp_x64' => 'ReverseTcp_x64',
202
'reverse_sctp_x64' => 'ReverseSctp_x64',
203
'block_api_x64' => 'BlockApi_x64',
204
'exitfunk_x64' => 'Exitfunk_x64',
205
'reverse_http_x64' => 'ReverseHttp_x64',
206
'rc4_x64' => 'Rc4_x64',
207
'bind_tcp_x64' => 'BindTcp_x64',
208
'reverse_win_http_x64' => 'ReverseWinHttp_x64',
209
'reflective_dll_inject_x64' => 'ReflectiveDllInject_x64',
210
'reverse_win_https_x64' => 'ReverseWinHttps_x64',
211
'reflective_pe_loader_x64' => 'ReflectivePELoader_x64',
212
'migrate_http_x64' => 'MigrateHttp_x64',
213
'migrate_common_x64' => 'MigrateCommon_x64',
214
'migrate_tcp_x64' => 'MigrateTcp_x64',
215
'migrate_named_pipe_x64' => 'MigrateNamedPipe_x64',
216
'reverse_named_pipe_x64' => 'ReverseNamedPipe_x64',
217
'meterpreter_loader_x64' => 'MeterpreterLoader_x64',
218
'rftransceiver' => 'RFTransceiver',
219
'dtc' => 'DTC',
220
'uds' => 'UDS',
221
'v1_0' => 'V1_0',
222
'php_include' => 'PHPInclude',
223
'psexec_ms17_010' => 'Psexec_MS17_010',
224
'bind_tcp_rc4_x64' => 'BindTcpRc4_x64',
225
'reverse_tcp_rc4_x64' => 'ReverseTcpRc4_x64',
226
'reverse_https_x64' => 'ReverseHttps_x64',
227
'bind_named_pipe_x64' => 'BindNamedPipe_x64',
228
'addr_loader' => 'AddrLoader_x64',
229
'db_manager_proxy' => 'DBManagerProxy',
230
'wmap_scan_ssl' => 'WmapScanSSL',
231
'http_db_manager_service' => 'HttpDBManagerService',
232
'vyos' => 'VYOS',
233
'windows_constants' => 'Windows_Constants',
234
'tty' => 'TTY',
235
'meterpreter_java' => 'Meterpreter_Java_Java',
236
'meterpreter_android' => 'Meterpreter_Java_Android',
237
'meterpreter_zarch_linux' => 'Meterpreter_zarch_Linux',
238
'meterpreter_python' => 'Meterpreter_Python_Python',
239
'meterpreter_ppce500v2_linux' => 'Meterpreter_ppce500v2_Linux',
240
'meterpreter_x86_osx' => 'Meterpreter_x86_OSX',
241
'meterpreter_armbe_linux' => 'Meterpreter_armbe_Linux',
242
'meterpreter_ppc64le_linux' => 'Meterpreter_ppc64le_Linux',
243
'meterpreter_x64_linux' => 'Meterpreter_x64_Linux',
244
'meterpreter_armle_linux' => 'Meterpreter_armle_Linux',
245
'meterpreter_aarch64_linux' => 'Meterpreter_aarch64_Linux',
246
'meterpreter_x86_win' => 'Meterpreter_x86_Win',
247
'meterpreter_armle_apple_ios' => 'Meterpreter_armle_Apple_iOS',
248
'meterpreter_mipsle_linux' => 'Meterpreter_mipsle_Linux',
249
'meterpreter_x86_bsd' => 'Meterpreter_x86_BSD',
250
'meterpreter_mips64_linux' => 'Meterpreter_mips64_Linux',
251
'meterpreter_x86_linux' => 'Meterpreter_x86_Linux',
252
'meterpreter_mipsbe_linux' => 'Meterpreter_mipsbe_Linux',
253
'meterpreter_aarch64_apple_ios' => 'Meterpreter_aarch64_Apple_iOS',
254
'meterpreter_x64_osx' => 'Meterpreter_x64_OSX',
255
'meterpreter_aarch64_osx' => 'Meterpreter_aarch64_OSX',
256
'meterpreter_ppc_linux' => 'Meterpreter_ppc_Linux',
257
'meterpreter_x64_win' => 'Meterpreter_x64_Win',
258
'meterpreter_php' => 'Meterpreter_Php_Php',
259
'meterpreter_multi' => 'Meterpreter_Multi',
260
'hwbridge' => 'HWBridge',
261
'vncinject_options' => 'VncInjectOptions',
262
'vncinject' => 'VncInject',
263
'json_hash_file' => 'JSONHashFile',
264
'jwt' => 'JWT',
265
'ndr' => 'NDR',
266
'ci_document' => 'CIDocument',
267
'fusionvm_document' => 'FusionVMDocument',
268
'group_policy_preferences' => 'GPP',
269
'ip360_aspl_xml' => 'IP360ASPLXMLStreamParser',
270
'ip360_xml' => 'IP360XMLStreamParser',
271
'nessus_xml' => 'NessusXMLStreamParser',
272
'netsparker_xml' => 'NetSparkerXMLStreamParser',
273
'nexpose_xml' => 'NexposeXMLStreamParser',
274
'nmap_xml' => 'NmapXMLStreamParser',
275
'openvas_document' => 'OpenVASDocument',
276
'retina_xml' => 'RetinaXMLStreamParser',
277
'graphml' => 'GraphML',
278
'apple_backup_manifestdb' => 'AppleBackupManifestDB',
279
'winscp' => 'WinSCP',
280
'acpp' => 'ACPP',
281
'tftp' => 'TFTP',
282
'ipmi' => 'IPMI',
283
'channel_auth_reply' => 'Channel_Auth_Reply',
284
'open_session_reply' => 'Open_Session_Reply',
285
'rakp2' => 'RAKP2',
286
'pjl' => 'PJL',
287
'dhcp' => 'DHCP',
288
'addp' => 'ADDP',
289
'rfb' => 'RFB',
290
'io' => 'IO',
291
'ntfs' => 'NTFS',
292
'bitlocker' => 'BITLOCKER',
293
'adb' => 'ADB',
294
'drda' => 'DRDA',
295
'tlv' => 'TLV',
296
'svcctl' => 'SVCCTL',
297
'wdscp' => 'WDSCP',
298
'appapi' => 'AppApi',
299
'uds_errors' => 'UDSErrors',
300
'smb_hash_capture' => 'SMBHashCapture',
301
'rex_ntlm' => 'RexNTLM',
302
'teamcity' => 'TeamCity',
303
'nist_sp_800_38f' => 'NIST_SP_800_38f',
304
'nist_sp_800_108' => 'NIST_SP_800_108',
305
'sccm' => 'SCCM',
306
'pfsense' => 'PfSense',
307
'opnsense' => 'OPNSense',
308
'pgadmin' => 'PgAdmin',
309
}
310
end
311
312
def config_paths
313
[
314
{ path: "#{__dir__}/msf/", namespace: Msf },
315
{ path: "#{__dir__}/rex/", namespace: Rex }
316
]
317
end
318
319
# Enables :prepend to inject existing loader
320
def create_loader
321
Zeitwerk::Loader.new
322
end
323
324
# Enables :prepend to override the configuration items pass to the loader
325
def configure(loader)
326
config_paths.each do |entry|
327
if entry[:namespace]
328
loader.push_dir(entry[:path], namespace: entry[:namespace])
329
else
330
loader.push_dir(entry[:path])
331
end
332
end
333
loader.ignore(ignore_list)
334
loader.collapse(collapse_list)
335
loader.inflector = TempInflector.new
336
loader.inflector.inflect(custom_inflections)
337
end
338
339
# Enables :prepend to suppress the loader finalization
340
def finalize_loader(loader)
341
loader.setup # ready!
342
end
343
end
344
345
# global autoload of common gems
346
autoload :Faker, 'faker'
347
autoload :BinData, 'bindata'
348
autoload :RubySMB, 'ruby_smb'
349
autoload :MetasploitPayloads, 'metasploit-payloads'
350
autoload :PacketFu, 'packetfu'
351
autoload :DidYouMean, 'did_you_mean'
352
353
require 'rexml/document'
354
# Load IO#expect moneypatch
355
require 'expect'
356
357
# XXX: Should be removed once the `lib/metasploit` folder is loaded by Zeitwerk
358
require 'metasploit/framework/hashes'
359
360