From 758e729f304385a5df96b9027193feea61951cff Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 12 Jul 2018 13:59:05 -0400 Subject: [PATCH] clean up a lot of code --- gui/ProfileManager.cpp | 2 +- gui/command.cpp | 2 +- vnr/texthook/host/hookman.cc | 83 +------------------------------- vnr/texthook/host/hookman.h | 2 +- vnr/texthook/host/host.cc | 78 +++++++++--------------------- vnr/texthook/host/host.h | 3 +- vnr/texthook/host/host_p.h | 2 +- vnr/texthook/host/textthread.h | 12 +---- vnr/texthook/host/textthread_p.h | 2 - vnr/vnrhook/src/main.cc | 2 +- vnr/vnrhook/src/pipe.cc | 2 +- 11 files changed, 32 insertions(+), 158 deletions(-) diff --git a/gui/ProfileManager.cpp b/gui/ProfileManager.cpp index 01a7742..ccefbef 100644 --- a/gui/ProfileManager.cpp +++ b/gui/ProfileManager.cpp @@ -170,7 +170,7 @@ DWORD WINAPI InjectThread(LPVOID lpThreadParameter) for (auto hp = pf->Hooks().begin(); hp != pf->Hooks().end(); ++hp) { std::string name = toMultiByteString((*hp)->Name()); - Host_InsertHook(pid, const_cast(&(*hp)->HP()), name.c_str()); + InsertHook(pid, const_cast(&(*hp)->HP()), name); } } return status; diff --git a/gui/command.cpp b/gui/command.cpp index 4c275ab..cdf077a 100644 --- a/gui/command.cpp +++ b/gui/command.cpp @@ -41,7 +41,7 @@ DWORD ProcessCommand(const std::wstring& cmd, DWORD pid) { HookParam hp = {}; if (Parse(m[1].str(), hp)) - Host_InsertHook(pid, &hp); + InsertHook(pid, &hp); } else if (regex_match(cmd, m, wregex(L":(?:h|help)", wregex::icase))) { diff --git a/vnr/texthook/host/hookman.cc b/vnr/texthook/host/hookman.cc index 79daabc..4ff1397 100644 --- a/vnr/texthook/host/hookman.cc +++ b/vnr/texthook/host/hookman.cc @@ -200,13 +200,6 @@ void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe) 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->process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); record->hookman_mutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str()); - //if (NT_SUCCESS(NtOpenProcess(&hProc, - // PROCESS_QUERY_INFORMATION| - // PROCESS_CREATE_THREAD| - // PROCESS_VM_READ| - // PROCESS_VM_WRITE| - // PROCESS_VM_OPERATION, - // &oa,&id))) if (attach) attach(pid); @@ -216,8 +209,6 @@ void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe) void HookManager::UnRegisterProcess(DWORD pid) { HM_LOCK; - //ConsoleOutput("vnrhost:UnRegisterProcess: lock"); - //EnterCriticalSection(&hmcs); ProcessRecord pr = *processRecordsByIds[pid]; CloseHandle(pr.hookman_mutex); @@ -225,76 +216,19 @@ void HookManager::UnRegisterProcess(DWORD pid) CloseHandle(pr.process_handle); CloseHandle(pr.hookman_section); processRecordsByIds.erase(pid); - //CloseHandle(text_pipes[i]); - //CloseHandle(cmd_pipes[i]); - //CloseHandle(recv_threads[i]); - //CloseHandle(record[i].hookman_mutex); - - ////if (::ith_has_section) - //NtUnmapViewOfSection(NtCurrentProcess(), record[i].hookman_map); - ////else - //// delete[] record[i].hookman_map; - - //CloseHandle(record[i].process_handle); - //CloseHandle(record[i].hookman_section); - - //for (; i < MAX_REGISTER; i++) { - // record[i] = record[i+1]; - // text_pipes[i] = text_pipes[i+1]; - // cmd_pipes[i] = cmd_pipes[i+1]; - // recv_threads[i] = recv_threads[i+1]; - // if (text_pipes[i] == 0) - // break; - //} - //register_count--; - //if (current_pid == pid) - // current_pid = register_count ? record[0].pid_register : 0; RemoveProcessContext(pid); - //pid_map->Clear(pid>>2); - - //if (register_count == 1) - // NtSetEvent(destroy_event, 0); - //LeaveCriticalSection(&hmcs); - //ConsoleOutput("vnrhost:UnRegisterProcess: unlock"); if (detach) detach(pid); } -// jichi 9/28/2013: I do not need this -//void HookManager::SetName(DWORD type) -//{ -// WCHAR c; -// if (type & PRINT_DWORD) -// c = L'H'; -// else if (type & USING_UNICODE) { -// if (type & STRING_LAST_CHAR) -// c = L'L'; -// else if (type & USING_STRING) -// c = L'Q'; -// else -// c = L'W'; -// } else { -// if (type & USING_STRING) -// c = L'S'; -// else if (type & BIG_ENDIAN) -// c = L'A'; -// else -// c = L'B'; -// } -// //swprintf(user_entry,L"UserHook%c",c); -//} - void HookManager::DispatchText(DWORD pid, const BYTE *text, DWORD hook, DWORD retn, DWORD spl, int len) { // jichi 20/27/2013: When PID is zero, the text comes from console, which I don't need if (!text || !pid || len <= 0) return; HM_LOCK; - //bool flag=false; ThreadParameter tp = {pid, hook, retn, spl}; - //ConsoleOutput("vnrhost:DispatchText: lock"); - //EnterCriticalSection(&hmcs); TextThread *it; if (!(it = threadTable[tp])) { @@ -313,45 +247,30 @@ void HookManager::AddConsoleOutput(LPCWSTR text) { int len = wcslen(text) * 2; TextThread *console = threadTable[{0, -1UL, -1UL, -1UL}]; - //EnterCriticalSection(&hmcs); console->AddSentence(std::wstring(text)); - //LeaveCriticalSection(&hmcs); } } void HookManager::ClearCurrent() { HM_LOCK; - //ConsoleOutput("vnrhost:ClearCurrent: lock"); - //EnterCriticalSection(&hmcs); if (current) { current->Reset(); if (reset) reset(current); } - //current->ResetEditText(); - //LeaveCriticalSection(&hmcs); - //ConsoleOutput("vnrhost:ClearCurrent: unlock"); } ProcessRecord *HookManager::GetProcessRecord(DWORD pid) { HM_LOCK; - //EnterCriticalSection(&hmcs); return processRecordsByIds[pid]; - //ProcessRecord *pr = i < MAX_REGISTER ? record + i : nullptr; - //LeaveCriticalSection(&hmcs); - //return pr; } -HANDLE HookManager::GetHostPipeByPID(DWORD pid) +HANDLE HookManager::GetCommandPipe(DWORD pid) { HM_LOCK; - //EnterCriticalSection(&hmcs); return processRecordsByIds[pid] ? processRecordsByIds[pid]->hostPipe : nullptr; - //HANDLE h = i < MAX_REGISTER ? cmd_pipes[i] : 0; - //LeaveCriticalSection(&hmcs); - //return h; } MK_BASIC_TYPE(DWORD) diff --git a/vnr/texthook/host/hookman.h b/vnr/texthook/host/hookman.h index 82972da..93defbb 100644 --- a/vnr/texthook/host/hookman.h +++ b/vnr/texthook/host/hookman.h @@ -62,7 +62,7 @@ public: void UnRegisterProcess(DWORD pid); //void SetName(DWORD); - HANDLE GetHostPipeByPID(DWORD pid); + HANDLE GetCommandPipe(DWORD pid); ThreadEventCallback RegisterThreadCreateCallback(ThreadEventCallback cf) { return (ThreadEventCallback)_InterlockedExchange((long*)&create,(long)cf); } diff --git a/vnr/texthook/host/host.cc b/vnr/texthook/host/host.cc index d263b5f..04349ae 100644 --- a/vnr/texthook/host/host.cc +++ b/vnr/texthook/host/host.cc @@ -82,15 +82,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused) return true; } -enum { IHS_SIZE = 0x80 }; -enum { IHS_BUFF_SIZE = IHS_SIZE - sizeof(HookParam) }; - -struct InsertHookStruct -{ - SendParam sp; - BYTE name_buffer[IHS_SIZE]; -}; - IHFSERVICE bool IHFAPI OpenHost() { bool success; @@ -208,9 +199,8 @@ IHFSERVICE bool IHFAPI InjectProcessById(DWORD processId, DWORD timeout) IHFSERVICE bool IHFAPI DetachProcessById(DWORD processId) { - ITH_SYNC_HOOK; DWORD command = HOST_COMMAND_DETACH; - return WriteFile(man->GetHostPipeByPID(processId), &command, sizeof(command), nullptr, nullptr); + return WriteFile(man->GetCommandPipe(processId), &command, sizeof(command), nullptr, nullptr); } IHFSERVICE void IHFAPI GetHostHookManager(HookManager** hookman) @@ -229,60 +219,36 @@ IHFSERVICE void IHFAPI GetHostSettings(Settings **p) } } -// Artikash 5/11/2018: I don't understand the following operations, so I'm making minimal changes in cleanup - -IHFSERVICE DWORD IHFAPI Host_InsertHook(DWORD pid, HookParam *hp, LPCSTR name) +IHFSERVICE DWORD IHFAPI InsertHook(DWORD pid, HookParam *hp, std::string name) { - ITH_SYNC_HOOK; - - HANDLE hCmd = man->GetHostPipeByPID(pid); - if (hCmd == 0) + HANDLE commandPipe = man->GetCommandPipe(pid); + if (commandPipe == nullptr) return -1; - InsertHookStruct s; - s.sp.type = HOST_COMMAND_NEW_HOOK; - s.sp.hp = *hp; - size_t len; - if (name) - len = ::strlen(name); - else - len = 0; - if (len) { - if (len >= IHS_BUFF_SIZE) len = IHS_BUFF_SIZE - 1; - memcpy(s.name_buffer, name, len); - } - s.name_buffer[len] = 0; - IO_STATUS_BLOCK ios; - DWORD unused; - WriteFile(hCmd, &s, IHS_SIZE, &unused, nullptr); + BYTE buffer[PIPE_BUFFER_SIZE] = {}; + *(DWORD*)buffer = HOST_COMMAND_NEW_HOOK; + memcpy(buffer + 4, hp, sizeof(HookParam)); + if (name.size()) strcpy((char*)buffer + 4 + sizeof(HookParam), name.c_str()); - //memcpy(&sp.hp,hp,sizeof(HookParam)); - //cmdq->AddRequest(sp, pid); + WriteFile(commandPipe, buffer, 4 + sizeof(HookParam) + name.size(), nullptr, nullptr); return 0; } IHFSERVICE DWORD IHFAPI Host_RemoveHook(DWORD pid, DWORD addr) { - ITH_SYNC_HOOK; - - HANDLE hRemoved,hCmd; - hCmd = man->GetHostPipeByPID(pid); - if (hCmd == 0) - return -1; - hRemoved = CreateEventW(nullptr, TRUE, FALSE, ITH_REMOVEHOOK_EVENT); - SendParam sp = {}; - IO_STATUS_BLOCK ios; - sp.type = HOST_COMMAND_REMOVE_HOOK; - sp.hp.address = addr; - //cmdq -> AddRequest(sp, pid); - DWORD unused; - WriteFile(hCmd, &sp, sizeof(sp), &unused, nullptr); - // jichi 10/22/2013: Timeout might crash vnrsrv - //const LONGLONG timeout = HOOK_TIMEOUT; - //NtWaitForSingleObject(hRemoved, 0, (PLARGE_INTEGER)&timeout); - WaitForSingleObject(hRemoved, MAXDWORD); - CloseHandle(hRemoved); - man -> RemoveSingleHook(pid, sp.hp.address); + HANDLE commandPipe = man->GetCommandPipe(pid); + if (commandPipe == nullptr) + return -1; + + HANDLE hookRemovalEvent = CreateEventW(nullptr, TRUE, FALSE, ITH_REMOVEHOOK_EVENT); + BYTE buffer[8]; + *(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK; + *(DWORD*)(buffer + 4) = addr; + + WriteFile(commandPipe, buffer, 8, nullptr, nullptr); + WaitForSingleObject(hookRemovalEvent, 1000); + CloseHandle(hookRemovalEvent); + man->RemoveSingleHook(pid, addr); return 0; } diff --git a/vnr/texthook/host/host.h b/vnr/texthook/host/host.h index 8ede41c..ea27c05 100644 --- a/vnr/texthook/host/host.h +++ b/vnr/texthook/host/host.h @@ -7,6 +7,7 @@ //#include "host/settings.h" #include "config.h" #include "host/hookman.h" +#include struct Settings; struct HookParam; @@ -23,7 +24,7 @@ IHFSERVICE DWORD IHFAPI Host_GetPIDByName(LPCWSTR pwcTarget); IHFSERVICE bool IHFAPI InjectProcessById(DWORD pid, DWORD timeout = 5000); IHFSERVICE bool IHFAPI DetachProcessById(DWORD pid); IHFSERVICE bool IHFAPI Host_HijackProcess(DWORD pid); -IHFSERVICE DWORD IHFAPI Host_InsertHook(DWORD pid, HookParam *hp, LPCSTR name = nullptr); +IHFSERVICE DWORD IHFAPI InsertHook(DWORD pid, HookParam *hp, std::string name = ""); IHFSERVICE DWORD IHFAPI Host_ModifyHook(DWORD pid, HookParam *hp); IHFSERVICE DWORD IHFAPI Host_RemoveHook(DWORD pid, DWORD addr); IHFSERVICE DWORD IHFAPI Host_AddLink(DWORD from, DWORD to); diff --git a/vnr/texthook/host/host_p.h b/vnr/texthook/host/host_p.h index 896497c..1dbdf20 100644 --- a/vnr/texthook/host/host_p.h +++ b/vnr/texthook/host/host_p.h @@ -36,7 +36,7 @@ DWORD WINAPI CmdThread(LPVOID lpThreadParameter); DWORD GetCurrentPID(); //DWORD GetProcessIDByPath(LPWSTR str); -HANDLE GetHostPipeByPID(DWORD pid); +HANDLE GetCommandPipe(DWORD pid); //DWORD Inject(HANDLE hProc); //DWORD InjectByPID(DWORD pid); //DWORD PIDByName(LPWSTR target); diff --git a/vnr/texthook/host/textthread.h b/vnr/texthook/host/textthread.h index 4d4241f..423d6d4 100644 --- a/vnr/texthook/host/textthread.h +++ b/vnr/texthook/host/textthread.h @@ -9,14 +9,6 @@ #include #include -struct RepeatCountNode { - short repeat; - short count; - RepeatCountNode *next; - - //RepeatCountNode() : repeat(0), count(0), next(nullptr) {} -}; - struct ThreadParameter { DWORD pid; // jichi: 5/11/2014: The process ID DWORD hook; // Artikash 6/6/2018: The start address of the hook @@ -40,8 +32,6 @@ struct ThreadParameter { #define REPEAT_NEWLINE 0x40000 class TextThread; -typedef void (* ConsoleCallback)(LPCSTR text); -typedef void (* ConsoleWCallback)(LPCWSTR text); typedef DWORD (* ThreadOutputFilterCallback)(TextThread *,const BYTE *, DWORD, DWORD); typedef DWORD (* ThreadEventCallback)(TextThread *); @@ -76,7 +66,7 @@ private: ThreadParameter tp; std::vector sentenceBuffer; - WORD thread_number; + unsigned int thread_number; ThreadOutputFilterCallback output; DWORD status; }; diff --git a/vnr/texthook/host/textthread_p.h b/vnr/texthook/host/textthread_p.h index a719c4c..bdfc21c 100644 --- a/vnr/texthook/host/textthread_p.h +++ b/vnr/texthook/host/textthread_p.h @@ -18,8 +18,6 @@ template class MyVector { public: - int Used() const { return used; } - T *Storage() const { return storage; } void LockVector() { EnterCriticalSection(&cs_store); } void UnlockVector() { LeaveCriticalSection(&cs_store); } MyVector() : size(default_size), used(0) diff --git a/vnr/vnrhook/src/main.cc b/vnr/vnrhook/src/main.cc index 1e1b98c..8029060 100644 --- a/vnr/vnrhook/src/main.cc +++ b/vnr/vnrhook/src/main.cc @@ -164,7 +164,7 @@ DWORD NewHook(const HookParam &hp, LPCSTR name, DWORD flag) } ConsoleOutput("vnrcli:NewHook: try inserting hook:"); - ConsoleOutput(name); + ConsoleOutput(str); // jichi 7/13/2014: This function would raise when too many hooks added ::hookman[current].InitHook(hp, str, flag & 0xffff); diff --git a/vnr/vnrhook/src/pipe.cc b/vnr/vnrhook/src/pipe.cc index 232c379..06d117b 100644 --- a/vnr/vnrhook/src/pipe.cc +++ b/vnr/vnrhook/src/pipe.cc @@ -25,7 +25,7 @@ DWORD WINAPI PipeManager(LPVOID unused) while (::running) { DWORD count; - BYTE* buffer = new BYTE[PIPE_BUFFER_SIZE]; + BYTE buffer[PIPE_BUFFER_SIZE]; HANDLE hostPipe = ::hookPipe = INVALID_HANDLE_VALUE, pipeAcquisitionMutex = CreateMutexW(nullptr, TRUE, ITH_GRANTPIPE_MUTEX);