-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
361 lines (281 loc) · 8.26 KB
/
Copy pathmain.cpp
File metadata and controls
361 lines (281 loc) · 8.26 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <Windows.h>
#include <stdio.h>
#include <vector>
#include <tlhelp32.h>
using namespace std;
#define PROCESS_THREAD_ACCESS PROCESS_CREATE_THREAD|PROCESS_QUERY_INFORMATION|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE
// Undocumented function and relative structure
// http://securityxploded.com/ntcreatethreadex.php
// Using CreateRemoteThread :
// Terminal Services isolates each terminal session by design. Therefore, CreateRemoteThread fails if the target process is in a different session than the calling process.
typedef NTSTATUS (WINAPI *LPFUN_NtCreateThreadEx)
(
OUT PHANDLE hThread,
IN ACCESS_MASK DesiredAccess,
IN LPVOID ObjectAttributes,
IN HANDLE ProcessHandle,
IN LPTHREAD_START_ROUTINE lpStartAddress,
IN LPVOID lpParameter,
IN BOOL CreateSuspended,
IN ULONG StackZeroBits,
IN ULONG SizeOfStackCommit,
IN ULONG SizeOfStackReserve,
OUT LPVOID lpBytesBuffer
);
//Buffer argument passed to NtCreateThreadEx function
struct NtCreateThreadExBuffer
{
ULONG Size;
ULONG Unknown1;
ULONG Unknown2;
PULONG Unknown3;
ULONG Unknown4;
ULONG Unknown5;
ULONG Unknown6;
PULONG Unknown7;
ULONG Unknown8;
};
bool IsWindowsNT( )
{
return ( GetVersion() < 0x80000000 ) ? true : false;
}
vector<PROCESSENTRY32> GetProcessListExeName( PCSTR szExeName )
{
HANDLE hSnapProcess = NULL;
PROCESSENTRY32 pe32 = {0};
vector<PROCESSENTRY32> ape32ProcessList;
if( !IsWindowsNT( ) || !szExeName )
return ape32ProcessList;
if( !( hSnapProcess = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ) ) )
return ape32ProcessList;
pe32.dwSize = sizeof( PROCESSENTRY32 );
if( Process32First( hSnapProcess, &pe32 ) )
{
do
{
if( strcmp( pe32.szExeFile, szExeName ) == 0 )
{
ape32ProcessList.push_back( pe32 );
}
}
while( Process32Next( hSnapProcess, &pe32 ) );
}
CloseHandle(hSnapProcess);
return ape32ProcessList;
}
BOOL SetPrivilege(
HANDLE hToken,
LPCTSTR lpPrivilege,
BOOL bEnablePrivilege
)
{
// Initializing variables
TOKEN_PRIVILEGES tkp = {0};
LUID luid = {0};
TOKEN_PRIVILEGES tkpPrevious = {0};
DWORD cbPrevious = 0;
// Check the parameters passed to the function
if( ( !hToken ) || ( !lpPrivilege ) )
{
return FALSE;
}
if( !LookupPrivilegeValue( NULL, lpPrivilege, &luid ) )
{
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = luid;
tkp.Privileges[0].Attributes = 0;
cbPrevious = sizeof( TOKEN_PRIVILEGES );
AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof( TOKEN_PRIVILEGES ), &tkpPrevious, &cbPrevious );
if( GetLastError() != ERROR_SUCCESS )
{
return FALSE;
}
tkpPrevious.PrivilegeCount = 1;
tkpPrevious.Privileges[0].Luid = luid;
if( bEnablePrivilege )
{
tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
}
else
{
tkpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tkpPrevious.Privileges[0].Attributes);
}
AdjustTokenPrivileges( hToken, FALSE, &tkpPrevious, cbPrevious, NULL, NULL );
if( GetLastError() != ERROR_SUCCESS )
{
return FALSE;
}
return TRUE;
}
//
// Set debug privilege
//
BOOL SetDebugPrivilege( BOOL bEnable )
{
HANDLE hToken = NULL;
if( !OpenProcessToken( GetCurrentProcess( ), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
return FALSE;
// Enable/Disable Debug Privilege
if( !SetPrivilege( hToken, SE_DEBUG_NAME, bEnable ) )
{
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
return TRUE;
}
HANDLE GetHandleModuleInjected( DWORD dwPID, char *szModulePath )
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if( hModuleSnap == INVALID_HANDLE_VALUE )
{
printf( "[ERROR] : CreateToolhelp32Snapshot error 0x%08X\n", GetLastError() );
return hModuleSnap;
}
// Set the size of the structure before using it.
me32.dwSize = sizeof( MODULEENTRY32 );
// Retrieve information about the first module,
// and exit if unsuccessful
if( !Module32First( hModuleSnap, &me32 ) )
{
printf( "[ERROR] : Module32First error 0x%08X\n", GetLastError() ); // show cause of failure
CloseHandle( hModuleSnap ); // clean the snapshot object
return hModuleSnap;
}
// Now walk the module list of the process,
// and display information about each module
do
{
if( strstr( szModulePath, me32.szModule ) )
{
break;
}
}
while( Module32Next( hModuleSnap, &me32 ) );
CloseHandle( hModuleSnap );
return hModuleSnap;
}
void main( int argc, char* argv[] )
{
DWORD dwPID;
HANDLE hThread = NULL;
NTSTATUS status;
char *szDllPath;
LPVOID pRemoteMem = NULL;
if( argc < 3 )
{
printf( "Usage : %s <PROCESS_NAME> <DLL_PATH>\n", argv[0] );
return;
}
szDllPath = argv[2];
vector<PROCESSENTRY32> ape32ProcessList = GetProcessListExeName( argv[1] );
if( ape32ProcessList.size() > 0 )
{
dwPID = ape32ProcessList[0].th32ProcessID;
}
printf( "# Injecting %s in %s (PID %d)\n", szDllPath, argv[1], dwPID );
SetDebugPrivilege(TRUE);
// Open remote process
HANDLE hProcess = OpenProcess( PROCESS_THREAD_ACCESS, FALSE, dwPID );
if( hProcess == NULL )
{
printf( "[ERROR] : Can't open process (0x%08X)\n", GetLastError() );
goto end;
}
// Load LoadLibraryA function address
HMODULE hModule = GetModuleHandle("kernel32.dll");
if( hModule == NULL )
{
printf( "[ERROR] : GetModuleHandle error (0x%08X)\n", GetLastError() );
goto end;
}
PTHREAD_START_ROUTINE pThreadRoutine = (PTHREAD_START_ROUTINE)GetProcAddress( hModule, "LoadLibraryA" );
if( pThreadRoutine == NULL )
{
printf( "[ERROR] : LoadLibrary not found (0x%08X)\n", GetLastError() );
goto end;
}
HMODULE modNtDll = GetModuleHandle("ntdll.dll");
if( modNtDll == NULL )
{
printf( "[ERROR] : GetModuleHandle error (0x%08X)\n", GetLastError() );
goto end;
}
printf( "# Remote memory Allocation.\n" );
pRemoteMem = (LPVOID)VirtualAllocEx( hProcess, NULL, strlen(szDllPath), MEM_COMMIT, PAGE_EXECUTE_READWRITE );
if( pRemoteMem == NULL )
{
printf( "[ERROR] : Can't allocate memory (0x%08X)\n", GetLastError() );
goto end;
}
printf( "# Remote memory allocated : 0x%08X\n", pRemoteMem );
printf( "# Writing remote memory.\n" );
int n = WriteProcessMemory( hProcess, pRemoteMem, szDllPath, strlen(szDllPath), NULL );
if( n == 0 )
{
printf( "[ERROR] : Can't write on memory process (0x%08X)\n", GetLastError() );
goto end;
}
//
// We're using this undocumented func because the simple CreateRemoteThread can't access
// from a session to another session (session separation)
//
LPFUN_NtCreateThreadEx funNtCreateThreadEx = (LPFUN_NtCreateThreadEx) GetProcAddress(modNtDll, "NtCreateThreadEx");
if( !funNtCreateThreadEx )
{
printf( "[ERROR] : can't get function address (0x%08X)", GetLastError() );
goto end;
}
NtCreateThreadExBuffer ntbuffer;
memset (&ntbuffer,0,sizeof(NtCreateThreadExBuffer));
DWORD temp1 = 0;
DWORD temp2 = 0;
ntbuffer.Size = sizeof(NtCreateThreadExBuffer);
ntbuffer.Unknown1 = 0x10003;
ntbuffer.Unknown2 = 0x8;
ntbuffer.Unknown3 = &temp2;
ntbuffer.Unknown4 = 0;
ntbuffer.Unknown5 = 0x10004;
ntbuffer.Unknown6 = 4;
ntbuffer.Unknown7 = &temp1;
ntbuffer.Unknown8 = 0;
status =
funNtCreateThreadEx(
&hThread,
0x1FFFFF,
NULL,
hProcess,
(LPTHREAD_START_ROUTINE) pThreadRoutine,
pRemoteMem,
FALSE,
NULL,
NULL,
NULL,
&ntbuffer
);
//ThreadID = CreateRemoteThread( hProcess, NULL, 0, pThreadRoutine, pRemoteMem, NULL, NULL );
if( hThread == NULL )
{
printf( "[ERROR] : Can't create remote thread (0x%08X)\n", status );
}
DWORD dwInjectedDllBase = 0;
printf( "[SUCCESS] : the remote thread was successfully created\n" );
WaitForSingleObject( hThread, INFINITE );
GetExitCodeThread( hProcess, &dwInjectedDllBase );
CloseHandle(hThread);
end:
if( pRemoteMem != 0 )
{
VirtualFreeEx( hProcess, pRemoteMem, 0, MEM_RELEASE );
}
if( hProcess != NULL )
{
CloseHandle(hProcess);
}
return;
}