2016-01-06 00:01:17 +09:00
|
|
|
// pipe.cc
|
|
|
|
// 8/24/2013 jichi
|
|
|
|
// Branch IHF/pipe.cpp, rev 93
|
|
|
|
|
2018-07-19 00:46:52 -04:00
|
|
|
#include "pipe.h"
|
2018-07-17 17:01:56 -04:00
|
|
|
#include "host.h"
|
2018-07-20 16:26:27 -04:00
|
|
|
#include "../vnrhook/include/defs.h"
|
|
|
|
#include "../vnrhook/include/const.h"
|
2018-05-20 13:11:55 -04:00
|
|
|
#include <atlbase.h>
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-06-01 02:36:51 -04:00
|
|
|
void CreateNewPipe()
|
|
|
|
{
|
2018-08-21 22:43:30 -04:00
|
|
|
std::thread([]()
|
2018-05-20 13:11:55 -04:00
|
|
|
{
|
2018-08-17 13:20:45 -04:00
|
|
|
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);
|
2018-08-21 22:43:30 -04:00
|
|
|
HANDLE hostPipe = CreateNamedPipeW(ITH_COMMAND_PIPE, PIPE_ACCESS_OUTBOUND, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, PIPE_BUFFER_SIZE, PIPE_BUFFER_SIZE, MAXDWORD, NULL);
|
2018-08-17 13:20:45 -04:00
|
|
|
ConnectNamedPipe(hookPipe, nullptr);
|
2018-05-20 13:11:55 -04:00
|
|
|
|
2018-08-17 13:20:45 -04:00
|
|
|
// jichi 9/27/2013: why recursion?
|
2018-08-21 22:43:30 -04:00
|
|
|
// Artikash 5/20/2018: Easy way to create a new pipe for another process
|
2018-08-17 13:20:45 -04:00
|
|
|
CreateNewPipe();
|
2018-05-20 13:11:55 -04:00
|
|
|
|
2018-08-17 13:20:45 -04:00
|
|
|
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))
|
2018-05-20 13:11:55 -04:00
|
|
|
{
|
2018-08-17 13:20:45 -04:00
|
|
|
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;
|
|
|
|
}
|
2018-08-21 22:43:30 -04:00
|
|
|
else DispatchText(
|
|
|
|
{
|
|
|
|
processId,
|
|
|
|
*(DWORD*)buffer, // Hook address
|
|
|
|
*(DWORD*)(buffer + sizeof(DWORD)), // Return address
|
|
|
|
*(DWORD*)(buffer + sizeof(DWORD) * 2) // Split
|
|
|
|
},
|
2018-07-19 00:46:52 -04:00
|
|
|
buffer + HEADER_SIZE, // Data
|
|
|
|
bytesRead - HEADER_SIZE // Data size
|
2018-07-17 19:18:36 -04:00
|
|
|
);
|
2018-05-20 13:11:55 -04:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:20:45 -04:00
|
|
|
DisconnectNamedPipe(hookPipe);
|
|
|
|
DisconnectNamedPipe(hostPipe);
|
|
|
|
UnregisterProcess(processId);
|
|
|
|
CloseHandle(hookPipe);
|
|
|
|
CloseHandle(hostPipe);
|
2018-08-21 22:43:30 -04:00
|
|
|
}).detach();
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// EOF
|