Textractor/GUI/host/host.cc

288 lines
8.5 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"
2018-11-10 23:29:12 -05:00
#include "../vnrhook/texthook.h"
2018-08-24 12:50:20 -04:00
namespace
2018-08-23 11:53:23 -04:00
{
class ProcessRecord
2018-08-24 12:50:20 -04:00
{
public:
ProcessRecord(DWORD processId, HANDLE hostPipe) :
hostPipe(hostPipe),
section(OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(processId)).c_str())),
sectionMap(MapViewOfFile(section, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2)), // jichi 1/16/2015: Changed to half to hook section size
sectionMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId))
{}
~ProcessRecord()
{
UnmapViewOfFile(sectionMap);
CloseHandle(section);
}
TextHook GetHook(uint64_t addr)
{
if (sectionMap == nullptr) return {};
LOCK(sectionMutex);
auto hooks = (const TextHook*)sectionMap;
for (int i = 0; i < MAX_HOOK; ++i)
if (hooks[i].hp.insertion_address == addr) return hooks[i];
return {};
}
HANDLE hostPipe;
private:
2018-08-24 12:50:20 -04:00
HANDLE section;
LPVOID sectionMap;
WinMutex sectionMutex;
2018-08-24 12:50:20 -04:00
};
2018-11-04 17:12:25 -05:00
ThreadEventCallback OnCreate, OnDestroy;
2018-08-24 12:50:20 -04:00
ProcessEventCallback OnAttach, OnDetach;
2018-08-22 21:31:15 -04:00
std::unordered_map<ThreadParam, std::shared_ptr<TextThread>> textThreadsByParams;
std::unordered_map<DWORD, std::unique_ptr<ProcessRecord>> processRecordsByIds;
std::recursive_mutex hostMutex;
2018-07-23 12:25:02 -07:00
2018-11-10 23:29:12 -05:00
DWORD DUMMY;
2018-11-01 19:51:23 -04:00
ThreadParam CONSOLE{ 0, -1ULL, -1ULL, -1ULL }, CLIPBOARD{ 0, 0, -1ULL, -1ULL };
2018-08-24 12:50:20 -04:00
void DispatchText(ThreadParam tp, const BYTE* text, int len)
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-10-30 20:50:50 -04:00
if (textThreadsByParams[tp] == nullptr)
{
2018-11-04 02:13:51 -05:00
if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(TOO_MANY_THREADS);
2018-11-01 15:03:30 -04:00
OnCreate(textThreadsByParams[tp] = std::make_shared<TextThread>(tp, Host::GetHookParam(tp), Host::GetHookName(tp)));
2018-10-30 20:50:50 -04:00
}
2018-11-03 21:41:38 -04:00
textThreadsByParams[tp]->Push(text, len);
2018-08-24 12:50:20 -04:00
}
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);
for (auto it = textThreadsByParams.begin(); it != textThreadsByParams.end();)
if (auto curr = it++; removeIf(curr->first))
2018-08-24 12:50:20 -04:00
{
2018-11-04 17:12:25 -05:00
OnDestroy(curr->second);
textThreadsByParams.erase(curr->first);
2018-08-24 12:50:20 -04:00
}
}
2018-07-23 12:25:02 -07:00
void RegisterProcess(DWORD processId, HANDLE hostPipe)
2018-08-24 12:50:20 -04:00
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
processRecordsByIds.insert({ processId, std::make_unique<ProcessRecord>(processId, hostPipe) });
OnAttach(processId);
2018-08-24 12:50:20 -04:00
}
void UnregisterProcess(DWORD processId)
2018-08-24 12:50:20 -04:00
{
OnDetach(processId);
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
processRecordsByIds.erase(processId);
2018-11-04 20:48:46 -05:00
RemoveThreads([&](ThreadParam tp) { return tp.processId == processId; });
2018-08-24 12:50:20 -04:00
}
2018-11-04 01:34:49 -05:00
void CreatePipe()
2018-08-24 12:50:20 -04:00
{
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 };
2018-10-31 20:09:29 -04:00
HANDLE hookPipe = CreateNamedPipeW(HOOK_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 0, PIPE_BUFFER_SIZE, MAXDWORD, &pipeSA);
HANDLE hostPipe = CreateNamedPipeW(HOST_PIPE, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, 0, 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-11-04 01:34:49 -05:00
CreatePipe();
2018-09-09 22:37:48 -04:00
2018-08-24 12:50:20 -04:00
while (ReadFile(hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr))
2018-11-10 23:29:12 -05:00
switch (*(HostNotificationType*)buffer)
2018-08-24 12:50:20 -04:00
{
case HOST_NOTIFICATION_RMVHOOK:
{
auto info = *(HookRemovedNotif*)buffer;
2018-11-04 20:48:46 -05:00
RemoveThreads([&](ThreadParam tp) { return tp.processId == processId && tp.addr == info.address; });
2018-08-24 12:50:20 -04:00
}
break;
case HOST_NOTIFICATION_TEXT:
{
auto info = *(ConsoleOutputNotif*)buffer;
2018-11-01 21:59:13 -04:00
Host::AddConsoleOutput(StringToWideString(info.message));
2018-08-24 12:50:20 -04:00
}
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-11-04 01:34:49 -05:00
UnregisterProcess(processId);
2018-08-24 12:50:20 -04:00
DisconnectNamedPipe(hookPipe);
DisconnectNamedPipe(hostPipe);
CloseHandle(hookPipe);
CloseHandle(hostPipe);
}).detach();
}
2018-11-01 19:51:23 -04:00
void StartCapturingClipboard()
{
std::thread([]
{
while (true)
{
Sleep(50);
if (IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL))
{
if (HANDLE clipboardHandle = GetClipboardData(CF_UNICODETEXT))
{
if (wchar_t* clipboardData = (wchar_t*)GlobalLock(clipboardHandle))
{
2018-11-04 01:34:49 -05:00
static std::wstring last;
if (last != clipboardData) Host::GetThread(CLIPBOARD)->AddSentence(last = clipboardData);
2018-11-01 19:51:23 -04:00
GlobalUnlock(clipboardHandle);
}
}
CloseClipboard();
}
}
}).detach();
}
2018-08-24 12:50:20 -04:00
}
2018-07-23 12:25:02 -07:00
namespace Host
{
2018-11-04 17:12:25 -05:00
void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onDestroy, TextThread::OutputCallback output)
{
2018-11-04 17:12:25 -05:00
OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnDestroy = onDestroy; TextThread::Output = output;
2018-11-04 20:48:46 -05:00
RegisterProcess(CONSOLE.processId, INVALID_HANDLE_VALUE);
2018-11-01 15:03:30 -04:00
OnCreate(textThreadsByParams[CONSOLE] = std::make_shared<TextThread>(CONSOLE, HookParam{}, L"Console"));
2018-11-01 19:51:23 -04:00
OnCreate(textThreadsByParams[CLIPBOARD] = std::make_shared<TextThread>(CLIPBOARD, HookParam{}, L"Clipboard"));
StartCapturingClipboard();
2018-11-04 01:34:49 -05:00
CreatePipe();
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-09-29 16:05:08 -04:00
// Artikash 7/25/2018: This is only called when Textractor 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);
processRecordsByIds.clear();
textThreadsByParams.clear();
2018-08-22 18:05:45 -04:00
#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-11-04 02:13:51 -05:00
AddConsoleOutput(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)
{
2018-11-04 02:13:51 -05:00
AddConsoleOutput(ARCHITECTURE_MISMATCH);
2018-08-22 15:11:58 -04:00
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
2018-11-04 02:13:51 -05:00
AddConsoleOutput(INJECT_FAILED);
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)
{
LOCK(hostMutex);
2018-11-10 23:29:12 -05:00
HostCommandType buffer(HOST_COMMAND_DETACH);
WriteFile(processRecordsByIds.at(processId)->hostPipe, &buffer, sizeof(buffer), &DUMMY, nullptr);
2018-07-23 12:25:02 -07:00
}
void InsertHook(DWORD processId, HookParam hp, std::string name)
2018-07-23 12:25:02 -07:00
{
LOCK(hostMutex);
2018-11-10 23:29:12 -05:00
InsertHookCmd buffer(hp, name);
WriteFile(processRecordsByIds.at(processId)->hostPipe, &buffer, sizeof(buffer), &DUMMY, nullptr);
2018-07-23 12:25:02 -07:00
}
void RemoveHook(DWORD processId, uint64_t addr)
2018-07-23 12:25:02 -07:00
{
LOCK(hostMutex);
2018-11-10 23:29:12 -05:00
RemoveHookCmd buffer(addr);
WriteFile(processRecordsByIds.at(processId)->hostPipe, &buffer, sizeof(buffer), &DUMMY, nullptr);
}
HookParam GetHookParam(DWORD processId, uint64_t addr)
2018-07-23 12:25:02 -07:00
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
return processRecordsByIds.at(processId)->GetHook(addr).hp;
2018-07-23 12:25:02 -07:00
}
std::wstring GetHookName(DWORD processId, uint64_t addr)
{
2018-08-28 17:21:20 -04:00
LOCK(hostMutex);
2018-11-01 21:59:13 -04:00
return StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName);
}
std::shared_ptr<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-11-01 15:03:30 -04:00
void AddConsoleOutput(std::wstring text)
{
GetThread(CONSOLE)->AddSentence(text);
}
}
// EOF