-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetach.cpp
executable file
·92 lines (72 loc) · 2.61 KB
/
detach.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <windows.h>
#include <iostream>
#include <psapi.h>
#define endl "\n";
DWORD UnloadDll(DWORD TargetId, std::wstring DLL_NAME)
{
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, TargetId);
if (process == INVALID_HANDLE_VALUE)
return 3;
HMODULE modhandles[1024];
DWORD nb;
if (!EnumProcessModulesEx(process, modhandles, sizeof(modhandles), &nb, LIST_MODULES_ALL))
{
CloseHandle(process);
return 4;
}
unsigned char* TARGET_REMOTE_BASE_ADDRESS = 0x0;
for (size_t i = 0; i < (nb / sizeof(HMODULE)); i++)
{
TCHAR modname[MAX_PATH];
if (GetModuleFileNameEx(process, modhandles[i], modname, sizeof(modname) / sizeof(TCHAR)))
{
if (wcsstr(modname, DLL_NAME.c_str()))
{
MEMORY_BASIC_INFORMATION modinfo;
VirtualQueryEx(process, modhandles[i], &modinfo, sizeof(modinfo));
std::wcout << DLL_NAME << " Base Address: " << modinfo.BaseAddress << endl;
std::wcout << "Full Path: " << modname << endl;
TARGET_REMOTE_BASE_ADDRESS = (unsigned char*)modinfo.BaseAddress;
}
}
}
if (TARGET_REMOTE_BASE_ADDRESS == 0x0)
return 5;
// Create Remote Library Caller
LPTHREAD_START_ROUTINE freelib_caller = (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "FreeLibrary");
if (!freelib_caller)
return 8;
std::cout << "FreeLibrary Caller created at remote process: " << (PVOID)freelib_caller << endl;
// Create and Start Remote freelib inside target process
HANDLE thread = CreateRemoteThread(process, NULL, 0, freelib_caller, TARGET_REMOTE_BASE_ADDRESS, 0, NULL);
if (!thread)
return 9;
WaitForSingleObject(thread, INFINITE);
DWORD exitdw = 0;
if (!GetExitCodeThread(thread, &exitdw))
return 10;
CloseHandle(thread);
CloseHandle(process);
return 1;
}
int
main(int argc, char**argv)
{
if (argc != 3)
{
std::cout << "Usage: " << argv[0] << " (PID)1337 " << " (DLLNAME)lib.dll" << endl;
return 1;
}
DWORD TargetId = atoi(argv[1]);
std::cout << "Target PID: " << TargetId << endl;
std::cout << "Target Lib: " << argv[2] << endl;
DWORD code = UnloadDll(TargetId, std::wstring(argv[2], argv[2] + strlen(argv[2])));
if (code)
{
std::cout << "Successful Unload!" << endl;
}
else {
std::cout << "Unload Error: " << code << endl;
}
return 0;
}