Debug Labs


Heap Corruption/Buffer Overrun

This lab will demonstrate a heap corruption in a CGI Executable caused by a buffer overwrite. 

 

1- Lab Setup
	

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

 

1- Set the IIS Admin Service to interact with the Desktop, and verify the service is running under the system account.

- In "Administrative Tools" click "Services"

- Double Click "IIS Admin Service"

- Click the "Log On" Tab

- Verify that the radio button "Local System Account" is selected

- Check "Allow Services to interact with Desktop" checkbox

2- Set the "Image File Execution Options" for the CGI Executable "badCgi.exe"

- Start Registry Editor (Regedt32.exe).

- Locate the following registry key: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\]

- Create a subkey with the name "badCgi.exe"

- To the "badCgi.exe" subkey, add the REG_SZ value named Debugger and the full path to the debugger with the "-g" switch: eg: c:\debugger\windbg -g

 

3- Set the "CreateProcessAsUser" metabase prooperty to "false"

- Open the command prompt (cmd)

- Switch to "Inepub\Adminscripts" folder

- Type in: CScript.exe adsutil.vbs set w3svc/CreateProcessAsUser "false" and hit Enter

 

 

2- Run Scenario

1- Click the "Corrupt Heap" button.
 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".                     

                 

 

1- Debug Annotation

Definition:

 

Heap Corruption occurs when a thread allocates a block of heap memory of a given size and then writes to memory

addresses beyond the requested size of the heap block. Another common cause of heap corruption is writing to block

of memory that has been freed or freeing a block of memory already freed.

 

In this scenario, we will try to demonstrate heap corruption caused by overwriting to a heap block beyond the requested

size (Buffer Overwrite) 

 

 

1) Without Page Heap Enabled.

 

The faulting Stack

 

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

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:

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

Symbol search path is: SRV*C:\LabSymbols*\\symbols\symbols

 

0:008> .sympath+ C :\IISDebugLabs\symbols\Pri (where private 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 AV'd would look like:

 

0:000> g

HEAP[badcgi.exe]: Number of free blocks in arena (2) does not match number in the free lists (3)

(770.b5c): Break instruction exception - code 80000003 (first chance)

eax=00230000 ebx=00230050 ecx=0006f604 edx=0006f3ba esi=00000003 edi= 00230000

eip=77f97704 esp=0006f588 ebp=0006f58c iopl=0 nv up ei pl nz na pe nc

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

ntdll!DbgBreakPoint:

77f97704 cc int 3

 

 

0:000> kvn

# ChildEBP RetAddr Args to Child

00 0006f584 77fb73c0 0006f5bc 77fb7163 00230000 ntdll!DbgBreakPoint (FPO: [0,0,0]) [..\i386\debug2.asm @ 42]

01 0006f58c 77fb7163 00230000 00230000 00230000 ntdll!RtlpBreakPointHeap+0x26 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 2374]

02 0006f5bc 77fad14f 00000208 00000040 00000003 ntdll!RtlpValidateHeap+0x33c (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdbg.c @ 2332]

03 0006f614 77fad222 00230000 00000000 00000000 ntdll!RtlValidateHeap+0xd9 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdll.c @ 2641]

04 0006fe40 77f9b001 00000001 77f896c3 7ffdf000 ntdll!RtlValidateProcessHeaps+0x79 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\rtl\heapdll.c @ 2767]

05 0006fe98 77e9cfb1 77f82b95 00000001 7ffdf000 ntdll!LdrShutdownProcess+0xbf (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\ntos\dll\ldrinit.c @ 1543]

06 0006ff5c 7800c347 00000001 77f83920 7800c2af KERNEL32!ExitProcess+0x51 (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\process.c @ 2881]

07 0006ff78 01001e7c 00000001 77f82b95 77f83920 MSVCRT!doexit+0xb0 (FPO: [3,0,2]) (CONV: cdecl) [crt0dat.c @ 394]

08 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi!mainCRTStartup+0x10c (CONV: cdecl) [crtexe.c @ 343]

09 0006fff0 00000000 01001d70 00000000 000000c8 KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]

 

 

The faulting stack in a heap corruption issue will involve heap operations. In this scenario, we are dealing with one thread

in the process "badCgi.exe". Once the HTTP request is processed, the process will get unloaded. So what happened here, is

The request was processed and while shutting down the process, the heap could not validated because it was corrupt, hence an AV.

 

 

Basically, we know in advance that the culprit is "gadCgi.exe" because we are debugging it, but we still don't know what

function corrupted it!

 

To know who corrupted the heap, we need to Enable Page Heap (A memory management feature to track more closely all heap operations).

This second part of this debug annotation shows how to do that and identify the culprit.

 

2) With Page Heap Enabled.

 

a) Turn On "Enable Page Heap"

 

- Go to the Command Prompt

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

- Type in "GFlags" and hit Enter

- In the "Image File Name", Enter "inetinfo.exe" without quotes

- Click "Image File Options" radio button

- Click "Enable Page Heap" check box

- Click "Apply"

- Click "OK"

- Restart IIS

 

b) Run the scenario again

 

- Click "Corrupt Heap" one time

	- 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
       - In the WinDbg command line, type in "g" and press "Enter" 

						
            - Notice the "Page Heap" Debug output. 
                    
	c) Line up the Private Symbols                    

 

- 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

 

 

The faulting Stack

 

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

 

 

0:000> kvn

# ChildEBP RetAddr Args to Child

00 0006ff70 01001e6f 00000001 003b6fd0 00350f30 badcgi!doHC1+0x23 (CONV: stdcall) [H:\nT\PRIVATE\inet\iis\svcs\w3\gateways\cgiDBG\badcgi.cpp @ 251]

01 0006ffc0 77e9ca90 77f82b95 77f83920 7ffdf000 badcgi!mainCRTStartup+0xff (CONV: cdecl) [crtexe.c @ 338]

02 0006fff0 00000000 01001d70 00000000 eeeeeeee KERNEL32!BaseProcessStart+0x3d (FPO: [Non-Fpo]) (CONV: stdcall) [D:\nt\private\windows\base\client\support.c @ 504]

 

 

So basically, this is the thread that corrupted the heap, It's shows now that the culprit is the function call "doHC1" as well as the line of code

in "badCgi.cpp"

 

Verify the filter's symbols are lined up.

 

 

0:000> lmv mbadcgi

start end module name

01000000 01004000 badcgi (private pdb symbols) c:\iisdebuglabs\symbols\pri\badcgi.pdb

Loaded symbol image file: badcgi.exe Image path: C:\IISDebugLabs\www\app\badcgi.exe

Timestamp: Sun Jun 22 20:08:49 2003 (3EF64511) Checksum: 00002AC2

 

Line 251 looks like

 

DWORD doHC1(int Variation) //Buffer Overwrite

{

if (1 == Variation) // Straight forward to corrupt the heap

{

char *ptr, *tmp;

int i;

ptr = (char*)GlobalAlloc(GMEM_FIXED, 16);

tmp = ptr;

for (i = 0; i < 32; ++i)

*(tmp++) = 'a';

free(ptr); // line 251

}

}

 

 

 

 

 


- Microsoft Confidential -