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/mushishi.cpp
Views: 11766
#include <windows.h>1#include <stdlib.h>2#include <stdio.h>34#include "byakugan.h"5#include "mushishi.h"6#include "stdwindbg.h"78#define CRUSH_DR_CONTEXT "e esp+0x34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00; g"9#define REWRITE_IMAGE_SIZE "t; ed 0x%08x 0x%08x; g;"1011ULONG originalImageSize;1213BOOL maskHardwareBreaks(void) {14ULONG64 funcAddr64;15PDEBUG_BREAKPOINT bp;1617if ((funcAddr64 = resolveFunctionByName("RtlDispatchException")) == NULL)18return (FALSE);19g_ExtControl->AddBreakpoint(DEBUG_BREAKPOINT_CODE, DEBUG_ANY_ID, &bp);20bp->SetCommand(CRUSH_DR_CONTEXT);21bp->SetOffset(funcAddr64);22bp->SetFlags(DEBUG_BREAKPOINT_ENABLED);2324return (TRUE);25}2627// FIXME28BOOL DetectHardwareBreakCheck(void) {29ULONG64 funcAddr64;30PDEBUG_BREAKPOINT bp;3132if ((funcAddr64 = resolveFunctionByName("RtlDispatchException")) == NULL)33return (FALSE);34g_ExtControl->AddBreakpoint(DEBUG_BREAKPOINT_CODE, DEBUG_ANY_ID, &bp);35bp->SetCommand(CRUSH_DR_CONTEXT);36bp->SetOffset(funcAddr64);37bp->SetFlags(DEBUG_BREAKPOINT_ENABLED);3839return (TRUE);40}4142ULONG64 getPointerToImageSize() {43ULONG64 ptr;4445ptr = GetExpression("poi(poi(poi(fs:[0x30]) + 0xC) + 0xC)");46ptr += 0x20;4748return (ptr);49}5051BOOL protectImageSize() {52ULONG64 imageSizePtr;53PDEBUG_BREAKPOINT bp;54char rewriteCommand[64];5556imageSizePtr = getPointerToImageSize();5758memset(rewriteCommand, 0, 64);59_snprintf_s(rewriteCommand, 64 - 1, "poi(0x%08x)", imageSizePtr);60originalImageSize = GetExpression(rewriteCommand);61dprintf("[Mushishi] Original Image Size: 0x%08x\n", originalImageSize);6263bp = detectWriteByAddr(imageSizePtr, "overwrite of PEB image size");64memset(rewriteCommand, 0, 64);65_snprintf_s(rewriteCommand, 64 - 1, REWRITE_IMAGE_SIZE, imageSizePtr, originalImageSize);66bp->SetCommand(rewriteCommand);6768return (TRUE);69}7071BOOL detectImageSizeOverwrite() {72ULONG64 imageSizePtr;73PDEBUG_BREAKPOINT bp;7475imageSizePtr = getPointerToImageSize();76dprintf("[Mushishi] ImageSize found at 0x%08x\n", imageSizePtr);77bp = detectWriteByAddr(imageSizePtr, "overwrite of PEB image size");7879return (TRUE);80}8182void mushishiDetect(void) {8384// 1) Check for a call to CheckRemoteDebuggerPresent85detectCallByName("CheckRemoteDebuggerPresent", "CheckRemoteDebuggerPresent");8687// 2) Check for reading of the dr0-dr3 section of CONTEXT structs88//DetectHardwareBreakCheck();8990// 3) Check for a call to OutputDebugString which is sensitive to an attached debugger91detectCallByName("OutputDebugString", "OutputDebugString");9293// 4) Check for an overwrite of the image size94detectImageSizeOverwrite();9596// 5) Check for setLastError97detectCallByName("SetLastError", "SetLastError");98}99100void mushishiDefeat(void) {101// 1) Call CheckRemoteDebuggerPresent to detect attached debugger102// Disable the check for remote debugger103if (disableFunctionFalse("CheckRemoteDebuggerPresent") == FALSE)104dprintf("[Mushishi] Unable to disable \"CheckRemoteDebuggerPresent\" function!\n");105106// 2) Force a hardware exception, then check the SEH CONTEXT to see if dr0-dr3 are set107// Clear hardware breakpoints from SEH CONTEXT108if (maskHardwareBreaks() == FALSE)109dprintf("[Mushishi] Unable to disable Hardware Breakpoint checks from CONTEXT!\n");110111// 3)112if (protectImageSize() == FALSE)113dprintf("[Mushishi] Unable to protect the image size!\n");114}115116117