Skip to content

Commit dc2adc0

Browse files
committed
Land rapid7#19259, warn on weak meterpreter keys
Fixing meterpreter to support is_weak_key byte flag from mettle
2 parents f263932 + 09debbb commit dc2adc0

File tree

53 files changed

+77
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+77
-59
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PATH
4242
metasploit-model
4343
metasploit-payloads (= 2.0.166)
4444
metasploit_data_models
45-
metasploit_payloads-mettle (= 1.0.26)
45+
metasploit_payloads-mettle (= 1.0.28)
4646
mqtt
4747
msgpack (~> 1.6.0)
4848
mutex_m
@@ -304,7 +304,7 @@ GEM
304304
railties (~> 7.0)
305305
recog
306306
webrick
307-
metasploit_payloads-mettle (1.0.26)
307+
metasploit_payloads-mettle (1.0.28)
308308
method_source (1.1.0)
309309
mime-types (3.5.2)
310310
mime-types-data (~> 3.2015)

LICENSE_GEMS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ metasploit-framework, 6.4.15, "New BSD"
9090
metasploit-model, 5.0.2, "New BSD"
9191
metasploit-payloads, 2.0.166, "3-clause (or ""modified"") BSD"
9292
metasploit_data_models, 6.0.3, "New BSD"
93-
metasploit_payloads-mettle, 1.0.26, "3-clause (or ""modified"") BSD"
93+
metasploit_payloads-mettle, 1.0.28, "3-clause (or ""modified"") BSD"
9494
method_source, 1.1.0, MIT
9595
mime-types, 3.5.2, MIT
9696
mime-types-data, 3.2024.0604, MIT

lib/msf/base/sessions/meterpreter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ def bootstrap(datastore = {}, handler = nil)
175175
end
176176

177177
session.commands.concat(session.core.get_loaded_extension_commands('core'))
178-
178+
if session.tlv_enc_key[:weak_key?]
179+
print_warning("Meterpreter session #{session.sid} is using a weak encryption key.")
180+
print_warning('Meterpreter start up operations have been aborted. Use the session at your own risk.')
181+
return nil
182+
end
179183
# Unhook the process prior to loading stdapi to reduce logging/inspection by any AV/PSP
180184
if datastore['AutoUnhookProcess'] == true
181185
console.run_single('load unhook')

lib/rex/post/meterpreter/client_core.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Meterpreter
2525
#
2626
###
2727
class ClientCore < Extension
28-
28+
2929
METERPRETER_TRANSPORT_TCP = 0
3030
METERPRETER_TRANSPORT_HTTP = 1
3131
METERPRETER_TRANSPORT_HTTPS = 2
@@ -710,7 +710,7 @@ def migrate(target_pid, writable_dir = nil, opts = {})
710710

