Skip to content

Commit 1a5cc06

Browse files
Debug DllUnInject, it works now
1 parent 647ce93 commit 1a5cc06

File tree

4 files changed

+19
-56
lines changed

4 files changed

+19
-56
lines changed

APIHOOK/DllUnInject/DllUnInject.cpp

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,20 @@ void DoSearchModule(DWORD dwProcessId)
9696
CloseHandle(hModuleSnap);
9797

9898
//do DllUnInject
99-
if (hEasyHook64 || hHookDll)
99+
if (hHookDll)
100100
{
101-
DoDllUnInject(dwProcessId, hEasyHook64, hHookDll);
101+
OutputDebugString(L"HookDll.dll UnInject START\n");
102+
DoDllUnInject(dwProcessId, hHookDll);
103+
}
104+
if (hEasyHook64)
105+
{
106+
OutputDebugString(L"EasyHook64.dll UnInject START\n");
107+
DoDllUnInject(dwProcessId, hEasyHook64);
102108
}
103-
104109
}
105110

106-
void DoDllUnInject(DWORD dwProcessId, HMODULE hEasyHook64, HMODULE hHookDll)
111+
void DoDllUnInject(DWORD dwProcessId, HMODULE hModule)
107112
{
108-
SIZE_T stBufSize = sizeof(HMODULE);
109-
SIZE_T stWriteSize = 0;
110-
LPVOID pRemoteBuf = NULL;
111113
HMODULE hKernel32 = NULL;
112114
PTHREAD_START_ROUTINE pThreadProc = NULL;
113115
HANDLE hThread = NULL;
@@ -117,57 +119,15 @@ void DoDllUnInject(DWORD dwProcessId, HMODULE hEasyHook64, HMODULE hHookDll)
117119
hKernel32 = GetModuleHandle(L"kernel32.dll");
118120
pThreadProc = (PTHREAD_START_ROUTINE)GetProcAddress(hKernel32, "FreeLibrary");
119121

120-
//alloc in the target process
121-
pRemoteBuf = VirtualAllocEx(hProcess, NULL, stBufSize, MEM_COMMIT, PAGE_READWRITE);
122-
if (!pRemoteBuf)
123-
{
124-
OutputDebugString(L"VirtualAllocEx ERROR\n");
125-
return;
126-
}
127-
128-
//do UnInject to HookDll.dll
129-
if (hHookDll)
130-
{
131-
OutputDebugString(L"HookDll.dll UnInject START\n");
132-
WriteProcessMemory(hProcess, pRemoteBuf, (LPVOID)&hHookDll, stBufSize, &stWriteSize);
133-
if (stBufSize != stWriteSize)
134-
{
135-
OutputDebugString(L"WriteProcessMemory ERROR\n");
136-
return;
137-
}
138-
hThread = CreateRemoteThread(hProcess,
139-
NULL,
140-
0,
141-
(LPTHREAD_START_ROUTINE)pThreadProc,
142-
pRemoteBuf,
143-
0,
144-
NULL);
145-
if (hThread)
146-
{
147-
WaitForSingleObject(hThread, INFINITE);
148-
CloseHandle(hThread);
149-
}
150-
else
151-
{
152-
OutputDebugString(L"CreateRemoteThread ERROR\n");
153-
}
154-
}
155-
156-
//do UnInject to EasyHook64.dll
157-
if (hEasyHook64)
122+
//do UnInject
123+
if (hModule)
158124
{
159-
OutputDebugString(L"EasyHook64.dll UnInject START\n");
160-
WriteProcessMemory(hProcess, pRemoteBuf, (LPVOID)&hEasyHook64, stBufSize, &stWriteSize);
161-
if (stBufSize != stWriteSize)
162-
{
163-
OutputDebugString(L"WriteProcessMemory ERROR\n");
164-
return;
165-
}
125+
166126
hThread = CreateRemoteThread(hProcess,
167127
NULL,
168128
0,
169129
(LPTHREAD_START_ROUTINE)pThreadProc,
170-
pRemoteBuf,
130+
hModule,
171131
0,
172132
NULL);
173133
if (hThread)
@@ -180,8 +140,6 @@ void DoDllUnInject(DWORD dwProcessId, HMODULE hEasyHook64, HMODULE hHookDll)
180140
OutputDebugString(L"CreateRemoteThread ERROR\n");
181141
}
182142
}
183-
184-
VirtualFreeEx(hProcess, pRemoteBuf, stBufSize, MEM_RELEASE);
185143
}
186144

187145
void DoReleaseSemaphoreStatus()

APIHOOK/DllUnInject/DllUnInject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
void DoSearchProcess(LPCWCHAR dwProcesName);
66
void DoSearchModule(DWORD dwProcessId);
7-
void DoDllUnInject(DWORD dwProcessId, HMODULE hEasyHook64, HMODULE hHookDll);
7+
void DoDllUnInject(DWORD dwProcessId, HMODULE hModule);
88
void DoReleaseSemaphoreStatus();
99
BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);

APIHOOK/HookDll/dllmain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ BOOL APIENTRY DllMain( HMODULE hModule,
1111
case DLL_PROCESS_ATTACH:
1212
{
1313
OutputDebugString(L"DllMain: DLL_PROCESS_ATTACH\n");
14+
printf("%s", "DllMain: DLL_PROCESS_ATTACH\n");
1415
}
1516
case DLL_THREAD_ATTACH:
1617
{
1718
OutputDebugString(L"DllMain: DLL_THREAD_ATTACH\n");
19+
printf("%s", "DllMain: DLL_THREAD_ATTACH\n");
1820
}
1921
case DLL_THREAD_DETACH:
2022
{
2123
OutputDebugString(L"DllMain: DLL_THREAD_DETACH\n");
24+
printf("%s", "DllMain: DLL_THREAD_DETACH\n");
2225
}
2326
case DLL_PROCESS_DETACH:
2427
{
2528
OutputDebugString(L"DllMain: DLL_PROCESS_DETACH\n");
29+
printf("%s", "DllMain: DLL_PROCESS_DETACH\n");
2630
}
2731
break;
2832
}

APIHOOK/HookDll/stdafx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
1111
// Windows 头文件:
1212
#include <windows.h>
13+
#include <stdio.h>
1314

1415

1516

0 commit comments

Comments
 (0)