Debug Labs


Stack Overrun/Corruption

This lab will demonstrate overwriting data in 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-Install the ISAPI Filter

 

- Right Click the Server Name

- Click "Properties"

- In the "WWW Service" Master Properties, click "Edit"

- In the "ISAPI Filters" tab, click "Add"

- In the "Filter Name" type in "BadFilter"

- In the "Executable", browse to C:\IISDebugLabs\www\App\badfil.dll

- Click "Apply" and click "Ok" all teh way out.

 

2- Restart the IIS Admin service

- Right Click the Server Name

- Click "Restart IIS" and Click "Ok"

 

3- Attach the debugger to inetinfo.exe

a) Using the GUI:

- Click "File"

- Click "Attach to Process"

- Select "inetinfo.exe" and click "OK"

b) Using the command prompt

-Type in "windng -pn inetinfo.exe" without quotation marks and hit "Enter"

 

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

 

 

2- Run Scenario

1- Click the "Cause Stack Corruption" button the first time to cause a 1st chance AV in inetinfo.exe.
 Note that the debugger breaks and diplays 
	a- Comments about the exception
b- The value of the registers
c- The module where the AV happened
d- The assembly line that caused the AV
2- In the WinDbg command line, type in "g" and press "Enter", a 2nd chance AV is now encountered 

                 

 

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:007> 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 0129f5b8 78002041 00e8b430 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:007> r

eax=00000015 ebx=00000003 ecx=00000000 edx=0129f5a4 esi=016b1405 edi=0129f5d9

eip=39383736 esp=0129f5bc 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:007> u eip

39383736 ?? ???

^ Memory access error in 'u eip'

 

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

 

First, we need to add the public symbol path(company wide symbols) as well as 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 a nd hit Enter

b) Using the ".sympath+" command:

0:008> .sympath+ SRV*C:\LabSymbols*http:symbols\symbols

Symbol search path is: SRV*C:\LabSymbols*http://msdl.microsoft.com/download/symbols

 

0:008> .sympath+ C :\IISDebugLabs\symbols\Pub (where public symbols of badFil.dll are located)

Symbol search path is: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 caused the stack corruption:

 

0:009> kvn50

# ChildEBP RetAddr Args to Child

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

00 012ef5b8 78002040 00e23858 33323130 37363534 0x39383736

01 012ef5dc 016b168c 00000001 00e945a8 00e94488 MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 012ef624 77e199fb 012ef8cb 012ef8cf 77e198f2 badfil!HttpFilterProc+0x18a

03 012ef674 012ef444 00000006 00000000 012efca8 USER32!wvsprintfA+0x367 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\w32\ntuser\client\wsprintf.c @ 312]

04 012ef8a8 6c694670 50726574 5d636f72 77654e20 0x12ef444

05 7474485b 00000000 00000000 00000000 00000000 0x6c694670  

 

 

With symbols lined up, we can see that the call was originating from "badFil.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:009> dc esp

012ef5bc 78002041 00e23858 33323130 37363534 A .xX8..01234567

012ef5cc 31303938 35343332 39383736 016b1600 890123456789..k.

012ef5dc 012efbf4 016b168c 00000001 00e945a8 ......k......E..

012ef5ec 00e94488 00000008 00000000 00000000 .D..............

012ef5fc 00000000 00000000 00000000 00000000 ................

012ef60c 00000000 00000000 012ef8cb 012ef8cb ................

012ef61c 0000000a 00000004 012ef674 77e199fb ........t......w

012ef62c 012ef8cb 012ef8cf 77e198f2 016b128c ...........w..k.

 

 

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

012ef5bc 78002041 MSVCRT!strncmp [intel\strncmp.asm @ 62]

012ef5c0 00e23858

012ef5c4 33323130

012ef5c8 37363534

012ef5cc 31303938

012ef5d0 35343332

012ef5d4 39383736

012ef5d8 016b1600 badfil!HttpFilterProc+0xfe

012ef5dc 012efbf4

012ef5e0 016b168c badfil!HttpFilterProc+0x18a

012ef5e4 00000001

012ef5e8 00e945a8

012ef5ec 00e94488

012ef5f0 00000008

012ef5f4 00000000

012ef5f8 00000000

012ef5fc 00000000

012ef600 00000000

012ef604 00000000

012ef608 00000000

012ef60c 00000000

012ef610 00000000

012ef614 012ef8cb

012ef618 012ef8cb

012ef61c 0000000a

012ef620 00000004

012ef624 012ef674

012ef628 77e199fb USER32!wvsprintfA+0x367 [D:\nt\private\ntos\w32\ntuser\client\wsprintf.c @ 312]

012ef62c 012ef8cb

012ef630 012ef8cf

012ef634 77e198f2 USER32!wvsprintfA+0x6e [D:\nt\private\ntos\w32\ntuser\client\wsprintf.c @ 217]

