receive hook removal via pipe

This commit is contained in:
Akash Mozumdar 2018-08-02 17:17:54 -04:00
parent 11d75c2987
commit 24407f8979
4 changed files with 13 additions and 22 deletions

View File

@ -73,8 +73,7 @@ namespace Host
DLLEXPORT void Open() DLLEXPORT void Open()
{ {
TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++); TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, USING_UNICODE);
console->Status() |= USING_UNICODE;
if (onCreate) onCreate(console); if (onCreate) onCreate(console);
CreateNewPipe(); CreateNewPipe();
} }
@ -142,18 +141,11 @@ namespace Host
DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr) DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr)
{ {
HANDLE hostPipe = processRecordsByIds[pid].hostPipe;
if (hostPipe == nullptr) return false;
HANDLE hookRemovalEvent = CreateEventW(nullptr, TRUE, FALSE, ITH_REMOVEHOOK_EVENT);
BYTE buffer[sizeof(DWORD) * 2] = {}; BYTE buffer[sizeof(DWORD) * 2] = {};
*(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK; *(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK;
*(DWORD*)(buffer + sizeof(DWORD)) = addr; *(DWORD*)(buffer + sizeof(DWORD)) = addr;
DWORD unused; DWORD unused;
WriteFile(hostPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr); return WriteFile(processRecordsByIds[pid].hostPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr);
WaitForSingleObject(hookRemovalEvent, 1000);
CloseHandle(hookRemovalEvent);
RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; }, { pid, addr, 0, 0 });
return true;
} }
DLLEXPORT HookParam GetHookParam(DWORD pid, DWORD addr) DLLEXPORT HookParam GetHookParam(DWORD pid, DWORD addr)
@ -219,8 +211,7 @@ void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE * t
TextThread *it; TextThread *it;
if ((it = textThreadsByParams[tp]) == nullptr) if ((it = textThreadsByParams[tp]) == nullptr)
{ {
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++); it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, Host::GetHookParam(pid, hook).type);
if (Host::GetHookParam(pid, hook).type & USING_UNICODE) it->Status() |= USING_UNICODE;
if (onCreate) onCreate(it); if (onCreate) onCreate(it);
} }
it->AddText(text, len); it->AddText(text, len);

View File

@ -47,12 +47,16 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
if (*(DWORD*)buffer == HOST_NOTIFICATION) if (*(DWORD*)buffer == HOST_NOTIFICATION)
{ {
USES_CONVERSION; switch (*(DWORD*)(buffer + sizeof(DWORD))) // Artikash 7/17/2018: Notification type
switch (*(DWORD*)(buffer + 4)) // Artikash 7/17/2018: Notification type
{ {
case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later
break; break;
case HOST_NOTIFICATION_RMVHOOK:
RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; },
{ processId, *(DWORD*)(buffer + sizeof(DWORD) * 2) }); // Address
break;
case HOST_NOTIFICATION_TEXT: case HOST_NOTIFICATION_TEXT:
USES_CONVERSION;
Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text
break; break;
} }

View File

@ -13,13 +13,12 @@ extern HWND dummyWindow;
#define TT_LOCK CriticalSectionLocker ttLocker(ttCs) // Synchronized scope for accessing private data #define TT_LOCK CriticalSectionLocker ttLocker(ttCs) // Synchronized scope for accessing private data
TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay) : TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status) :
storage(), storage(),
sentenceBuffer(), sentenceBuffer(),
status(0), status(status),
flushTimer(0), flushTimer(0),
threadNumber(threadNumber), threadNumber(threadNumber),
splitDelay(splitDelay),
output(nullptr), output(nullptr),
tp(tp) tp(tp)
{ {
@ -77,7 +76,7 @@ void TextThread::AddText(const BYTE *con, int len)
{ {
TT_LOCK; TT_LOCK;
sentenceBuffer.insert(sentenceBuffer.end(), con, con + len); sentenceBuffer.insert(sentenceBuffer.end(), con, con + len);
flushTimer = SetTimer(dummyWindow, (UINT_PTR)this, splitDelay, flushTimer = SetTimer(dummyWindow, (UINT_PTR)this, 250, // TODO: Let user change delay before sentenceBuffer is flushed
[](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) [](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{ {
KillTimer(hWnd, idEvent); KillTimer(hWnd, idEvent);

View File

@ -31,14 +31,12 @@ typedef std::function<std::wstring(TextThread*, std::wstring)> ThreadOutputCallb
class TextThread class TextThread
{ {
public: public:
TextThread(ThreadParameter tp, unsigned int threadNumber, unsigned int splitDelay = 250); TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status);
~TextThread(); ~TextThread();
virtual std::wstring GetStore(); virtual std::wstring GetStore();
DWORD &Status() { return status; }
WORD Number() const { return threadNumber; } WORD Number() const { return threadNumber; }
ThreadParameter GetThreadParameter() { return tp; } ThreadParameter GetThreadParameter() { return tp; }
void SetSplitDelay(unsigned int splitDelay) { this->splitDelay = splitDelay; }
void RegisterOutputCallBack(ThreadOutputCallback cb) { output = cb; } void RegisterOutputCallBack(ThreadOutputCallback cb) { output = cb; }
@ -55,7 +53,6 @@ private:
ThreadParameter tp; ThreadParameter tp;
unsigned int threadNumber; unsigned int threadNumber;
unsigned int splitDelay;
DWORD status; DWORD status;
unsigned int flushTimer; unsigned int flushTimer;
}; };