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 Component" button to load the COM Component in the dllhost process memory space. You should see the COM Component "badCom.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 Stack Corruption

					

                                                                           

 

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:020> 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 01e4f0d0 78002041 0040ac08 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:020> r

eax=00000015 ebx=00000003 ecx=00000000 edx=01e4f0bc esi=0203142d edi=01e4f0f1

eip=39383736 esp=01e4f0d4 ebp=35343332 iopl=0 nv up ei pl nz na pe nc

cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202

39383736 ?? ???

 

c) The disassmbly code

 

0:020> u eip

39383736 ?? ???

^ Memory access error in 'u eip'

 

 

 

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

 

First, we need to add the private 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

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

 

 

0:020> kvn50

# ChildEBP RetAddr Args to Child

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

00 01e4f0d0 78002040 0040ac08 33323130 37363534 0x39383736

01 01e4f0ec 02032900 01e4f124 02032945 00000001 MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 01e4f10c 0203289d 01e4f954 0040bf08 02031250 badcom!CScenarios::processSc+0xb6

03 01e4f264 6b658a10 6b65b5d8 00000000 01e4f954 badcom!CScenarios::processSc+0x53

04 01e4f9b8 6b658a10 6b659348 00000000 00000000 vbscript!except_handler3

05 01e4fbd0 749fb6f5 749f3bd0 ffffffff 01e4fbec vbscript!except_handler3

06 01e4fbec 749f593d 01e4fc4c 00000000 00000000 asp!vector::`vector deleting destructor'+0x5d (FPO: [EBP 0x01e4fbec] [1,0,4]) (CONV: thiscall)

07 01e4fc28 749f54c8 01a11fa0 01591e90 01e4fc94 asp!ExecuteRequest+0x19e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 326]

08 01e4fc70 749fcf9b 01a11fa0 01591e90 01e4fc94 asp!Execute+0x17e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 190]

09 01e4fcc0 749fcc37 00000000 000be330 01e4fd58 asp!CHitObj::ViperAsyncCallback+0x452 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\hitobj.cpp @ 2487]

0a 01e4fcd8 787c214a 00000000 77acd455 01b51e90 asp!CViperAsyncRequest::OnCall+0x3f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\viperint.cpp @ 194]

0b 01e4fce0 77acd455 01b51e90 77aa71a0 00000000 COMSVCS!STAActivityWorkHelper+0xa (FPO: [1,0,0]) (CONV: stdcall) [.\stathred.cpp @ 1321]

0c 01e4fd24 77acd318 000751d8 00085860 787c2140 ole32!EnterForCallback+0x6a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1758]

0d 01e4fe50 77acd1dd 00085860 787c2140 01b51e90 ole32!SwitchForCallback+0x12b (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1643]

0e 01e4fe78 77aac61a 00085860 787c2140 01b51e90 ole32!PerformCallback+0x50 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1558]

0f 01e4fed4 77ad1185 000757c0 787c2140 01b51e90 ole32!CObjectContext::InternalContextCallback+0xf5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\context.cxx @ 3942]

10 01e4fef4 787beff3 000757c0 787c2140 01b51e90 ole32!CObjectContext::DoCallback+0x1a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\context.cxx @ 3798]

11 01e4ff24 787c11ad 00010007 00000000 01e4ffec COMSVCS!STAActivityWork::DoWork+0x73 (FPO: [0,4,2]) (CONV: thiscall) [..\inc\STAThred.h @ 340]

12 01e4ffb4 77e887dd 0088f364 00010007 00000000 COMSVCS!STAThread::STAThreadWorker+0x465 (FPO: [EBP 0x01e4ffec] [1,31,4]) (CONV: stdcall) [.\stathred.cpp @ 628]

13 01e4ffec 00000000 787c0d48 0088f364 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 "badCom.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:020> dc esp

01e4f0d4 78002041 0040ac08 33323130 37363534 A .x..@.01234567

01e4f0e4 31303938 35343332 39383736 02032900 890123456789.)..

01e4f0f4 01e4f124 02032945 00000001 63733d73 $...E)......s=sc

01e4f104 31765f31 01e4f100 01e4f264 0203289d 1_v1....d....(..

01e4f114 01e4f954 0040bf08 02031250 000c51ac T.....@.P....Q..

01e4f124 01e4f140 779d7b9d 00408910 00408a70 @....{.w..@.p.@.

01e4f134 00000000 020441bc 01e4f134 01e4f1d0 .....A..4.......

01e4f144 77a22c42 00408910 00000024 00000004 B,.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:020> dds esp

01e4f0d4 78002041 MSVCRT!strncmp [intel\strncmp.asm @ 62]

01e4f0d8 0040ac08

01e4f0dc 33323130

01e4f0e0 37363534

01e4f0e4 31303938

01e4f0e8 35343332

01e4f0ec 39383736

01e4f0f0 02032900 badcom!CScenarios::processSc+0xb6

01e4f0f4 01e4f124

01e4f0f8 02032945 badcom!CScenarios::processSc+0xfb

01e4f0fc 00000001

01e4f100 63733d73

01e4f104 31765f31

01e4f108 01e4f100

01e4f10c 01e4f264

01e4f110 0203289d badcom!CScenarios::processSc+0x53

01e4f114 01e4f954

01e4f118 0040bf08

01e4f11c 02031250 badcom!ATL::CComObject::`vftable'

01e4f120 000c51ac

01e4f124 01e4f140

01e4f128 779d7b9d OLEAUT32!tPushValJmpTab+0xf5 [D:\OA\src\dispatch\WIN32\i386\invoke.asm @ 340]