711711
# Renegotiate TLV encryption on the migrated session
712712
secure
713-
713+
714714
# Load all the extensions that were loaded in the previous instance (using the correct platform/binary_suffix)
715715
client.ext.aliases.keys.each { |e|
716716
client.core.use(e)
@@ -758,19 +758,32 @@ def valid_transport?(transport)
758758
#
759759
def negotiate_tlv_encryption(timeout: client.comm_timeout)
760760
sym_key = nil
761+
is_weak_key = nil
761762
rsa_key = OpenSSL::PKey::RSA.new(2048)
762763
rsa_pub_key = rsa_key.public_key
763764

764-
request = Packet.create_request(COMMAND_ID_CORE_NEGOTIATE_TLV_ENCRYPTION)
765+
request = Packet.create_request(COMMAND_ID_CORE_NEGOTIATE_TLV_ENCRYPTION)
765766
request.add_tlv(TLV_TYPE_RSA_PUB_KEY, rsa_pub_key.to_der)
766767

767768
begin
768769
response = client.send_request(request, timeout)
769770
key_enc = response.get_tlv_value(TLV_TYPE_ENC_SYM_KEY)
770771
key_type = response.get_tlv_value(TLV_TYPE_SYM_KEY_TYPE)
771-
772+
key_length = { Packet::ENC_FLAG_AES128 => 16, Packet::ENC_FLAG_AES256 => 32 }[key_type]
772773
if key_enc
773-
sym_key = rsa_key.private_decrypt(key_enc, OpenSSL::PKey::RSA::PKCS1_PADDING)
774+
key_dec_data = rsa_key.private_decrypt(key_enc, OpenSSL::PKey::RSA::PKCS1_PADDING)
775+
if !key_dec_data
776+
raise Rex::Post::Meterpreter::RequestError
777+
end
778+
sym_key = key_dec_data[0..key_length - 1]
779+
is_weak_key = false
780+
if key_dec_data.length > key_length
781+
key_dec_data = key_dec_data[key_length...]
782+
if key_dec_data.length > 0
783+
key_strength = key_dec_data[0]
784+
is_weak_key = key_strength != "\x00"
785+
end
786+
end
774787
else
775788
sym_key = response.get_tlv_value(TLV_TYPE_SYM_KEY)
776789
end
@@ -781,7 +794,8 @@ def negotiate_tlv_encryption(timeout: client.comm_timeout)
781794

782795
{
783796
key: sym_key,
784-
type: key_type
797+
type: key_type,
798+
weak_key?: is_weak_key
785799
}
786800
end
787801

metasploit-framework.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Gem::Specification.new do |spec|
7676
# Needed for Meterpreter
7777
spec.add_runtime_dependency 'metasploit-payloads', '2.0.166'
7878
# Needed for the next-generation POSIX Meterpreter
79-
spec.add_runtime_dependency 'metasploit_payloads-mettle', '1.0.26'
79+
spec.add_runtime_dependency 'metasploit_payloads-mettle', '1.0.28'
8080
# Needed by msfgui and other rpc components
8181
# Locked until build env can handle newer version. See: https://github.com/msgpack/msgpack-ruby/issues/334
8282
spec.add_runtime_dependency 'msgpack', '~> 1.6.0'

modules/payloads/singles/apple_ios/aarch64/meterpreter_reverse_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Module generated by tools/modules/generate_mettle_payloads.rb
88
module MetasploitModule
99

10-
CachedSize = 796620
10+
CachedSize = 796948
1111

1212
include Msf::Payload::Single
1313
include Msf::Sessions::MeterpreterOptions

modules/payloads/singles/apple_ios/aarch64/meterpreter_reverse_https.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Module generated by tools/modules/generate_mettle_payloads.rb
88
module MetasploitModule
99

10-
CachedSize = 796620
10+
CachedSize = 796948
1111

1212
include Msf::Payload::Single
1313
include Msf::Sessions::MeterpreterOptions

modules/payloads/singles/apple_ios/aarch64/meterpreter_reverse_tcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Module generated by tools/modules/generate_mettle_payloads.rb
88
module MetasploitModule
99

10-
CachedSize = 796620
10+
CachedSize = 796948
1111

1212
include Msf::Payload::Single
1313
include Msf::Sessions::MeterpreterOptions

modules/payloads/singles/apple_ios/armle/meterpreter_reverse_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Module generated by tools/modules/generate_mettle_payloads.rb
88
module MetasploitModule
99

10-
CachedSize = 643568
10+
CachedSize = 643872
1111

1212
include Msf::Payload::Single
1313
include Msf::Sessions::MeterpreterOptions

modules/payloads/singles/apple_ios/armle/meterpreter_reverse_https.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Module generated by tools/modules/generate_mettle_payloads.rb
88
module MetasploitModule
99

10-
CachedSize = 643568
10+
CachedSize = 643872
1111

1212
include Msf::Payload::Single
1313
include Msf::Sessions::MeterpreterOptions

0 commit comments

Comments
 (0)