Intro to Binary Exploitation
(pwn)
The Stack
Stack Basics
Call Stack Basics
Buffer Overflow
(bof)
Buffer Overflow
… why not just use strings?
ok so what?
ok so what?
how did that happen?
we can do more!
ok so what?
making pwn easier - pwntools
python3 -m pip install pwntools
pwntools basics
making pwn easier - gef
INSTALL:� bash -c "$(wget https://gef.blah.cat/sh -O -)"
gdb/gef basics - where do I set my breakpoint?
gdb/gef basics - where do I set my breakpoint?
gdb/gef basics - how do I find the offset?
gdb/gef basics - how do I find the offset?
buffer overflow on the stack is powerful
buffer overflow can also mean reading data
Shellcode
sally sells seashells by the sea shore sally sells seashells by the sea shore sally sells seashells by the
executable stack???
writing (and understanding shellcode)
example shellcode for running /bin/sh
calling execve - explained
example shellcode stack
luckily - we can steal shellcode!
how to exploit?
what if I don't know where my shellcode will execute?
sometimes shellcode is a little more complicated
pro tips
ROP Gadgets
Rop Rop Rop - Rop to the Top
Basic Buffer Overflow is limited
interesting_function is rare
can't we just return to assembly?
memory is never write + execute
gef➤ vmmap
[ Legend: Code | Heap | Stack ]
Start End Offset Perm Path
0x0000555555554000 0x0000555555555000 0x0000000000000000 r-- /my-program
0x0000555555555000 0x0000555555556000 0x0000000000001000 r-x /my-program
0x0000555555556000 0x0000555555557000 0x0000000000002000 r-- /my-program
0x0000555555557000 0x0000555555558000 0x0000000000002000 r-- /my-program
0x0000555555558000 0x0000555555559000 0x0000000000003000 rw- /my-program
0x0000555555559000 0x000055555557a000 0x0000000000000000 rw- [heap]
0x00007ffff7dbc000 0x00007ffff7dde000 0x0000000000000000 r-- /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007ffff7dde000 0x00007ffff7f56000 0x0000000000022000 r-x /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007ffff7f56000 0x00007ffff7fa4000 0x000000000019a000 r-- /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007ffff7fa4000 0x00007ffff7fa8000 0x00000000001e7000 r-- /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007ffff7fa8000 0x00007ffff7faa000 0x00000000001eb000 rw- /usr/lib/x86_64-linux-gnu/libc-2.31.so
0x00007ffff7faa000 0x00007ffff7fb0000 0x0000000000000000 rw-
0x00007ffff7fca000 0x00007ffff7fce000 0x0000000000000000 r-- [vvar]
0x00007ffff7fce000 0x00007ffff7fcf000 0x0000000000000000 r-x [vdso]
0x00007ffff7fcf000 0x00007ffff7fd0000 0x0000000000000000 r-- /usr/lib/x86_64-linux-gnu/ld-2.31.so
0x00007ffff7fd0000 0x00007ffff7ff3000 0x0000000000001000 r-x /usr/lib/x86_64-linux-gnu/ld-2.31.so
0x00007ffff7ff3000 0x00007ffff7ffb000 0x0000000000024000 r-- /usr/lib/x86_64-linux-gnu/ld-2.31.so
0x00007ffff7ffc000 0x00007ffff7ffd000 0x000000000002c000 r-- /usr/lib/x86_64-linux-gnu/ld-2.31.so
0x00007ffff7ffd000 0x00007ffff7ffe000 0x000000000002d000 rw- /usr/lib/x86_64-linux-gnu/ld-2.31.so
0x00007ffff7ffe000 0x00007ffff7fff000 0x0000000000000000 rw-
0x00007ffffffdd000 0x00007ffffffff000 0x0000000000000000 rw- [stack]
Binary |
Shared Libraries |
Dynamic Loader |
DEP means no jumping to shellcode
do we need new instructions?
Useful Info - The GOT & PLT
$ ROPgadget --binary prog
Gadgets information
============================================================
0x00000000004010bd : add ah, dh ; nop ; endbr64 ; ret
0x00000000004010eb : add bh, bh ; loopne 0x401155 ; nop ; ret
...
0x000000000040124c : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040124e : pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000401250 : pop r14 ; pop r15 ; ret
0x0000000000401252 : pop r15 ; ret
0x000000000040124b : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040124f : pop rbp ; pop r14 ; pop r15 ; ret
0x000000000040115d : pop rbp ; ret
0x0000000000401253 : pop rdi ; ret
0x0000000000401251 : pop rsi ; pop r15 ; ret
0x000000000040124d : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040101a : ret
...
quick example…
…AAAAAAAA |
0x401253 |
42 |
0x401251 |
1337 |
(don't care) |
function |
… |
how do I exploit this?
short aside on stack alignment
Return to libc
(ret2libc)
We are almost there!
Where are the library functions?
Why can't we just call system?
How can we use the PLT & GOT?
How can we do it?
Two-step attack plan:
Step 1: libc leak
Step 2: returning into libc
Example Ret2Libc
Format Strings
(printf)
printf primer
Hi my name is adam
100 64
0x55d2cd15a2a0 adam
man 3 printf
User-controlled format string
Stack Leaking
Memory Writing
adam thonk
4
How to get the pointer?
What to overwrite?
Be creative!
how do we exploit?
Binary Security
checksec
NX (remember this?)
RELRO (remember this?)
PIE
Stack Canaries (Stack Cookies)
Final Thoughts
Final Thoughts
Resources