2016-01-06 00:01:17 +09:00
|
|
|
// host.cc
|
|
|
|
// 8/24/2013 jichi
|
|
|
|
// Branch IHF/main.cpp, rev 111
|
|
|
|
|
|
|
|
#include "host.h"
|
2018-07-19 00:46:52 -04:00
|
|
|
#include "pipe.h"
|
2018-08-23 11:53:23 -04:00
|
|
|
#include "const.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "../vnrhook/hijack/texthook.h"
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
struct ProcessRecord
|
|
|
|
{
|
|
|
|
HANDLE processHandle;
|
|
|
|
HANDLE sectionMutex;
|
|
|
|
HANDLE section;
|
|
|
|
LPVOID sectionMap;
|
|
|
|
HANDLE hostPipe;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Artikash 5/31/2018: required for unordered_map to work with struct key
|
|
|
|
template <> struct std::hash<ThreadParam> { size_t operator()(const ThreadParam& tp) const { return std::hash<__int64>()((tp.pid + tp.hook) ^ (tp.retn + tp.spl)); } };
|
2018-08-23 13:15:24 -04:00
|
|
|
bool operator==(const ThreadParam& one, const ThreadParam& two) { return one.pid == two.pid && one.hook == two.hook && one.retn == two.retn && one.spl == two.spl; }
|
2018-08-22 21:31:15 -04:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
// Artikash 7/20/2018: similar to std::lock guard but use Winapi objects for cross process comms
|
|
|
|
class MutexLocker
|
2018-08-22 21:31:15 -04:00
|
|
|
{
|
2018-08-23 11:53:23 -04:00
|
|
|
HANDLE mutex;
|
|
|
|
public:
|
|
|
|
MutexLocker(HANDLE mutex) : mutex(mutex) { WaitForSingleObject(mutex, 0); }
|
|
|
|
~MutexLocker() { if (mutex != INVALID_HANDLE_VALUE && mutex != nullptr) ReleaseMutex(mutex); }
|
2018-08-22 21:31:15 -04:00
|
|
|
};
|
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
std::unordered_map<ThreadParam, TextThread*> textThreadsByParams;
|
2018-08-21 22:43:30 -04:00
|
|
|
std::unordered_map<DWORD, ProcessRecord> processRecordsByIds;
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-21 22:43:30 -04:00
|
|
|
std::recursive_mutex hostMutex;
|
2018-07-23 12:25:02 -07:00
|
|
|
|
2018-08-21 22:43:30 -04:00
|
|
|
ThreadEventCallback OnCreate, OnRemove;
|
|
|
|
ProcessEventCallback OnAttach, OnDetach;
|
2018-07-23 12:25:02 -07:00
|
|
|
|
2018-08-21 22:43:30 -04:00
|
|
|
DWORD DUMMY[100];
|
2018-07-23 12:25:02 -07:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
ThreadParam CONSOLE{ 0, -1ULL, -1ULL, -1ULL };
|
2018-08-22 18:05:45 -04:00
|
|
|
|
2018-08-21 22:43:30 -04:00
|
|
|
#define HOST_LOCK std::lock_guard<std::recursive_mutex> hostLocker(hostMutex) // Synchronized scope for accessing private data
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-07-23 12:25:02 -07:00
|
|
|
namespace Host
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-08-21 22:43:30 -04:00
|
|
|
DLLEXPORT void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove)
|
2018-05-11 16:46:05 -04:00
|
|
|
{
|
2018-08-22 00:57:32 -04:00
|
|
|
OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnRemove = onRemove;
|
2018-08-22 18:05:45 -04:00
|
|
|
OnCreate(textThreadsByParams[CONSOLE] = new TextThread(CONSOLE, USING_UNICODE));
|
2018-08-23 11:53:23 -04:00
|
|
|
CreatePipe();
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DLLEXPORT void Close()
|
|
|
|
{
|
2018-07-25 01:11:23 -07:00
|
|
|
// Artikash 7/25/2018: This is only called when NextHooker is closed, at which point Windows should free everything itself...right?
|
2018-08-22 18:25:23 -04:00
|
|
|
#ifdef _DEBUG // Check memory leaks
|
2018-08-21 22:43:30 -04:00
|
|
|
HOST_LOCK;
|
2018-08-22 11:51:28 -04:00
|
|
|
OnRemove = [](TextThread* textThread) { delete textThread; };
|
2018-08-21 22:43:30 -04:00
|
|
|
for (auto i : processRecordsByIds) UnregisterProcess(i.first);
|
2018-08-22 18:05:45 -04:00
|
|
|
delete textThreadsByParams[CONSOLE];
|
|
|
|
#endif
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DLLEXPORT bool InjectProcess(DWORD processId, DWORD timeout)
|
|
|
|
{
|
|
|
|
if (processId == GetCurrentProcessId()) return false;
|
|
|
|
|
|
|
|
CloseHandle(CreateMutexW(nullptr, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId)).c_str()));
|
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
2018-07-28 12:41:21 -07:00
|
|
|
AddConsoleOutput(L"already injected");
|
2018-07-23 12:25:02 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMODULE textHooker = LoadLibraryExW(ITH_DLL, nullptr, DONT_RESOLVE_DLL_REFERENCES);
|
|
|
|
wchar_t textHookerPath[MAX_PATH];
|
|
|
|
unsigned int textHookerPathSize = GetModuleFileNameW(textHooker, textHookerPath, MAX_PATH) * 2 + 2;
|
|
|
|
FreeLibrary(textHooker);
|
|
|
|
|
|
|
|
if (HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId))
|
2018-08-22 15:11:58 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN64
|
|
|
|
BOOL invalidProcess = FALSE;
|
|
|
|
IsWow64Process(processHandle, &invalidProcess);
|
|
|
|
if (invalidProcess)
|
|
|
|
{
|
|
|
|
AddConsoleOutput(L"architecture mismatch: try 32 bit NextHooker instead");
|
|
|
|
CloseHandle(processHandle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-23 12:25:02 -07:00
|
|
|
if (LPVOID remoteData = VirtualAllocEx(processHandle, nullptr, textHookerPathSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))
|
2018-08-22 15:11:58 -04:00
|
|
|
{
|
|
|
|
WriteProcessMemory(processHandle, remoteData, textHookerPath, textHookerPathSize, nullptr);
|
|
|
|
if (HANDLE thread = CreateRemoteThread(processHandle, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr))
|
|
|
|
{
|
|
|
|
WaitForSingleObject(thread, timeout);
|
|
|
|
CloseHandle(thread);
|
|
|
|
VirtualFreeEx(processHandle, remoteData, 0, MEM_RELEASE);
|
|
|
|
CloseHandle(processHandle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
VirtualFreeEx(processHandle, remoteData, 0, MEM_RELEASE);
|
|
|
|
CloseHandle(processHandle);
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 12:25:02 -07:00
|
|
|
|
|
|
|
AddConsoleOutput(L"couldn't inject dll");
|
2018-07-17 17:01:56 -04:00
|
|
|
return false;
|
2018-05-11 16:46:05 -04:00
|
|
|
}
|
2018-07-23 12:25:02 -07:00
|
|
|
|
|
|
|
DLLEXPORT bool DetachProcess(DWORD processId)
|
2018-05-11 16:46:05 -04:00
|
|
|
{
|
2018-08-23 11:53:23 -04:00
|
|
|
int command = HOST_COMMAND_DETACH;
|
2018-08-21 22:43:30 -04:00
|
|
|
return WriteFile(processRecordsByIds[processId].hostPipe, &command, sizeof(command), DUMMY, nullptr);
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DLLEXPORT bool InsertHook(DWORD pid, HookParam hp, std::string name)
|
|
|
|
{
|
2018-08-23 11:53:23 -04:00
|
|
|
auto info = InsertHookCmd(hp, name);
|
|
|
|
return WriteFile(processRecordsByIds[pid].hostPipe, &info, sizeof(info), DUMMY, nullptr);
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
|
|
|
|
2018-08-23 12:18:03 -04:00
|
|
|
DLLEXPORT bool RemoveHook(DWORD pid, unsigned __int64 addr)
|
2018-07-23 12:25:02 -07:00
|
|
|
{
|
2018-08-23 11:53:23 -04:00
|
|
|
auto info = RemoveHookCmd(addr);
|
|
|
|
return WriteFile(processRecordsByIds[pid].hostPipe, &info, sizeof(info), DUMMY, nullptr);
|
2018-05-11 16:46:05 -04:00
|
|
|
}
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-23 12:18:03 -04:00
|
|
|
DLLEXPORT HookParam GetHookParam(DWORD pid, unsigned __int64 addr)
|
2018-07-23 12:25:02 -07:00
|
|
|
{
|
|
|
|
HOST_LOCK;
|
|
|
|
HookParam ret = {};
|
|
|
|
ProcessRecord pr = processRecordsByIds[pid];
|
2018-08-22 21:31:15 -04:00
|
|
|
if (pr.sectionMap == nullptr) return ret;
|
|
|
|
MutexLocker locker(pr.sectionMutex);
|
2018-08-23 11:53:23 -04:00
|
|
|
const TextHook* hooks = (const TextHook*)pr.sectionMap;
|
2018-07-23 12:25:02 -07:00
|
|
|
for (int i = 0; i < MAX_HOOK; ++i)
|
2018-08-23 12:18:03 -04:00
|
|
|
if (hooks[i].Address() == addr)
|
2018-07-23 12:25:02 -07:00
|
|
|
ret = hooks[i].hp;
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
DLLEXPORT HookParam GetHookParam(ThreadParam tp) { return GetHookParam(tp.pid, tp.hook); }
|
2018-08-21 22:43:30 -04:00
|
|
|
|
2018-08-23 12:18:03 -04:00
|
|
|
DLLEXPORT std::wstring GetHookName(DWORD pid, unsigned __int64 addr)
|
2018-05-11 16:46:05 -04:00
|
|
|
{
|
2018-07-23 22:57:54 -07:00
|
|
|
if (pid == 0) return L"Console";
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
|
|
|
std::string buffer = "";
|
|
|
|
ProcessRecord pr = processRecordsByIds[pid];
|
2018-08-22 21:31:15 -04:00
|
|
|
if (pr.sectionMap == nullptr) return L"";
|
|
|
|
MutexLocker locker(pr.sectionMutex);
|
2018-08-23 11:53:23 -04:00
|
|
|
const TextHook* hooks = (const TextHook*)pr.sectionMap;
|
2018-07-23 12:25:02 -07:00
|
|
|
for (int i = 0; i < MAX_HOOK; ++i)
|
2018-08-23 12:18:03 -04:00
|
|
|
if (hooks[i].Address() == addr)
|
2018-07-23 12:25:02 -07:00
|
|
|
{
|
|
|
|
buffer.resize(hooks[i].NameLength());
|
2018-08-22 21:31:15 -04:00
|
|
|
ReadProcessMemory(pr.processHandle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
|
|
|
USES_CONVERSION;
|
|
|
|
return std::wstring(A2W(buffer.c_str()));
|
2018-05-11 16:46:05 -04:00
|
|
|
}
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
DLLEXPORT TextThread* GetThread(ThreadParam tp)
|
2018-07-23 12:25:02 -07:00
|
|
|
{
|
|
|
|
HOST_LOCK;
|
2018-08-21 22:43:30 -04:00
|
|
|
return textThreadsByParams[tp];
|
2018-07-23 12:25:02 -07:00
|
|
|
}
|
2018-05-11 16:46:05 -04:00
|
|
|
|
2018-07-23 12:25:02 -07:00
|
|
|
DLLEXPORT void AddConsoleOutput(std::wstring text)
|
2018-05-11 16:46:05 -04:00
|
|
|
{
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
2018-08-22 18:05:45 -04:00
|
|
|
textThreadsByParams[CONSOLE]->AddSentence(std::wstring(text));
|
2018-05-11 16:46:05 -04:00
|
|
|
}
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
void DispatchText(ThreadParam tp, const BYTE* text, int len)
|
2018-05-11 16:46:05 -04:00
|
|
|
{
|
2018-08-21 22:43:30 -04:00
|
|
|
if (!text || len <= 0) return;
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
|
|
|
TextThread *it;
|
|
|
|
if ((it = textThreadsByParams[tp]) == nullptr)
|
2018-08-21 22:43:30 -04:00
|
|
|
OnCreate(it = textThreadsByParams[tp] = new TextThread(tp, Host::GetHookParam(tp).type));
|
2018-07-23 12:25:02 -07:00
|
|
|
it->AddText(text, len);
|
2018-05-11 16:46:05 -04:00
|
|
|
}
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-08-23 11:53:23 -04:00
|
|
|
void RemoveThreads(bool(*RemoveIf)(ThreadParam, ThreadParam), ThreadParam cmp)
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
2018-08-23 11:53:23 -04:00
|
|
|
std::vector<ThreadParam> removedThreads;
|
2018-07-23 12:25:02 -07:00
|
|
|
for (auto i : textThreadsByParams)
|
|
|
|
if (RemoveIf(i.first, cmp))
|
|
|
|
{
|
2018-08-21 22:43:30 -04:00
|
|
|
OnRemove(i.second);
|
2018-07-24 10:39:02 -07:00
|
|
|
//delete i.second; // Artikash 7/24/2018: FIXME: Qt GUI updates on another thread, so I can't delete this yet.
|
2018-07-23 12:25:02 -07:00
|
|
|
removedThreads.push_back(i.first);
|
|
|
|
}
|
|
|
|
for (auto i : removedThreads) textThreadsByParams.erase(i);
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
2018-07-23 12:25:02 -07:00
|
|
|
void RegisterProcess(DWORD pid, HANDLE hostPipe)
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
|
|
|
ProcessRecord record;
|
|
|
|
record.hostPipe = hostPipe;
|
2018-08-22 21:31:15 -04:00
|
|
|
record.section = OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(pid)).c_str());
|
|
|
|
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.processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
|
|
|
|
record.sectionMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str());
|
2018-07-23 12:25:02 -07:00
|
|
|
processRecordsByIds[pid] = record;
|
2018-08-21 22:43:30 -04:00
|
|
|
OnAttach(pid);
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
2018-07-23 12:25:02 -07:00
|
|
|
void UnregisterProcess(DWORD pid)
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-07-23 12:25:02 -07:00
|
|
|
HOST_LOCK;
|
|
|
|
ProcessRecord pr = processRecordsByIds[pid];
|
|
|
|
if (!pr.hostPipe) return;
|
2018-08-22 21:31:15 -04:00
|
|
|
CloseHandle(pr.sectionMutex);
|
|
|
|
UnmapViewOfFile(pr.sectionMap);
|
|
|
|
CloseHandle(pr.processHandle);
|
|
|
|
CloseHandle(pr.section);
|
2018-08-21 22:43:30 -04:00
|
|
|
processRecordsByIds[pid] = {};
|
2018-07-23 12:25:02 -07:00
|
|
|
RemoveThreads([](auto one, auto two) { return one.pid == two.pid; }, { pid, 0, 0, 0 });
|
2018-08-21 22:43:30 -04:00
|
|
|
OnDetach(pid);
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
2018-07-24 10:39:02 -07:00
|
|
|
// EOF
|