Memory corruption vulnerability exploitation in PHP (CVE-2014-8142 and CVE-2015-0231) (part 3) 0x00
- Author: Qsl1pknotp, security consultant at cigbench
- Question: Exploiting memory Upload uption bugs in PHP Part 3: Popping Remote Shells
- Address: http://www.inulledmyself.com/2015/05/exploiting-memory-corruption-bugs-in.html
This article takes longer time than I thought, but it is worth it! I want to use the video to explain how to exploit this vulnerability, so this article is not described in detail in the previous two articles.
Some people may be disappointed that this article only describes how to write a POC (and does not actually release the POC ). the video at the end of the article will show you my automated remote exploitation tool and the tips & trick used to make the POC run successfully in the real environment.
0x01 search for data
To make it easier to explain how to exploit this vulnerability, I will use the following code for demonstration.
#!php
Our goal is to be able to execute arbitrary PHP code. of course, we can try to inject shellcode to achieve this goal, but this method is neither creative nor elegant (a later version of PHP may not succeed ). if you still remember Part1, to execute any PHP code, we need to call php_execute_script and zend_eval_string. however, we want to launch remote attacks, so we must find executor_globals and JMP_BUF. as to why we want to do this, we will introduce it in detail later.
In short, we need to find (no particular order ):
- Executor_globals
- Zend_eval_string
- JMP_BUF
- Writing arbitrary data to a stack
Fortunately, some of the requirements listed above are still relatively easy to find, because they are in binary. we directly dump the strtab of PHP binay.
Great! We can find the zend_eval_string address directly from it, and then verify that the address is correct in GDB.
-
Find the zend_eval_string address
-
View the corresponding address in gdb
-
Find the address of executor_global
-
View the corresponding address in gdb
Awesome! How can we find JMP_BUF? After reading the code, we found the _ zend_executor_globals object, and found a JMP_BUF pointer named mongolout. let's hook up GDB to check whether the address is correct.
-
View the zend_executor_globals object
-
Print zend_executor_globals-> export lout
Okay. we got this address, but what is it pointing? What is the purpose? Okay. in PHP, JMP_BUF is used to implement the "try {}-catch {}" mechanism of PHP. this will be detailed later.
0x02 method 1-ROP
There is only one difference: writing arbitrary data into the stack. using method 2, we will discuss in detail the methods published by Stefan in 2010 Syscan. now that we can release any memory, what should we do next? How can we write data to the stack? How can we ensure that the written data will not be overwritten in the future? (Google is your friend :))
RFC 1867
This RFC indicates that the data of POST requests with multipart/form-data can be written to the stack and will not be overwritten by php (for various reasons ). let's upload a "normal" file.
Awesome! We can write arbitrary data to the stack. but what should we write to the stack?
Hint: what we previously looked :)
Since we spent so much time finding so many addresses, it's time to use them! So how should we use the previous address? After some research, we need to layout it like this:
From the very beginning: We used readelf to get the zend_eval_string address. ret Pointer and zend_mongolout are ignored (which may cause PHP crash ). we fill in the pointer to eval_string twice in the stack. Below is our existing data:
- POP; RET -?????
- Xchg eax, ESP; RET -????
- Zend_Eval_String-0x082da150
- Zend_Bailout-0x00000000
- Pointer_To_Eval_String-0xbfffda04
- Ret Pointer-0x00000000
- Pointer_To_Eval_String-0xbfffda04
Sweet! We are about to fill out these items, excellent! However, it seems that we still need some ROP gadgets. I personally prefer ROPGadget, but other tools are also acceptable. we need to find xchg eax, ESP; RET (0x94 0xc3) and pop ebp; RET (0x5d 0x3c ). once these gadgets are found, we can proceed to the next step.
We have obtained these two addresses (because these addresses are relatively different), we can continue to complete the data in the stack:
- POP; RET-0x000e8e68
- Xchg eax, ESP; RET-0x000057b7
- Zend_Eval_String-0x082da150
- Zend_Bailout-0x00000000
- Pointer_To_Eval_String-0xbfffda04
- Ret Pointer-0x00000000
- Pointer_To_Eval_String-0xbfffda04
Okay, it's time to test.
Hmmm, this is not the result I want. what should I do now? It seems that our code is trying to jump to our gadget (c394 ). unfortunately, you still need to know something. SPLObjectStorage requires that these gadgets be accessible in php, so we need to modify them. after modification:
0x03 method 2-Stefan
Method 1 ends, and Method 1 can only affect older versions of PHP. we will continue to study the use of new versions of PHP.
Fortunately, the php_execute_script and jmp_buf addresses are found and will be used in the new exploit.
Jmp_buf is used to save the "environment" in setjmp & longjmp to prevent "unrecoverable" errors. in a 32-bit system, jmp_buf is an array that stores 6 int values. in a 64-bit system, jmp_buf stores 8 int values. unfortunately, you need to check the code to determine the order of the registers stored in jmp_buf. here is a jmp_buf sample layout. let's take a look at the content in PHP...
On my machine, the Register sequence is: ebx, esp, ebp, esi, edi, eip. this is also the case here. our edi & eip seems to be confused by Glibc. there is a macro called PTR_MANGLE in Glibc. in the video, we will explain how to crack JMPBUF.
Once the edi & eip is cracked, we can continue to rewrite and release the memory. fortunately, we can continue to use SPLObjectStorage to remotely release the memory. the rest is how to write to the stack. and Part 2, we can manipulate PHP memory at will. we first release some memory, and then write 7 bytes of data to fill in. When php overwrites our data, we repeat the previous operations. the second rewrite allows us to write data of any length to the stack (this length can reach 2048 bytes during my test ). the data we write is similar to the previous example using drop. we will continue to "Encrypt" The data we write into the stack. this is the attack effect:
0x04 video address
Video, self-built ladder
Video Notes
- X86 Instruction Chart-http://sparksandflames.com/files/x86InstructionChart.html
- Elf Header lowest 3 bits are 000
- Elf layout-http://geezer.osdevbrasil.net/osd/exec/elf.txt
- PMAP is your friend when trying to find the "Magic"
- A look at PTR_MANGLE http://hmarco.org/bugs/CVE-2013-4788.html
0x05 translator's summary
As the author said, this article is not detailed in the previous two articles.
1. Where did the author come from the PHP binary file?
There is a comment in the original article. The author said that he obtained the entire php binary file through memory leak.
Under normal circumstances, the general routine is:
- Find ELF magic header \ x7fELF and find the starting address
- Use strtab and symtab to find the zend_eval_string, php_execute_script, and executor_globals addresses.
2. what is jmpbuf?
Jmpbuf is the data structure used by setjmp and longjmp to implement the try-catch mechanism, which is similar to the goto syntax. setjmp is equivalent to the label at a certain position, and longjmp is equivalent to goto, however, the goto syntax cannot jump across functions. jmpbuf stores the caller register information to facilitate longjmp recovery. in addition, glibc obfuscated some register values (except for the vulnerability glibc ).
3. how to solve jmpbuf?
First, check the setjmp code:
#! Bash (gdb) disassemble setjmpDump of javaser code for function setjmp: 0xb7c94410 <+ 0>: mov eax, dword ptr [esp + 0x4] 0xb7c94414 <+ 4>: mov dword ptr [eax], ebx #1. save ebx 0xb7c94416 <+ 6>: mov dword ptr [eax + 0x4], esi #2. save esi 0xb7c94419 <+ 9>: mov dword ptr [eax + 0x8], edi #3. save edi 0xb7c9441c <+ 12>: lea ecx, [esp + 0x4] 0xb7c94420 <+ 16>: xor ecx, dword ptr gs: 0x18 0xb7c94427 <+ 23>: rol ecx, 0x9 0xb7c9442a <+ 26>: mov dword ptr [eax + 0x10], ecx #4. save esp 0xb7c9442d <+ 29>: mov ecx, dword ptr [esp] 0xb7c94430 <+ 32>: xor ecx, dword ptr gs: 0x18 0xb7c94437 <+ 39>: rol ecx, 0x9 0xb7c9443a <+ 42>: mov dword ptr [eax + 0x14], ecx #5. save eip 0xb7c9443d <+ 45>: mov dword ptr [eax + 0xc], ebp #6. save ebp 0xb7c94440 <+ 48>: push 0x1 0xb7c94442 <+ 50>: push dword ptr [esp + 0x8] 0xb7c94446 <+ 54>: call 0xb7c943c0 <__sigjmp_save> 0xb7c9444b <+ 59>: pop ecx 0xb7c9444c <+ 60>: pop edx 0xb7c9444d <+ 61>: ret
The above registers are all stored in the caller Register status, in which esp and eip are all obfuscated (the author's own diagram is also that esp and eip are obfuscated), that is, PTR_MANGLE is used for obfuscation.
The PTR_MANGLE and PTR_DEMANGLE macros are defined as follows:
#!cpp# define PTR_MANGLE(reg) xorl %gs:POINTER_GUARD, reg; \ roll $9, reg# define PTR_DEMANGLE(reg) rorl $9, reg; \ xorl %gs:POINTER_GUARD, reg
Where gs: 0x18 is the above POINTER_GUARD
Setjmp () uses PTR_MANGLE for the obfuscation register, and longjmp () uses PTR_DEMANGLE for the normal register. to ensure that jmpbuf can be normally overwritten in the future, we need to obtain the value of POINTER_GUARD. because the jmpbuf data structure can be read out of bounds and the caller eip can also be obtained, the value of POINTER_GUARD can be obtained through PTR_DEMANGLE.
4. how can I get the eip of setjmp caller?
By reading the code, we can know that php_execute_script calls setjmp and saves jmpbuf to EG (batch lout). by leaking the php_execute_script address, we can know the eip address from the call to setjmp.
5. how to override jmpbuf?
The jmpbuf address searches for the forward value XX 00 00 00 (XX> 0x0c and XX <0x8f). after such a value is found, the value can be treated as a memory block.
Let's take a look at several ZMM structs:
#!cpp/* mm block type */typedef struct _zend_mm_block_info { size_t _size; size_t _prev;} zend_mm_block_info;
.
#!cpptypedef struct _zend_mm_free_block { zend_mm_block_info info; struct _zend_mm_free_block *prev_free_block; struct _zend_mm_free_block *next_free_block; struct _zend_mm_free_block **parent; struct _zend_mm_free_block *child[2];} zend_mm_free_block;
.
#!cppstruct _zend_mm_heap { int use_zend_alloc; void *(*_malloc)(size_t); void (*_free)(void*); void *(*_realloc)(void*, size_t); size_t free_bitmap; size_t large_free_bitmap; size_t block_size; size_t compact_size; zend_mm_segment *segments_list; zend_mm_storage *storage; size_t real_size; size_t real_peak; size_t limit; size_t size; size_t peak; size_t reserve_size; void *reserve; int overflow; int internal;#if ZEND_MM_CACHE unsigned int cached; zend_mm_free_block *cache[ZEND_MM_NUM_BUCKETS];#endif zend_mm_free_block *free_buckets[ZEND_MM_NUM_BUCKETS*2]; zend_mm_free_block *large_free_buckets[ZEND_MM_NUM_BUCKETS]; zend_mm_free_block *rest_buckets[2]; int rest_count;};
We need to pay attention to the cached in _ zend_mm_heap. ZMM will put 0x10 memory blocks into cached, so when we find a memory block that can be used as a memory block, the last few bytes (7 bytes of data) forge a memory header (_ zend_mm_block_info) and use string to reuse the forged memory block. if the write length is insufficient to cover jmpbuf, continue to forge the memory header-related operations, until jmpbuf can be overwritten.
6. can I overwrite jmpbuf?
Set the eip to zend_eval_string, set esp to a directly controllable stack (for example, after jmpbuf), fill in jmpbuf, and continue obfuscation of this obfuscated register. then set the zend_eval_string parameter on the controllable stack. The definition of zend_eval_string is as follows:
#!cZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC)
Finally, an exception is triggered to execute the code we want.
7. PHP 7 changes?
The zval format of php7 has changed significantly. the zval structure covered by string data cannot be used to read any address data, only data can be read backward (libnex, author of drops, says he has a solution and looks forward to a new article ).
#!cstruct _zval_struct { zend_value value; /* value */ union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2;};
.
#!cstruct _zend_string { zend_refcounted_h gc; zend_ulong h; /* hash value */ size_t len; char val[1];};
If zval_struct is overwritten by data, you can only modify len to implement backward reading.
Summarize the steps for using exploit
- The method described in part 2 can leak the std_object_handlers information and randomly find an address with a small value.
- Read data from any address introduced in part 2 until \ x7FELF appears.
- Using strtab, symtab can leak zend_eval_string, php_execute_script, and executor_globals (The author saves time and the article is local readelf)
- You can use excutor_globals to obtain the mongolout address (that is, the jmpbuf address)
- Use php_execute_script to obtain the eip when setjmp is called.
- Get the eip of setjmp caller, and get the value after eip obfuscation in the jmpbuf address. you can get the value of POINTER_GUARD through PTR_DEMANGLE.
- By repeatedly releasing the reused memory, it will continue to overwrite the jmpbuf
- Write the zend_eval_string address and the previous POINTER_GUARD PTR_MANGLE to the eip of jmpbuf.
- Set esp to a writable stack range. for example, the memory after jmpbuf is written to the esp of jmpbuf after PTR_MANGLE.
- Write the return address, php code address, php function name, php result return address, php file name, and php code after the memory block that can overwrite jmpbuf.
- An exception is triggered.
If you have any questions, you can go to the author's video and the tree man's paper. if I have added any incorrect information, please kindly advise.