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()
{
TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++);
console->Status() |= USING_UNICODE;
TextThread* console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, USING_UNICODE);
if (onCreate) onCreate(console);
CreateNewPipe();
}
@ -142,18 +141,11 @@ namespace Host
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] = {};
*(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK;
*(DWORD*)(buffer + sizeof(DWORD)) = addr;
DWORD unused;
WriteFile(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;
return WriteFile(processRecordsByIds[pid].hostPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr);
}
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;
if ((it = textThreadsByParams[tp]) == nullptr)
{
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++);
if (Host::GetHookParam(pid, hook).type & USING_UNICODE) it->Status() |= USING_UNICODE;
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, Host::GetHookParam(pid, hook).type);
if (onCreate) onCreate(it);
}
it->AddText(text, len);

View File

@ -47,12 +47,16 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
if (*(DWORD*)buffer == HOST_NOTIFICATION)
{
USES_CONVERSION;
switch (*(DWORD*)(buffer + 4)) // Artikash 7/17/2018: Notification type
switch (*(DWORD*)(buffer + sizeof(DWORD))) // 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
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:
USES_CONVERSION;
Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text
break;
}

View File

@ -13,13 +13,12 @@ extern HWND dummyWindow;
#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(),
sentenceBuffer(),
status(0),
status(status),
flushTimer(0),
threadNumber(threadNumber),
splitDelay(splitDelay),
output(nullptr),
tp(tp)
{
@ -77,7 +76,7 @@ void TextThread::AddText(const BYTE *con, int len)
{
TT_LOCK;
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)
{
KillTimer(hWnd, idEvent);

View File

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