This commit is contained in:
Akash Mozumdar 2019-01-20 09:52:35 -05:00
parent 7c3ca92db3
commit 332e5aecaf
3 changed files with 16 additions and 32 deletions

View File

@ -21,11 +21,7 @@ namespace
LONG WINAPI ExceptionLogger(EXCEPTION_POINTERS* exception) LONG WINAPI ExceptionLogger(EXCEPTION_POINTERS* exception)
{ {
thread_local static auto _ = std::invoke([] thread_local static auto _ = std::set_terminate(Terminate);
{
std::set_terminate(Terminate);
return 0;
});
MEMORY_BASIC_INFORMATION info = {}; MEMORY_BASIC_INFORMATION info = {};
VirtualQuery(exception->ExceptionRecord->ExceptionAddress, &info, sizeof(info)); VirtualQuery(exception->ExceptionRecord->ExceptionAddress, &info, sizeof(info));
@ -47,10 +43,5 @@ namespace
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }
auto _ = std::invoke([] auto _ = (AddVectoredExceptionHandler(FALSE, ExceptionLogger), SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); }));
{
AddVectoredExceptionHandler(FALSE, ExceptionLogger);
SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); });
return 0;
});
} }

View File

@ -120,16 +120,6 @@ namespace
processRecordsByIds->erase(processId); processRecordsByIds->erase(processId);
}).detach(); }).detach();
} }
void StartCapturingClipboard()
{
SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam)
{
if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE)
if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value());
return CallNextHookEx(NULL, statusCode, wParam, lParam);
}, NULL, GetCurrentThreadId());
}
} }
namespace Host namespace Host
@ -144,8 +134,14 @@ namespace Host
processRecordsByIds->try_emplace(console.processId, console.processId, INVALID_HANDLE_VALUE); processRecordsByIds->try_emplace(console.processId, console.processId, INVALID_HANDLE_VALUE);
textThreadsByParams->insert({ console, std::make_unique<TextThread>(console, HookParam{}, CONSOLE) }); textThreadsByParams->insert({ console, std::make_unique<TextThread>(console, HookParam{}, CONSOLE) });
textThreadsByParams->insert({ Host::clipboard, std::make_unique<TextThread>(Host::clipboard, HookParam{}, CLIPBOARD) }); textThreadsByParams->insert({ Host::clipboard, std::make_unique<TextThread>(Host::clipboard, HookParam{}, CLIPBOARD) });
StartCapturingClipboard();
CreatePipe(); CreatePipe();
SetWindowsHookExW(WH_GETMESSAGE, [](int statusCode, WPARAM wParam, LPARAM lParam)
{
if (statusCode == HC_ACTION && wParam == PM_REMOVE && ((MSG*)lParam)->message == WM_CLIPBOARDUPDATE)
if (auto text = Util::GetClipboardText()) Host::GetThread(Host::clipboard)->AddSentence(text.value());
return CallNextHookEx(NULL, statusCode, wParam, lParam);
}, NULL, GetCurrentThreadId());
} }
bool InjectProcess(DWORD processId, DWORD timeout) bool InjectProcess(DWORD processId, DWORD timeout)
@ -174,7 +170,7 @@ namespace Host
#endif #endif
if (LPVOID remoteData = VirtualAllocEx(process, nullptr, (location.size() + 1) * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)) if (LPVOID remoteData = VirtualAllocEx(process, nullptr, (location.size() + 1) * sizeof(wchar_t), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))
{ {
WriteProcessMemory(process, remoteData, location.c_str(), location.size() * 2 + 2, nullptr); WriteProcessMemory(process, remoteData, location.c_str(), (location.size() + 1) * sizeof(wchar_t), nullptr);
if (AutoHandle<> thread = CreateRemoteThread(process, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr)) if (AutoHandle<> thread = CreateRemoteThread(process, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr))
{ {
WaitForSingleObject(thread, timeout); WaitForSingleObject(thread, timeout);

View File

@ -5,15 +5,12 @@
namespace Util namespace Util
{ {
std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module) std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module)
{
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
{ {
std::vector<wchar_t> buffer(MAX_PATH); std::vector<wchar_t> buffer(MAX_PATH);
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data(); if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data();
return {}; return {};
} }
return {};
}
std::optional<std::wstring> GetModuleFilename(HMODULE module) std::optional<std::wstring> GetModuleFilename(HMODULE module)
{ {
@ -27,10 +24,10 @@ namespace Util
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {}; if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {};
if (!OpenClipboard(NULL)) return {}; if (!OpenClipboard(NULL)) return {};
std::optional<std::wstring> ret; std::optional<std::wstring> text;
if (AutoHandle<Functor<GlobalUnlock>> clipboard = GetClipboardData(CF_UNICODETEXT)) ret = (wchar_t*)GlobalLock(clipboard); if (AutoHandle<Functor<GlobalUnlock>> clipboard = GetClipboardData(CF_UNICODETEXT)) text = (wchar_t*)GlobalLock(clipboard);
CloseClipboard(); CloseClipboard();
return ret; return text;
} }
std::optional<std::wstring> StringToWideString(std::string text, UINT encoding) std::optional<std::wstring> StringToWideString(std::string text, UINT encoding)
@ -45,7 +42,7 @@ namespace Util
wchar_t* end = text.data() + text.size(); wchar_t* end = text.data() + text.size();
for (int len = text.size() / 3; len > 6; --len) for (int len = text.size() / 3; len > 6; --len)
if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0) if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0)
return true | RemoveRepetition(text = end - len); return RemoveRepetition(text = end - len), true;
return false; return false;
} }
} }