DBG: MemPatch + PatchInRange + PatchGet functions
This commit is contained in:
parent
ad4eda86d2
commit
b8754441d7
|
@ -3,6 +3,8 @@
|
||||||
#include "assemble.h"
|
#include "assemble.h"
|
||||||
#include "debugger.h"
|
#include "debugger.h"
|
||||||
#include "addrinfo.h"
|
#include "addrinfo.h"
|
||||||
|
#include "patches.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
static DBGFUNCTIONS _dbgfunctions;
|
static DBGFUNCTIONS _dbgfunctions;
|
||||||
|
|
||||||
|
@ -11,7 +13,7 @@ const DBGFUNCTIONS* dbgfunctionsget()
|
||||||
return &_dbgfunctions;
|
return &_dbgfunctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool sectionfromaddr(duint addr, char* section)
|
static bool _sectionfromaddr(duint addr, char* section)
|
||||||
{
|
{
|
||||||
HMODULE hMod=(HMODULE)modbasefromaddr(addr);
|
HMODULE hMod=(HMODULE)modbasefromaddr(addr);
|
||||||
if(!hMod)
|
if(!hMod)
|
||||||
|
@ -40,13 +42,40 @@ static bool sectionfromaddr(duint addr, char* section)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool _patchget(duint addr)
|
||||||
|
{
|
||||||
|
return patchget(addr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _patchinrange(duint start, duint end)
|
||||||
|
{
|
||||||
|
if(start > end)
|
||||||
|
{
|
||||||
|
duint a=start;
|
||||||
|
start=end;
|
||||||
|
end=a;
|
||||||
|
}
|
||||||
|
for(duint i=start; i<end+1; i++)
|
||||||
|
if(_patchget(i))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _mempatch(duint va, const unsigned char* src, duint size)
|
||||||
|
{
|
||||||
|
return mempatch(fdProcessInfo->hProcess, (void*)va, src, size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void dbgfunctionsinit()
|
void dbgfunctionsinit()
|
||||||
{
|
{
|
||||||
_dbgfunctions.AssembleAtEx=assembleat;
|
_dbgfunctions.AssembleAtEx=assembleat;
|
||||||
_dbgfunctions.SectionFromAddr=sectionfromaddr;
|
_dbgfunctions.SectionFromAddr=_sectionfromaddr;
|
||||||
_dbgfunctions.ModNameFromAddr=modnamefromaddr;
|
_dbgfunctions.ModNameFromAddr=modnamefromaddr;
|
||||||
_dbgfunctions.ModBaseFromAddr=modbasefromaddr;
|
_dbgfunctions.ModBaseFromAddr=modbasefromaddr;
|
||||||
_dbgfunctions.ModBaseFromName=modbasefromname;
|
_dbgfunctions.ModBaseFromName=modbasefromname;
|
||||||
_dbgfunctions.ModSizeFromAddr=modsizefromaddr;
|
_dbgfunctions.ModSizeFromAddr=modsizefromaddr;
|
||||||
_dbgfunctions.Assemble=assemble;
|
_dbgfunctions.Assemble=assemble;
|
||||||
|
_dbgfunctions.PatchGet=_patchget;
|
||||||
|
_dbgfunctions.PatchInRange=_patchinrange;
|
||||||
|
_dbgfunctions.MemPatch=_mempatch;
|
||||||
}
|
}
|
|
@ -8,6 +8,9 @@ typedef duint (*MODBASEFROMADDR)(duint addr);
|
||||||
typedef duint (*MODBASEFROMNAME)(const char* modname);
|
typedef duint (*MODBASEFROMNAME)(const char* modname);
|
||||||
typedef duint (*MODSIZEFROMADDR)(duint addr);
|
typedef duint (*MODSIZEFROMADDR)(duint addr);
|
||||||
typedef bool (*ASSEMBLE)(duint addr, unsigned char* dest, int* size, const char* instruction, char* error);
|
typedef bool (*ASSEMBLE)(duint addr, unsigned char* dest, int* size, const char* instruction, char* error);
|
||||||
|
typedef bool (*PATCHGET)(duint addr);
|
||||||
|
typedef bool (*PATCHINRANGE)(duint start, duint end);
|
||||||
|
typedef bool (*MEMPATCH)(duint va, const unsigned char* src, duint size);
|
||||||
|
|
||||||
struct DBGFUNCTIONS
|
struct DBGFUNCTIONS
|
||||||
{
|
{
|
||||||
|
@ -18,6 +21,9 @@ struct DBGFUNCTIONS
|
||||||
MODBASEFROMNAME ModBaseFromName;
|
MODBASEFROMNAME ModBaseFromName;
|
||||||
MODSIZEFROMADDR ModSizeFromAddr;
|
MODSIZEFROMADDR ModSizeFromAddr;
|
||||||
ASSEMBLE Assemble;
|
ASSEMBLE Assemble;
|
||||||
|
PATCHGET PatchGet;
|
||||||
|
PATCHINRANGE PatchInRange;
|
||||||
|
MEMPATCH MemPatch;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef BUILD_DBG
|
#ifdef BUILD_DBG
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "debugger.h"
|
#include "debugger.h"
|
||||||
|
#include "patches.h"
|
||||||
|
|
||||||
uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size)
|
uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size)
|
||||||
{
|
{
|
||||||
|
@ -83,6 +84,23 @@ bool memwrite(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mempatch(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten)
|
||||||
|
{
|
||||||
|
if(!hProcess or !lpBaseAddress or !lpBuffer or !nSize) //generic failures
|
||||||
|
return false;
|
||||||
|
unsigned char* olddata=(unsigned char*)emalloc(nSize, "mempatch:olddata");
|
||||||
|
if(!memread(hProcess, lpBaseAddress, olddata, nSize, 0))
|
||||||
|
{
|
||||||
|
efree(olddata, "mempatch:olddata");
|
||||||
|
return memwrite(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
|
||||||
|
}
|
||||||
|
unsigned char* newdata=(unsigned char*)lpBuffer;
|
||||||
|
for(uint i=0; i<nSize; i++)
|
||||||
|
patchset((uint)lpBaseAddress+i, olddata[i], newdata[i]);
|
||||||
|
efree(olddata, "mempatch:olddata");
|
||||||
|
return memwrite(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
|
||||||
|
}
|
||||||
|
|
||||||
bool memisvalidreadptr(HANDLE hProcess, uint addr)
|
bool memisvalidreadptr(HANDLE hProcess, uint addr)
|
||||||
{
|
{
|
||||||
unsigned char a=0;
|
unsigned char a=0;
|
||||||
|
|
|
@ -19,6 +19,7 @@ struct PATTERNBYTE
|
||||||
uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size);
|
uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size);
|
||||||
bool memread(HANDLE hProcess, const void* lpBaseAddress, void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead);
|
bool memread(HANDLE hProcess, const void* lpBaseAddress, void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead);
|
||||||
bool memwrite(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten);
|
bool memwrite(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten);
|
||||||
|
bool mempatch(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten);
|
||||||
bool memisvalidreadptr(HANDLE hProcess, uint addr);
|
bool memisvalidreadptr(HANDLE hProcess, uint addr);
|
||||||
void* memalloc(HANDLE hProcess, uint addr, DWORD size, DWORD fdProtect);
|
void* memalloc(HANDLE hProcess, uint addr, DWORD size, DWORD fdProtect);
|
||||||
void memfree(HANDLE hProcess, uint addr);
|
void memfree(HANDLE hProcess, uint addr);
|
||||||
|
|
Loading…
Reference in New Issue