Debug Labs


Stack Overrun/Corruption

This lab will demonstrate overwriting data on the stack and corrupting it resulting in an Access Violation

 

1- Lab Setup
	

Note: This lab assumes that you have already installed the necessary lab files to run it.

1- Verify the site is running in medium protection

2- Verify that the process "dllhost.exe" mapped to the out of process pooled application is running and note down its PID

To find the PID of the desired dllhost, do the follwing:

- Open Component Services

- Expand "Component Services" Node

- Expand "Computers" Node

- Expand "My Computer" Node

- Select "COM+ Applications"

- Click "Status View" icon and notice the PID column on the right hand pane

3- Attach windbg to the dllhost process with the PID found in step 2

a) Using the GUI:

- Click "File"

- Click "Attach to Process"

- Select "dllhost.exe" with the PID found in step 2 and click "OK"

b) Using the command prompt:

- Switch to thr folder where the debugging tools for windows is installed

- Type in "windbg -p <PID>" and hit Enter

 

4- In windbg command line, type in "g" with no quotation marks and press Enter

 

 

2- Run Scenario

1- Click the "Invoke Extension" button to load the ISAPI Extension in the dllhost process memory space. You should see the ISAPI extension "badExt.dll"among the list of modules loaded in dllhost.exe
2- Click the "Cause Stack Corruption" button the first time to cause a 1st chance Exception in dllhost.exe.
 Note that the debugger breaks and diplays 
	a- Comments about the exception
b- The value of the registers
c- The module where the Exception happened
d- The assembly line that caused the AV

					

                                                                           

 

1- Debug Annotation

 

1) Without symbols

 

a) The faulting Stack

 

The stack backtrace of the thread that caused the stack corruption would look like:

 

0:004> kvn50

*** ERROR: Symbol file could not be found. Defaulted to export symbols for -

# ChildEBP RetAddr Args to Child WARNING: Frame IP not in any known module. Following frames may be wrong.

00 0136f6e8 78002041 0209ac78 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:004> r

eax=00000015 ebx=00000003 ecx=00000000 edx=0136f6d4 esi=06471159 edi=0136f709

eip=39383736 esp=0136f6ec 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 disassmbly code

 

0:004> u eip

39383736 ?? ???

^ Memory access error in 'u eip'

 

 

 

2) With symbols including public symbols of "badExt.dll"

 

 

First, we need to add the symbol path(company wide symbols) as well as public symbols of our dll

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pub and hit Enter

b) Using the ".sympath+" command:

.sympath+ SRV*C:\LabSymbols*\\symbols\symbols;C :\IISDebugLabs\symbols\Pub

 

Note: For more information on setting the symbol path for windbg, please refer to the debugger documentation for the .sympath command. This will describe

the "downstream store" feature that lets you download symbols to a symbol server of your own.  

 

 

The stack backtrace of the thread that AV'd would look like:

 

0:004> .reload;kvn50

.........................................................................

# ChildEBP RetAddr Args to Child

WARNING: Frame IP not in any known module. Following frames may be wrong.

00 0136f6e8 78002040 0209ac78 33323130 37363534 0x39383736

01 0136f70c 06471381 01c31eb0 00000001 01c31f7c MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 0136f728 65d82b06 01c31eb0 01c31f7c 01c31eb0 badext!HttpExtensionProc+0xb8

03 0136f73c 65d82a97 01c31eb0 01c31f7c 01c31eb0 wam!HSE_APPDLL::ExecuteRequest+0x93 (FPO: [1,0,3]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]

04 0136f770 65d823a2 0209abb0 01c32025 01c31eb0 wam!WAM::InvokeExtension+0x35 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]

05 0136f7b4 77d339c0 006194c8 00000000 000000fa wam!WAM::ProcessRequest+0x240 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 643]

06 0136f7dc 77d93570 65d8223f 0136f7f8 00000005 RPCRT4!Invoke+0x30 [..\i386\stubless.asm @ 88]

