Heap Exploitation Techniques in CSE 545 Fall 2020

CSE 545 F2020, Week 11
Heap: Fastbins & others
Tiffany Bao
tbao@asu.edu
 
Choose from the following menu:
0. [exit]
1. [m]alloc with size, e.g. m 2
2. [f]ree with index, e.g, f 1
3. [e]dit allocated chunk's content, e.g, e 2
4. [l]ist all pointers, e.g, l
1
2
tcache
3
Use After Free
Double Free
tcache
Overflow
4
Use After Free
Double Free
tcache
fastbin
unsorted bin
small bin
large bin
Overflow
5
Use After Free
Double Free
tcache
fastbin
unsorted bin
small bin
large bin
Overflow
Off-by-null
6
Use After Free
Double Free
tcache
fastbin
unsorted bin
small bin
large bin
Overflow
Off-by-null
tcache poisoning
fastbin reverse
house of spirit
house of force
Outline
×
Fastbin use-after-free (UAF) vulnerabilities
×
Fastbin double-free vulnerabilities
×
Leaking libc address
×
via unsorted bin
×
via program’s data segment
×
How to debug
7
fastbin reverse
Fastbin Use After Free
8
9
fd pointer
fd pointer
00000
10
fd pointer
fd pointer
000000
11
Identify Fake Chunk
A Fake Chunk Should Satisfy:
1.
Include the 
victim memory
2.
The fd pointer (start of
data) is 
0
 or points to
another chunk that will
ultimately with fd pointer as
0
12
Link Fake Chunk to Fastbin Freelist
1.
Find a use-after-free
vulnerability
2.
Edit the fd pointer
13
14
Fast Bin
fd pointer
3
malloc() x 7
15
Tcache Bin
Fast Bin
fd pointer
3
malloc() x 7
16
Tcache Bin
Fast Bin
fd pointer
4
malloc()
17
Tcache Bin
Fast Bin
fd pointer
4
malloc()
18
Tcache Bin
Fast Bin
4
malloc()
19
Tcache Bin
Fast Bin
4
malloc()
20
Tcache Bin
Fast Bin
4
malloc()
21
Tcache Bin
5
malloc()
Tcache Bin
__malloc__hook
×
“The GNU C Library lets you modify the behavior
of malloc, realloc, and free by specifying appropriate hook
functions. You can use these hooks to help you debug
programs that use dynamic memory allocation, 
×
__malloc_hook: 
The value of this variable is a pointer to
the function that malloc uses whenever it is called.
×
__free_hook, __realloc_hook
22
https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html
Heap is fun!
Service IP:  107.21.135.41              Port: 1
5555
Service file:
×
https://cse545.tiffanybao.com/labs/week1
5
/
use_after
_free.c
×
https://cse545.tiffanybao.com/labs/week1
5
/
use_after
_free
×
https://cse545.tiffanybao.com/labs/week1
5
/libc.so.6
×
https://cse545.tiffanybao.com/labs/week1
5
/ld-2.27.so
ASLR
 
is
 
off,
 
libc’s
 
base
 
address:
 
0x7ffff79e4000
23
24
fd pointer
fd pointer
000000
0000000
25
Fast Bin
fd pointer
fd pointer
00000000
3
malloc() x 7
__malloc_hook
__malloc_hook-0x10
26
Fast Bin
fd pointer
fd pointer
00000000
3
malloc() x 7
__malloc_hook
__malloc_hook-0x10
Tcache Bin
27
Fast Bin
fd pointer
fd pointer
00000000
__malloc_hook
__malloc_hook-0x10
Tcache Bin
4
malloc()
28
Tcache Bin
Fast Bin
4
malloc()
29
Tcache Bin
Fast Bin
5
malloc()
30
Tcache Bin
Fast Bin
5
malloc()
31
2
fd pointer
fd pointer
000000
0000000
32
Tcache Bin
Fast Bin
5
malloc()
How to debug
33
check video
 
