2016-01-05 23:01:17 +08:00
|
|
|
// host.cc
|
|
|
|
// 8/24/2013 jichi
|
|
|
|
// Branch IHF/main.cpp, rev 111
|
|
|
|
|
|
|
|
#include "host.h"
|
2018-07-19 12:46:52 +08:00
|
|
|
#include "pipe.h"
|
2018-07-21 04:26:27 +08:00
|
|
|
#include "../vnrhook/include/const.h"
|
|
|
|
#include "../vnrhook/include/defs.h"
|
|
|
|
#include "../vnrhook/include/types.h"
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
HANDLE preventDuplicationMutex;
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
HookManager* man;
|
2018-05-12 04:46:05 +08:00
|
|
|
HWND dummyWindow;
|
2018-07-18 05:01:56 +08:00
|
|
|
bool running;
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-19 12:09:58 +08:00
|
|
|
namespace
|
2018-05-12 04:46:05 +08:00
|
|
|
{ // unnamed
|
2018-07-19 12:09:58 +08:00
|
|
|
void GetDebugPrivileges() // Artikash 5/19/2018: Is it just me or is this function 100% superfluous?
|
|
|
|
{
|
2018-05-12 04:46:05 +08:00
|
|
|
HANDLE processToken;
|
2018-07-19 11:40:44 +08:00
|
|
|
TOKEN_PRIVILEGES Privileges = { 1, {0x14, 0, SE_PRIVILEGE_ENABLED} };
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-05-12 04:46:05 +08:00
|
|
|
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &processToken);
|
|
|
|
AdjustTokenPrivileges(processToken, FALSE, &Privileges, 0, nullptr, nullptr);
|
|
|
|
CloseHandle(processToken);
|
|
|
|
}
|
2016-01-05 23:01:17 +08:00
|
|
|
} // unnamed namespace
|
|
|
|
|
2018-05-12 04:46:05 +08:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused)
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-05-12 04:46:05 +08:00
|
|
|
switch (fdwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
DisableThreadLibraryCalls(hinstDLL);
|
|
|
|
GetDebugPrivileges();
|
|
|
|
// jichi 8/24/2013: Create hidden window so that ITH can access timer and events
|
|
|
|
dummyWindow = CreateWindowW(L"Button", L"InternalWindow", 0, 0, 0, 0, 0, 0, 0, hinstDLL, 0);
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
2018-07-19 12:09:58 +08:00
|
|
|
CloseHost();
|
2018-05-12 04:46:05 +08:00
|
|
|
DestroyWindow(dummyWindow);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
DLLEXPORT bool StartHost()
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-05-12 04:46:05 +08:00
|
|
|
preventDuplicationMutex = CreateMutexW(nullptr, TRUE, ITH_SERVER_MUTEX);
|
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS || ::running)
|
|
|
|
{
|
2018-07-19 12:09:58 +08:00
|
|
|
MessageBoxW(nullptr, L"I am sorry that this game is attached by some other VNR ><\nPlease restart the game and try again!", L"Error", MB_ICONERROR);
|
2018-07-18 05:01:56 +08:00
|
|
|
return false;
|
2018-05-12 04:46:05 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
::running = true;
|
|
|
|
::man = new HookManager;
|
2018-07-18 05:01:56 +08:00
|
|
|
return true;
|
2018-05-12 04:46:05 +08:00
|
|
|
}
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
DLLEXPORT void OpenHost()
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-05-12 04:46:05 +08:00
|
|
|
CreateNewPipe();
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
DLLEXPORT void CloseHost()
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-05-12 04:46:05 +08:00
|
|
|
if (::running)
|
|
|
|
{
|
2018-05-21 04:10:07 +08:00
|
|
|
::running = false;
|
2018-05-12 04:46:05 +08:00
|
|
|
delete man;
|
|
|
|
CloseHandle(preventDuplicationMutex);
|
|
|
|
}
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 02:09:07 +08:00
|
|
|
DLLEXPORT bool InjectProcess(DWORD processId, DWORD timeout)
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-07-19 11:40:44 +08:00
|
|
|
if (processId == GetCurrentProcessId()) return false;
|
2018-05-12 04:46:05 +08:00
|
|
|
|
2018-06-13 08:02:41 +08:00
|
|
|
CloseHandle(CreateMutexW(nullptr, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId)).c_str()));
|
2018-05-12 04:46:05 +08:00
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
|
|
|
man->AddConsoleOutput(L"already locked");
|
2018-07-18 05:01:56 +08:00
|
|
|
return false;
|
2018-05-12 04:46:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-21 01:11:55 +08:00
|
|
|
HMODULE textHooker = LoadLibraryExW(ITH_DLL, nullptr, DONT_RESOLVE_DLL_REFERENCES);
|
2018-05-12 04:46:05 +08:00
|
|
|
wchar_t textHookerPath[MAX_PATH];
|
|
|
|
unsigned int textHookerPathSize = GetModuleFileNameW(textHooker, textHookerPath, MAX_PATH) * 2 + 2;
|
|
|
|
FreeLibrary(textHooker);
|
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
if (HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId))
|
2018-05-12 04:46:05 +08:00
|
|
|
if (LPVOID remoteData = VirtualAllocEx(processHandle, nullptr, textHookerPathSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))
|
|
|
|
if (WriteProcessMemory(processHandle, remoteData, textHookerPath, textHookerPathSize, nullptr))
|
2018-07-18 05:01:56 +08:00
|
|
|
if (HANDLE thread = CreateRemoteThread(processHandle, nullptr, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, remoteData, 0, nullptr))
|
2018-05-12 04:46:05 +08:00
|
|
|
{
|
|
|
|
WaitForSingleObject(thread, timeout);
|
|
|
|
CloseHandle(thread);
|
2018-07-20 11:22:55 +08:00
|
|
|
VirtualFreeEx(processHandle, remoteData, 0, MEM_RELEASE);
|
2018-07-18 05:01:56 +08:00
|
|
|
CloseHandle(processHandle);
|
|
|
|
return true;
|
2018-05-12 04:46:05 +08:00
|
|
|
}
|
2018-07-19 12:09:58 +08:00
|
|
|
|
2018-07-18 05:01:56 +08:00
|
|
|
man->AddConsoleOutput(L"couldn't inject dll");
|
|
|
|
return false;
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 02:09:07 +08:00
|
|
|
DLLEXPORT bool DetachProcess(DWORD processId)
|
2018-05-12 04:46:05 +08:00
|
|
|
{
|
2018-05-21 04:10:07 +08:00
|
|
|
DWORD command = HOST_COMMAND_DETACH;
|
2018-07-14 08:51:09 +08:00
|
|
|
DWORD unused;
|
2018-07-18 05:01:56 +08:00
|
|
|
return WriteFile(man->GetHostPipe(processId), &command, sizeof(command), &unused, nullptr);
|
2018-05-12 04:46:05 +08:00
|
|
|
}
|
2016-01-05 23:01:17 +08:00
|
|
|
|
2018-07-22 02:09:07 +08:00
|
|
|
DLLEXPORT HookManager* GetHostHookManager()
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-07-22 02:09:07 +08:00
|
|
|
return man;
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 02:09:07 +08:00
|
|
|
DLLEXPORT bool InsertHook(DWORD pid, HookParam hp, std::string name)
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-07-19 12:09:58 +08:00
|
|
|
HANDLE commandPipe = man->GetHostPipe(pid);
|
|
|
|
if (commandPipe == nullptr) return false;
|
|
|
|
|
|
|
|
BYTE buffer[PIPE_BUFFER_SIZE] = {};
|
|
|
|
*(DWORD*)buffer = HOST_COMMAND_NEW_HOOK;
|
2018-07-22 02:09:07 +08:00
|
|
|
*(HookParam*)(buffer + sizeof(DWORD)) = hp;
|
2018-07-19 12:09:58 +08:00
|
|
|
if (name.size()) strcpy((char*)buffer + sizeof(DWORD) + sizeof(HookParam), name.c_str());
|
|
|
|
DWORD unused;
|
2018-07-19 12:46:52 +08:00
|
|
|
return WriteFile(commandPipe, buffer, sizeof(DWORD) + sizeof(HookParam) + name.size(), &unused, nullptr);
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:09:58 +08:00
|
|
|
DLLEXPORT bool RemoveHook(DWORD pid, DWORD addr)
|
2016-01-05 23:01:17 +08:00
|
|
|
{
|
2018-07-18 05:01:56 +08:00
|
|
|
HANDLE commandPipe = man->GetHostPipe(pid);
|
2018-07-19 12:09:58 +08:00
|
|
|
if (commandPipe == nullptr) return false;
|
|
|
|
|
2018-07-13 01:59:05 +08:00
|
|
|
HANDLE hookRemovalEvent = CreateEventW(nullptr, TRUE, FALSE, ITH_REMOVEHOOK_EVENT);
|
2018-07-19 12:09:58 +08:00
|
|
|
|
2018-07-18 07:18:36 +08:00
|
|
|
BYTE buffer[sizeof(DWORD) * 2] = {};
|
2018-07-13 01:59:05 +08:00
|
|
|
*(DWORD*)buffer = HOST_COMMAND_REMOVE_HOOK;
|
2018-07-18 07:18:36 +08:00
|
|
|
*(DWORD*)(buffer + sizeof(DWORD)) = addr;
|
2018-07-14 08:51:09 +08:00
|
|
|
DWORD unused;
|
2018-07-19 12:09:58 +08:00
|
|
|
WriteFile(commandPipe, buffer, sizeof(DWORD) * 2, &unused, nullptr);
|
|
|
|
|
|
|
|
WaitForSingleObject(hookRemovalEvent, 1000);
|
|
|
|
CloseHandle(hookRemovalEvent);
|
|
|
|
|
2018-07-23 09:10:00 +08:00
|
|
|
man->RemoveThreads([](auto one, auto two) { return one.pid == two.pid && one.hook == two.hook; }, { pid, addr, 0, 0 });
|
2018-07-19 12:09:58 +08:00
|
|
|
return true;
|
2016-01-05 23:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EOF
|