-
Notifications
You must be signed in to change notification settings - Fork 6
/
Injector.cpp
178 lines (148 loc) · 5.37 KB
/
Injector.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "Injector.h"
#include "Util.h"
//look for more information about DLL injection at Wikipedia
//https://en.wikipedia.org/wiki/DLL_injection
/// <summary>Load a DLL into a remote process</summary>
/// <param name="dwTargetPID">ProcessId of the target process</param>
/// <param name="DllPath">The path to the DLL to inject</param>
/// <returns>true on success</returns>
bool InjectDll( DWORD dwTargetPID, char* DllPath )
{
//Note that I did these comments some time ago to show a friend how dll-injection works
size_t DllPathLen = strlen(DllPath);
if ( DllPathLen == NULL )
return false;
// The System Dlls get mapped to the same address in every process :D
static HMODULE hKernel32 = NULL;
if ( hKernel32 == NULL )
hKernel32 = GetModuleHandleA("kernel32.dll");
static void* FncLoadLibraryAddr = NULL;
if ( FncLoadLibraryAddr == NULL )
FncLoadLibraryAddr = (PVOID)GetProcAddress(hKernel32, "LoadLibraryA");
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwTargetPID);
if ( hProcess == INVALID_HANDLE_VALUE || hProcess == NULL )
return false;
//Allocate space in the targets process for our string
char* pDllPath = (char*)VirtualAllocEx(hProcess, 0, DllPathLen+5, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if ( pDllPath == NULL ) //check if remote allocation failed
{ CloseHandle( hProcess ); return false;}
//write the string into the fresh allocated space:
BOOL bWriteProcMemSuccess =
WriteProcessMemory(hProcess, pDllPath, (LPVOID)DllPath, DllPathLen, NULL);
if ( bWriteProcMemSuccess != TRUE )
{ CloseHandle( hProcess ); return false;}
//Remote starting LoadLibrary:
HANDLE hThread = CreateRemoteThread( hProcess,
NULL,
NULL,
(LPTHREAD_START_ROUTINE)FncLoadLibraryAddr,
pDllPath, //<= The parameter
NULL,
NULL
);
if ( hThread == NULL || hThread == INVALID_HANDLE_VALUE )
{ CloseHandle( hProcess ); return false;}
//Lets wait till LoadLibrary is finished:
WaitForSingleObject( hThread, INFINITE );
Sleep( 100 );
//the dll path is not longer needed lets free that:
VirtualFreeEx( hProcess, pDllPath, NULL, MEM_RELEASE );
CloseHandle( hProcess );
return true;
}
#include <TlHelp32.h>
DWORD_PTR GetRemoteModuleAddress(DWORD dwTargetPID, char *szModulePath )
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwTargetPID);
if (hSnap == INVALID_HANDLE_VALUE || hSnap == NULL )
return NULL;
MODULEENTRY32 ModuleEntry32;
ZeroMemory( &ModuleEntry32, sizeof( MODULEENTRY32 ) );
ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
if (Module32First(hSnap, &ModuleEntry32))
{
while (true)
{
//printf("me.szExePath = \"%s\"\n",me.szExePath);
if (StrCmpToLower(ModuleEntry32.szExePath, szModulePath) == true)
{
CloseHandle(hSnap);
return (DWORD_PTR)ModuleEntry32.modBaseAddr;
}
if ( Module32Next(hSnap, &ModuleEntry32) != TRUE )
break;
}
}
CloseHandle(hSnap);
return NULL;
}
bool UnloadDllEx( HANDLE hProcess, DWORD_PTR dwModuleAddressToUnload )
{
static HMODULE hNtDll = NULL;
if ( hNtDll == NULL )
hNtDll = GetModuleHandleA("ntdll.dll");
static DWORD64 LdrUnloadDll = NULL;
if ( LdrUnloadDll == NULL )
LdrUnloadDll = (DWORD64)GetProcAddress(hNtDll, "LdrUnloadDll");
HANDLE hThread = CreateRemoteThread( hProcess,
NULL,
NULL,
(LPTHREAD_START_ROUTINE)LdrUnloadDll,
(LPVOID)dwModuleAddressToUnload,
NULL,
NULL
);
if ( hThread == INVALID_HANDLE_VALUE || hThread == NULL )
{
CloseHandle( hProcess );
return false;
}
CloseHandle( hThread );
return true;
}
/// <summary>Unload a DLL from a remote process</summary>
/// <param name="dwTargetPID">ProcessId of the target process.</param>
/// <param name="DllPath">Path to the DLL to unload</param>
/// <returns>true on success</returns>
bool UnloadDll( DWORD dwTargetPID, char* DllPath )
{
DWORD_PTR dwAddress = GetRemoteModuleAddress( dwTargetPID, DllPath );
if ( dwAddress == NULL )
return false; //dll seems to be not loaded
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, dwTargetPID );
if ( hProcess == NULL || hProcess == INVALID_HANDLE_VALUE )
return false;
if ( UnloadDllEx( hProcess, dwAddress ) != true )
{
CloseHandle( hProcess );
return false;
}
CloseHandle( hProcess );
return true;
}
/// <summary>Gets all ProcessIds from processes with the specified name</summary>
/// <param name="ProcName">process name to look for</param>
/// <returns>std::vector of ProcessIds</returns>
std::vector<DWORD> GetProcsId(char* ProcName)
{
std::vector<DWORD> result; result.clear();
PROCESSENTRY32 ProcEntry32;
ZeroMemory( &ProcEntry32, sizeof(PROCESSENTRY32) );
ProcEntry32.dwSize = sizeof( PROCESSENTRY32 );
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if ( hSnapshot == NULL || hSnapshot == INVALID_HANDLE_VALUE )
return result;
if ( Process32First( hSnapshot, &ProcEntry32 ) != TRUE )
return result;
while (true)
{
if (StrCmpToLower(ProcEntry32.szExeFile, ProcName) == true)
{
DWORD dwProcessID = ProcEntry32.th32ProcessID;
result.push_back( dwProcessID );
}
if ( Process32Next( hSnapshot, &ProcEntry32 ) != TRUE ) break;
}
CloseHandle( hSnapshot );
return result;
}