-
Notifications
You must be signed in to change notification settings - Fork 1
/
PyASM.py
85 lines (55 loc) · 2.85 KB
/
PyASM.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import argparse
from argparse import RawTextHelpFormatter
from AsmExec import do_interactive, run_asm
# TODO: Create git repo and upload to github
# TODO: Add linux support
# TODO: Check what ctypes.cdll has for linux
DESCRIPTION = """
Play with ASM from python.
This program will assemble your ASM lines, create an executable buffer, and run your code.
Execution will start from the first assembly line, unless a start label is specifically given in the 'entry_point' arg.
I added some convenience 'cheat' functions. You can call these using 'call !funcname' .
Use --magic-funcs to see a list of available functions.
In addition, you can allocate and get a reference to a string by simply using 'mov reg, "str"'.
See the example ASM dir for examples of all this.
"""
def print_magic_help():
print """
You can call magic function at any point using 'call !func' (notice the bang before the function's name).
These function are either built-in function by me, or function exported by ctyptes.cdll .
To call cdll functions, you should use 'call !dllname.dllfunc' for instance, calling printf is 'call !msvcrt.printf'
Don't forget the calling convention is probably cdecl in these cases.
Available built-in funcs are:
- void debug(void) - Will use printf to dump the register state at the given point
You can find available cdll funcs by iterating over ctypes.cdll (for instance, try dir(ctypes.cdll.msvcrt)
"""
def main():
# Check for keystone installation:
try:
import keystone
except ImportError:
raise Exception("Keystone is required for assembling text. "
"You can get it from here: "
"http://www.keystone-engine.org/download/#python-module-for-windows---binaries--")
parser = argparse.ArgumentParser(description=DESCRIPTION, formatter_class=RawTextHelpFormatter)
parser.add_argument('-f', '--file', default=None,
help='File containing ASM text to run')
parser.add_argument('-e', '--entry-point', default=None,
help='Label to start execution from. '
'Note: If you use an entry point, make sure the first ret after that label is where the program ends. '
'Otherwise python will crash on return')
parser.add_argument('--magic-funcs', action='store_true',
help='Print help about using magic functions and exists')
parser.add_argument('-a', '--asm-args', nargs='+', default=[],
help='Arguments to pass to the assembly function')
args = parser.parse_args()
if args.magic_funcs:
print_magic_help()
sys.exit(0)
if args.file:
run_asm(open(args.file, 'r').readlines(), args.entry_point, *args.asm_args)
else:
do_interactive()
if __name__ == '__main__':
main()