#!/usr/bin/env python2 ## -*- coding: utf-8 -*- ## ## Jonathan Salwan - 2014-05-13 ## Florian Meier - 2014-08-31 - The 64b ROP chain generation ## ## http://shell-storm.org ## http://twitter.com/JonathanSalwan ## import re from capstone import * class ROPMakerX64(object): def __init__(self, binary, gadgets, paddingLen, liboffset=0x0): self.__binary = binary self.__gadgets = gadgets self.paddingLen = paddingLen # 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 = re.search("mov .* ptr \[(?P([(rax)|(rbx)|(rcx)|(rdx)|(rsi)|(rdi)|(r9)|(r10)|(r11)|(r12)|(r13)|(r14)|(r15)]{3}))\], (?P([(rax)|(rbx)|(rcx)|(rdx)|(rsi)|(rdi)|(r9)|(r10)|(r11)|(r12)|(r13)|(r14)|(r15)]{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 print("# [+] Gadget found: 0x%x %s" %(gadget["vaddr"], gadget["gadget"])) 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 if g != "ret": # we need this to filterout 'ret' instructions with an offset like 'ret 0x6', because they ruin the stack pointer if g.split()[0] == "ret" and g.split()[1] != "": raise print("# [+] Gadget found: 0x%x %s" %(gadget["vaddr"], gadget["gadget"])) return gadget except: continue return None def __padding(self, gadget, regAlreadSetted): lg = gadget["gadget"].split(" ; ") for g in lg[1:]: if g.split()[0] == "pop": reg = g.split()[1] try: print("p += pack('