Heap - Fastbin Dup
Introduction
Vulnerable Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// compile with
// gcc -no-pie -Wl,-z,norelro -z now -ggdb test.c -o test
// Get this code from https://www.bordergate.co.uk/heap-fastbin-exploitation/
struct test {
char test[30];
} tests;
struct user {
char username[16];
char target[16];
} users;
int main () {
strcpy( users.target, "TARGET");
char *m_array [8];
m_array[0] = (char *)0x0;
m_array[1] = (char *)0x0;
m_array[2] = (char *)0x0;
m_array[3] = (char *)0x0;
m_array[4] = (char *)0x0;
m_array[5] = (char *)0x0;
m_array[6] = (char *)0x0;
m_array[7] = (char *)0x0;
setvbuf(stdout,(char *)0x0,2,0);
printf("Enter username: ");
read(STDIN_FILENO, users.username, 0x10);
int i;
int ChunkNumber = 0;
for (i = 1; i < 20; ++i)
{
int selection;
printf( "Target : %s\n", users.target);
printf("Next chunk number: %d/7 \n", ChunkNumber);
printf("1) malloc\n");
printf("2) free\n");
printf("3) quit\n");
printf(">");
scanf("%d", &selection);
fflush (stdin);
switch(selection){
int mallocSize;
char inputData[256];
case 1:
printf ("malloc size: \n");
scanf("%d", &mallocSize);
printf ("input data: \n");
scanf("%s", inputData);
// Allocate heap memory chunk. Size based on previous user input
char *heapChunk;
m_array[ChunkNumber] = (char *) malloc(mallocSize);
strcpy(m_array[ChunkNumber],inputData);
printf("chunk allocated: %d/7 \n", ChunkNumber);
ChunkNumber++;
break;
case 2:
printf("Select chunk to free: ");
scanf("%d", &selection);
printf("Freeing chunk: %d\n", selection);
free(m_array[selection]);
break;
case 3:
exit(0);
break;
default:
printf("Invalid selection\n");
break;
}
}
return(0);
}Target Code Analysis
Understanding Fastbins
What are Fastbins?
Fastbin Structure
Exploitation Steps
Step 1: Initial Setup

Step 2: Allocate Chunks in Fastbin Range


Step 3: Create Fastbin Dup

Step 4: Arbitrary Write Setup


Step 5: Overwrite Target


Code Execution
Detailed Breakdown
1. Chunk Size: 0x68 (104 bytes)
0x68 (104 bytes)2. Offset: -35 bytes
-35 bytes

3. Payload Size: 0x13 (19 bytes)
0x13 (19 bytes)4. One_gadget: 0xe1fa1
0xe1fa1

pwndbg Commands Cheat Sheet
Conclusion
References
Last updated