34
Fastbin double Free
35
36
Tcache Bin
fd pointer
fd pointer
37
Tcache Bin
fd pointer
fd pointer
Fast Bin
38
Fast Bin
39
Fast Bin
1
Empty tcache bins
40
Fast Bin
Tcache Bin
1
Empty tcache bins
41
Fast Bin
Tcache Bin
2
malloc()
42
Fast Bin
Tcache Bin
2
malloc()
43
Fast Bin
Tcache Bin
2
malloc()
44
Fast Bin
Tcache Bin
2
malloc()
data
metadata
00000000
45
Fast Bin
Tcache Bin
2
malloc()
data
metadata
00000000
00000000
46
Fast Bin
Tcache Bin
2
malloc()
data
metadata
fd pointer
00000000
00000000
47
Fast Bin
Tcache Bin
2
malloc()
fd pointer
48
Tcache Bin
3
Edit the chunk from Step 2
fd pointer
victim
49
Tcache Bin
4
malloc() x 3
fd pointer
victim
Leak Libc
Base Address
50
 
 
51
Approach 1: find a fake chunk with libc info
×
Find a memory location
that satisfy the necessary
condition for a fake chunk
×
The libc information is
contained in the fake
chunk
×
e.g., 0x602248
52
53
fd pointer
fd pointer
000000
libc-related
address
54
Fast Bin
fd pointer
fd pointer
00000000
3
malloc() x 7
55
Fast Bin
fd pointer
fd pointer
00000000
3
malloc() x 7
Tcache Bin
56
Fast Bin
fd pointer
fd pointer
00000000
Tcache Bin
4
malloc()
57
Tcache Bin
Fast Bin
4
malloc()
Disadvantage
×
Finding a fake chunk like such may not be easy
×
The address of fake chunk may also change
×
E.g., PIE and ASLR
58
Approach 2: unsorted bins
->
 Forward
<-
 Backward
59
siz0x20
chunk
chunk
BK
FD
arena -> bins
Unsorted Bin with one chunk
60
main_arena @ glibc
->
 Forward
 
 
61
Review
62
 
63
Reverse Engineering
Pwn
Stack
Heap
miscellaneous
Reverse Engineering
×
Goal: Understand the semantics of a program
×
Functions may be stripped --- no function name
×
Tool:
×
Static: Disassembler and Decompiler
×
IDA, Ghidra
×
Dynamic:
×
gdb (pwndbg, gef)
64
Pwning --- miscellaneous
×
Command Line Injection
   sprintf(command, “cd %s”, user_input);
   system(command);
×
Directory Traversal
   sprintf(directory, “../%s/%s”, user_input);
         
fopen(user_input, “r”);
65
pwning --- Memory corruption
×
Goal: Arbitrary execution
×
Approach: Take advantage 
a vulnerability
 to overwrite an
instruction pointer
 with 
malicious code pointer
66
pwning
×
Instruction pointer:
×
Overwrite saved return address
×
Overwrite GOT table
×
Overwrite hook functions in glibc (e.g., __malloc_hook)
67
pwning
×
Malicous code pointer:
×
the winning function
×
shellcode
×
ROP gadget
68
Vulnerabilities
×
Stack overflow
69
rbp ->
rsp ->
Overwrite
malicious instruction
rbp + 8 ->
Vulnerabilities
×
Format String
Goal: 
[0x555555554708] = 1000
char buf[] =
“%0200x%0200x%0200x%0200x%020
0x
%11$n
\n” + 
“padd”
 +
