From 11d75c29875f907c2ba533d93539455a5ab59a9e Mon Sep 17 00:00:00 2001
From: Akash Mozumdar <akashmozumdar@gmail.com>
Date: Thu, 2 Aug 2018 17:16:49 -0400
Subject: [PATCH] notify hook removal via pipe - fix hook rewrite bug

---
 vnrhook/include/const.h        |  1 +
 vnrhook/src/hijack/texthook.cc |  1 +
 vnrhook/src/main.h             |  1 +
 vnrhook/src/pipe.cc            | 17 +++++++++++------
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/vnrhook/include/const.h b/vnrhook/include/const.h
index d5630ca..1edabd5 100644
--- a/vnrhook/include/const.h
+++ b/vnrhook/include/const.h
@@ -82,6 +82,7 @@ enum HostNotificationType {
   HOST_NOTIFICATION = -1 // null type
   , HOST_NOTIFICATION_TEXT = 0
   , HOST_NOTIFICATION_NEWHOOK = 1
+  , HOST_NOTIFICATION_RMVHOOK = 2
 };
 
 // jichi 9/8/2013: The meaning are guessed
diff --git a/vnrhook/src/hijack/texthook.cc b/vnrhook/src/hijack/texthook.cc
index c0177ac..a4c9e77 100644
--- a/vnrhook/src/hijack/texthook.cc
+++ b/vnrhook/src/hijack/texthook.cc
@@ -725,6 +725,7 @@ int TextHook::ClearHook()
   //  current_available = this;
   currentHook--;
   ReleaseMutex(hmMutex);
+  NotifyHookRemove(hp.address);
   return err;
 }
 
diff --git a/vnrhook/src/main.h b/vnrhook/src/main.h
index d42c956..4cce4e0 100644
--- a/vnrhook/src/main.h
+++ b/vnrhook/src/main.h
@@ -9,6 +9,7 @@
 
 void ConsoleOutput(LPCSTR text); // jichi 12/25/2013: Used to return length of sent text
 void NotifyHookInsert(HookParam hp, LPCSTR name);
+void NotifyHookRemove(DWORD addr);
 DWORD NewHook(const HookParam &hp, LPCSTR name, DWORD flag = HOOK_ENGINE);
 DWORD RemoveHook(DWORD addr);
 DWORD SwitchTrigger(DWORD on);
diff --git a/vnrhook/src/pipe.cc b/vnrhook/src/pipe.cc
index de7be82..2b16258 100644
--- a/vnrhook/src/pipe.cc
+++ b/vnrhook/src/pipe.cc
@@ -69,8 +69,6 @@ DWORD WINAPI PipeManager(LPVOID unused)
 				break;
 			case HOST_COMMAND_REMOVE_HOOK:
 			{
-				HANDLE hookRemovalEvent = OpenEventW(SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, ITH_REMOVEHOOK_EVENT);
-
 				TextHook *in = hookman;
 				for (int i = 0; i < currentHook; in++)
 				{
@@ -84,9 +82,6 @@ DWORD WINAPI PipeManager(LPVOID unused)
 				{
 					in->ClearHook();
 				}
-
-				SetEvent(hookRemovalEvent);
-				CloseHandle(hookRemovalEvent);
 			}
 			break;
 			case HOST_COMMAND_DETACH:
@@ -111,7 +106,6 @@ void ConsoleOutput(LPCSTR text)
 	WriteFile(::hookPipe, buffer, strlen(text) + sizeof(DWORD) * 2, &unused, nullptr);
 }
 
-// Artikash 7/3/2018: TODO: Finish using this in vnrhost instead of section to deliver hook info
 void NotifyHookInsert(HookParam hp, LPCSTR name)
 {
     BYTE buffer[PIPE_BUFFER_SIZE];
@@ -124,4 +118,15 @@ void NotifyHookInsert(HookParam hp, LPCSTR name)
 	return;
 }
 
+void NotifyHookRemove(DWORD addr)
+{
+	BYTE buffer[sizeof(DWORD) * 3];
+	*(DWORD*)buffer = HOST_NOTIFICATION;
+	*(DWORD*)(buffer + sizeof(DWORD)) = HOST_NOTIFICATION_RMVHOOK;
+	*(DWORD*)(buffer + sizeof(DWORD) * 2) = addr;
+	DWORD unused;
+	WriteFile(::hookPipe, buffer, sizeof(DWORD) * 3, &unused, nullptr);
+	return;
+}
+
 // EOF