Textractor/GUI/host/host.cc

275 lines
8.6 KiB
C++
Raw Normal View History

// host.cc
// 8/24/2013 jichi
// Branch IHF/main.cpp, rev 111
#include "host.h"
2018-08-23 11:53:23 -04:00
#include "const.h"
#include "defs.h"
#include "../vnrhook/hijack/texthook.h"
2018-08-24 14:04:23 -04:00
#include <atlbase.h> // A2W
2018-08-24 12:50:20 -04:00
namespace
2018-08-23 11:53:23 -04:00
{
2018-08-24 12:50:20 -04:00
struct ProcessRecord
{
HANDLE processHandle;
HANDLE sectionMutex;
HANDLE section;
LPVOID sectionMap;
HANDLE hostPipe;
};
ThreadEventCallback OnCreate, OnRemove;
ProcessEventCallback OnAttach, OnDetach;
2018-08-22 21:31:15 -04:00
2018-08-24 12:50:20 -04:00
std::unordered_map<ThreadParam, TextThread*> textThreadsByParams;
std::unordered_map<DWORD, ProcessRecord> processRecordsByIds;
std::recursive_mutex hostMutex;
2018-07-23 12:25:02 -07:00
2018-08-24 12:50:20 -04:00
DWORD DUMMY[100];
ThreadParam CONSOLE{ 0, -1ULL, -1ULL, -1ULL };
void DispatchText(ThreadParam tp, const BYTE* text, int len)
{
if (!text || len <= 0) return;
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-08-24 12:50:20 -04:00
TextThread *it;
if ((it = textThreadsByParams[tp]) == nullptr)
OnCreate(it = textThreadsByParams[tp] = new TextThread(tp, Host::GetHookParam(tp).type));
it->AddText(text, len);
}
2018-07-23 12:25:02 -07:00
2018-08-24 12:50:20 -04:00
void RemoveThreads(std::function<bool(ThreadParam)> removeIf)
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-08-24 12:50:20 -04:00
std::vector<ThreadParam> removedThreads;
for (auto i : textThreadsByParams)
if (removeIf(i.first))
{
OnRemove(i.second);
//delete i.second; // Artikash 7/24/2018: FIXME: Qt GUI updates on another thread, so I can't delete this yet.
removedThreads.push_back(i.first);
}
for (auto i : removedThreads) textThreadsByParams.erase(i);
}
2018-07-23 12:25:02 -07:00
2018-08-24 12:50:20 -04:00
void RegisterProcess(DWORD pid, HANDLE hostPipe)
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-08-24 12:50:20 -04:00
ProcessRecord record;
record.hostPipe = hostPipe;
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());
processRecordsByIds[pid] = record;
OnAttach(pid);
}
void UnregisterProcess(DWORD pid)
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-08-24 12:50:20 -04:00
ProcessRecord pr = processRecordsByIds[pid];
if (!pr.hostPipe) return;
CloseHandle(pr.sectionMutex);
UnmapViewOfFile(pr.sectionMap);
CloseHandle(pr.processHandle);
CloseHandle(pr.section);
processRecordsByIds[pid] = {};
2018-09-01 14:11:48 -04:00
RemoveThreads([&](ThreadParam tp) { return tp.pid == pid; });
2018-08-24 12:50:20 -04:00
OnDetach(pid);
}
void StartPipe()
{
2018-09-01 14:11:48 -04:00
std::thread([]
2018-08-24 12:50:20 -04:00
{
2018-09-20 23:04:11 -04:00
SECURITY_DESCRIPTOR pipeSD = {};
InitializeSecurityDescriptor(&pipeSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&pipeSD, TRUE, NULL, FALSE); // Allow non-admin processes to connect to pipe created by admin host
SECURITY_ATTRIBUTES pipeSA = { sizeof(SECURITY_ATTRIBUTES), &pipeSD, FALSE };
HANDLE hookPipe = CreateNamedPipeW(ITH_TEXT_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA);
HANDLE hostPipe = CreateNamedPipeW(ITH_COMMAND_PIPE, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA);
2018-08-24 12:50:20 -04:00
ConnectNamedPipe(hookPipe, nullptr);
BYTE buffer[PIPE_BUFFER_SIZE + 1] = {};
DWORD bytesRead, processId;
ReadFile(hookPipe, &processId, sizeof(processId), &bytesRead, nullptr);
RegisterProcess(processId, hostPipe);
2018-09-09 22:37:48 -04:00
// jichi 9/27/2013: why recursion?
// Artikash 5/20/2018: Easy way to create a new pipe for another process
StartPipe();
2018-08-24 12:50:20 -04:00
while (ReadFile(hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr))
switch (*(int*)buffer)
{
//case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later
//break;
case HOST_NOTIFICATION_RMVHOOK:
{
auto info = *(HookRemovedNotif*)buffer;
2018-09-01 14:11:48 -04:00
RemoveThreads([&](ThreadParam tp) { return tp.pid == processId && tp.hook == info.address; });
2018-08-24 12:50:20 -04:00
}
break;
case HOST_NOTIFICATION_TEXT:
{
auto info = *(ConsoleOutputNotif*)buffer;
USES_CONVERSION;
Host::AddConsoleOutput(A2W(info.message));
}
break;
default:
{
ThreadParam tp = *(ThreadParam*)buffer;
buffer[bytesRead] = 0;
buffer[bytesRead + 1] = 0;
2018-08-24 12:50:20 -04:00
DispatchText(tp, buffer + sizeof(tp), bytesRead - sizeof(tp));
}
break;
}
2018-08-22 18:05:45 -04:00
2018-08-24 12:50:20 -04:00
DisconnectNamedPipe(hookPipe);
DisconnectNamedPipe(hostPipe);
UnregisterProcess(processId);
CloseHandle(hookPipe);
CloseHandle(hostPipe);
}).detach();
}
}
2018-07-23 12:25:02 -07:00
namespace Host
{
2018-08-24 12:50:20 -04:00
void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove)
{
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-24 12:50:20 -04:00
StartPipe();
2018-07-23 12:25:02 -07:00
}
2018-08-24 12:50:20 -04:00
void Close()
2018-07-23 12:25:02 -07:00
{
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-28 17:21:20 -04:00
LOCK(hostMutex);
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
}
2018-08-24 12:50:20 -04:00
bool InjectProcess(DWORD processId, DWORD timeout)
2018-07-23 12:25:02 -07:00
{
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];
2018-09-20 21:59:07 -04:00
DWORD textHookerPathSize = GetModuleFileNameW(textHooker, textHookerPath, MAX_PATH) * 2 + 2;
2018-07-23 12:25:02 -07:00
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-07-23 12:25:02 -07:00
2018-08-24 14:04:23 -04:00
void DetachProcess(DWORD processId)
{
2018-08-23 11:53:23 -04:00
int command = HOST_COMMAND_DETACH;
2018-08-24 14:04:23 -04:00
WriteFile(processRecordsByIds[processId].hostPipe, &command, sizeof(command), DUMMY, nullptr);
2018-07-23 12:25:02 -07:00
}
2018-08-24 14:04:23 -04:00
void InsertHook(DWORD pid, HookParam hp, std::string name)
2018-07-23 12:25:02 -07:00
{
2018-08-23 11:53:23 -04:00
auto info = InsertHookCmd(hp, name);
2018-08-24 14:04:23 -04:00
WriteFile(processRecordsByIds[pid].hostPipe, &info, sizeof(info), DUMMY, nullptr);
2018-07-23 12:25:02 -07:00
}
2018-09-20 21:59:07 -04:00
void RemoveHook(DWORD pid, uint64_t addr)
2018-07-23 12:25:02 -07:00
{
2018-08-23 11:53:23 -04:00
auto info = RemoveHookCmd(addr);
2018-08-24 14:04:23 -04:00
WriteFile(processRecordsByIds[pid].hostPipe, &info, sizeof(info), DUMMY, nullptr);
}
2018-09-20 21:59:07 -04:00
HookParam GetHookParam(DWORD pid, uint64_t addr)
2018-07-23 12:25:02 -07:00
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-07-23 12:25:02 -07:00
HookParam ret = {};
ProcessRecord pr = processRecordsByIds[pid];
2018-08-22 21:31:15 -04:00
if (pr.sectionMap == nullptr) return ret;
2018-08-24 12:50:20 -04:00
WaitForSingleObject(pr.sectionMutex, 0);
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-25 15:45:25 -04:00
if (hooks[i].hp.address == addr)
2018-07-23 12:25:02 -07:00
ret = hooks[i].hp;
2018-08-24 12:50:20 -04:00
ReleaseMutex(pr.sectionMutex);
2018-07-23 12:25:02 -07:00
return ret;
}
2018-08-24 12:50:20 -04:00
HookParam GetHookParam(ThreadParam tp) { return GetHookParam(tp.pid, tp.hook); }
2018-08-21 22:43:30 -04:00
2018-09-20 21:59:07 -04:00
std::wstring GetHookName(DWORD pid, uint64_t addr)
{
2018-07-23 22:57:54 -07:00
if (pid == 0) return L"Console";
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-07-23 12:25:02 -07:00
std::string buffer = "";
ProcessRecord pr = processRecordsByIds[pid];
2018-08-22 21:31:15 -04:00
if (pr.sectionMap == nullptr) return L"";
2018-08-24 12:50:20 -04:00
WaitForSingleObject(pr.sectionMutex, 0);
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-25 15:45:25 -04:00
if (hooks[i].hp.address == addr)
2018-07-23 12:25:02 -07:00
{
2018-08-25 15:45:25 -04:00
buffer.resize(hooks[i].name_length);
ReadProcessMemory(pr.processHandle, hooks[i].hook_name, &buffer[0], hooks[i].name_length, nullptr);
2018-07-23 12:25:02 -07:00
}
2018-08-24 12:50:20 -04:00
ReleaseMutex(pr.sectionMutex);
2018-07-23 12:25:02 -07:00
USES_CONVERSION;
return std::wstring(A2W(buffer.c_str()));
}
2018-08-24 12:50:20 -04:00
TextThread* GetThread(ThreadParam tp)
2018-07-23 12:25:02 -07:00
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-08-21 22:43:30 -04:00
return textThreadsByParams[tp];
2018-07-23 12:25:02 -07:00
}
2018-09-01 14:11:48 -04:00
void AddConsoleOutput(std::wstring text) { GetThread(CONSOLE)->AddSentence(std::wstring(text)); }
}
// EOF