From 47770120ac96a69832f8db26a2c2091624160316 Mon Sep 17 00:00:00 2001 From: AceVest Date: Thu, 21 Jul 2016 23:05:39 +0800 Subject: [PATCH] add asm2scs.py --- tools/hack/asm2scs.py | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 tools/hack/asm2scs.py diff --git a/tools/hack/asm2scs.py b/tools/hack/asm2scs.py new file mode 100755 index 0000000..38ae6fc --- /dev/null +++ b/tools/hack/asm2scs.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ------------------------------------------------------------------------ +# File Name: asm2scs.py +# Author: Zhao Yanbai +# 2016-07-21 22:08:07 Thursday CST +# Description: asm to shellcode string +# ------------------------------------------------------------------------ + +import os +import sys +import argparse +import commands +import re + +gArgs = None + + +def isValidAddr(addr) : + for c in addr : + if c not in set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']) : + return False + + + return True + +def getShellCode(asmpath) : + sc = '' + try : + cmd = 'nasm -f macho {0} -o {0}.o;otool -tjV {0}.o'.format(asmpath) + print cmd + output = commands.getoutput(cmd) + lines = [line.strip() for line in output.splitlines()] + for line in lines : + d = line.split('\t') + if len(d) < 3 or not isValidAddr(d[0]): + continue + + sc += d[1].strip().upper() + + except Exception, e: + print("Exception: {0}".format(e)) + + return sc + + +def formatShellCode(sc) : + fsc = '' + if len(sc) % 2 != 0 : + print("Invalid ShellCode") + else : + while sc != '' : + fsc += '\\x' + sc[:2] + sc = sc[2:] + + print fsc + return fsc + +def parseArguments() : + global gArgs + parser = argparse.ArgumentParser(prog='asm2scs', description='asm to shellcode string', epilog='') + parser.add_argument('-a', '--asm', action='store', default='', help='asm file path') + parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1') + gArgs = parser.parse_args() + + + if not gArgs.asm or gArgs.asm == '' : + print("invalid asm file path") + return False + + return True + +def main() : + if not parseArguments() : + return False + + + sc = getShellCode(gArgs.asm) + formatShellCode(sc) + + +if __name__ == "__main__" : + main() -- 2.44.0