little_end(0x555555554708);
70
rbp ->
rsp ->
rbp + 8 ->
Vulnerabilities
×
Tcache use after free
71
size
0x20
size
0x30
size
size
0x410
chunk
chunk
victim
Vulnerabilities
×
Tcache double free
72
size
0x20
size
0x30
size
size
0x410
chunk1
chunk1
In use
victim
Vulnerabilities
×
Tcache overflow
73
size
0x20
size
0x30
size
size
0x410
chunk1
victim
chunk0
chunk1
Vulnerabilities
×
Fastbin use after free
74
Vulnerabilities
×
Fastbin double free
75
Fast Bin
Defense
×
Stack Canary
×
NX
×
ASLR
×
PIE
76
Stack Canary
×
Additional bytes in stack
×
Week 6
×
Leak stack canary
×
Overwrite predefined canary
×
Overwrite function 
__stack_chk_fail
77
NX
×
No stack execution
×
the winning function: Yes
×
shellcode: 
No
×
ROP gadget: Yes
78
PIE
×
Position-independent code
×
Code will become offset
×
So does GOT
×
Winning function: 
Require the base of the code segment, but it
won’t change
×
Shellcode: 
Yes
×
ROP gadget: 
Require the base of libc, but it won’t change
79
ASLR
×
The base of code and data will be randomized
×
Winning function: 
Require the base of code segment
×
Shellcode: 
Require the base of stack
×
ROP gadget: 
Require the base of libc
80
pwning
×
Instruction pointer:
×
Overwrite saved return address
×
Overwrite GOT table
×
Overwrite hook functions in glibc (e.g., __malloc_hook)
81
82
Slide Note

https://sourceware.org/glibc/wiki/MallocInternals

https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/

https://www.bencode.net/posts/2019-10-19-heap-overflow/

https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/

Embed
Share

This collection of images covers various heap exploitation techniques discussed in CSE 545 Fall 2020, such as fastbin use-after-free vulnerabilities, tcache poisoning, double-free exploits, metadata manipulation, and more. The images depict scenarios involving tcache, fast bins, unsorted bins, and fake chunks to demonstrate how vulnerabilities like use-after-free can be exploited for malicious purposes.

  • Heap Exploitation
  • Vulnerabilities
  • CSE 545
  • Fall 2020
  • Memory Corruption

