forked from williballenthin/python-idb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_ida_script.py
More file actions
91 lines (76 loc) · 2.66 KB
/
run_ida_script.py
File metadata and controls
91 lines (76 loc) · 2.66 KB
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
86
87
88
89
90
91
#!/usr/bin/env python3
"""
some documentation
author: Willi Ballenthin
email: [email protected]
"""
import argparse
import logging
import os.path
import shlex
import sys
import idb
import idb.shim
logger = logging.getLogger(__name__)
def main(argv=None):
# TODO: do version check for 3.x
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Dump an IDB B-tree to a textual representation."
)
parser.add_argument(
"script_path",
type=str,
help="""Path to script file.
Command line arguments can be passed using quotes:
"myscrypt.py arg1 arg2 "arg3 arg3""
""",
)
parser.add_argument("idbpath", type=str, help="Path to input idb file")
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable debug logging"
)
parser.add_argument(
"-q", "--quiet", action="store_true", help="Disable all output but errors"
)
parser.add_argument("--ScreenEA", type=str, help="Prepare value of ScreenEA()")
args = parser.parse_args(args=argv)
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
elif args.quiet:
logging.basicConfig(level=logging.ERROR)
logging.getLogger().setLevel(logging.ERROR)
logging.getLogger("idb.netnode").setLevel(logging.ERROR)
logging.getLogger("idb.fileformat").setLevel(logging.ERROR)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger().setLevel(logging.INFO)
logging.getLogger("idb.netnode").setLevel(logging.ERROR)
logging.getLogger("idb.fileformat").setLevel(logging.ERROR)
with idb.from_file(args.idbpath) as db:
if args.ScreenEA:
if args.ScreenEA.startswith("0x"):
screenea = int(args.ScreenEA, 0x10)
else:
screenea = int(args.ScreenEA)
else:
screenea = list(sorted(idb.analysis.Segments(db).segments.keys()))[0]
hooks = idb.shim.install(db, ScreenEA=screenea)
script_args = shlex.split(args.script_path)
# update sys.path to point to directory containing script.
# so scripts can import .py files in the same directory.
script_dir = os.path.dirname(script_args[0])
sys.path.insert(0, script_dir)
# update idc.ARGV
hooks["idc"].ARGV = script_args
with open(script_args[0], "rb") as f:
g = {
"__name__": "__main__",
}
g.update(hooks)
exec(f.read(), g)
return 0
if __name__ == "__main__":
sys.exit(main())