1) Without symbols
a) The faulting Stack
The stack backtrace of the thread that caused the stack corruption looks like:
0:000> kvn
# ChildEBP RetAddr Args to Child
WARNING: Frame IP not in any known module. Following frames may be wrong.
00 0006fe2c 78002041 002324b0 33323130 37363534 0x39383736
01 35343332 00000000 00000000 00000000 00000000 MSVCRT!strncmp
In this Stack Backtrace, notice that frame 00 could not be resolved to a function call, this is because the memoryaddress 0x39383736 is invalid . This address is supposed to be a valid Instruction Pointer (eip)! The 2 major reasons why this memory address is not valid could be because:
1- This address falls into the memory space of a module that has been unloaded.
2- A function call overwrite the eip and hence corrupt the stack.
To verify if this address falls into the memory space of an unloaded module, check the loaded modules list displayed in the
debugger when you attach to the process. If you are analyzing a dump generated by adplus, check the log file.
So now, in this scenario, it appears that that a function call did overwrite the eip. As we will see further in the analysis, we cannot
determine exactly what function call did that but we can definitely identify the module.
b) The regiters values
0:000> r
eax=00000015 ebx=00000003 ecx=00000000 edx=0006fe18 esi=0100120d edi= 0006fe4d
eip=39383736 esp=0006fe30 ebp=35343332 iopl=0 nv up ei pl nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000 efl= 00000202
39383736 ?? ???
c) The dissassembly code
0:004> u eip
39383736 ?? ???
^ Memory access error in 'u eip'
2) With symbols including public symbols of "badCgi.exe"
0:000> .sympath+ SRV*C:\LabSymbols*http://msdl.microsoft.com/download/symbols
Symbol search path is: SRV*C:\LabSymbols*http://msdl.microsoft.com/download/symbols
0:000> .sympath+ C :\IISDebugLabs\symbols\Pub (where public symbols of badFil.dll are located)
Symbol search path is:SRV*C:\LabSymbols*http://msdl.microsoft.com/download/symbols; C :\IISDebugLabs\symbols\Pub
The stack backtrace of the thread that caused the stack corruption looks like:
0:000> .reload;kvn50
......
# ChildEBP RetAddr Args to Child
WARNING: Frame IP not in any known module. Following frames may be wrong.
00 0006fe2c 78002040 002324b0 33323130 37363534 0x39383736
*** ERROR: Module load completed but symbols could not be loaded for badcgi.exe
01 0006fe50 01001464 0006fe6c 00000001 77f82b95 MSVCRT!wcsncnt+0x23
02 0006fea4 77e88563 77e885c1 77fcf348 77e88580 badcgi+0x1464
03 0006ff10 78033238 ffffffff 0006ff60 78001045 KERNEL32!GetModuleFileNameW+0xfb
04 7800ffb8 565308ec 8bfc5557 458b0c5d 0440f708 MSVCRT!`string'+0x4c
05 83ec8b55 00000000 00000000 00000000 00000000 0x565308ec
With symbols lined up, teh information that we can get is that a function in this main process"badCgi.exe" was involved somehow in causing this stack corruption.
Keep in mind that the pattern that we are focunsing on is this memory address 0x39383736.
Now, with the public symbols lined up, we see that the last call that AV'd was to the function "GetSystemTime" in "kernel32.dll". this API was called from
the function "doAV2".
First let look for this pattern by dumping the stack pointer (esp)
0:000> dc esp
0006fe30 78002041 002324b0 33323130 37363534 A .x.$#.01234567
0006fe40 31303938 35343332 39383736 0006ff00 890123456789....
0006fe50 0006ff70 01001464 0006fe6c 00000001 p...d...l.......
0006fe60 77f82b95 77f83920 7ffdf000 63733d73 . +.w 9.w....s=sc
0006fe70 31765f31 00000000 00000000 f29f7b94 1 _v1.........{..
0006fe80 00230000 00070000 00120089 00000000 ..#.............
0006fe90 00000000 ffffff00 80460500 00071ee0 ..........F.....
0006fea0 01000000 0006ff10 77e88563 77e885c1 ........c..w...w
It looks like this address now is nothing more than a string! So where in the stack this pattern could be found? Let dump the
raw stack starting from esp.
0:000> dds esp
0006fe30 78002041 MSVCRT!strncmp [intel\strncmp.asm @ 62]
0006fe34 002324b0
0006fe38 33323130
0006fe3c 37363534
0006fe40 31303938
0006fe44 35343332
0006fe48 39383736
0006fe4c 0006ff00
0006fe50 0006ff70
0006fe54 01001464 badcgi!main+0xe4
0006fe58 0006fe6c
0006fe5c 00000001
0006fe60 77f82b95 ntdll!__except_handler3
0006fe64 77f83920 ntdll!`string'+0x148
0006fe68 7ffdf000
0006fe6c 63733d73
0006fe70 31765f31
0006fe74 00000000
0006fe78 00000000
0006fe7c f29f7b94
0006fe80 00230000
0006fe84 00070000
0006fe88 00120089
0006fe8c 00000000
0006fe90 00000000
0006fe94 ffffff00
0006fe98 80460500
0006fe9c 00071ee0
0006fea0 01000000 badcgi!_imp__RegOpenKeyExA
0006fea4 0006ff10
0006fea8 77e88563 KERNEL32!GetModuleFileNameW+0xfb
0006feac 77e885c1 KERNEL32!GetModuleFileNameW+0x177
As you can see, the pattern falls into a call from badCgi and not module loaded in this executable. But we cannot know what function
did that. However, we can definetly say that a function call directly in the main process badCgi caused the corruption.
3) with Private Symbols "badCgi.exe"
First, we need to add the private symbol path(company wide symbols) as well as public symbols of our exe
a) Using the GUI:
- Click "File"
- Click "Symbol File Path"
- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pri a
nd hit Enter b) Using the ".sympath+" command:
.sympath+ SRV*C:\LabSymbols*\\symbols\symbols;C :\IISDebugLabs\symbols\Pri
The stack backtrace of the thread that AV'd would look like:
0:000> .reload;kvn50
# ChildEBP RetAddr Args to Child
WARNING: Frame IP not in any known module. Following frames may be wrong.
00 0006fe2c 78002040 002324b0 33323130 37363534 0x39383736
01 0006fe50 01001464 0006fe6c 00000001 77f82b95 MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]
02 0006fea4 77e88563 77e885c1 77fcf348 77e88580 badcgi!main+0xe4 (CONV: cdecl) [C:\build\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 122]
03 0006ff10 78033238 ffffffff 0006ff60 78001045 KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]
......
# ChildEBP RetAddr Args to Child
00 0006fe48 0100163f 00000000 01001441 0006fe6c KERNEL32!GetSystemTime+0x34 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\datetime.c @ 177]
01 0006ff70 01001daf 00000001 00232468 00232cd8 badcgi!doAV2+0xf (CONV: stdcall) [C:\build\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 207]
02 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]
03 0006fff0 00000000 01001cb0 00000000 000000c8 KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]
Notice that all frames contain the source code information.
Let see what line 122 in badCgi.cpp looks like.
This how line 122 looks like:
DWORD main( int argc, char * argv[])
{
...
return doSC1(rgchQuery, 1); //line 122
....
return (1);
}
DWORD doSC1(CHAR szQS[DEFAULT_LEN], int Variation)
{
if (1 == Variation)
{
char _buffer[] = "01234567890123456789"; // more than 10 chars.
corrupt( _buffer );
} return 1;
}
void corrupt( const char *str )
{
char buffer[10];
strcpy( buffer, str ); // overrun buffer.
}
As you see, the stack corruption is caused by copying a buffer of 20 characters in a reserved buffer of 10 characters.
This is the call sequence in a stack format.
Badext!corrupt
Badext!doSC1
Badext!main
DWORD doAV1(CHAR szQS[DEFAULT_LENGTH], int Variation)
{ if (1 == Variation)
{
SYSTEMTIME* pSysTime = NULL;
GetSystemTime( pSysTime ); // Line 207 would av in GetSystemTime
}
return 1;
}
Advanced Technique
Remember that our web application is running in the IIS out of process pooled application (dllhost.exe).
The CGI executable is loaded in its own memory space and it is basically spawned by "inetinfo.exe". So for all http requests involving directly the the CGI
without passing via another extension will be processed by both inetinfo and the CGI executable without passing via dllhost. (for more information, please
refer to the class docs Chapter:...).
To get a feeling on how the CGI processing is made, let do the following:
1) In the command prompt, switch to the folder where the debugger is installed and run the following command:
c:\ debuggers>windbg -pn inetinfo.exe
2) In WinDbg, let set a breakpoint in CreateProcess function of Kernel32
In the windbg command line, type in "bp KERNEL32!CreateProcessA"
3) In Windbg command line, type in "g" and hit "Enter"
4) Click the "AV CGI" button in this page.
Notice that the debugger breaks, the command line is now available to run commands.
The stack backtrace of the thread where the break point was hit looks like:
0:010> kp
ChildEBP RetAddr
0136ee98 65f1be75 KERNEL32!CreateProcessA(char* lpApplicationName = 00000000 "", char* lpCommandLine = 0136f4b0 ""C:\IISDebugLabs\www\App\badCgi.exe" ", _SECURITY_ATTRIBUTES* lpProcessAttributes = 00000000 , _SECURITY_ATTRIBUTES* lpThreadAttributes = 00000000 , int bInheritHandles = 1, unsigned long dwCreationFlags = 0xc, void* lpEnvironment = 0136eed4 , char* lpCurrentDirectory = 0136fba8 "C:\IISDebugLabs\www", _STARTUPINFOA* lpStartupInfo = 0136f5b4 , _PROCESS_INFORMATION* lpProcessInformation = 0136f658 ) [D:\nt\private\windows\base\client\process.c @ 715]
0136f678 65f1b925 w3svc!HTTP_REQUEST::ProcessCGI(EXEC_DESCRIPTOR* pExec = 00e9eb2c , STR* pstrPath = 00000000 , STR* pstrWorkingDir = 0136fd10 , int* pfHandled = 0136feb0 , int* pfFinished = 0136fedc , STR* pstrCmdLine = 0136f5fc )+0x4c3 [D:\nt\private\inet\iis\svcs\w3\server\cgi.cxx @ 1179]
0136fd48 65f2a610 w3svc!HTTP_REQUEST::ProcessGateway(EXEC_DESCRIPTOR* pExec = 00e9eb2c , int* pfHandled = 0136feb0 , int* pfFinished = 0136fedc , int fTrusted = 0)+0x34e [D:\nt\private\inet\iis\svcs\w3\server\cgi.cxx @ 919]
0136fec4 65f020d7 w3svc!HTTP_REQUEST::DoWork(int* pfFinished = 0136fedc )+0x43f [D:\nt\private\inet\iis\svcs\w3\server\httpreq.cxx @ 839]
0136fee4 65f01b47 w3svc!CLIENT_CONN::DoWork(unsigned long BytesWritten = 0, unsigned long CompletionStatus = 0, int fIOCompletion = 0)+0x1aa [D:\nt\private\inet\iis\svcs\w3\server\conn.cxx @ 781]
0136ff08 65f01aa5 w3svc!CreateClient(_CLIENT_CONN_PARAMS* ClientParam = 0136ff20 )+0x7b [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 310]
0136ff4c 6d701443 w3svc!W3OnConnectEx(void* patqContext = 012742d8 , unsigned long cbWritten = 0xe4, unsigned long err = 0x1131e8, _OVERLAPPED* lpo = 00e897b8 )+0x118 [D:\nt\private\inet\iis\svcs\w3\server\connect.cxx @ 215]
0136ff80 6d702203 ISATQ!AtqpProcessContext(ATQ_CONTEXT* pAtqContext = 00000000 , unsigned long cbWritten = 0xe4, _OVERLAPPED* lpo = 012742dc , int fRet = 1)+0x23a [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 883]
0136ffb4 77e887dd ISATQ!AtqPoolThread(unsigned long* param = abcdef01 )+0x1a8 [D:\nt\private\inet\iis\svcs\infocomm\atq\atqsupp.cxx @ 1127]
0136ffec 00000000 KERNEL32!BaseThreadStart(* lpStartAddress = 6d7091f7 , void* lpParameter = abcdef01 )+0x52 [D:\nt\private\windows\base\client\support.c @ 460]
Note that the second parameter to CreateProcess is the command line to trun that process which is in this case "C:\IISDebugLabs\www\App\badCgi.exe"
Let try to get the whole HTTP request that w3svc received.
For this, let dump the EXEC_DESCRIPTOR class.
0:010> dt EXEC_DESCRIPTOR 00e9eb2c
+0x000 _pstrURL : 0x00e9e370
+0x004 _pstrPhysicalPath : 0x00e9e5e0
+0x008 _pstrUnmappedPhysicalPath : 0x00e9e610
+0x00c _pstrGatewayImage : 0x00e9eac0
+0x010 _pstrPathInfo : 0x00e9e400
+0x014 _pstrURLParams : 0x00e9e3d0
+0x018 _pdwScriptMapFlags : 0x00e9eabc -> 0
+0x01c _pGatewayType : 0x00e9e060 300002 ( GATEWAY_CGI )
+0x020 _pRequest : 0x00e9e008
+0x024 _hChildEvent : (null)
+0x028 _fMustWaitForChildEvent : 0 +0x02c _pParentWamRequest : (null)
+0x030 _dwExecFlags : 0
+0x034 _pMetaData : 0x00e9ecd8
+0x038 _pPathInfoMetaData : (null)
+0x03c _pPathInfoURIBlob : (null)
+0x040 _pAppPathURIBlob : (null)
Now let dump the HTTP_REQUEST class.
0:010> dt HTTP_REQUEST 0x00e9e008
+0x000 __VFN_table : 0x65f08680
+0x008 _cFilesSent : 0
+0x00c _cFilesReceived : 0
......
......
+0x9a8 _bufClientRequest : BUFFER
......
......
+0xbac _pFileNameLock : (null)
+0xbb0 _fPossibleDefaultExecute : 0y0
=65f465b0 HTTP_REQUEST::sm_GetInfoFuncs : [26] 0x65f26a87 w3svc!HTTP_REQUEST::GetInfoA+0
+0xbb4 _strDLCString : STR
The "_bufClientRequest " points to the buffer where the HTTP request is stored.
0:010> da poi(0x00e9e008 + 0x9a8 )
006ae398 "GET /IISDebugLabs/App/badCgi.exe"
006ae3b8 "?s=av1_v2 HTTP/1.1..Accept: */*."
006ae3d8 ".Accept-Language: en-us..Accept-"
006ae3f8 "Encoding: gzip, deflate..User-Ag"
006ae418 "ent: Mozilla/4.0 (compatible; MS"
006ae438 "IE 6.0; Windows NT 5.0)..Host: l"
006ae458 "ocalhost..Connection: Keep-Alive"
006ae478 "...."
5) In the windbg command line, type in "g" and hit "Enter"
Notice a second windbg window is displayed, this is the one attached to the CGI executable "badCgi.exe".
6) In the Windbg instance attached to "badCgi.exe" type in "q" and hit "Enter".
7) Finaly, back to the main WinDbg window, press "Ctrl-Break" to break in to the debugger, type in "q" and hit "Enter"