Merge pull request #4 from Artikash/encapsulate

Encapsulate
This commit is contained in:
Akash Mozumdar 2018-07-19 21:47:51 -04:00 committed by GitHub
commit 379024afc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 41 deletions

View File

@ -32,7 +32,7 @@ add_compile_options(
/MP /MP
/GS- /GS-
$<$<CONFIG:MinSizeRel>:/MT> $<$<CONFIG:MinSizeRel>:/MT>
$<$<CONFIG:Debug>:/MTd> #$<$<CONFIG:Debug>:/MTd>
) )
add_definitions( add_definitions(

View File

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

View File

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

View File

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

View File

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

View File

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