clean up host pipe code

This commit is contained in:
Akash Mozumdar 2018-08-17 13:20:45 -04:00
parent c431cda7d7
commit 2be7f72813
2 changed files with 39 additions and 59 deletions

View File

@ -8,45 +8,29 @@
#include "../vnrhook/include/const.h" #include "../vnrhook/include/const.h"
#include <atlbase.h> #include <atlbase.h>
struct Pipes
{
HANDLE hookPipe;
HANDLE hostPipe;
};
void CreateNewPipe() void CreateNewPipe()
{ {
CloseHandle(CreateThread(nullptr, 0, TextReceiver, new Pipes CloseHandle(CreateThread(nullptr, 0, [](auto)
{ {
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 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);
CreateNamedPipeW(ITH_COMMAND_PIPE, PIPE_ACCESS_OUTBOUND, 0, 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);
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? // jichi 9/27/2013: why recursion?
// Artikash 5/20/2018: To create a new pipe for another process // Artikash 5/20/2018: To create a new pipe for another process
CreateNewPipe(); CreateNewPipe();
while (true) BYTE buffer[PIPE_BUFFER_SIZE + 1] = {};
{ DWORD bytesRead, processId;
if (!ReadFile(pipes->hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr)) break; ReadFile(hookPipe, &processId, sizeof(processId), &bytesRead, nullptr);
RegisterProcess(processId, hostPipe);
while (ReadFile(hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr))
{
buffer[bytesRead] = 0; buffer[bytesRead] = 0;
buffer[bytesRead + 1] = 0; buffer[bytesRead + 1] = 0;
if (*(DWORD*)buffer == HOST_NOTIFICATION) if (*(DWORD*)buffer == HOST_NOTIFICATION)
{
switch (*(DWORD*)(buffer + sizeof(DWORD))) // 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 case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later
@ -60,10 +44,7 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text
break; break;
} }
} else DispatchText(processId,
else
{
DispatchText(processId,
*(DWORD*)buffer, // Hook address *(DWORD*)buffer, // Hook address
*(DWORD*)(buffer + sizeof(DWORD)), // Return address *(DWORD*)(buffer + sizeof(DWORD)), // Return address
*(DWORD*)(buffer + sizeof(DWORD) * 2), // Split *(DWORD*)(buffer + sizeof(DWORD) * 2), // Split
@ -71,15 +52,15 @@ DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
bytesRead - HEADER_SIZE // Data size bytesRead - HEADER_SIZE // Data size
); );
} }
}
DisconnectNamedPipe(pipes->hookPipe); DisconnectNamedPipe(hookPipe);
DisconnectNamedPipe(pipes->hostPipe); DisconnectNamedPipe(hostPipe);
UnregisterProcess(processId); UnregisterProcess(processId);
CloseHandle(pipes->hookPipe); CloseHandle(hookPipe);
CloseHandle(pipes->hostPipe); CloseHandle(hostPipe);
delete pipes; return (DWORD)0;
return 0; },
nullptr, 0, nullptr));
} }
// EOF // EOF

View File

@ -6,6 +6,5 @@
#include <Windows.h> #include <Windows.h>
void CreateNewPipe(); void CreateNewPipe();
DWORD WINAPI TextReceiver(LPVOID lpThreadParam);
// EOF // EOF