2016-01-05 23:01:17 +08:00
|
|
|
// pipe.cc
|
|
|
|
// 8/24/2013 jichi
|
|
|
|
// Branch IHF/pipe.cpp, rev 93
|
|
|
|
// 8/24/2013 TODO: Clean up this file
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
#include "host.h"
|
2016-01-05 23:01:17 +08:00
|
|
|
#include "hookman.h"
|
|
|
|
#include "vnrhook/include/defs.h"
|
|
|
|
#include "vnrhook/include/const.h"
|
|
|
|
#include <stdio.h>
|
2018-05-21 01:11:55 +08:00
|
|
|
#include "growl.h"
|
|
|
|
#include <atlbase.h>
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
extern HookManager* man;
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-06-01 14:36:51 +08:00
|
|
|
struct Pipes
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-06-01 14:36:51 +08:00
|
|
|
HANDLE hookPipe;
|
|
|
|
HANDLE hostPipe;
|
|
|
|
};
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
DWORD WINAPI TextReceiver(LPVOID lpThreadParameter);
|
|
|
|
|
2018-06-01 14:36:51 +08:00
|
|
|
void CreateNewPipe()
|
|
|
|
{
|
2018-07-18 05:01:56 +08:00
|
|
|
CloseHandle(CreateThread(nullptr, 0, TextReceiver, new Pipes
|
2018-06-01 14:36:51 +08: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-18 05:01:56 +08:00
|
|
|
0, nullptr));
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-05-21 01:11:55 +08:00
|
|
|
DWORD WINAPI TextReceiver(LPVOID lpThreadParameter)
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-06-01 14:36:51 +08:00
|
|
|
Pipes* pipes = (Pipes*)lpThreadParameter;
|
|
|
|
ConnectNamedPipe(pipes->hookPipe, nullptr);
|
2018-05-21 01:11:55 +08:00
|
|
|
|
2018-07-12 08:18:04 +08:00
|
|
|
BYTE buffer[PIPE_BUFFER_SIZE] = {};
|
2018-05-21 01:11:55 +08:00
|
|
|
DWORD bytesRead, processId;
|
|
|
|
|
|
|
|
// Artikash 5/20/2018: Shouldn't Windows automatically close the handles when the host process stops running?
|
|
|
|
//if (!::running) {
|
|
|
|
// NtClose(hookPipe);
|
|
|
|
// return 0;
|
|
|
|
//}
|
|
|
|
|
2018-06-01 14:36:51 +08:00
|
|
|
ReadFile(pipes->hookPipe, &processId, sizeof(processId), &bytesRead, nullptr);
|
|
|
|
man->RegisterProcess(processId, pipes->hostPipe);
|
2018-05-21 01:11:55 +08:00
|
|
|
|
|
|
|
// jichi 9/27/2013: why recursion?
|
|
|
|
// Artikash 5/20/2018: To create a new pipe for another process
|
|
|
|
CreateNewPipe();
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
while (true)
|
2018-05-21 01:11:55 +08:00
|
|
|
{
|
2018-07-18 05:01:56 +08:00
|
|
|
if (!ReadFile(pipes->hookPipe, buffer, PIPE_BUFFER_SIZE, &bytesRead, nullptr)) break;
|
2018-05-21 01:11:55 +08:00
|
|
|
|
|
|
|
buffer[bytesRead] = 0;
|
|
|
|
buffer[bytesRead + 1] = 0;
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
if (*(DWORD*)buffer == HOST_NOTIFICATION)
|
2018-05-21 01:11:55 +08:00
|
|
|
{
|
2018-07-18 07:18:36 +08:00
|
|
|
USES_CONVERSION;
|
2018-07-18 05:01:56 +08:00
|
|
|
switch (*(DWORD*)(buffer + 4)) // Artikash 7/17/2018: Notification type
|
2018-05-21 01:11:55 +08:00
|
|
|
{
|
2018-07-18 07:18:36 +08:00
|
|
|
case HOST_NOTIFICATION_NEWHOOK:
|
|
|
|
man->SetHook(processId,
|
|
|
|
((HookParam*)(buffer + sizeof(DWORD) * 2))->address,
|
|
|
|
{
|
|
|
|
*(HookParam*)(buffer + sizeof(DWORD) * 2), // Hook address
|
|
|
|
std::wstring(A2W(
|
|
|
|
(const char*)buffer + sizeof(DWORD) * 2 + sizeof(HookParam) // Hook name
|
|
|
|
))
|
|
|
|
}
|
|
|
|
);
|
2018-05-31 16:44:33 +08:00
|
|
|
break;
|
2018-05-21 01:11:55 +08:00
|
|
|
case HOST_NOTIFICATION_TEXT:
|
2018-07-18 07:18:36 +08:00
|
|
|
man->AddConsoleOutput(A2W((LPCSTR)(buffer + sizeof(DWORD) * 2))); // Text
|
2018-05-21 01:11:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// jichi 9/28/2013: Debug raw data
|
|
|
|
//ITH_DEBUG_DWORD9(RecvLen - 0xc,
|
|
|
|
// buffer[0xc], buffer[0xd], buffer[0xe], buffer[0xf],
|
|
|
|
// buffer[0x10], buffer[0x11], buffer[0x12], buffer[0x13]);
|
2018-07-18 07:18:36 +08:00
|
|
|
man->DispatchText(processId, buffer + HEADER_SIZE,
|
|
|
|
*(DWORD*)buffer, // Hook address
|
|
|
|
*(DWORD*)(buffer + sizeof(DWORD)), // Return address
|
|
|
|
*(DWORD*)(buffer + sizeof(DWORD) * 2), // Split
|
|
|
|
bytesRead - HEADER_SIZE
|
|
|
|
);
|
2018-05-21 01:11:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 14:36:51 +08:00
|
|
|
DisconnectNamedPipe(pipes->hookPipe);
|
|
|
|
DisconnectNamedPipe(pipes->hostPipe);
|
2018-05-21 01:11:55 +08:00
|
|
|
man->UnRegisterProcess(processId);
|
2018-07-18 05:01:56 +08:00
|
|
|
CloseHandle(pipes->hookPipe);
|
|
|
|
CloseHandle(pipes->hostPipe);
|
2018-05-21 01:11:55 +08:00
|
|
|
|
2018-06-01 14:36:51 +08:00
|
|
|
delete pipes;
|
2018-05-21 01:11:55 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EOF
|