forked from williballenthin/python-idb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_scripts.py
More file actions
62 lines (50 loc) · 2.01 KB
/
dump_scripts.py
File metadata and controls
62 lines (50 loc) · 2.01 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
#!/usr/bin/env python3
"""
Extract scripts embedded within IDA Pro databases.
author: Willi Ballenthin
email: willi.ballenthin@gmail.com
"""
import sys
import logging
import argparse
import idb
import idb.netnode
logger = logging.getLogger(__name__)
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description="Extract scripts embedded within IDA Pro databases.")
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")
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)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger().setLevel(logging.INFO)
with idb.from_file(args.idbpath) as db:
try:
for script in idb.analysis.enumerate_script_snippets(db):
logger.debug("script: %s", script.name)
logger.debug("language: %s", script.language)
logger.debug("code: \n%s", script.code)
if script.language == "Python":
ext = ".py"
elif script.language == "IDC":
ext = ".idc"
else:
raise ValueError("unexpected script language: " + script.language)
filename = script.name + ext
logger.info("writing %s script %s to %s", script.language, script.name, filename)
with open(filename, "wb") as f:
f.write(script.code.encode("utf-8"))
except KeyError:
logger.warning("not found script snippets")
return 0
if __name__ == "__main__":
sys.exit(main())