1
0
Fork 0

DBG: MemPatch + PatchInRange + PatchGet functions

This commit is contained in:
Mr. eXoDia 2014-07-05 21:41:10 +02:00
parent ad4eda86d2
commit b8754441d7
4 changed files with 56 additions and 2 deletions

View File

@ -3,6 +3,8 @@
#include "assemble.h"
#include "debugger.h"
#include "addrinfo.h"
#include "patches.h"
#include "memory.h"
static DBGFUNCTIONS _dbgfunctions;
@ -11,7 +13,7 @@ const DBGFUNCTIONS* dbgfunctionsget()
return &_dbgfunctions;
}
static bool sectionfromaddr(duint addr, char* section)
static bool _sectionfromaddr(duint addr, char* section)
{
HMODULE hMod=(HMODULE)modbasefromaddr(addr);
if(!hMod)
@ -40,13 +42,40 @@ static bool sectionfromaddr(duint addr, char* section)
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()
{
_dbgfunctions.AssembleAtEx=assembleat;
_dbgfunctions.SectionFromAddr=sectionfromaddr;
_dbgfunctions.SectionFromAddr=_sectionfromaddr;
_dbgfunctions.ModNameFromAddr=modnamefromaddr;
_dbgfunctions.ModBaseFromAddr=modbasefromaddr;
_dbgfunctions.ModBaseFromName=modbasefromname;
_dbgfunctions.ModSizeFromAddr=modsizefromaddr;
_dbgfunctions.Assemble=assemble;
_dbgfunctions.PatchGet=_patchget;
_dbgfunctions.PatchInRange=_patchinrange;
_dbgfunctions.MemPatch=_mempatch;
}

View File

@ -8,6 +8,9 @@ typedef duint (*MODBASEFROMADDR)(duint addr);
typedef duint (*MODBASEFROMNAME)(const char* modname);
typedef duint (*MODSIZEFROMADDR)(duint addr);
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
{
@ -18,6 +21,9 @@ struct DBGFUNCTIONS
MODBASEFROMNAME ModBaseFromName;
MODSIZEFROMADDR ModSizeFromAddr;
ASSEMBLE Assemble;
PATCHGET PatchGet;
PATCHINRANGE PatchInRange;
MEMPATCH MemPatch;
};
#ifdef BUILD_DBG

View File

@ -1,5 +1,6 @@
#include "memory.h"
#include "debugger.h"
#include "patches.h"
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;
}
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)
{
unsigned char a=0;

View File

@ -19,6 +19,7 @@ struct PATTERNBYTE
uint memfindbaseaddr(HANDLE hProcess, uint addr, uint* size);
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 mempatch(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten);
bool memisvalidreadptr(HANDLE hProcess, uint addr);
void* memalloc(HANDLE hProcess, uint addr, DWORD size, DWORD fdProtect);
void memfree(HANDLE hProcess, uint addr);