07 0136fa54 77d949ac 021c6fb0 002115d4 021c8660 RPCRT4!NdrStubCall2+0x63d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1724]

08 0136fab8 77b29e7f 021c6fb0 021c8660 002115d4 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]

09 0136fafc 77b29d6e 021c8660 021c30c4 00224750 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1136]

0a 0136fb44 77aa7ae7 021c8660 021c2f28 021c6fb0 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]

0b 0136fba8 77aa7a33 002115d4 00000000 021c6fb0 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]

0c 0136fbc4 77b29c89 021c8660 00000001 021c6fb0 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]

0d 0136fbf4 77b29a89 021c8618 002115d4 021c6fb0 ole32!AppInvoke+0xb5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1013]

0e 0136fcb4 77b2a12c 001e9c68 00000000 021c30a8 ole32!ComInvokeWithLockAndIPID+0x29e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1578]

0f 0136fcf4 77d33721 021c8618 021c30a8 021c7f40 ole32!ThreadInvoke+0x1b7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 3835]

10 0136fd2c 77d33667 77b29f7b 021c7f40 0136fe0c RPCRT4!DispatchToStubInC+0x84 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\dispatch.c @ 82]

11 0136fd84 77d33579 00000000 00000000 0136fe0c RPCRT4!RPC_INTERFACE::DispatchToStubWorker+0x100 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 543]

12 0136fda4 77d48e81 021c7f40 00000000 0136fe0c RPCRT4!RPC_INTERFACE::DispatchToStub+0x5e (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 407]

13 0136fdd4 77d34bc6 021c7f40 021c7f04 00000000 RPCRT4!RPC_INTERFACE::DispatchToStubWithObject+0xa9 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 795]

14 0136fe10 77d346c5 021c7cd8 001b6f70 80030001 RPCRT4!LRPC_SCALL::DealWithRequestMessage+0x1c6 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 2665]

15 0136fe28 77d422ff 021c7e48 0136fe50 021c7cd8 RPCRT4!LRPC_ADDRESS::DealWithLRPCRequest+0x10c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 867]

16 0136ff74 77d420d9 77d42528 001b6f70 00070000 RPCRT4!LRPC_ADDRESS::ReceiveLotsaCalls+0x1eb (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 1175]

17 0136ff78 77d42528 001b6f70 00070000 01000002 RPCRT4!RecvLotsaCallsWrapper+0x9 (FPO: [1,0,0]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 37]

18 0136ffa8 77d424da 00213ff0 0136ffec 77e887dd RPCRT4!BaseCachedThreadRoutine+0x4f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 3586]

19 0136ffb4 77e887dd 00214048 00070000 01000002 RPCRT4!ThreadStartRoutine+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\threads.cxx @ 230]

1a 0136ffec 00000000 77d424c2 00214048 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 460]

 

With symbols lined up, we can see that the call was originating from "badext.dll". Keep in mind that the pattern that we are focunsing

on is this memory address 0x39383736.

 

Advanced Technique

 

First let look for this pattern by dumping the stack pointer (esp)

0:004> dc esp

0136f6ec 78002041 0209ac78 33323130 37363534 A .xx...01234567

0136f6fc 31303938 35343332 39383736 0136f700 890123456789..6.

