aboutsummaryrefslogtreecommitdiffstats
path: root/bin/disasm
blob: 30d237dbf7b340707a6b5dfe175c638fa1e65212 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3

import argparse
import code
import readline
import signal
import sys
from capstone import *
from capstone.x86 import *


def SigHandler_SIGINT(signum, frame):
    print()
    sys.exit(0)


class Argparser(object):
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--hex", type=str, help="the hex code")
        parser.add_argument("--sp", type=str, help="separator")
        parser.add_argument("--dbg", action="store_true",
                            help="debug", default=False)
        self.args = parser.parse_args()

# write code here


def premain(argparser):
    signal.signal(signal.SIGINT, SigHandler_SIGINT)
    # here
    code = bytes()
    if argparser.args.hex:
        if argparser.args.sp:
            hex_str = argparser.args.hex.split(argparser.args.sp)
        else:
            hex_str = argparser.args.hex.split(" ")
    code = [int(byte, 16) for byte in hex_str]
    md = Cs(CS_ARCH_X86, CS_MODE_64)
    for i in md.disasm(bytes(code), 0x0):
        print(hex(i.address).ljust(7), i.mnemonic.ljust(7), i.op_str)


def main():
    argparser = Argparser()
    if argparser.args.dbg:
        try:
            premain(argparser)
        except:
            variables = globals().copy()
            variables.update(locals())
            shell = code.InteractiveConsole(variables)
            shell.interact(banner="DEBUG REPL")
    else:
        premain(argparser)


if __name__ == "__main__":
    main()