This commit is contained in:
Akash Mozumdar 2018-07-19 01:43:31 -04:00
parent 05a14d201e
commit e4f5f3cfd5
2 changed files with 118 additions and 147 deletions

View File

@ -15,7 +15,7 @@
#include "winmutex/winmutex.h"
#include <atlbase.h>
#define HM_LOCK CriticalSectionLocker locker(hmcs) // Synchronized scope for accessing private data
#define HM_LOCK CriticalSectionLocker locker(hmCs) // Synchronized scope for accessing private data
HookManager::HookManager() :
current(nullptr),
@ -28,67 +28,58 @@ HookManager::HookManager() :
textThreadsByParams(),
processRecordsByIds()
{
InitializeCriticalSection(&hmCs);
TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay);
consoleTextThread->Status() |= USING_UNICODE;
SetCurrent(consoleTextThread);
InitializeCriticalSection(&hmcs);
}
HookManager::~HookManager()
{
DeleteCriticalSection(&hmcs);
HM_LOCK;
DeleteCriticalSection(&hmCs);
}
TextThread *HookManager::FindSingle(DWORD number)
{
HM_LOCK;
for (auto i : textThreadsByParams)
{
if (i.second->Number() == number)
{
return i.second;
}
}
return nullptr;
}
void HookManager::SetCurrent(TextThread *it)
{
if (current)
HM_LOCK;
current->Status() &= ~CURRENT_SELECT;
current = it;
if (it)
it->Status() |= CURRENT_SELECT;
}
void HookManager::SelectCurrent(DWORD num)
{
if (TextThread *st = FindSingle(num)) {
HM_LOCK;
if (TextThread *st = FindSingle(num))
{
SetCurrent(st);
if (reset)
reset(st);
//st->ResetEditText();
if (reset) reset(st);
}
}
void HookManager::RemoveSingleHook(DWORD pid, DWORD addr)
{
HM_LOCK;
std::vector<ThreadParameter> removedThreads;
for (auto i : textThreadsByParams)
{
if (i.first.pid == pid && i.first.hook == addr)
{
if (remove)
{
remove(i.second);
}
if (remove) remove(i.second);
delete i.second;
removedThreads.push_back(i.first);
}
}
for (auto i : removedThreads)
{
textThreadsByParams.erase(i);
}
for (auto i : removedThreads) textThreadsByParams.erase(i);
SelectCurrent(0);
}
@ -97,44 +88,31 @@ void HookManager::RemoveProcessContext(DWORD pid)
HM_LOCK;
std::vector<ThreadParameter> removedThreads;
for (auto i : textThreadsByParams)
if (i.first.pid == pid)
{
if (i.first.hook == pid)
{
if (remove)
{
remove(i.second);
}
if (remove) remove(i.second);
delete i.second;
removedThreads.push_back(i.first);
}
}
for (auto i : removedThreads)
{
textThreadsByParams.erase(i);
}
for (auto i : removedThreads) textThreadsByParams.erase(i);
SelectCurrent(0);
}
void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe)
{
HM_LOCK;
ProcessRecord* record = processRecordsByIds[pid] = new ProcessRecord;
record->hostPipe = hostPipe;
record->hookman_section = OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(pid)).c_str());
record->hookman_map = MapViewOfFile(record->hookman_section, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2); // jichi 1/16/2015: Changed to half to hook section size
record->process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
record->hookman_mutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str());
if (attach)
attach(pid);
if (attach) attach(pid);
}
void HookManager::UnRegisterProcess(DWORD pid)
{
HM_LOCK;
ProcessRecord pr = *processRecordsByIds[pid];
CloseHandle(pr.hookman_mutex);
UnmapViewOfFile(pr.hookman_map);
@ -142,9 +120,7 @@ void HookManager::UnRegisterProcess(DWORD pid)
CloseHandle(pr.hookman_section);
processRecordsByIds.erase(pid);
RemoveProcessContext(pid);
if (detach)
detach(pid);
if (detach) detach(pid);
}
void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, const BYTE *text, int len)
@ -153,21 +129,19 @@ void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, con
if (!text || !pid || len <= 0)
return;
HM_LOCK;
ThreadParameter tp = {pid, hook, retn, spl};
ThreadParameter tp = { pid, hook, retn, spl };
TextThread *it;
if (!(it = textThreadsByParams[tp]))
if ((it = textThreadsByParams[tp]) == nullptr)
{
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay);
if (create)
{
create(it);
}
if (create) create(it);
}
it->AddText(text, len);
}
void HookManager::AddConsoleOutput(LPCWSTR text)
{
HM_LOCK;
if (text)
{
int len = wcslen(text) * 2;
@ -179,11 +153,8 @@ void HookManager::AddConsoleOutput(LPCWSTR text)
void HookManager::ClearCurrent()
{
HM_LOCK;
if (current) {
current->Reset();
if (reset)
reset(current);
}
if (reset) reset(current);
}
ProcessRecord *HookManager::GetProcessRecord(DWORD pid)
@ -200,24 +171,25 @@ HANDLE HookManager::GetHostPipe(DWORD pid)
HookParam HookManager::GetHookParam(DWORD pid, DWORD addr)
{
HM_LOCK;
HookParam ret = {};
ProcessRecord* pr = GetProcessRecord(pid);
if (pr == nullptr) return ret;
WaitForSingleObject(pr->hookman_mutex, 0);
MutexLocker locker(pr->hookman_mutex);
const Hook* hooks = (const Hook*)pr->hookman_map;
for (int i = 0; i < MAX_HOOK; ++i)
if (hooks[i].Address() == addr)
ret = hooks[i].hp;
ReleaseMutex(pr->hookman_mutex);
return ret;
}
std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
{
HM_LOCK;
std::string buffer;
ProcessRecord* pr = GetProcessRecord(pid);
if (pr == nullptr) return L"";
WaitForSingleObject(pr->hookman_mutex, 0);
MutexLocker locker(pr->hookman_mutex);
USES_CONVERSION;
const Hook* hooks = (const Hook*)pr->hookman_map;
for (int i = 0; i < MAX_HOOK; ++i)
@ -228,7 +200,6 @@ std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
ReadProcessMemory(pr->process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
}
}
ReleaseMutex(pr->hookman_mutex);
return std::wstring(A2W(buffer.c_str()));
}

View File

@ -63,7 +63,7 @@ private:
std::unordered_map<ThreadParameter, TextThread*, ThreadParameterHasher> textThreadsByParams;
std::unordered_map<DWORD, ProcessRecord*> processRecordsByIds;
CRITICAL_SECTION hmcs;
CRITICAL_SECTION hmCs;
TextThread *current;