My last advisory IA42 “Zoner Photo Studio v15 Build3 (Zps.exe) Registry Value Parsing Local Buffer Overflow” looks like a general exploitable vulnerability, but it is quite interesting to exploit because there is a major memory protection in use: SafeSEH.
But first of all, let’s have a look at the PoC-Script, which demonstrates the vulnerability:
The application loads the value from the “Issuer” Registry key during the launch and crashes:
May the exploit development begin. But if you try to overwrite the EIP with an address pointing to memory you control, you’ll recognize that something abnormally happens:
The application terminates unexpectedly and the EIP points to:
And this is a hint for a memory protection called SafeSEH. SafeSEH is a mechanism introduced with Microsoft Visual Studio 2005 (Compiler option /SAFESEH). It protects the exception handler chain by adding a “safe” exception handler table. This basically means that if the flow is changed within the exception handler chain and the address of the SE handler is not within such a table , SafeSEH will terminate the application. This prevents the code execution through an exploited exception handler chain. For more detailed information on this specific topic, visit: http://msdn.microsoft.com/en-us/library/9a89h429(VS.80).aspx
The following screen will clarify what I mean:
Parts of the stack are overwritten including the “SE handler”. Now the ntdll.KiUserCallbackDispatcher walks down the exception handler chain and checks if the exception handler pointers are valid. But at which point is an exception handler declared as invalid ?
- The exception handler points to an address on the stack
- The exception handler does fall within the list of loaded modules (and the application image itself) and does not match the list of valid registered exception handlers of the module.
But how can SafeSEH be bypassed ?
Find a module which is not protected by SafeSEH!
This is the easy way. SafeSEH cannot protect something where it is not enabled. So let’s have a look at the loaded modules during runtime and check if there is a module which is not compiled with the /SAFESEH instruction. Mona.py can do this work for you easily:
There is only one module, which is not SafeSEH-enabled, but it’s a small one (size of 0x00003000) and it does not contain any useful instructions to complete the exploit. That’s bad. What’s next ?
The way around SafeSEH!
What does SafeSEH think if the address is outside the range of a loaded module and the application image ? Correct. SafeSEH does consider this address to be safe! This probably makes the exploit a lot more unrealiable than using an instruction from an SafeSEH-disabled module but I don’t care for this demonstration ;-).
Let’s have a look at those addresses. Another pretty cool Corelan script “pvefindaddr.py” for the Immunity debugger helps finding such “out-of-scope” instructions. Pvefindaddr.py is the predecessor of mona.py - most of its functions have been migrated to mona.py but I’m missing this one:
There are some usable addresses (and after some reboots and reinstalls of Zoner Photo Studio I recognized that the marked address was always available, which makes the further exploit more reliable). The address 0x0C7D8F13 contains a
which points exactly to the beginning of the controlled memory (EBP-18):
The ntdll.KiUserCallbackDispatcher interprets the address 0x0C7D8F13 (which is out of scope :-) ) as valid, wich leads to the execution of our controllable memory part:
Now after executing “JMP DWORD PTR SS:[EBP-18]” the EIP points to the “Pointer to next SEH record” (sometimes referred as “nseh” in other exploits - I’ll adopt this ;-) ). All you have to do is to jump over the next few bytes, which contain the overwritten SE handler. And since we’re talking assembler a simple short jump (EB 06) will jump over the SE handler to the next part of our controlled input:
Now you can put the shellcode right after the overwritten SE handler. But be careful when building the shellcode - there are some characters which may break your .reg file like:
(it took some time to find out which chars are evil :-D )
And finally the working exploit code for Windows XP SP3 (German):
A short side - notice:
This exploit will only work on Windows XP SP3 German, because there are for example completely different out-of-scope addresses on Windows 7 SP1 (x64, German):