From 2be7f72813b583553ee76bfacdf3681dfbaee366 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 17 Aug 2018 13:20:45 -0400 Subject: [PATCH] clean up host pipe code --- host/pipe.cc | 97 +++++++++++++++++++++------------------------------- host/pipe.h | 1 - 2 files changed, 39 insertions(+), 59 deletions(-) diff --git a/host/pipe.cc b/host/pipe.cc index b9ac4f3..bb065f4 100644 --- a/host/pipe.cc +++ b/host/pipe.cc @@ -8,62 +8,43 @@ #include "../vnrhook/include/const.h" #include -struct Pipes -{ - HANDLE hookPipe; - HANDLE hostPipe; -}; - void CreateNewPipe() { - CloseHandle(CreateThread(nullptr, 0, TextReceiver, new Pipes - { - CreateNamedPipeW(ITH_TEXT_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, NULL), - CreateNamedPipeW(ITH_COMMAND_PIPE, PIPE_ACCESS_OUTBOUND, 0, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, NULL) - }, - 0, nullptr)); -} - -DWORD WINAPI TextReceiver(LPVOID lpThreadParameter) -{ - Pipes* pipes = (Pipes*)lpThreadParameter; - ConnectNamedPipe(pipes->hookPipe, nullptr); - - BYTE buffer[PIPE_BUFFER_SIZE] = {}; - DWORD bytesRead, processId; - ReadFile(pipes->hookPipe, &processId, sizeof(processId), &bytesRead, nullptr); - RegisterProcess(processId, pipes->hostPipe); - - // jichi 9/27/2013: why recursion? - // Artikash 5/20/2018: To create a new pipe for another process - CreateNewPipe(); - - while (true) + CloseHandle(CreateThread(nullptr, 0, [](auto) { - if (!ReadFile(pipes->hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr)) break; + HANDLE hookPipe = CreateNamedPipeW(ITH_TEXT_PIPE, PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, NULL); + HANDLE hostPipe = CreateNamedPipeW(ITH_COMMAND_PIPE, PIPE_ACCESS_OUTBOUND, 0, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, NULL); + ConnectNamedPipe(hookPipe, nullptr); - buffer[bytesRead] = 0; - buffer[bytesRead + 1] = 0; + // jichi 9/27/2013: why recursion? + // Artikash 5/20/2018: To create a new pipe for another process + CreateNewPipe(); - if (*(DWORD*)buffer == HOST_NOTIFICATION) + BYTE buffer[PIPE_BUFFER_SIZE + 1] = {}; + DWORD bytesRead, processId; + ReadFile(hookPipe, &processId, sizeof(processId), &bytesRead, nullptr); + RegisterProcess(processId, hostPipe); + + while (ReadFile(hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr)) { - 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; - } - } - else - { - DispatchText(processId, + buffer[bytesRead] = 0; + buffer[bytesRead + 1] = 0; + + if (*(DWORD*)buffer == HOST_NOTIFICATION) + 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; + } + else DispatchText(processId, *(DWORD*)buffer, // Hook address *(DWORD*)(buffer + sizeof(DWORD)), // Return address *(DWORD*)(buffer + sizeof(DWORD) * 2), // Split @@ -71,15 +52,15 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter) bytesRead - HEADER_SIZE // Data size ); } - } - DisconnectNamedPipe(pipes->hookPipe); - DisconnectNamedPipe(pipes->hostPipe); - UnregisterProcess(processId); - CloseHandle(pipes->hookPipe); - CloseHandle(pipes->hostPipe); - delete pipes; - return 0; + DisconnectNamedPipe(hookPipe); + DisconnectNamedPipe(hostPipe); + UnregisterProcess(processId); + CloseHandle(hookPipe); + CloseHandle(hostPipe); + return (DWORD)0; + }, + nullptr, 0, nullptr)); } // EOF diff --git a/host/pipe.h b/host/pipe.h index 78f7546..961c06f 100644 --- a/host/pipe.h +++ b/host/pipe.h @@ -6,6 +6,5 @@ #include void CreateNewPipe(); -DWORD WINAPI TextReceiver(LPVOID lpThreadParam); // EOF \ No newline at end of file