remove direct access to process records

This commit is contained in:
Akash Mozumdar 2018-07-19 20:51:49 -04:00
parent 5ae1e811d7
commit d6a90e5426
5 changed files with 28 additions and 40 deletions

View File

@ -118,7 +118,7 @@ void ProcessWindow::RefreshThread(int index)
item.iItem = index;
ListView_GetItem(hlProcess, &item);
DWORD pid = item.lParam;
bool isAttached = man->GetProcessRecord(pid) != NULL;
bool isAttached = man->GetHostPipe(pid) != NULL;
RefreshThreadWithPID(pid, isAttached);
}

View File

@ -171,12 +171,11 @@ std::wstring GetCode(const HookParam& hp, DWORD pid)
std::wstring GetModuleFileNameAsString(DWORD pid, PVOID allocationBase)
{
const ProcessRecord* pr = man->GetProcessRecord(pid);
if (pr)
UniqueHandle hProc(OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid));
if (hProc.get())
{
HANDLE hProc = pr->process_handle;
WCHAR path[MAX_PATH];
if (GetModuleFileNameEx(hProc, (HMODULE)allocationBase, path, MAX_PATH))
if (GetModuleFileNameEx(hProc.get(), (HMODULE)allocationBase, path, MAX_PATH))
return path;
}
return L"";
@ -184,12 +183,11 @@ std::wstring GetModuleFileNameAsString(DWORD pid, PVOID allocationBase)
PVOID GetAllocationBase(DWORD pid, LPCVOID addr)
{
const ProcessRecord *pr = man->GetProcessRecord(pid);
if (pr)
UniqueHandle hProc(OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid));
if (hProc.get())
{
MEMORY_BASIC_INFORMATION info;
HANDLE hProc = pr->process_handle;
if (VirtualQueryEx(hProc, addr, &info, sizeof(info)))
if (VirtualQueryEx(hProc.get(), addr, &info, sizeof(info)))
{
if (info.Type & MEM_IMAGE)
return info.AllocationBase;

View File

@ -438,15 +438,12 @@ void ThreadCreate(TextThread* thread)
//thread->RegisterFilterCallBack(ThreadFilter, 0);
AddToCombo(*thread, false);
auto tp = thread->GetThreadParameter();
auto pr = man->GetProcessRecord(tp.pid);
if (pr == NULL)
return;
if (IsUnicodeHook(*pr, tp.hook))
thread->Status() |= USING_UNICODE;
auto hook = man->GetHookParam(tp.pid, tp.hook);
if (hook.type & USING_UNICODE) thread->Status() |= USING_UNICODE;
auto pf = pfman->GetProfile(tp.pid);
if (!pf)
return;
const std::wstring& hook_name = GetHookNameByAddress(*pr, thread->GetThreadParameter().hook);
const std::wstring& hook_name = man->GetHookName(tp.pid, tp.hook);
auto thread_profile = pf->FindThread(&thread->GetThreadParameter(), hook_name);
if (thread_profile != pf->Threads().end())
{

View File

@ -101,19 +101,19 @@ void HookManager::RemoveProcessContext(DWORD pid)
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());
ProcessRecord record = processRecordsByIds[pid];
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);
}
void HookManager::UnRegisterProcess(DWORD pid)
{
HM_LOCK;
ProcessRecord pr = *processRecordsByIds[pid];
ProcessRecord pr = processRecordsByIds[pid];
CloseHandle(pr.hookman_mutex);
UnmapViewOfFile(pr.hookman_map);
CloseHandle(pr.process_handle);
@ -154,26 +154,20 @@ void HookManager::ClearCurrent()
if (reset) reset(current);
}
ProcessRecord *HookManager::GetProcessRecord(DWORD pid)
{
HM_LOCK;
return processRecordsByIds[pid];
}
HANDLE HookManager::GetHostPipe(DWORD pid)
{
HM_LOCK;
return processRecordsByIds[pid] ? processRecordsByIds[pid]->hostPipe : nullptr;
return processRecordsByIds[pid].hostPipe;
}
HookParam HookManager::GetHookParam(DWORD pid, DWORD addr)
{
HM_LOCK;
HookParam ret = {};
ProcessRecord* pr = GetProcessRecord(pid);
if (pr == nullptr) return ret;
MutexLocker locker(pr->hookman_mutex);
const Hook* hooks = (const Hook*)pr->hookman_map;
ProcessRecord pr = processRecordsByIds[pid];
if (pr.hookman_map == nullptr) return ret;
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;
@ -184,17 +178,17 @@ std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
{
HM_LOCK;
std::string buffer;
ProcessRecord* pr = GetProcessRecord(pid);
if (pr == nullptr) return L"";
MutexLocker locker(pr->hookman_mutex);
ProcessRecord pr = processRecordsByIds[pid];
if (pr.hookman_map == nullptr) return L"";
MutexLocker locker(pr.hookman_mutex);
USES_CONVERSION;
const Hook* hooks = (const Hook*)pr->hookman_map;
const Hook* hooks = (const Hook*)pr.hookman_map;
for (int i = 0; i < MAX_HOOK; ++i)
{
if (hooks[i].Address() == addr)
{
buffer.resize(hooks[i].NameLength());
ReadProcessMemory(pr->process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
ReadProcessMemory(pr.process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
}
}
return std::wstring(A2W(buffer.c_str()));

View File

@ -38,7 +38,6 @@ public:
~HookManager();
TextThread *FindSingle(DWORD number);
ProcessRecord *GetProcessRecord(DWORD pid);
HANDLE GetHostPipe(DWORD pid);
void ClearCurrent();
void SelectCurrent(DWORD num);
@ -62,7 +61,7 @@ public:
private:
std::unordered_map<ThreadParameter, TextThread*, ThreadParameterHasher> textThreadsByParams;
std::unordered_map<DWORD, ProcessRecord*> processRecordsByIds;
std::unordered_map<DWORD, ProcessRecord> processRecordsByIds;
CRITICAL_SECTION hmCs;