Skip to content

Commit 3b4302d

Browse files
authored
Land #18441, Add at rest encryption to Meterpreter payloads
2 parents 369c66a + c73e815 commit 3b4302d

File tree

15 files changed

+76
-35
lines changed

15 files changed

+76
-35
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PATH
3333
metasploit-concern
3434
metasploit-credential
3535
metasploit-model
36-
metasploit-payloads (= 2.0.156)
36+
metasploit-payloads (= 2.0.159)
3737
metasploit_data_models
3838
metasploit_payloads-mettle (= 1.0.26)
3939
mqtt
@@ -278,7 +278,7 @@ GEM
278278
activemodel (~> 7.0)
279279
activesupport (~> 7.0)
280280
railties (~> 7.0)
281-
metasploit-payloads (2.0.156)
281+
metasploit-payloads (2.0.159)
282282
metasploit_data_models (6.0.3)
283283
activerecord (~> 7.0)
284284
activesupport (~> 7.0)

LICENSE_GEMS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ metasploit-concern, 5.0.2, "New BSD"
8282
metasploit-credential, 6.0.6, "New BSD"
8383
metasploit-framework, 6.3.41, "New BSD"
8484
metasploit-model, 5.0.2, "New BSD"
85-
metasploit-payloads, 2.0.156, "3-clause (or ""modified"") BSD"
85+
metasploit-payloads, 2.0.159, "3-clause (or ""modified"") BSD"
8686
metasploit_data_models, 6.0.3, "New BSD"
8787
metasploit_payloads-mettle, 1.0.26, "3-clause (or ""modified"") BSD"
8888
method_source, 1.0.0, MIT

lib/msf/core/payload/android.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ def generate_jar(opts={})
127127
[ "AndroidManifest.xml" ],
128128
[ "resources.arsc" ]
129129
]
130-
jar.add_files(files, MetasploitPayloads.path("android", "apk"))
130+
131+
files.each do |file|
132+
path = ['android', 'apk', file].flatten.join('/')
133+
contents = ::MetasploitPayloads.read(path)
134+
jar.add_file(file.join('/'), contents)
135+
end
136+
131137
jar.add_file("classes.dex", fix_dex_header(classes))
132138
jar.build_manifest
133139

lib/msf/core/payload/java.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ def generate_jar(opts={})
5858
jar = Rex::Zip::Jar.new
5959
jar.add_sub("metasploit") if opts[:random]
6060
jar.add_file("metasploit.dat", stager_config(opts))
61-
jar.add_files(paths, ::MetasploitPayloads.path('java'))
61+
jar.add_file('metasploit/', '') # Create the metasploit dir
62+
63+
paths.each do |path_parts|
64+
path = ['java', path_parts].flatten.join('/')
65+
contents = ::MetasploitPayloads.read(path)
66+
jar.add_file(path_parts.join('/'), contents)
67+
end
68+
6269
jar.build_manifest(:main_class => main_class)
6370

6471
jar
@@ -103,7 +110,14 @@ def generate_war(opts={})
103110
zip.add_file('WEB-INF/', '')
104111
zip.add_file('WEB-INF/web.xml', web_xml)
105112
zip.add_file("WEB-INF/classes/", "")
106-
zip.add_files(paths, MetasploitPayloads.path('java'), 'WEB-INF/classes/')
113+
zip.add_file('metasploit/', '') # Create the metasploit dir
114+
115+
paths.each do |path_parts|
116+
path = ['java', path_parts].flatten.join('/')
117+
contents = ::MetasploitPayloads.read(path)
118+
zip.add_file(path_parts.join('/'), contents)
119+
end
120+
107121
zip.add_file("WEB-INF/classes/metasploit.dat", stager_config(opts))
108122

109123
zip
@@ -138,7 +152,14 @@ def generate_axis2(opts={})
138152
zip = Rex::Zip::Jar.new
139153
zip.add_file('META-INF/', '')
140154
zip.add_file('META-INF/services.xml', services_xml)
141-
zip.add_files(paths, MetasploitPayloads.path('java'))
155+
zip.add_file('metasploit/', '') # Create the metasploit dir
156+
157+
paths.each do |path_parts|
158+
path = ['java', path_parts].flatten.join('/')
159+
contents = ::MetasploitPayloads.read(path)
160+
zip.add_file(path_parts.join('/'), contents)
161+
end
162+
142163
zip.add_file('metasploit.dat', stager_config(opts))
143164
zip.build_manifest(:app_name => app_name)
144165

lib/msf/core/payload/windows/dll_inject.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,8 @@ def handle_connection_stage(conn, opts = {})
205205
data = library_name + "\x00"
206206

207207
begin
208-
File.open(library_path, "rb") { |f|
209-
data += f.read
210-
}
208+
encrypted_contents = ::File.binread(library_path)
209+
data += ::MetasploitPayloads::Crypto.decrypt(ciphertext: encrypted_contents)
211210
rescue
212211
print_error("Failed to load DLL: #{$!}.")
213212

