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
|
|
|
struct Pipes
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-06-01 02:36:51 -04:00
|
|
|
HANDLE hookPipe;
|
|
|
|
HANDLE hostPipe;
|
|
|
|
};
|
2016-01-06 00:01:17 +09:00
|
|
|
|
2018-06-01 02:36:51 -04:00
|
|
|
void CreateNewPipe()
|
|
|
|
{
|
2018-07-17 17:01:56 -04:00
|
|
|
CloseHandle(CreateThread(nullptr, 0, TextReceiver, new Pipes
|
2018-06-01 02:36:51 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
},
|
2018-07-17 17:01:56 -04:00
|
|
|
0, nullptr));
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
2018-05-20 13:11:55 -04:00
|
|
|
DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
|
2016-01-06 00:01:17 +09:00
|
|
|
{
|
2018-06-01 02:36:51 -04:00
|
|
|
Pipes* pipes = (Pipes*)lpThreadParameter;
|
|
|
|
ConnectNamedPipe(pipes->hookPipe, nullptr);
|
2018-05-20 13:11:55 -04:00
|
|
|
|
2018-07-11 20:18:04 -04:00
|
|
|
BYTE buffer[PIPE_BUFFER_SIZE] = {};
|
2018-05-20 13:11:55 -04:00
|
|
|
DWORD bytesRead, processId;
|
2018-06-01 02:36:51 -04:00
|
|
|
ReadFile(pipes->hookPipe, &processId, sizeof(processId), &bytesRead, nullptr);
|
2018-07-23 12:25:02 -07:00
|
|
|
RegisterProcess(processId, pipes->hostPipe);
|
2018-05-20 13:11:55 -04:00
|
|
|
|
|
|
|
// jichi 9/27/2013: why recursion?
|
|
|
|
// Artikash 5/20/2018: To create a new pipe for another process
|
|
|
|
CreateNewPipe();
|
|
|
|
|
2018-07-17 17:01:56 -04:00
|
|
|
while (true)
|
2018-05-20 13:11:55 -04:00
|
|
|
{
|
2018-07-17 17:01:56 -04:00
|
|
|
if (!ReadFile(pipes->hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr)) break;
|
2018-05-20 13:11:55 -04:00
|
|
|
|
|
|
|
buffer[bytesRead] = 0;
|
|
|
|
buffer[bytesRead + 1] = 0;
|
|
|
|
|
2018-07-17 17:01:56 -04:00
|
|
|
if (*(DWORD*)buffer == HOST_NOTIFICATION)
|
2018-05-20 13:11:55 -04:00
|
|
|
{
|
2018-07-17 19:18:36 -04:00
|
|
|
USES_CONVERSION;
|
2018-07-17 17:01:56 -04:00
|
|
|
switch (*(DWORD*)(buffer + 4)) // Artikash 7/17/2018: Notification type
|
2018-05-20 13:11:55 -04:00
|
|
|
{
|
2018-07-18 23:40:44 -04:00
|
|
|
case HOST_NOTIFICATION_NEWHOOK: // Artikash 7/18/2018: Useless for now, but could be used to implement smth later
|
2018-05-31 04:44:33 -04:00
|
|
|
break;
|
2018-05-20 13:11:55 -04:00
|
|
|
case HOST_NOTIFICATION_TEXT:
|
2018-07-23 12:25:02 -07:00
|
|
|
Host::AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text
|
2018-05-20 13:11:55 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-23 12:25:02 -07:00
|
|
|
DispatchText(processId,
|
2018-07-17 19:18:36 -04:00
|
|
|
*(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-06-01 02:36:51 -04:00
|
|
|
DisconnectNamedPipe(pipes->hookPipe);
|
|
|
|
DisconnectNamedPipe(pipes->hostPipe);
|
2018-07-23 12:25:02 -07:00
|
|
|
UnregisterProcess(processId);
|
2018-07-17 17:01:56 -04:00
|
|
|
CloseHandle(pipes->hookPipe);
|
|
|
|
CloseHandle(pipes->hostPipe);
|
2018-06-01 02:36:51 -04:00
|
|
|
delete pipes;
|
2018-05-20 13:11:55 -04:00
|
|
|
return 0;
|
2016-01-06 00:01:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// EOF
|