Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Nasm assembler with Keystone engine #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 1 addition & 35 deletions lib/nasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
14 changes: 13 additions & 1 deletion peda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -754,7 +756,17 @@ def assemble(self, asmcode, bits=None):
"""
if bits is None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an helper function instead of putting everything here.

(arch, bits) = self.getarch()
return Nasm.assemble(asmcode, bits)

if bits == 16:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about elif instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know, but this is simple enough that i did not bother.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now

mode = KS_MODE_16
if bits == 32:
mode = KS_MODE_32
if bits == 64:
mode = KS_MODE_64

ks = Ks(KS_ARCH_X86, mode)
encoding, count = ks.asm(asmcode)
return encoding

def disassemble(self, *arg):
"""
Expand Down