improve read code

This commit is contained in:
Akash Mozumdar 2018-08-26 22:21:15 -04:00
parent 0509bc13a2
commit 41e4b9fd9a
3 changed files with 31 additions and 16 deletions

View File

@ -58,10 +58,13 @@ namespace
return {}; return {};
} }
RCode.remove(0, 1); RCode.remove(0, 1);
QRegExp stringGap("^\\-?[\\dA-F]+"); QRegExp stringGap("^\\*(\\-?[\\dA-F]+)");
if (stringGap.indexIn(RCode) == -1) return {}; if (stringGap.indexIn(RCode) != -1)
hp.offset = stringGap.cap(0).toInt(nullptr, 16); {
RCode.remove(0, stringGap.cap(0).length()); hp.index = stringGap.cap(1).toInt(nullptr, 16);
RCode.remove(0, stringGap.cap(0).length());
hp.type |= DATA_INDIRECT;
}
if (RCode.at(0).unicode() != L'@') return {}; if (RCode.at(0).unicode() != L'@') return {};
RCode.remove(0, 1); RCode.remove(0, 1);
QRegExp address("[\\dA-F]+$"); QRegExp address("[\\dA-F]+$");

View File

@ -12,7 +12,7 @@ struct HookParam
typedef bool(*hook_fun_t)(DWORD esp, HookParam *hp); // jichi 10/24/2014: Add generic hook function, return false if stop execution. typedef bool(*hook_fun_t)(DWORD esp, HookParam *hp); // jichi 10/24/2014: Add generic hook function, return false if stop execution.
unsigned __int64 address; // absolute or relative address unsigned __int64 address; // absolute or relative address
short offset, // offset of the data in the memory int offset, // offset of the data in the memory
index, // deref_offset1 index, // deref_offset1
split, // offset of the split character split, // offset of the split character
split_index; // deref_offset2 split_index; // deref_offset2

View File

@ -264,7 +264,13 @@ bool TextHook::UnsafeInsertHookCode()
} }
BYTE* original; BYTE* original;
if (MH_CreateHook((void*)hp.address, (void*)trampoline, (void**)&original) != MH_OK) return false; if (int err = MH_CreateHook((void*)hp.address, (void*)trampoline, (void**)&original))
if (err == MH_ERROR_ALREADY_CREATED) RemoveHook(hp.address);
else
{
ConsoleOutput(("NextHooker: UnsafeInsertHookCode: FAILED: error " + std::to_string(err)).c_str());
return false;
}
void* thisPtr = (void*)this; void* thisPtr = (void*)this;
void* funcPtr = (void*)((BYTE*)ProcessHook - (BYTE*)(trampoline + 19)); void* funcPtr = (void*)((BYTE*)ProcessHook - (BYTE*)(trampoline + 19));
@ -290,15 +296,26 @@ bool TextHook::UnsafeInsertHookCode()
} }
#endif // _WIN32 #endif // _WIN32
DWORD WINAPI ReaderThread(LPVOID threadParam) DWORD WINAPI ReaderThread(LPVOID hookPtr)
{ {
TextHook* hook = (TextHook*)threadParam; TextHook* hook = (TextHook*)hookPtr;
BYTE buffer[PIPE_BUFFER_SIZE] = {}; BYTE buffer[PIPE_BUFFER_SIZE] = {};
unsigned int changeCount = 0; unsigned int changeCount = 0;
int dataLen = 0; int dataLen = 0;
const char* currentAddress = (char*)hook->hp.address; const void* currentAddress = (void*)hook->hp.address;
while (true) while (true)
{ {
if (!IthGetMemoryRange((void*)hook->hp.address, nullptr, nullptr))
{
ConsoleOutput("NextHooker: can't read desired address");
break;
}
if (hook->hp.type & DATA_INDIRECT) currentAddress = *((char**)hook->hp.address + hook->hp.index);
if (!IthGetMemoryRange(currentAddress, nullptr, nullptr))
{
ConsoleOutput("NextHooker: can't read desired address");
break;
}
Sleep(500); Sleep(500);
if (memcmp(buffer + sizeof(ThreadParam), currentAddress, dataLen + 1) == 0) if (memcmp(buffer + sizeof(ThreadParam), currentAddress, dataLen + 1) == 0)
{ {
@ -308,20 +325,20 @@ DWORD WINAPI ReaderThread(LPVOID threadParam)
if (++changeCount > 10) if (++changeCount > 10)
{ {
ConsoleOutput("NextHooker: memory constantly changing, useless to read"); ConsoleOutput("NextHooker: memory constantly changing, useless to read");
ConsoleOutput("NextHooker: remove read code");
break; break;
} }
if (hook->hp.type & USING_UNICODE) if (hook->hp.type & USING_UNICODE)
dataLen = wcslen((const wchar_t*)currentAddress) * 2; dataLen = wcslen((const wchar_t*)currentAddress) * 2;
else else
dataLen = strlen(currentAddress); dataLen = strlen((const char*)currentAddress);
*(ThreadParam*)buffer = { GetCurrentProcessId(), hook->hp.address, 0, 0 }; *(ThreadParam*)buffer = { GetCurrentProcessId(), hook->hp.address, 0, 0 };
memcpy(buffer + sizeof(ThreadParam), currentAddress, dataLen + 1); memcpy(buffer + sizeof(ThreadParam), currentAddress, dataLen + 1);
DWORD unused; DWORD unused;
WriteFile(::hookPipe, buffer, dataLen + sizeof(ThreadParam), &unused, nullptr); WriteFile(::hookPipe, buffer, dataLen + sizeof(ThreadParam), &unused, nullptr);
} }
ConsoleOutput("NextHooker: remove read code");
hook->ClearHook(); hook->ClearHook();
return 0; return 0;
} }
@ -329,11 +346,6 @@ DWORD WINAPI ReaderThread(LPVOID threadParam)
bool TextHook::InsertReadCode() bool TextHook::InsertReadCode()
{ {
RemoveHook(hp.address); // Artikash 8/25/2018: clear existing RemoveHook(hp.address); // Artikash 8/25/2018: clear existing
if (!IthGetMemoryRange((LPCVOID)hp.address, 0, 0))
{
ConsoleOutput("NextHooker:InsertReadCode failed: cannot access read address");
return false;
}
hp.readerHandle = CreateThread(nullptr, 0, ReaderThread, this, 0, nullptr); hp.readerHandle = CreateThread(nullptr, 0, ReaderThread, this, 0, nullptr);
return true; return true;
} }