2015-12-15 23:05:18 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2017-11-19 15:53:15 +00:00
|
|
|
import re
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
from Emulator import Emulator, CodeParseException, REGISTERS
|
2015-12-17 00:16:01 +00:00
|
|
|
from TikzPainter import TikzPainter
|
2017-11-19 15:53:15 +00:00
|
|
|
import Junk
|
|
|
|
|
|
|
|
def parse_args ():
|
|
|
|
parser = argparse.ArgumentParser(description="For fun x86-64 emulator and stack visualizer.")
|
2017-11-19 15:58:11 +00:00
|
|
|
parser.add_argument('-i', '--input-file', default='-', help = '.S file to use as input', dest = 'input-file')
|
|
|
|
parser.add_argument('-o', '--output-file', default='-', help = '.S file to use as output', dest = 'output-file')
|
2017-11-19 15:53:15 +00:00
|
|
|
for register in REGISTERS:
|
|
|
|
parser.add_argument('--'+register[1:], nargs = 1, type = int, default = [Junk.Junk()], dest = register)
|
|
|
|
args = vars(parser.parse_args())
|
|
|
|
|
|
|
|
# Determine args #
|
|
|
|
registers_init = {}
|
|
|
|
for register in REGISTERS:
|
|
|
|
registers_init[register[1:]] = args[register][0]
|
|
|
|
registers_init['rip'] = 0
|
|
|
|
registers_init['rsp'] = 0
|
|
|
|
registers_init['rbp'] = Junk.Junk('old bp')
|
|
|
|
|
2017-11-19 15:58:11 +00:00
|
|
|
#
|
2017-11-19 15:53:15 +00:00
|
|
|
program = ""
|
2017-11-19 15:58:11 +00:00
|
|
|
if args['input-file'] == '-':
|
2017-11-19 15:53:15 +00:00
|
|
|
program = sys.stdin.read()
|
|
|
|
else:
|
2017-11-19 15:58:11 +00:00
|
|
|
with open(args['input-file']) as file:
|
2017-11-19 15:53:15 +00:00
|
|
|
program = file.read()
|
|
|
|
|
2017-11-19 15:58:11 +00:00
|
|
|
# Output file:
|
|
|
|
output_file = None
|
|
|
|
if args['output-file'] == '-':
|
|
|
|
output_file = sys.stdout
|
|
|
|
else:
|
|
|
|
output_file = open(args['output-file'], 'w')
|
|
|
|
|
|
|
|
##
|
|
|
|
|
|
|
|
return (program, registers_init, output_file)
|
2017-11-19 15:53:15 +00:00
|
|
|
|
|
|
|
def main ():
|
|
|
|
|
2017-11-19 15:58:11 +00:00
|
|
|
(program, registers_init, output_file) = parse_args()
|
2017-11-19 15:53:15 +00:00
|
|
|
|
|
|
|
# Determine registers to display
|
|
|
|
registers_to_draw = ["%rip","%rbp","%rsp", ""]
|
|
|
|
for match in re.findall(r"%r[a-z0-9]{1,2}", program):
|
|
|
|
if match not in registers_to_draw:
|
|
|
|
registers_to_draw.append(match)
|
|
|
|
|
|
|
|
# Setup emulator and drawer
|
|
|
|
emu = Emulator(program)
|
|
|
|
emu.setRegs(**registers_init)
|
|
|
|
emu.setStack(Junk.Junk("junk..."), Junk.Junk("calling eip"))
|
|
|
|
|
|
|
|
painter = TikzPainter(registers_to_draw)
|
|
|
|
|
|
|
|
# Iteratively draw states.
|
|
|
|
painter.drawState(emu)
|
|
|
|
for line_nr in emu:
|
|
|
|
painter.drawState(emu, line_nr)
|
|
|
|
painter.drawNames(emu)
|
|
|
|
|
|
|
|
# Display result
|
2017-11-19 15:58:11 +00:00
|
|
|
output_file.write(painter.to_string())
|
|
|
|
output_file.close()
|
2015-12-16 23:52:47 +00:00
|
|
|
|
2015-12-17 00:16:01 +00:00
|
|
|
if __name__ == "__main__":
|
2017-11-19 15:53:15 +00:00
|
|
|
main()
|
|
|
|
|
|
|
|
|