0136f70c 0136f728 06471381 01c31eb0 0000000 1 (.6...G.........

0136f71c 01c31f7c 01c31eb0 0209abb0 0136f770 |...........p.6.

0136f72c 65d82b06 01c31eb0 01c31f7c 01c31eb0 .+.e....|.......

0136f73c 01c31f9c 65d82a97 01c31eb0 01c31f7c .....*.e....|...

0136f74c 01c31eb0 01c31f9c 006194c8 0136f748 ..........a.H.6.

0136f75c 006194c8 0136f7a8 65d89953 65d82b28 ..a...6.S..e(+.e

 

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:004> dds esp

0136f6ec 78002041 MSVCRT!strncmp [intel\strncmp.asm @ 62]

0136f6f0 0209ac78

0136f6f4 33323130

0136f6f8 37363534

0136f6fc 31303938

0136f700 35343332

0136f704 39383736

0136f708 0136f700

0136f70c 0136f728

0136f710 06471381 badext!HttpExtensionProc+0xb8

0136f714 01c31eb0

0136f718 00000001

0136f71c 01c31f7c

0136f720 01c31eb0

0136f724 0209abb0

0136f728 0136f770

0136f72c 65d82b06 wam!HSE_APPDLL::ExecuteRequest+0x93 [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]

0136f730 01c31eb0

0136f734 01c31f7c

0136f738 01c31eb0

0136f73c 01c31f9c

0136f740 65d82a97 wam!WAM::InvokeExtension+0x35 [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]

0136f744 01c31eb0

0136f748 01c31f7c

0136f74c 01c31eb0

0136f750 01c31f9c

0136f754 006194c8

0136f758 0136f748

0136f75c 006194c8

0136f760 0136f7a8

0136f764 65d89953 wam!except_handler3 0136f768 65d82b28 wam!`string'+0x24

 

As you can see, the pattern falls into a call from "badext.dll". But we cannot confirm for a fact that the last function call in "badext.dll"

that caused the corruption is "HttpExtensionProc". However, we can definetly say that the culprit is "badext.dll".

 

To verify that the symbols are lined up properly for our extension, issue this command.

 

0:006> lmv mbadext

start end module name 06470000 06475000

badext (pdb symbols) C :\IISDebugLabs\symbols\Pub

Loaded symbol image file: C:\IISDebuglabs\www\App\badext.dll Image path: C:\IISDebuglabs\www\App\badext.dll

Timestamp: Wed Jan 29 18:07:14 2003 (3E385EA2)

Checksum: 0000EBCD

File version: 5.0.0.0

Product version: 5.0.0.0

File flags: 0 (Mask 3F)

File OS: 40004 NT Win32

File type: 2.0 Dll

File date: 00000000.00000000

CompanyName: Microsoft Corporation

ProductName: Internet Information Services

InternalName: badEXT.dll

OriginalFilename: badEXT.dll

ProductVersion: 5.00.0984

FileVersion: 5.00.0984

FileDescription: badEXT Test

LegalCopyright: Copyright (C) Microsoft Corp. 1981-1999

3) with Private Symbols "badExt.dll"

 

 

First, we need to add the symbol path(company wide symbols) as well as private symbols of our dll

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\IISDebugLabs\symbols\Pri and 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:

 

 

# ChildEBP RetAddr

Args to Child WARNING: Frame IP not in any known module. Following frames may be wrong.

00 0136f6e8 78002040 0209ac78 33323130 37363534 0x39383736

01 0136f70c 06471381 01c31eb0 00000001 01c31f7c MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 0136f728 65d82b06 01c31eb0 01c31f7c 01c31eb0 badext!HttpExtensionProc+0xb8 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\extDBG\badext.cpp @ 101]

03 0136f73c 65d82a97 01c31eb0 01c31f7c 01c31eb0 wam!HSE_APPDLL::ExecuteRequest+0x93 (FPO: [1,0,3]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\isplocal.cxx @ 450]

04 0136f770 65d823a2 0209abb0 01c32025 01c31eb0 wam!WAM::InvokeExtension+0x35 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 913]

05 0136f7b4 77d339c0 006194c8 00000000 000000fa wam!WAM::ProcessRequest+0x240 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\wam\object\wamobj.cxx @ 643]

06 0136f7dc 77d93570 65d8223f 0136f7f8 00000005 RPCRT4!Invoke+0x30 [..\i386\stubless.asm @ 88]

07 0136fa54 77d949ac 021c6fb0 002115d4 021c8660 RPCRT4!NdrStubCall2+0x63d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\srvcall.c @ 1724]

08 0136fab8 77b29e7f 021c6fb0 021c8660 002115d4 RPCRT4!CStdStubBuffer_Invoke+0xec (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\ndr20\stub.c @ 1571]

09 0136fafc 77b29d6e 021c8660 021c30c4 00224750 ole32!SyncStubInvoke+0x61 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1136]

0a 0136fb44 77aa7ae7 021c8660 021c2f28 021c6fb0 ole32!StubInvoke+0xa8 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1334]

0b 0136fba8 77aa7a33 002115d4 00000000 021c6fb0 ole32!CCtxComChnl::ContextInvoke+0xbb (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\ctxchnl.cxx @ 1204]

0c 0136fbc4 77b29c89 021c8660 00000001 021c6fb0 ole32!MTAInvoke+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\callctrl.cxx @ 1952]

0d 0136fbf4 77b29a89 021c8618 002115d4 021c6fb0 ole32!AppInvoke+0xb5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1013]

0e 0136fcb4 77b2a12c 001e9c68 00000000 021c30a8 ole32!ComInvokeWithLockAndIPID+0x29e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 1578]

0f 0136fcf4 77d33721 021c8618 021c30a8 021c7f40 ole32!ThreadInvoke+0x1b7 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\channelb.cxx @ 3835]

10 0136fd2c 77d33667 77b29f7b 021c7f40 0136fe0c RPCRT4!DispatchToStubInC+0x84 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\dispatch.c @ 82]

11 0136fd84 77d33579 00000000 00000000 0136fe0c RPCRT4!RPC_INTERFACE::DispatchToStubWorker+0x100 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 543]

12 0136fda4 77d48e81 021c7f40 00000000 0136fe0c RPCRT4!RPC_INTERFACE::DispatchToStub+0x5e (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 407]

13 0136fdd4 77d34bc6 021c7f40 021c7f04 00000000 RPCRT4!RPC_INTERFACE::DispatchToStubWithObject+0xa9 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 795]

14 0136fe10 77d346c5 021c7cd8 001b6f70 80030001 RPCRT4!LRPC_SCALL::DealWithRequestMessage+0x1c6 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 2665]

15 0136fe28 77d422ff 021c7e48 0136fe50 021c7cd8 RPCRT4!LRPC_ADDRESS::DealWithLRPCRequest+0x10c (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 867]

16 0136ff74 77d420d9 77d42528 001b6f70 00070000 RPCRT4!LRPC_ADDRESS::ReceiveLotsaCalls+0x1eb (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 1175]

17 0136ff78 77d42528 001b6f70 00070000 01000002 RPCRT4!RecvLotsaCallsWrapper+0x9 (FPO: [1,0,0]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\lpcsvr.cxx @ 37]

18 0136ffa8 77d424da 00213ff0 0136ffec 77e887dd RPCRT4!BaseCachedThreadRoutine+0x4f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\hndlsvr.cxx @ 3586]

19 0136ffb4 77e887dd 00214048 00070000 01000002 RPCRT4!ThreadStartRoutine+0x18 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\rpc\runtime\mtrt\threads.cxx @ 230]

1a 0136ffec 00000000 77d424c2 00214048 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private

 

One more thing that we can point here when the private symbols are lined up is the call convention in "badext.dll" which is "Standard Call" (stdcall).

In stdcall, the callee cleans the stack which means that the function(s) called from "HttpExtensionProc" corrupted the stack

while exiting.

 

Looking at the source code: This

 

DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *lpEcb)

{

...

return doSC1(lpEcb, 1);

....

return HSE_STATUS_SUCCESS;

}

 

DWORD doSC1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation)

{

if (1 == Variation)

{

char _buffer[] = "01234567890123456789"; // more than 10 chars.

corrupt( _buffer );

} return HSE_STATUS_SUCCESS;

}

 

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!HttpExtensionProc

 

 


- Microsoft Confidential -