This commit is contained in:
Akash Mozumdar 2018-08-22 21:31:15 -04:00
parent fc00a3c1ed
commit 98649ffbcc
2 changed files with 28 additions and 28 deletions

View File

@ -9,6 +9,15 @@
#include "../vnrhook/include/defs.h" #include "../vnrhook/include/defs.h"
#include "../vnrhook/include/types.h" #include "../vnrhook/include/types.h"
struct ThreadParameterHasher
{
size_t operator()(const ThreadParameter& tp) const
{
return std::hash<__int64>()(tp.pid << 6) + std::hash<__int64>()(tp.hook) + std::hash<__int64>()(tp.retn) + std::hash<__int64>()(tp.spl);
}
};
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;
@ -25,7 +34,6 @@ ThreadParameter CONSOLE{ 0, -1UL, -1UL, -1UL };
namespace Host namespace Host
{ {
DLLEXPORT void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove) DLLEXPORT void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove)
{ {
OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnRemove = onRemove; OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnRemove = onRemove;
@ -120,9 +128,9 @@ namespace Host
HOST_LOCK; HOST_LOCK;
HookParam ret = {}; HookParam ret = {};
ProcessRecord pr = processRecordsByIds[pid]; ProcessRecord pr = processRecordsByIds[pid];
if (pr.hookman_map == nullptr) return ret; if (pr.sectionMap == nullptr) return ret;
MutexLocker locker(pr.hookman_mutex); MutexLocker locker(pr.sectionMutex);
const Hook* hooks = (const Hook*)pr.hookman_map; const Hook* hooks = (const Hook*)pr.sectionMap;
for (int i = 0; i < MAX_HOOK; ++i) for (int i = 0; i < MAX_HOOK; ++i)
if ((DWORD)hooks[i].Address() == addr) if ((DWORD)hooks[i].Address() == addr)
ret = hooks[i].hp; ret = hooks[i].hp;
@ -137,14 +145,14 @@ namespace Host
HOST_LOCK; HOST_LOCK;
std::string buffer = ""; std::string buffer = "";
ProcessRecord pr = processRecordsByIds[pid]; ProcessRecord pr = processRecordsByIds[pid];
if (pr.hookman_map == nullptr) return L""; if (pr.sectionMap == nullptr) return L"";
MutexLocker locker(pr.hookman_mutex); MutexLocker locker(pr.sectionMutex);
const Hook* hooks = (const Hook*)pr.hookman_map; const Hook* hooks = (const Hook*)pr.sectionMap;
for (int i = 0; i < MAX_HOOK; ++i) for (int i = 0; i < MAX_HOOK; ++i)
if ((DWORD)hooks[i].Address() == addr) if ((DWORD)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.processHandle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
} }
USES_CONVERSION; USES_CONVERSION;
return std::wstring(A2W(buffer.c_str())); return std::wstring(A2W(buffer.c_str()));
@ -192,10 +200,10 @@ void RegisterProcess(DWORD pid, HANDLE hostPipe)
HOST_LOCK; HOST_LOCK;
ProcessRecord record; 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.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.sectionMap = MapViewOfFile(record.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.processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
record.hookman_mutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str()); record.sectionMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str());
processRecordsByIds[pid] = record; processRecordsByIds[pid] = record;
OnAttach(pid); OnAttach(pid);
} }
@ -205,10 +213,10 @@ void UnregisterProcess(DWORD pid)
HOST_LOCK; HOST_LOCK;
ProcessRecord pr = processRecordsByIds[pid]; ProcessRecord pr = processRecordsByIds[pid];
if (!pr.hostPipe) return; if (!pr.hostPipe) return;
CloseHandle(pr.hookman_mutex); CloseHandle(pr.sectionMutex);
UnmapViewOfFile(pr.hookman_map); UnmapViewOfFile(pr.sectionMap);
CloseHandle(pr.process_handle); CloseHandle(pr.processHandle);
CloseHandle(pr.hookman_section); CloseHandle(pr.section);
processRecordsByIds[pid] = {}; processRecordsByIds[pid] = {};
RemoveThreads([](auto one, auto two) { return one.pid == two.pid; }, { pid, 0, 0, 0 }); RemoveThreads([](auto one, auto two) { return one.pid == two.pid; }, { pid, 0, 0, 0 });
OnDetach(pid); OnDetach(pid);

View File

@ -12,24 +12,16 @@
struct ProcessRecord struct ProcessRecord
{ {
HANDLE process_handle; HANDLE processHandle;
HANDLE hookman_mutex; HANDLE sectionMutex;
HANDLE hookman_section; HANDLE section;
LPVOID hookman_map; LPVOID sectionMap;
HANDLE hostPipe; HANDLE hostPipe;
}; };
typedef std::function<void(DWORD)> ProcessEventCallback; typedef std::function<void(DWORD)> ProcessEventCallback;
typedef std::function<void(TextThread*)> ThreadEventCallback; typedef std::function<void(TextThread*)> ThreadEventCallback;
struct ThreadParameterHasher
{
size_t operator()(const ThreadParameter& tp) const
{
return std::hash<__int64>()(tp.pid << 6) + std::hash<__int64>()(tp.hook) + std::hash<__int64>()(tp.retn) + std::hash<__int64>()(tp.spl);
}
};
namespace Host namespace Host
{ {
DLLEXPORT void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove); DLLEXPORT void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove);