-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinject-thread.c
461 lines (343 loc) · 9.08 KB
/
inject-thread.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* CreateRemoteThread for Linux
*
* Copyright (c) 2018, ilammy
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
#include <getopt.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elf.h"
#include "inject-thread.h"
#include "procfs.h"
#include "ptrace.h"
#include "shell.h"
#include "syscall.h"
static const struct option long_options[] = {
{ .name = "help", .has_arg = 0, .val = 'h'},
{ .name = "target", .has_arg = 1, .val = 't'},
{ .name = "payload", .has_arg = 1, .val = 'p'},
{ .name = "entry", .has_arg = 1, .val = 'e'},
};
static void usage(const char *name)
{
printf(
"Usage:\n"
" %s [option]...\n"
"\n"
"Options:\n"
" --target <PID> PID of the target process\n"
" --payload <path/to/payload.so> payload shared object to inject\n"
" --entry <entry_point> name of the function in the payload\n"
"\n"
"All options are required and must be specified.\n"
"\n",
name);
}
pid_t target;
pid_t shell_tid;
char payload[256];
char entry[256];
unsigned long dlopen_vaddr;
unsigned long dlsym_vaddr;
unsigned long pthread_create_vaddr;
unsigned long pthread_detach_vaddr;
unsigned long syscall_ret_vaddr;
unsigned long shellcode_text_vaddr;
unsigned long shellcode_stack_vaddr;
static int inject_thread(void);
int main(int argc, char *argv[])
{
int opt;
while ((opt = getopt_long(argc, argv, "h", long_options, NULL)) != -1) {
switch (opt) {
case 'h':
usage(argv[0]);
return 0;
case 't':
target = atoi(optarg);
break;
case 'p':
strncpy(payload, optarg, sizeof(payload) - 1);
break;
case 'e':
strncpy(entry, optarg, sizeof(entry) - 1);
break;
default:
usage(argv[0]);
return 1;
}
}
if (!target || !payload[0] || !entry[0]) {
usage(argv[0]);
return 1;
}
return inject_thread() ? 2 : 0;
}
static int resolve_libdl_symbols()
{
int err = 0;
struct library libdl;
printf("[-] locating and mapping libdl...\n");
err = map_remote_library(target, "/libdl-", &libdl);
if (err < 0)
goto out;
printf("[+] mapped %zu regions\n", libdl.region_count);
printf("[-] locating dynamic symbol table of libdl...\n");
struct symbol_table *symbols = find_dynamic_symbol_table(&libdl);
if (!symbols) {
err = -1;
goto unmap;
}
printf("[+] found dynamic symbol table\n");
printf("[-] resolving symbols from libdl...\n");
dlopen_vaddr = resolve_symbol("dlopen", symbols);
dlsym_vaddr = resolve_symbol("dlsym", symbols);
if (!dlopen_vaddr || !dlsym_vaddr) {
err = -1;
goto free_symbols;
}
printf("[+] resolved dlopen(): %lx\n", dlopen_vaddr);
printf("[+] resolved dlsym(): %lx\n", dlsym_vaddr);
free_symbols:
free_symbol_table(symbols);
unmap:
unmap_remote_library(&libdl);
out:
return err;
}
static int resolve_libpthread_symbols()
{
int err = 0;
struct library libpthread;
printf("[-] locating and mapping libpthread...\n");
err = map_remote_library(target, "/libpthread-", &libpthread);
if (err < 0)
goto out;
printf("[+] mapped %zu regions\n", libpthread.region_count);
printf("[-] locating dynamic symbol table of libpthread...\n");
struct symbol_table *symbols = find_dynamic_symbol_table(&libpthread);
if (!symbols) {
err = -1;
goto unmap;
}
printf("[+] found dynamic symbol table\n");
printf("[-] resolving symbols from libpthread...\n");
pthread_create_vaddr = resolve_symbol("pthread_create", symbols);
pthread_detach_vaddr = resolve_symbol("pthread_detach", symbols);
if (!pthread_create_vaddr || !pthread_detach_vaddr) {
err = -1;
goto free_symbols;
}
printf("[+] resolved pthread_create(): %lx\n", pthread_create_vaddr);
printf("[+] resolved pthread_detach(): %lx\n", pthread_detach_vaddr);
free_symbols:
free_symbol_table(symbols);
unmap:
unmap_remote_library(&libpthread);
out:
return err;
}
static int locate_syscall_trampoline()
{
int err = 0;
struct library libc;
printf("[-] locating and mapping libc...\n");
err = map_remote_library(target, "/libc-", &libc);
if (err < 0)
goto out;
printf("[+] mapped %zu regions\n", libc.region_count);
printf("[-] locating SYSCALL instruction in libc...\n");
syscall_ret_vaddr = find_syscall_instruction(&libc);
if (!syscall_ret_vaddr) {
err = -1;
goto unmap;
}
printf("[+] found SYSCALL instruction at %lx\n", syscall_ret_vaddr);
unmap:
unmap_remote_library(&libc);
out:
return err;
}
static int prepare_shellcode()
{
int err = 0;
printf("[-] mapping memory pages for the shellcode...\n");
shellcode_text_vaddr =
remote_mmap(target, syscall_ret_vaddr, 0,
SHELLCODE_TEXT_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
shellcode_stack_vaddr =
remote_mmap(target, syscall_ret_vaddr, 0,
SHELLCODE_STACK_SIZE,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_STACK |
MAP_GROWSDOWN, -1, 0);
if (!shellcode_text_vaddr || !shellcode_stack_vaddr)
goto error_unmap_shellcode;
printf("[+] mapped a page for shellcode text at %lx\n",
shellcode_text_vaddr);
printf("[+] mapped pages for shellcode stack at %lx\n",
shellcode_stack_vaddr);
err = write_shellcode();
if (err)
goto error_unmap_shellcode;
printf("[-] making shellcode text read-only...\n");
err = remote_mprotect(target, syscall_ret_vaddr,
shellcode_text_vaddr, SHELLCODE_TEXT_SIZE,
PROT_READ | PROT_EXEC);
if (err)
goto error_unmap_shellcode;
printf("[+] shellcode ready\n");
return 0;
error_unmap_shellcode:
if (shellcode_text_vaddr)
remote_munmap(target, syscall_ret_vaddr,
shellcode_text_vaddr, SHELLCODE_TEXT_SIZE);
if (shellcode_stack_vaddr)
remote_munmap(target, syscall_ret_vaddr,
shellcode_stack_vaddr, SHELLCODE_STACK_SIZE);
return -1;
}
static int unmap_shellcode()
{
int err = 0;
err |= remote_munmap(target, syscall_ret_vaddr,
shellcode_text_vaddr, SHELLCODE_TEXT_SIZE);
err |= remote_munmap(target, syscall_ret_vaddr,
shellcode_stack_vaddr, SHELLCODE_STACK_SIZE);
return err;
}
static int spawn_shell_thread()
{
printf("[-] spawning a helper thread\n");
shell_tid = remote_clone(target, syscall_ret_vaddr,
CLONE_FILES | CLONE_FS | CLONE_IO | CLONE_SIGHAND |
CLONE_SYSVSEM | CLONE_THREAD | CLONE_VM,
shellcode_stack_vaddr + SHELLCODE_STACK_SIZE - 8);
if (!shell_tid)
return -1;
printf("[+] new suspended thread: %d\n", shell_tid);
return 0;
}
static int disable_clone_tracing_in_shell_thread()
{
int err = 0;
printf("[-] disabling tracing of suspended thread...\n");
err = clear_ptrace_options(shell_tid);
if (err)
goto out;
printf("[+] helper thread ready\n");
out:
return err;
}
static int detach_from_target()
{
printf("[-] detaching from process %d...\n", target);
return ptrace_detach(target);
}
static int detach_from_shell_thread()
{
printf("[-] detaching from helper %d...\n", shell_tid);
return ptrace_detach(shell_tid);
}
static int resume_target_thread()
{
printf("[-] resuming target...\n");
return resume_thread(target);
}
static int resume_shell_thread()
{
printf("[-] resuming helper thread...\n");
return resume_thread(shell_tid);
}
static int wait_for_shell_thread_enter()
{
printf("[-] waiting for helper thread start...\n");
return ignore_thread_stop(shell_tid);
}
static int wait_for_shell_thread_exit()
{
printf("[-] waiting for helper to exit...\n");
int exit_code = wait_for_process_exit(shell_tid);
if (exit_code < 0)
return -1;
printf("[+] shell thread exited with %d\n", exit_code);
return 0;
}
static int stop_target_thread()
{
printf("[-] stopping target thread...\n");
return stop_thread(target);
}
static int inject_thread()
{
int err;
printf("[-] attaching to process %d...\n", target);
err = ptrace_attach(target);
if (err)
goto out;
printf("[+] attached\n");
err = resolve_libdl_symbols();
if (err)
goto detach;
err = resolve_libpthread_symbols();
if (err)
goto detach;
err = locate_syscall_trampoline();
if (err)
goto detach;
err = prepare_shellcode();
if (err)
goto detach;
err = spawn_shell_thread();
if (err)
goto detach;
err = wait_for_shell_thread_enter();
if (err)
goto detach;
err = disable_clone_tracing_in_shell_thread();
if (err)
goto detach;
err = resume_target_thread();
if (err)
goto detach;
err = resume_shell_thread();
if (err)
goto detach;
err = wait_for_shell_thread_exit();
if (err)
goto detach;
err = stop_target_thread();
if (err)
goto detach;
unmap_shellcode();
err = detach_from_target();
if (err)
goto detach_shell;
printf("[+] we're done\n");
return 0;
detach:
ptrace_detach(target);
out:
return err;
detach_shell:
ptrace_detach(shell_tid);
return err;
}