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