diff --git a/README b/README index d759b17..ab98794 100644 --- a/README +++ b/README @@ -17,6 +17,7 @@ writing custom interactive Python GDB commands. - GDB 7.x - Python 2.6+ - Utilities: nasm, readelf, objdump + - Keystone engine as dependency (www.keystone-engine.org) 3. Installation - Download diff --git a/lib/config.py b/lib/config.py index 5704fc7..585fcf6 100644 --- a/lib/config.py +++ b/lib/config.py @@ -16,7 +16,6 @@ # external binaries, required for some commands READELF = "/usr/bin/readelf" OBJDUMP = "/usr/bin/objdump" -NASM = "/usr/bin/nasm" NDISASM = "/usr/bin/ndisasm" # PEDA global options diff --git a/lib/nasm.py b/lib/nasm.py index 13888f4..2c2eb52 100644 --- a/lib/nasm.py +++ b/lib/nasm.py @@ -16,45 +16,11 @@ class Nasm(object): """ - Wrapper class for assemble/disassemble using nasm/ndisassm + Wrapper class for disassemble using ndisassm """ def __init__(self): pass - @staticmethod - def assemble(asmcode, mode=32): - """ - Assemble ASM instructions using NASM - - asmcode: input ASM instructions, multiple instructions are separated by ";" (String) - - mode: 16/32/64 bits assembly - - Returns: - - bin code (raw bytes) - """ - if not os.path.exists(config.NASM): - error_msg("%s binary not found, please install NASM for asm/rop functions" % config.NASM) - raise UserWarning("missing requirement") - - asmcode = asmcode.strip('"').strip("'") - asmcode = asmcode.replace(";", "\n") - asmcode = ("BITS %d\n" % mode) + asmcode - asmcode = decode_string_escape(asmcode) - asmcode = re.sub("PTR|ptr|ds:|DS:", "", asmcode) - infd = tmpfile() - outfd = tmpfile(is_binary_file=True) - infd.write(asmcode) - infd.flush() - execute_external_command("%s -f bin -o %s %s" % (config.NASM, outfd.name, infd.name)) - infd.close() - - if os.path.exists(outfd.name): - bincode = outfd.read() - outfd.close() - return bincode - # reopen it so tempfile will not complain - open(outfd.name,'w').write('B00B') - return None - @staticmethod def disassemble(buf, mode=32): """ diff --git a/peda.py b/peda.py index f525d52..4db9b3f 100644 --- a/peda.py +++ b/peda.py @@ -19,6 +19,8 @@ import traceback import codecs +from keystone import * + # point to absolute path of peda.py PEDAFILE = os.path.abspath(os.path.expanduser(__file__)) if os.path.islink(PEDAFILE): @@ -746,7 +748,7 @@ def restore_session(self, filename=None): @memoized def assemble(self, asmcode, bits=None): """ - Assemble ASM instructions using NASM + Assemble ASM instructions using Keystone - asmcode: input ASM instructions, multiple instructions are separated by ";" (String) Returns: @@ -754,7 +756,19 @@ def assemble(self, asmcode, bits=None): """ if bits is None: (arch, bits) = self.getarch() - return Nasm.assemble(asmcode, bits) + + if bits == 16: + mode = KS_MODE_16 + elif bits == 32: + mode = KS_MODE_32 + else: + mode = KS_MODE_64 + + ks = Ks(KS_ARCH_X86, mode) + # turn on Nasm syntax to be backward compatible + ks.syntax = KS_OPT_SYNTAX_NASM + encoding, count = ks.asm(asmcode) + return ''.join(map(chr, encoding)) def disassemble(self, *arg): """