lib/msf/core/post/windows/reflective_dll_injection.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def inject_dll_into_process(process, dll_path, loader_name: 'ReflectiveLoader',
7878
# @return [Array] Tuple of allocated memory address and offset to the
7979
# +ReflectiveLoader+ function.
8080
def inject_dll_data_into_process(process, dll_data, loader_name: 'ReflectiveLoader', loader_ordinal: EXPORT_REFLECTIVELOADER)
81-
offset = load_rdi_dll_from_data(dll_data, loader_name: loader_name, loader_ordinal: loader_ordinal)
82-
dll_mem = inject_into_process(process, dll_data)
81+
decrypted_dll_data = ::MetasploitPayloads::Crypto.decrypt(ciphertext: dll_data)
82+
offset = load_rdi_dll_from_data(decrypted_dll_data, loader_name: loader_name, loader_ordinal: loader_ordinal)
83+
dll_mem = inject_into_process(process, decrypted_dll_data)
8384

8485
return dll_mem, offset
8586
end

lib/msf/core/reflective_dll_loader.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module Msf::ReflectiveDLLLoader
2424
# @return [Array] Tuple of DLL contents and offset to the
2525
# +ReflectiveLoader+ function within the DLL.
2626
def load_rdi_dll(dll_path, loader_name: 'ReflectiveLoader', loader_ordinal: EXPORT_REFLECTIVELOADER)
27-
dll = ''
28-
::File.open(dll_path, 'rb') { |f| dll = f.read }
27+
encrypted_dll = ::File.binread(dll_path)
28+
dll = ::MetasploitPayloads::Crypto.decrypt(ciphertext: encrypted_dll)
2929

3030
offset = parse_pe(dll, loader_name: loader_name, loader_ordinal: loader_ordinal)
3131

@@ -43,7 +43,8 @@ def load_rdi_dll(dll_path, loader_name: 'ReflectiveLoader', loader_ordinal: EXPO
4343
#
4444
# @return [Integer] offset to the +ReflectiveLoader+ function within the DLL.
4545
def load_rdi_dll_from_data(dll_data, loader_name: 'ReflectiveLoader', loader_ordinal: EXPORT_REFLECTIVELOADER)
46-
offset = parse_pe(dll_data, loader_name: loader_name, loader_ordinal: loader_ordinal)
46+
decrypted_dll_data = ::MetasploitPayloads::Crypto.decrypt(ciphertext: dll_data)
47+
offset = parse_pe(decrypted_dll_data, loader_name: loader_name, loader_ordinal: loader_ordinal)
4748

4849
unless offset
4950
raise 'Cannot find the ReflectiveLoader entry point in DLL data'

lib/msf/util/exe.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,14 @@ def self.to_jar(exe, opts = {})
15991599
paths = [
16001600
[ "metasploit", "Payload.class" ],
16011601
]
1602-
zip.add_files(paths, MetasploitPayloads.path('java'))
1602+
1603+
zip.add_file('metasploit/', '')
1604+
paths.each do |path_parts|
1605+
path = ['java', path_parts].flatten.join('/')
1606+
contents = ::MetasploitPayloads.read(path)
1607+
zip.add_file(path_parts.join('/'), contents)
1608+
end
1609+
16031610
zip.build_manifest :main_class => "metasploit.Payload"
16041611
config = "Spawn=#{spawn}\r\nExecutable=#{exe_name}\r\n"
16051612
zip.add_file("metasploit.dat", config)

lib/rex/post/meterpreter/client_core.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ def load_library(opts)
258258
end
259259

260260
if library_image
261-
request.add_tlv(TLV_TYPE_DATA, library_image, false, client.capabilities[:zlib])
261+
decrypted_library_image = ::MetasploitPayloads::Crypto.decrypt(ciphertext: library_image)
262+
request.add_tlv(TLV_TYPE_DATA, decrypted_library_image, false, client.capabilities[:zlib])
262263
else
263264
raise RuntimeError, "Failed to serialize library #{library_path}.", caller
264265
end

lib/rex/post/meterpreter/extensions/priv/priv.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ def getsystem(technique=TECHNIQUE[:any])
8282
raise RuntimeError, "#{elevators.chomp(', ')} not found", caller
8383
end
8484

85-
elevator_data = ''
86-
87-
::File.open(elevator_path, 'rb') { |f|
88-
elevator_data += f.read(f.stat.size)
89-
}
85+
encrypted_elevator_data = ::File.binread(elevator_path)
86+
elevator_data = ::MetasploitPayloads::Crypto.decrypt(ciphertext: encrypted_elevator_data)
9087

9188
request.add_tlv(TLV_TYPE_ELEVATE_SERVICE_DLL, elevator_data)
9289
request.add_tlv(TLV_TYPE_ELEVATE_SERVICE_LENGTH, elevator_data.length)

0 commit comments

Comments
 (0)