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_autoload.rb
Views: 1903
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
'osx' => 'OSX',
128
'webrtc' => 'WebRTC',
129
'json' => 'JSON',
130
'sip' => 'SIP',
131
'ntlm' => 'NTLM',
132
'mssql_commands' => 'MSSQL_COMMANDS',
133
'mssql' => 'MSSQL',
134
'pdf' => 'PDF',
135
'fileformat' => 'FILEFORMAT',
136
'http' => 'HTTP',
137
'html' => 'HTML',
138
'pdf_parse' => 'PDF_Parse',
139
'vim_soap' => 'VIMSoap',
140
'ndmp' => 'NDMP',
141
'ndmp_socket' => 'NDMPSocket',
142
'dcerpc' => 'DCERPC',
143
'dcerpc_mgmt' => 'DCERPC_MGMT',
144
'dcerpc_epm' => 'DCERPC_EPM',
145
'dcerpc_lsa' => 'DCERPC_LSA',
146
'wdbrpc_client' => 'WDBRPC_Client',
147
'sunrpc' => 'SunRPC',
148
'mysql' => 'MySQL',
149
'ldap' => 'LDAP',
150
'sqli' => 'SQLi',
151
'dhcp_server' => 'DHCPServer',
152
'tns' => 'TNS',
153
'oracle' => 'ORACLE',
154
'dect_coa' => 'DECT_COA',
155
'wdbrpc' => 'WDBRPC',
156
'exe' => 'EXE',
157
'php_exe' => 'PhpEXE',
158
'mssql_sqli' => 'MSSQL_SQLI',
159
'snmp_client' => 'SNMPClient',
160
'afp' => 'AFP',
161
'zeromq' => 'ZeroMQ',
162
'tftp_server' => 'TFTPServer',
163
'db2' => 'DB2',
164
'rdp' => 'RDP',
165
'riff' => 'RIFF',
166
'dns' => 'DNS',
167
'smtp_deliver' => 'SMTPDeliver',
168
'send_uuid' => 'SendUUID',
169
'exec_x64' => 'Exec_x64',
170
'reflective_dll_injection' => 'ReflectiveDLLInjection',
171
'reflective_pe_loader' => 'ReflectivePELoader',
172
'pe_inject' => 'PEInject',
173
'payload_db_conf' => 'PayloadDBConf',
174
'reverse_tcp_x86' => 'ReverseTcp_x86',
175
'reverse_tcp_aarch64' => 'ReverseTcp_Aarch64',
176
'ruby_dl' => 'RubyDL',
177
'wmic' => 'WMIC',
178
'net_api' => 'NetAPI',
179
'rpc_base' => 'RPC_Base',
180
'rpc_plugin' => 'RPC_Plugin',
181
'rpc_db' => 'RPC_Db',
182
'rpc_console' => 'RPC_Console',
183
'rpc_session' => 'RPC_Session',
184
'rpc_auth' => 'RPC_Auth',
185
'rpc_job' => 'RPC_Job',
186
'rpc_core' => 'RPC_Core',
187
'rpc_health' => 'RPC_Health',
188
'rpc_module' => 'RPC_Module',
189
'cli' => 'CLI',
190
'sqlitei' => 'SQLitei',
191
'mysqli' => 'MySQLi',
192
'postgresql' => 'PostgreSQL',
193
'postgresqli' => 'PostgreSQLi',
194
'ssh' => 'SSH',
195
'winrm' => 'WinRM',
196
'smb' => 'SMB',
197
'uris' => 'URIs',
198
'jboss' => 'JBoss',
199
'send_uuid_x64' => 'SendUUID_x64',
200
'reverse_tcp_x64' => 'ReverseTcp_x64',
201
'reverse_sctp_x64' => 'ReverseSctp_x64',
202
'block_api_x64' => 'BlockApi_x64',
203
'exitfunk_x64' => 'Exitfunk_x64',
204
'reverse_http_x64' => 'ReverseHttp_x64',
205
'rc4_x64' => 'Rc4_x64',
206
'bind_tcp_x64' => 'BindTcp_x64',
207
'reverse_win_http_x64' => 'ReverseWinHttp_x64',
208
'reflective_dll_inject_x64' => 'ReflectiveDllInject_x64',
209
'reverse_win_https_x64' => 'ReverseWinHttps_x64',
210
'reflective_pe_loader_x64' => 'ReflectivePELoader_x64',
211
'migrate_http_x64' => 'MigrateHttp_x64',
212
'migrate_common_x64' => 'MigrateCommon_x64',
213
'migrate_tcp_x64' => 'MigrateTcp_x64',
214
'migrate_named_pipe_x64' => 'MigrateNamedPipe_x64',
215
'reverse_named_pipe_x64' => 'ReverseNamedPipe_x64',
216
'meterpreter_loader_x64' => 'MeterpreterLoader_x64',
217
'rftransceiver' => 'RFTransceiver',
218
'dtc' => 'DTC',
219
'uds' => 'UDS',
220
'v1_0' => 'V1_0',
221
'php_include' => 'PHPInclude',
222
'psexec_ms17_010' => 'Psexec_MS17_010',
223
'bind_tcp_rc4_x64' => 'BindTcpRc4_x64',
224
'reverse_tcp_rc4_x64' => 'ReverseTcpRc4_x64',
225
'reverse_https_x64' => 'ReverseHttps_x64',
226
'bind_named_pipe_x64' => 'BindNamedPipe_x64',
227
'addr_loader' => 'AddrLoader_x64',
228
'db_manager_proxy' => 'DBManagerProxy',
229
'wmap_scan_ssl' => 'WmapScanSSL',
230
'http_db_manager_service' => 'HttpDBManagerService',
231
'vyos' => 'VYOS',
232
'windows_constants' => 'Windows_Constants',
233
'tty' => 'TTY',
234
'meterpreter_java' => 'Meterpreter_Java_Java',
235
'meterpreter_android' => 'Meterpreter_Java_Android',
236
'meterpreter_zarch_linux' => 'Meterpreter_zarch_Linux',
237
'meterpreter_python' => 'Meterpreter_Python_Python',
238
'meterpreter_ppce500v2_linux' => 'Meterpreter_ppce500v2_Linux',
239
'meterpreter_x86_osx' => 'Meterpreter_x86_OSX',
240
'meterpreter_armbe_linux' => 'Meterpreter_armbe_Linux',
241
'meterpreter_ppc64le_linux' => 'Meterpreter_ppc64le_Linux',
242
'meterpreter_x64_linux' => 'Meterpreter_x64_Linux',
243
'meterpreter_armle_linux' => 'Meterpreter_armle_Linux',
244
'meterpreter_aarch64_linux' => 'Meterpreter_aarch64_Linux',
245
'meterpreter_x86_win' => 'Meterpreter_x86_Win',
246
'meterpreter_armle_apple_ios' => 'Meterpreter_armle_Apple_iOS',
247
'meterpreter_mipsle_linux' => 'Meterpreter_mipsle_Linux',
248
'meterpreter_x86_bsd' => 'Meterpreter_x86_BSD',
249
'meterpreter_mips64_linux' => 'Meterpreter_mips64_Linux',
250
'meterpreter_x86_linux' => 'Meterpreter_x86_Linux',
251
'meterpreter_mipsbe_linux' => 'Meterpreter_mipsbe_Linux',
252
'meterpreter_aarch64_apple_ios' => 'Meterpreter_aarch64_Apple_iOS',
253
'meterpreter_x64_osx' => 'Meterpreter_x64_OSX',
254
'meterpreter_aarch64_osx' => 'Meterpreter_aarch64_OSX',
255
'meterpreter_ppc_linux' => 'Meterpreter_ppc_Linux',
256
'meterpreter_x64_win' => 'Meterpreter_x64_Win',
257
'meterpreter_php' => 'Meterpreter_Php_Php',
258
'meterpreter_multi' => 'Meterpreter_Multi',
259
'hwbridge' => 'HWBridge',
260
'vncinject_options' => 'VncInjectOptions',
261
'vncinject' => 'VncInject',
262
'json_hash_file' => 'JSONHashFile',
263
'jwt' => 'JWT',
264
'ndr' => 'NDR',
265
'ci_document' => 'CIDocument',
266
'fusionvm_document' => 'FusionVMDocument',
267
'group_policy_preferences' => 'GPP',
268
'ip360_aspl_xml' => 'IP360ASPLXMLStreamParser',
269
'ip360_xml' => 'IP360XMLStreamParser',
270
'nessus_xml' => 'NessusXMLStreamParser',
271
'netsparker_xml' => 'NetSparkerXMLStreamParser',
272
'nexpose_xml' => 'NexposeXMLStreamParser',
273
'nmap_xml' => 'NmapXMLStreamParser',
274
'openvas_document' => 'OpenVASDocument',
275
'retina_xml' => 'RetinaXMLStreamParser',
276
'graphml' => 'GraphML',
277
'apple_backup_manifestdb' => 'AppleBackupManifestDB',
278
'winscp' => 'WinSCP',
279
'acpp' => 'ACPP',
280
'tftp' => 'TFTP',
281
'ipmi' => 'IPMI',
282
'channel_auth_reply' => 'Channel_Auth_Reply',
283
'open_session_reply' => 'Open_Session_Reply',
284
'rakp2' => 'RAKP2',
285
'pjl' => 'PJL',
286
'dhcp' => 'DHCP',
287
'addp' => 'ADDP',
288
'rfb' => 'RFB',
289
'io' => 'IO',
290
'ntfs' => 'NTFS',
291
'bitlocker' => 'BITLOCKER',
292
'adb' => 'ADB',
293
'drda' => 'DRDA',
294
'tlv' => 'TLV',
295
'svcctl' => 'SVCCTL',
296
'wdscp' => 'WDSCP',
297
'appapi' => 'AppApi',
298
'uds_errors' => 'UDSErrors',
299
'smb_hash_capture' => 'SMBHashCapture',
300
'rex_ntlm' => 'RexNTLM'
301
}
302
end
303
304
def config_paths
305
[
306
{ path: "#{__dir__}/msf/", namespace: Msf },
307
{ path: "#{__dir__}/rex/", namespace: Rex }
308
]
309
end
310
311
# Enables :prepend to inject existing loader
312
def create_loader
313
Zeitwerk::Loader.new
314
end
315
316
# Enables :prepend to override the configuration items pass to the loader
317
def configure(loader)
318
config_paths.each do |entry|
319
if entry[:namespace]
320
loader.push_dir(entry[:path], namespace: entry[:namespace])
321
else
322
loader.push_dir(entry[:path])
323
end
324
end
325
loader.ignore(ignore_list)
326
loader.collapse(collapse_list)
327
loader.inflector = TempInflector.new
328
loader.inflector.inflect(custom_inflections)
329
end
330
331
# Enables :prepend to suppress the loader finalization
332
def finalize_loader(loader)
333
loader.setup # ready!
334
end
335
end
336
337
# global autoload of common gems
338
autoload :Faker, 'faker'
339
autoload :BinData, 'bindata'
340
autoload :RubySMB, 'ruby_smb'
341
autoload :MetasploitPayloads, 'metasploit-payloads'
342
autoload :PacketFu, 'packetfu'
343
344
require 'rexml/document'
345
# Load IO#expect moneypatch
346
require 'expect'
347
348
# XXX: Should be removed once the `lib/metasploit` folder is loaded by Zeitwerk
349
require 'metasploit/framework/hashes'
350
351