Last active
May 16, 2020 00:06
-
-
Save wumb0/34622258d29598d8ec9ad5e4e5a95c89 to your computer and use it in GitHub Desktop.
assembler in a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''asm console via keystone for python 2.7 | |
pip install keystone-engine | |
python asmconsole.py -a ARM -m LITTLE_ENDIAN -f escape -b 0x000086e4 | |
Little endian arm print escape codes and make base address 0x000086e4 | |
''' | |
from __future__ import print_function | |
import keystone | |
import argparse | |
from sys import exit | |
import cmd | |
formats = ["hex", "escape", "int"] | |
class AsmCmd(object, cmd.Cmd): | |
def __init__(self, k, args): | |
cmd.Cmd.__init__(self) | |
self.args = args | |
self.do_base(args.base_address) | |
self.k = k | |
self.prompt = ">> " | |
self.do_status("") | |
def do_status(self, i): | |
'Show arch, mode, format, and base address' | |
print("Arch: {}, Mode: {}, Format: {}, Base Address: {}".format(args.arch, args.mode, args.format, hex(args.base_address))) | |
def do_format(self, i): | |
'Change the output format.' | |
if i not in formats: | |
print("Invalid format must be one of: {}".format(', '.join(formats))) | |
self.args.format = i | |
def do_base(self, i): | |
'Change the base address' | |
self.args.base_address = int(i, 0) | |
def emptyline(self): | |
pass | |
def do_quit(self, i): | |
raise SystemExit | |
def do_exit(self, i): | |
self.do_quit(i) | |
do_EOF = do_exit | |
def default(self, ins): | |
try: | |
for i in self.k.asm(ins, self.args.base_address)[0]: | |
if self.args.format == "hex": | |
print(hex(i), end=" ") | |
elif self.args.format == "int": | |
print(i, end=" ") | |
elif self.args.format == "escape": | |
print("\\x{:02x}".format(i), end="") | |
print("") | |
except keystone.KsError: | |
print("Invalid instruction") | |
if __name__ == '__main__': | |
p = argparse.ArgumentParser() | |
p.add_argument("--arch", "-a", default="X86") | |
p.add_argument("--mode", "-m", default="32") | |
p.add_argument("--list-archs", "-A", action="store_true") | |
p.add_argument("--list-modes", "-M", action="store_true") | |
p.add_argument("--base-address", "-b", default="0") | |
p.add_argument("--format", "-f", choices=formats, default="escape") | |
args = p.parse_args() | |
e = 0 | |
archs = [i[8:] for i in dir(keystone) if i.startswith("KS_ARCH_")] | |
modes = [i[8:] for i in dir(keystone) if i.startswith("KS_MODE_")] | |
if args.list_archs: | |
print("Supported architectures: ") | |
print("\t" + ', '.join(archs)) | |
e = 1 | |
if args.list_modes: | |
print("Supported modes: ") | |
print("\t" + ', '.join(modes)) | |
e = 1 | |
try: | |
arch = getattr(keystone, "KS_ARCH_" + args.arch) | |
except AttributeError: | |
print("Invalid arch " + args.arch) | |
e = 2 | |
try: | |
mode = getattr(keystone, "KS_MODE_" + args.mode) | |
except AttributeError: | |
print("Invalid mode " + args.mode) | |
e = 2 | |
try: | |
k = keystone.Ks(arch, mode) | |
except keystone.KsError as err: | |
if err.errno == keystone.KS_ERR_MODE: | |
print("Invalid arch-mode combination: {} & {}".format(args.arch, args.mode)) | |
else: | |
print(err.errno) | |
e = 2 | |
if e: | |
exit(e) | |
asmcmd = AsmCmd(k, args) | |
asmcmd.cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment