-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCreateRemoteThreadInjector2.c
More file actions
177 lines (149 loc) · 4.57 KB
/
CreateRemoteThreadInjector2.c
File metadata and controls
177 lines (149 loc) · 4.57 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
#include <windows.h>
#include <stdio.h>
HANDLE RtlCreateUserThread(
HANDLE hProcess,
LPVOID lpBaseAddress,
LPVOID lpSpace
)
{
//The structure of RtlCreateUserThread from undocumented.ntinternals.com
typedef DWORD (WINAPI * functypeRtlCreateUserThread)(
HANDLE ProcessHandle,
PSECURITY_DESCRIPTOR SecurityDescriptor,
BOOL CreateSuspended,
ULONG StackZeroBits,
PULONG StackReserved,
PULONG StackCommit,
LPVOID StartAddress,
LPVOID StartParameter,
HANDLE ThreadHandle,
LPVOID ClientID
);
//Get handle for ntdll which contains RtlCreateUserThread
HANDLE hRemoteThread = NULL;
HMODULE hNtDllModule = GetModuleHandle("ntdll.dll");
if( hNtDllModule == NULL )
{
return NULL;
}
functypeRtlCreateUserThread funcRtlCreateUserThread = (functypeRtlCreateUserThread)GetProcAddress(hNtDllModule, "RtlCreateUserThread");
if( !funcRtlCreateUserThread )
{
return NULL;
}
funcRtlCreateUserThread(hProcess, NULL, 0, 0, 0, 0, lpBaseAddress,lpSpace, &hRemoteThread, NULL);
DWORD lastError = GetLastError();
return hRemoteThread;
}
HANDLE NtCreateThreadEx(
HANDLE hProcess,
LPVOID lpBaseAddress,
LPVOID lpSpace
)
{
//The structure of NtCreateThreadEx from undocumented.ntinternals.com
typedef DWORD (WINAPI * functypeNtCreateThreadEx)(
PHANDLE ThreadHandle,
ACCESS_MASK DesiredAccess,
LPVOID ObjectAttributes,
HANDLE ProcessHandle,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
BOOL CreateSuspended,
DWORD dwStackSize,
DWORD Unknown1,
DWORD Unknown2,
LPVOID Unknown3
);
HANDLE hRemoteThread = NULL;
HMODULE hNtDllModule = NULL;
functypeNtCreateThreadEx funcNtCreateThreadEx = NULL;
//Get handle for ntdll which contains NtCreateThreadEx
hNtDllModule = GetModuleHandle( "ntdll.dll" );
if ( hNtDllModule == NULL )
{
return NULL;
}
funcNtCreateThreadEx = (functypeNtCreateThreadEx)GetProcAddress( hNtDllModule, "NtCreateThreadEx" );
if ( !funcNtCreateThreadEx )
{
return NULL;
}
funcNtCreateThreadEx( &hRemoteThread, GENERIC_ALL, NULL, hProcess, (LPTHREAD_START_ROUTINE)lpBaseAddress, lpSpace, FALSE, NULL, NULL, NULL, NULL );
return hRemoteThread;
}
//TODO: find some way to differentiate from infosec institute article
int injectIntoPID(int process, int method)
{
DWORD pid = (DWORD) process;
char* dll = "inject.dll";
//Gets the process handle for the target process
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if(OpenProcess == NULL)
{
puts("Could not find process");
}
//Retrieves kernel32.dll module handle for getting loadlibrary base address
HMODULE hModule = GetModuleHandle("kernel32.dll");
//Gets address for LoadLibraryA in kernel32.dll
LPVOID lpBaseAddress = (LPVOID)GetProcAddress(hModule,"LoadLibraryA");
if(lpBaseAddress == NULL)
{
puts("Unable to locate LoadLibraryA");
return -1;
}
//Allocates space inside for inject.dll to our target process
LPVOID lpSpace = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(dll), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if(lpSpace == NULL)
{
printf("Could not allocate memory in process %u", (int)process);
return -1;
}
//Write inject.dll to memory of process
int n = WriteProcessMemory(hProcess, lpSpace, dll, strlen(dll), NULL);
if(n == 0)
{
puts("Could not write to process's address space");
return -1;
}
HANDLE hThread;
switch( method )
{
case 1:
hThread = NtCreateThreadEx(hProcess, lpBaseAddress, lpSpace);
break;
case 2:
hThread = RtlCreateUserThread(hProcess, lpBaseAddress, lpSpace);
break;
default:
hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpBaseAddress, lpSpace, NULL, NULL);
}
if(hThread == NULL)
{
return -1;
}
else
{
DWORD threadId = GetThreadId(hThread);
DWORD processId = GetProcessIdOfThread(hThread);
printf("Injected thread id: %u for pid: %u", threadId, processId);
getchar();
getchar();
CloseHandle(hProcess);
return 0;
}
}
int main(int argc, char* argv)
{
int pid;
int method;
puts("Inject into which PID?");
scanf("%u", &pid);
puts("Which method? (0 (default): CRT, 1: NtCreateThread, 2: RtlCreateUserThread)");
scanf("%u", &method);
int result = injectIntoPID(pid,method);
if(result == -1)
{
puts("Could not inject into PID");
}
}