Uploaded on Oct 06, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSE 545 F2020, Week 11 Heap: Fastbins& others Tiffany Bao tbao@asu.edu

  2. tcache 2

  3. tcache Use After Free Double Free Overflow 3

  4. tcache Use After Free fastbin unsorted bin Double Free small bin Overflow large bin Arena 4

  5. tcache Off-by-null Use After Free fastbin unsorted bin Double Free small bin Overflow large bin Arena 5

  6. tcache poisoning tcache Off-by-null fastbin reverse Use After Free fastbin house of spirit unsorted bin Double Free small bin house of force Overflow large bin Arena 6

  7. Fastbin use-after-free (UAF) vulnerabilities Fastbin double-free vulnerabilities fastbin reverse Leaking libc address via unsorted bin via program s data segment How to debug 7

  8. FastbinUse After Free 8

  9. Tcache Bin metadata fd pointer metadata fd pointer 00000 data data Fast Bin metadata fd pointer metadata fd pointer metadata 00000 data data data 9

  10. Fast Bin metadata fd pointer metadata fd pointer metadata 000000 data data data 1 metadata fd pointer 2 0000 victim (malloc@got) Fake Chunk 10

  11. metadata 00000000 1 A Fake Chunk Should Satisfy: 1. Include the victim memory 2. The fd pointer (start of data) is 0 or points to another chunk that will ultimately with fd pointer as 0 victim (malloc@got) Fake Chunk metadata fd pointer 0000 victim (malloc@got) Fake Chunk 12

  12. metadata fd pointer 1. Find a use-after-free vulnerability Edit the fd pointer data 2. metadata fd pointer 2 victim (malloc@got) Fake Chunk 13

  13. malloc() x 7 Tcache Bin 3 metadata fd pointer metadata fd pointer 00000 data data Fast Bin metadata fd pointer metadata fd pointer 0000 victim data (malloc@got) 14

  14. Tcache Bin malloc() x 7 3 Fast Bin metadata fd pointer metadata fd pointer 0000 victim data (malloc@got) 15

  15. Tcache Bin malloc() 4 Fast Bin metadata fd pointer metadata fd pointer 0000 victim data (malloc@got) 16

  16. Tcache Bin malloc() 4 Fast Bin metadata fd pointer metadata fd pointer 0000 victim data (malloc@got) 17

  17. Tcache Bin malloc() 4 Fast Bin metadata fd pointer 0000 victim (malloc@got) 18

  18. Tcache Bin malloc() 4 metadata fd pointer victim (malloc@got) Fast Bin 0000 19

  19. Tcache Bin malloc() 4 metadata fd pointer victim (malloc@got) Fast Bin 0000 20

  20. Tcache Bin malloc() 5 metadata fd pointer 0000 victim (malloc@got) Tcache Bin metadata fd pointer victim (malloc@got) 21

  21. The GNU C Library lets you modify the behavior of malloc, realloc, and free by specifying appropriate hook functions. You can use these hooks to help you debug programs that use dynamic memory allocation, __ __malloc_hook malloc_hook : : The value of this variable is a pointer to the function that malloc uses whenever it is called. __free_hook, __realloc_hook https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html 22

  22. Service IP: 107.21.135.41 Port: 15555 Service file: https://cse545.tiffanybao.com/labs/week15/use_after_free.c https://cse545.tiffanybao.com/labs/week15/use_after_free https://cse545.tiffanybao.com/labs/week15/libc.so.6 https://cse545.tiffanybao.com/labs/week15/ld-2.27.so ASLR is off, libc s base address: 0x7ffff79e4000 23

  23. How to debug 33

  24. 34

  25. Fastbindouble Free 35

  26. Tcache Bin metadata fd pointer metadata fd pointer data data 36

  27. Tcache Bin metadata fd pointer metadata fd pointer data data Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 37

  28. Tcache Bin metadata fd pointer metadata fd pointer 00000 data data Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 38

  29. Empty tcache bins Tcache Bin 1 metadata fd pointer metadata fd pointer 00000 data data Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 39

  30. Empty tcache bins Tcache Bin 1 Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 40

  31. Tcache Bin malloc() 2 Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 41

  32. Tcache Bin malloc() 2 Fast Bin metadata fd pointer metadata fd pointer metadata fd pointer data data data 42

  33. Tcache Bin malloc() 2 Fast Bin metadata fd pointer metadata fd pointer data data 43

  34. Tcache Bin malloc() 2 metadata 00000000 data Fast Bin metadata fd pointer data 44

  35. Tcache Bin malloc() 2 metadata 00000000 data Fast Bin metadata fd pointer metadata 00000000 data data 45

  36. Tcache Bin malloc() 2 metadata fd pointer metadata 00000000 data data Fast Bin metadata 00000000 data 46

  37. metadata fd pointer Tcache Bin malloc() 2 data metadata fd pointer metadata fd pointer metadata fd pointer data data data Fast Bin 47

  38. metadata fd pointer Edit the chunk from Step 2 Tcache Bin 3 data metadata fd pointer metadata fd pointer metadata fd pointer data data data metadata victim Fake Chunk 48

  39. Tcache Bin malloc() x 3 4 metadata fd pointer metadata fd pointer metadata fd pointer data data data metadata victim Fake Chunk 49

  40. Leak Libc Base Address 50

  41. 51

  42. Find a memory location that satisfy the necessary condition for a fake chunk The libc information is contained in the fake chunk e.g., 0x602248 52

  43. Fast Bin metadata fd pointer metadata fd pointer metadata 000000 data data data 1 0x602248 metadata 2 libc-related address Fake Chunk 53

  44. Tcache Bin malloc() x 7 3 metadata fd pointer metadata fd pointer 00000 data data Fast Bin 0x602248 metadata 00000000 metadata fd pointer metadata fd pointer data victim data 54

  45. Tcache Bin malloc() x 7 3 Fast Bin 0x602248 metadata 00000000 metadata fd pointer metadata fd pointer data victim data 55

  46. Tcache Bin malloc() 4 Fast Bin 0x602248 metadata 00000000 metadata fd pointer metadata fd pointer data victim data 56

  47. Tcache Bin malloc() 4 0x602248 metadata fd pointer metadata 00000000 data victim Fast Bin 57

  48. Finding a fake chunk like such may not be easy The address of fake chunk may also change E.g., PIE and ASLR 58

  49. arena -> bins siz0x20 FD BK -> Forward <- Backward chunk chunk 59

  50. metadata fd pointer bw pointer -> Forward metadata fd pointer bw pointer data main_arena @ glibc 60

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#