forked from plasma-disassembler/plasma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.py
executable file
·82 lines (66 loc) · 2.56 KB
/
reverse.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
#!/usr/bin/env python3
#
# Reverse : Generate an indented asm code (pseudo-C) with colored syntax.
# Copyright (C) 2015 Joel
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
from lib import GlobalContext, AddrContext
from lib.utils import info, die
from lib.ui.vim import generate_vim_syntax
# Generates the file custom_colors.py at the beginning
import lib.colors
if __name__ == '__main__':
gctx = GlobalContext()
gctx.parse_args()
if gctx.color and lib.colors.VERSION < lib.colors.CURR_VERSION:
info("There is a new version of custom_colors.py. If you did any")
info("modifications you can delete it. Otherwise you can copy it")
info("somewhere, run again your command then merge the file at hand.")
die()
if gctx.interactive_mode:
from lib.ui.console import Console
i = Console(gctx)
elif gctx.filename is not None:
if not gctx.load_file():
die()
if gctx.list_sections:
for s in gctx.dis.binary.iter_sections():
s.print_header()
sys.exit(0)
if gctx.syms:
gctx.dis.print_symbols(gctx.sectionsname)
sys.exit(0)
ctx = gctx.get_addr_context(gctx.entry)
if ctx is None:
sys.exit(0)
if gctx.do_dump:
ctx.dump_asm(gctx.nb_lines).print()
sys.exit(0)
o = ctx.decompile()
if gctx.graph:
ctx.gph.dot_graph(gctx.dis.jmptables)
if o is not None:
if gctx.vim:
base = os.path.basename(gctx.filename) + "_" + gctx.entry
# re-assign if no colors
gctx.libarch.process_ast.assign_colors(ctx, ctx.ast)
gctx.color = False
generate_vim_syntax(ctx, base + ".vim")
sys.stdout = open(base + ".rev", "w+")
o.print()
if gctx.vim:
print("run : vim {0}.rev -S {0}.vim".format(base), file=sys.stderr)