Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/injectsu/noxheap.c
Views: 11779
#include <windows.h>1#include <detours.h>2#define NTSTATUS ULONG34#include "../heapStructs.h"56/* NoxHeapINT(ELLiGENCE) - because heap stupidity means no 0day7* (or Tenketsu)8* ------------------------------------------------------------9* By Lin0xx / Pusscat10* ------------------------------------------------------------11* This dll is intended to be injected into a target12* application whose heap allocations, reallocations, and frees13* need to be tracked. The end goal for this program is to be14* able to communicate to a visualization server that will draw15* the heap as it is in real time. This view of the heap can16* then be diff'd via walking the heap by finding the heap base17* in the process environment block. By doing this, one will18* be able to understand how an application is molding the heap19* along with the nature of the overflow in question.20*/2122#define NTSTATUS ULONG23#define BUFSIZE 409624/* UNDOCUMENTED HEAP STRUCTURES */2526typedef struct _RTL_HEAP_DEFINITION {27ULONG Length;28ULONG Unknown1;29ULONG Unknown2;30ULONG Unknown3;31ULONG Unknown4;32ULONG Unknown5;33ULONG Unknown6;34ULONG Unknown7;35ULONG Unknown8;36ULONG Unknown9;37ULONG Unknown10;38ULONG Unknown11;39ULONG Unknown12;40} RTL_HEAP_DEFINITION, *PRTL_HEAP_DEFINITION;414243LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\tenketsu");44HANDLE hPipe;45DWORD dwMode = PIPE_READMODE_MESSAGE;46DWORD bytesWritten;4748/* Functions to be hooked */49PVOID (WINAPI *realRtlAllocateHeap)(PVOID heapHandle, ULONG flags, ULONG size);50PVOID (WINAPI *realRtlReallocateHeap)(PVOID heapHandle, ULONG flags, PVOID memoryPointer, ULONG size);51PVOID (WINAPI *realRtlFreeHeap)(PVOID heapHandle, ULONG flags, PVOID memoryPointer);5253PVOID (WINAPI *realRtlCreateHeap)(ULONG flags, PVOID base, ULONG reserve, ULONG commit, BOOLEAN lock, PRTL_HEAP_DEFINITION RtlHeapParams);5455NTSTATUS (WINAPI *realRtlDestroyHeap)(PVOID heapHandle);56PVOID (WINAPI *realRtlpCoalesceFreeBlocks)(PVOID, ULONG, ULONG, ULONG);5758/* TO ADD:59* RtlAllocateMemoryBlockLookaside60* RtlpCoalesceFreeBlocks61*/6263/* End hooking section */6465PVOID WINAPI noxRtlFreeHeap(PVOID heapHandle, ULONG flags, PVOID memoryPointer){66PVOID ret;67struct FreeStruct freeinfo;6869ret = (*realRtlFreeHeap)(heapHandle, flags, memoryPointer);7071freeinfo.type = FREESTRUCT;72freeinfo.heapHandle = heapHandle;73freeinfo.flags = flags;74freeinfo.memoryPointer = memoryPointer;75freeinfo.ret = ret;7677__asm78{79push ebx80mov ebx, [ebp+4]81mov freeinfo.caller, ebx82pop ebx83}8485WriteFile(hPipe, &freeinfo, sizeof(struct FreeStruct), &bytesWritten, NULL);8687return (ret);8889}9091PVOID WINAPI noxRtlReallocateHeap(PVOID heapHandle, ULONG flags, PVOID memoryPointer, ULONG size){92PVOID ret;93struct ReallocateStruct reallocinfo;9495ret = (*realRtlReallocateHeap)(heapHandle, flags, memoryPointer, size);96reallocinfo.type = REALLOCATESTRUCT;97reallocinfo.heapHandle = heapHandle;98reallocinfo.flags = flags;99reallocinfo.memoryPointer = memoryPointer;100reallocinfo.size = size;101reallocinfo.ret = ret;102103__asm104{105push ebx106mov ebx, [ebp+4]107mov reallocinfo.caller, ebx108pop ebx109}110111WriteFile(hPipe, &reallocinfo, sizeof(struct ReallocateStruct), &bytesWritten, NULL);112113return (ret);114}115116PVOID WINAPI noxRtlAllocateHeap(PVOID heapHandle, ULONG flags, ULONG size){117PVOID ret;118struct AllocateStruct allocinfo;119120ret = (*realRtlAllocateHeap)(heapHandle, flags, size);121122allocinfo.type = ALLOCATESTRUCT;123allocinfo.heapHandle = heapHandle;124allocinfo.flags = flags;125allocinfo.size = size;126allocinfo.ret = ret;127128__asm129{130push ebx131mov ebx, [ebp+4]132mov allocinfo.caller, ebx133pop ebx134}135136WriteFile(hPipe, &allocinfo, sizeof(struct AllocateStruct), &bytesWritten, NULL);137138return (ret);139}140141PVOID WINAPI noxRtlCreateHeap( ULONG flags,142PVOID base,143ULONG reserve,144ULONG commit,145BOOLEAN lock,146PRTL_HEAP_DEFINITION RtlHeapParams) {147PVOID ret;148struct CreateStruct createinfo;149150ret = (*realRtlCreateHeap)(flags, base, reserve, commit, lock, RtlHeapParams);151152createinfo.type = CREATESTRUCT;153createinfo.flags = flags;154createinfo.base = base;155createinfo.reserve = reserve;156createinfo.commit = commit;157createinfo.lock = lock;158createinfo.RtlHeapParams = RtlHeapParams;159createinfo.ret = ret;160161WriteFile(hPipe, &createinfo, sizeof(struct CreateStruct), &bytesWritten, NULL);162163return (ret);164}165166NTSTATUS WINAPI noxRtlDestroyHeap(PVOID heapHandle) {167NTSTATUS ret;168struct DestroyStruct destroyinfo;169170ret = (*realRtlDestroyHeap)(heapHandle);171172destroyinfo.type = DESTROYSTRUCT;173destroyinfo.heapHandle = heapHandle;174destroyinfo.ret = ret;175176WriteFile(hPipe, &destroyinfo, sizeof(struct DestroyStruct), &bytesWritten, NULL);177178return (ret);179}180181// PLACEHOLDER FUNCTION182PVOID WINAPI noxRtlpCoalesceFreeBlocks(PVOID heapHandle, ULONG arg2, ULONG arg3, ULONG arg4) {183struct CoalesceStruct coalesceinfo;184PVOID ret;185186coalesceinfo.type = COALESCESTRUCT;187coalesceinfo.heapHandle = heapHandle;188coalesceinfo.arg2 = arg2;189coalesceinfo.arg3 = arg3;190coalesceinfo.arg4 = arg4;191192WriteFile(hPipe, &coalesceinfo, sizeof(struct CoalesceStruct), &bytesWritten, NULL);193194ret = (*realRtlpCoalesceFreeBlocks)(heapHandle, arg2, arg3, arg4);195196return (ret);197}198199BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD attachReason, LPVOID reserved) {200ULONG bytesRead;201TCHAR buf[BUFSIZE];202NTSTATUS fSuccess;203204if(attachReason == DLL_PROCESS_ATTACH){205DetourTransactionBegin();206DetourUpdateThread(GetCurrentThread());207208/* There's no other way to find these addresses than dynamically */209realRtlAllocateHeap = DetourFindFunction("ntdll.dll", "RtlAllocateHeap");210realRtlReallocateHeap = DetourFindFunction("ntdll.dll", "RtlReAllocateHeap");211realRtlFreeHeap = DetourFindFunction("ntdll.dll", "RtlFreeHeap");212realRtlCreateHeap = DetourFindFunction("ntdll.dll", "RtlCreateHeap");213realRtlDestroyHeap = DetourFindFunction("ntdll.dll", "RtlDestroyHeap");214215216217/* Start hooking */218DetourAttach(&(PVOID)realRtlAllocateHeap, noxRtlAllocateHeap);219DetourAttach(&(PVOID)realRtlReallocateHeap, noxRtlReallocateHeap);220DetourAttach(&(PVOID)realRtlFreeHeap, noxRtlFreeHeap);221DetourAttach(&(PVOID)realRtlCreateHeap, noxRtlCreateHeap);222DetourAttach(&(PVOID)realRtlDestroyHeap, noxRtlDestroyHeap);223//while (1) {224hPipe = CreateFile( lpszPipename,225GENERIC_READ | GENERIC_WRITE,2260,227NULL,228OPEN_EXISTING,2290,230NULL);231232if (hPipe == INVALID_HANDLE_VALUE) // got a handle, so we're done233__asm {int 3}234235// WaitNamedPipe(lpszPipename, 2000); // Wait two seconds before retry236//}237SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL);238239// Get addresses of unexposed heap functions if the debugger has symbols240ReadFile( hPipe,241&realRtlpCoalesceFreeBlocks,242//BUFSIZE*sizeof(TCHAR),2434,244&bytesRead,245NULL);246if (realRtlpCoalesceFreeBlocks != NULL)247DetourAttach(&(PVOID)realRtlpCoalesceFreeBlocks, noxRtlpCoalesceFreeBlocks);248249//FlushFileBuffers(hPipe);250251DetourTransactionCommit();252253}254255if(attachReason == DLL_PROCESS_DETACH){256DetourTransactionBegin();257DetourUpdateThread(GetCurrentThread());258259/* Start unhooking */260DetourDetach(&(PVOID)realRtlAllocateHeap, noxRtlAllocateHeap);261DetourDetach(&(PVOID)realRtlReallocateHeap, noxRtlReallocateHeap);262DetourDetach(&(PVOID)realRtlFreeHeap, noxRtlFreeHeap);263DetourDetach(&(PVOID)realRtlCreateHeap, noxRtlCreateHeap);264DetourDetach(&(PVOID)realRtlDestroyHeap, noxRtlDestroyHeap);265266DetourTransactionCommit();267}268269return TRUE;270}271272273274275