01e4f12c 00408910

01e4f130 00408a70

01e4f134 00000000

01e4f138 020441bc

01e4f13c 01e4f134

01e4f140 01e4f1d0

01e4f144 77a22c42 OLEAUT32!CTypeInfo2::Invoke+0x244 [D:\OA\src\typelib2\tiinvoke.cpp @ 629]

01e4f148 00408910

01e4f14c 00000024

01e4f150 00000004

 

 

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

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

 

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

 

 

0:020> lmv mbadcom

start end module name

02030000 02038000 badcom (pdb symbols) c:\iisdebuglabs\symbols\pub\badcom.pdb

Loaded symbol image file: badcom.dll

Image path: c:\IISDEB~1\www\app\badcom.dll

Timestamp: Tue Jun 10 22:45:51 2003 (3EE697DF) Checksum: 0000F229

File version: 1.0.0.1

Product version: 1.0.0.1

File flags: 0 (Mask 3F)

File OS: 4 Unknown Win32

File type: 2.0 Dll

File date: 00000000.00000000

CompanyName:

ProductName: badCOM

Module InternalName: badCOM

OriginalFilename: badCOM.DLL

ProductVersion: 1, 0, 0, 1

FileVersion: 1, 0, 0, 1

FileDescription: badCOM Module

LegalCopyright: Copyright 2003

 

 

3) with Private Symbols "badExt.dll"

 

First, we need to add the private 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 encountered the stack corruption would look like:

 

 

 

0:020> kvn50

# ChildEBP RetAddr Args to Child

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

00 01e4f0d0 78002040 0040ac08 33323130 37363534 0x39383736

01 01e4f0ec 02032900 01e4f124 02032945 00000001 MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 01e4f10c 0203289d 01e4f954 0040bf08 02031250 badcom!CScenarios::processSc+0xb6 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 118]

03 01e4f264 6b658a10 6b65b5d8 00000000 01e4f954 badcom!CScenarios::processSc+0x53 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\comDBG\scenarios.cpp @ 99]

04 01e4f9b8 6b658a10 6b659348 00000000 00000000 vbscript!except_handler3

05 01e4fbd0 749fb6f5 749f3bd0 ffffffff 01e4fbec vbscript!except_handler3

06 01e4fbec 749f593d 01e4fc4c 00000000 00000000 asp!vector::`vector deleting destructor'+0x5d (FPO: [EBP 0x01e4fbec] [1,0,4]) (CONV: thiscall)

07 01e4fc28 749f54c8 01a11fa0 01591e90 01e4fc94 asp!ExecuteRequest+0x19e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 326]

08 01e4fc70 749fcf9b 01a11fa0 01591e90 01e4fc94 asp!Execute+0x17e (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\exec.cpp @ 190]

09 01e4fcc0 749fcc37 00000000 000be330 01e4fd58 asp!CHitObj::ViperAsyncCallback+0x452 (FPO: [Non-Fpo]) (CONV: thiscall) [D:\nt\private\inet\iis\svcs\cmp\asp\hitobj.cpp @ 2487]

0a 01e4fcd8 787c214a 00000000 77acd455 01b51e90 asp!CViperAsyncRequest::OnCall+0x3f (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\inet\iis\svcs\cmp\asp\viperint.cpp @ 194]

0b 01e4fce0 77acd455 01b51e90 77aa71a0 00000000 COMSVCS!STAActivityWorkHelper+0xa (FPO: [1,0,0]) (CONV: stdcall) [.\stathred.cpp @ 1321]

0c 01e4fd24 77acd318 000751d8 00085860 787c2140 ole32!EnterForCallback+0x6a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1758]

0d 01e4fe50 77acd1dd 00085860 787c2140 01b51e90 ole32!SwitchForCallback+0x12b (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1643]

0e 01e4fe78 77aac61a 00085860 787c2140 01b51e90 ole32!PerformCallback+0x50 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\crossctx.cxx @ 1558]

0f 01e4fed4 77ad1185 000757c0 787c2140 01b51e90 ole32!CObjectContext::InternalContextCallback+0xf5 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\context.cxx @ 3942]

10 01e4fef4 787beff3 000757c0 787c2140 01b51e90 ole32!CObjectContext::DoCallback+0x1a (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ole32\com\dcomrem\context.cxx @ 3798]

11 01e4ff24 787c11ad 00010007 00000000 01e4ffec COMSVCS!STAActivityWork::DoWork+0x73 (FPO: [0,4,2]) (CONV: thiscall) [..\inc\STAThred.h @ 340]

12 01e4ffb4 77e887dd 0088f364 00010007 00000000 COMSVCS!STAThread::STAThreadWorker+0x465 (FPO: [EBP 0x01e4ffec] [1,31,4]) (CONV: stdcall) [.\stathred.cpp @ 628]

13 01e4ffec 00000000 787c0d48 0088f364 00000000 KERNEL32!BaseThreadStart+0x52 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 460]

 

 

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 "processSc " corrupted the stack while exiting.

 

 

Looking at the source code:

 

STDMETHODIMP CScenarios::ProcessSc(VARIANT *pVar) 

{

...

return doSC1(1);

....

return S_OK;

}

 

DWORD doSC1(int Variation)

{

if (1 == Variation)

{

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

corrupt( _buffer );

} return S_OK;

}

 

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.

 

badcom!CScenarios::corrupt

badcom!CScenarios::doSC1

badcom!CScenarios::processSc

 

 


- Microsoft Confidential -