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/stdwindbg.cpp
Views: 11766
#include <stdio.h>1#include <string.h>23#include "byakugan.h"4#include "stdwindbg.h"56char bypassFalse[] = "\x33\xc0" // xor eax, eax7"\xc2\x08\x00"; // ret 889struct debugClientNode *headDebugClient = NULL;101112detectionCallBack::detectionCallBack() : type(NULL), count(0) {}1314HRESULT __stdcall detectionCallBack::QueryInterface(const IID & iid, PVOID * ref) {15*ref = NULL;1617if (iid == __uuidof(IDebugEventCallbacks))18*ref = this;19else if (iid == __uuidof(IUnknown))20*ref = static_cast<IUnknown *>(this);21else22return E_NOINTERFACE;2324return S_OK;25}2627ULONG __stdcall detectionCallBack::AddRef(void) {28return InterlockedIncrement(&count);29}3031ULONG __stdcall detectionCallBack::Release(void) {32ULONG c = InterlockedDecrement(&count);33if (c == 0)34delete this;35return c;36}3738HRESULT __stdcall detectionCallBack::Breakpoint(PDEBUG_BREAKPOINT bp) {39typeNode *detected = type;40ULONG bpid;41dprintf("CAUGHT A BP\n");42bp->GetId(&bpid);43while (detected != NULL && detected->bpid != bpid)44detected = detected->next;45if (detected == NULL) // Not one of ours.46return (DEBUG_STATUS_BREAK);4748dprintf("[Mushishi] Detected %s anti-debugging technique.\n", detected->name);49return (DEBUG_STATUS_BREAK);50//return (DEBUG_STATUS_BREAK);51}5253HRESULT __stdcall detectionCallBack::GetInterestMask(PULONG mask) {54if (mask != NULL)55*mask = DEBUG_EVENT_BREAKPOINT;56return (S_OK);57}5859HRESULT __stdcall detectionCallBack::Exception(PEXCEPTION_RECORD64 exception, ULONG firstChance) {60return E_NOTIMPL;61}6263HRESULT __stdcall detectionCallBack::CreateThread(ULONG64 handle, ULONG64 dataOffset, ULONG64 startOffset) {64return E_NOTIMPL;65}6667HRESULT __stdcall detectionCallBack::ExitThread(ULONG exitCode) {68return E_NOTIMPL;69}7071HRESULT __stdcall detectionCallBack::CreateProcess(72ULONG64 imageFileHandle,73ULONG64 handle,74ULONG64 baseOffset,75ULONG moduleSize,76PCSTR moduleName,77PCSTR imageName,78ULONG checkSum,79ULONG timeDateStamp,80ULONG64 initialThreadHandle,81ULONG64 threadDataOffset,82ULONG64 startOffset) {83return E_NOTIMPL;84}8586HRESULT __stdcall detectionCallBack::ExitProcess(ULONG exitCode) {87return E_NOTIMPL;88}8990HRESULT __stdcall detectionCallBack::LoadModule(91ULONG64 imageFileHandle,92ULONG64 baseOffset,93ULONG moduleSize,94PCSTR moduleName,95PCSTR imageName,96ULONG checkSum,97ULONG timeDateStamp) {98return E_NOTIMPL;99}100101HRESULT __stdcall detectionCallBack::UnloadModule(PCSTR imageBaseName, ULONG64 baseOffset) {102return E_NOTIMPL;103}104105HRESULT __stdcall detectionCallBack::SystemError(ULONG error, ULONG level) {106return E_NOTIMPL;107}108109HRESULT __stdcall detectionCallBack::SessionStatus(ULONG status) {110return E_NOTIMPL;111}112113HRESULT __stdcall detectionCallBack::ChangeDebuggeeState(ULONG flags, ULONG64 argument) {114return E_NOTIMPL;115}116117HRESULT __stdcall detectionCallBack::ChangeEngineState(ULONG flags, ULONG64 argument) {118return E_NOTIMPL;119}120121HRESULT __stdcall detectionCallBack::ChangeSymbolState(ULONG flags, ULONG64 argument) {122return E_NOTIMPL;123}124125126void detectionCallBack::addType(ULONG bpid, char *name) {127typeNode *curr, *newType;128129newType = (typeNode *) malloc(sizeof (typeNode));130if (newType == NULL)131return;132133newType->bpid = bpid;134newType->name = (char *) malloc(strlen(name) + 2);135if (newType->name == NULL) {136free(newType);137return;138}139strncpy(newType->name, name, strlen(name));140141newType->next = type;142type = newType;143}144145void detectionCallBack::recTypeNuke(typeNode *type) {146if (type == NULL)147return;148recTypeNuke(type->next);149free(type->name);150free(type);151}152153detectionCallBack::~detectionCallBack() {154recTypeNuke(type);155}156157struct debugClientNode *addDebugClient(void) {158159#if 0160struct debugClientNode *newNode, *cur;161162newNode = (struct debugClientNode *) malloc(sizeof(struct debugClientNode));163if (newNode == NULL)164return (newNode);165166g_ExtClient->CreateClient(&(newNode->debugClient));167newNode->dcb = new detectionCallBack;168169if (headDebugClient == NULL)170headDebugClient = newNode;171else {172cur = headDebugClient;173while (cur->next != NULL)174cur = cur->next;175cur->next = newNode;176}177#endif178179if (headDebugClient == NULL) {180headDebugClient = (struct debugClientNode *) malloc(sizeof(struct debugClientNode));181g_ExtClient->CreateClient(&(headDebugClient->debugClient));182headDebugClient->dcb = new detectionCallBack;183}184return (headDebugClient);185}186187// Take a function name, resolve it, and replace the first 5 bytes with188// a bypass that returns false.189BOOL disableFunctionFalse(char *funcName) {190ULONG64 funcAddr64;191192if ((funcAddr64 = resolveFunctionByName(funcName)) == NULL)193return (FALSE);194g_ExtData->WriteVirtual(funcAddr64, (PVOID) bypassFalse, 5, NULL);195return (TRUE);196}197198199ULONG64 resolveFunctionByName(char *funcName) {200ULONG64 funcAddr64;201202g_ExtSymbols->Reload("/f kernel32.dll");203if (g_ExtSymbols->GetOffsetByName(funcName, &funcAddr64) == E_FAIL)204funcAddr64 = NULL;205if (funcAddr64 != NULL)206dprintf("[Byakugan] Resolved function '%s' @ 0x%16x.\n", funcName, funcAddr64);207else {208dprintf("[Byakugan] Unable to resolve function '%s' :(\n", funcName);209return (NULL);210}211return (funcAddr64);212}213214BOOL detectCallByName(char *funcName, char *detectionName) {215if (detectExecByAddr(resolveFunctionByName(funcName), detectionName) == NULL)216return (FALSE);217return (TRUE);218}219220PDEBUG_BREAKPOINT detectExecByAddr(ULONG64 funcAddr64, char *detectionName) {221HRESULT retCode;222IDebugBreakpoint *bp;223ULONG id;224struct debugClientNode *newDebugClient;225226if (funcAddr64 == NULL)227return (NULL);228229newDebugClient = addDebugClient();230231g_ExtControl->AddBreakpoint(DEBUG_BREAKPOINT_CODE, DEBUG_ANY_ID, &bp);232bp->SetOffset(funcAddr64);233bp->SetFlags(DEBUG_BREAKPOINT_ENABLED|DEBUG_BREAKPOINT_ONE_SHOT);234bp->GetId(&id);235newDebugClient->dcb->addType(id, detectionName);236retCode = newDebugClient->debugClient->SetEventCallbacks(newDebugClient->dcb);237//dprintf("[Mushishi] SetEventCallbacks: 0x%08x\n", retCode);238return (bp);239}240241PDEBUG_BREAKPOINT detectWriteByAddr(ULONG64 funcAddr64, char *detectionName) {242HRESULT retCode;243IDebugBreakpoint *bp;244ULONG id;245struct debugClientNode *newDebugClient;246247if (funcAddr64 == NULL)248return (NULL);249250newDebugClient = addDebugClient();251252g_ExtControl->AddBreakpoint(DEBUG_BREAKPOINT_DATA, DEBUG_ANY_ID, &bp);253bp->SetOffset(funcAddr64);254bp->SetDataParameters(4, DEBUG_BREAK_WRITE);255bp->SetFlags(DEBUG_BREAKPOINT_ENABLED);256bp->GetId(&id);257258newDebugClient->dcb->addType(id, detectionName);259retCode = newDebugClient->debugClient->SetEventCallbacks(newDebugClient->dcb);260//dprintf("[Mushishi] SetEventCallbacks: 0x%08x\n", retCode);261return (bp);262}263264PDEBUG_BREAKPOINT detectReadByAddr(ULONG64 funcAddr64, char *detectionName) {265HRESULT retCode;266IDebugBreakpoint *bp;267ULONG id;268struct debugClientNode *newDebugClient;269270if (funcAddr64 == NULL)271return (NULL);272273newDebugClient = addDebugClient();274275g_ExtControl->AddBreakpoint(DEBUG_BREAKPOINT_DATA, DEBUG_ANY_ID, &bp);276bp->SetOffset(funcAddr64);277bp->SetDataParameters(4, DEBUG_BREAK_READ);278bp->SetFlags(DEBUG_BREAKPOINT_ENABLED);279bp->GetId(&id);280newDebugClient->dcb->addType(id, detectionName);281retCode = newDebugClient->debugClient->SetEventCallbacks(newDebugClient->dcb);282//dprintf("[Mushishi] SetEventCallbacks: 0x%08x\n", retCode);283return (bp);284}285286DWORD parseHexInput(char *hexInput, DWORD size, char *output) {287return (0);288}289290DWORD readFileIntoBuf(char *path, DWORD size, char **output, DWORD offset) {291HANDLE inputFile;292DWORD readOut = 1, i = 0;293char out;294BYTE state = 0;295296if((inputFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,297FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {298dprintf("[S] Unable to open file: %s\n", path);299return (0);300}301if (size == 0)302size = GetFileSize(inputFile, NULL) - 1;303304*output = (char *) malloc(size + 1);305if (!*output) {306dprintf("[S] Unable to allocate memory for %s\n", path);307return (0);308}309if(offset == 0) {310while (readOut > 0 && i < size) {311ReadFile(inputFile, &out, 1, &readOut, NULL);312(*output)[i++] = out;313}314}315else {316if(SetFilePointer(inputFile,offset, NULL,FILE_BEGIN) == INVALID_SET_FILE_POINTER ){317dprintf("[S] Unable to read at offset %d for %s\n", offset, path);318return (0);319}320while (readOut > 0 && i < size) {321322ReadFile(inputFile, &out, 1, &readOut, NULL);323(*output)[i++] = out;324}325}326return (i);327}328329330