012ef638 016b128c badfil!`string'+0x38

 

 

 

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

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

 

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

 

0:008> lmv mbadfil

start end module name

015a0000 015a6000 badfil (pdb symbols) C:\IISDebugLabs\Symbols\Pub

Loaded symbol image file: badfil.dll

Image path: C:\IISDebugLabs\www\App\badfil.dll

Timestamp: Wed May 28 01:11:01 2003 (3ED444E5)

Checksum: 0000D9A0

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: badFIL.dll

OriginalFilename: badFIL.dll

ProductVersion: 5.00.0984

FileVersion: 5.00.0984

FileDescription: badFIL Filter DLL

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

 

 

3) with Private Symbols "badFil.dll"

 

 

Point the debugger to use the private symbols

For company wide internal symbols, use \\symbols\symbols

For the component private symbols, use C:\IISDebugLabs\Symbols\Pri

 

Verify the component symbols are lined up.

 

0:008> lmv mbadfil

start end module name

015a0000 015a6000 badfil (pdb symbols) C:\IISDebugLabs\Symbols\Pri

Loaded symbol image file: badfil.dll

Image path: C:\IISDebugLabs\www\App\badfil.dll

Timestamp: Wed May 28 01:11:01 2003 (3ED444E5)

Checksum: 0000D9A0

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: badFIL.dll

OriginalFilename: badFIL.dll

ProductVersion: 5.00.0984

FileVersion: 5.00.0984

FileDescription: badFIL Filter DLL

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

 

 

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

 

 

 

0:009> kvn50

# ChildEBP RetAddr Args to Child

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

00 012ef5b8 78002040 00e23858 33323130 37363534 0x39383736

01 012ef5dc 016b168c 00000001 00e945a8 00e94488 MSVCRT!wcsncnt+0x23 (FPO: [2,0,1]) (CONV: cdecl) [w_map.c @ 52]

02 012ef624 77e199fb 012ef8cb 012ef8cf 77e198f2 badfil!HttpFilterProc+0x18a (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\filDBG\badfil.cpp @ 253]

03 012ef674 012ef444 00000006 00000000 012efca8 USER32!wvsprintfA+0x367 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\w32\ntuser\client\wsprintf.c @ 312]

04 012ef8a8 6c694670 50726574 5d636f72 77654e20 0x12ef444

05 7474485b 00000000 00000000 00000000 00000000 0x6c694670  

 

 

As you can see, because of stack corruption, you may not get a complete stack trace.

 

So what requests were running at the time of the stack corruption?

To answer this question, let find first the address of "listconnections ", a global baraible in w3svc containing the list of HTTP requests.

 

0:009> x w3svc!listconnections

65f46548 w3svc!listConnections = struct _LIST_ENTRY [ 0xe93ba8 - 0xe93ba8 ]

65f46548 w3svc!listConnections = struct _LIST_ENTRY [ 0xe93ba8 - 0xe93ba8 ]

 

listconnections is a doubly linked list of "CLIENT_CONN" structure

 

0:009> dt 0xe93ba8 CLIENT_CONN

+0x000 ListEntry : _LIST_ENTRY [ 0x65f46548 - 0x65f46548 ]

+0x008 _Signature : 0x20534343

+0x00c _fIsValid : 1 +0x010 _sClient : 0x544

+0x014 _ccState : 3 ( CCS_PROCESSING_CLIENT_REQ )

+0x018 _cRef : 2 +0x01c _achRemoteAddr : [20] "127.0.0.1"

+0x030 _achLocalAddr : [20] "127.0.0.1" +0x044 _sPort : 0x50

+0x046 _sRemotePort : 0x46a +0x048 _fSecurePort : 0

+0x04c _phttpReq : 0x00e93cd0

+0x050 m_pInstance : 0x00e84498

+0x054 m_pW3Endpoint : 0x00e88228

+0x058 m_pW3Stats : 0x00e84b58

 

 

_phttpReq is a pointer to the HTTP_REQ_BASE class

 

 

0:009> dt 0x00e93cd0 HTTP_REQ_BASE

+0x000 __VFN_table : 0x65f08680

+0x008 _cFilesSent : 0

....

....

+0x9a8 _bufClientRequest : BUFFER

....

....

 

_bufClientRequest holds the http request we are looking for

 

0:009> dt (0x00e93cd0 + 0x9a8) BUFFER

+0x000 m_pb : 0x006ac828 "GET /iisdebuglabs/site/fil_sc1.htm?sf=sc1_v1 HTTP/1.1???"

+0x004 m_cb : 0x1189

+0x008 m_rgb : [32] ""

+0x028 m_fIsDynAlloced : 0y1

+0x028 m_fValid : 0y1

 

 

Note that in this particular scenario, we have only one request. To find other requests, you need to keep dumping the doubly linked list pointers.

 

 

Here is how the code looks like:

 

DWORD WINAPI HttpFilterProc( HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData )

{

...

case SF_NOTIFY_PREPROC_HEADERS:

{

....

else if (!strncmp(szS, "sc1", 3))

return doSC1(1);

....

}

return dwRet;

}

 

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.

 

badfil!corrupt

badfil!doSC1

....

w3svc!HTTP_FILTER::NotifyPreProcHeaderFilters

 

   

 


- Microsoft Confidential -