-
Notifications
You must be signed in to change notification settings - Fork 9
/
md1_extract.py
executable file
·46 lines (33 loc) · 1.28 KB
/
md1_extract.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
#!/usr/bin/env python3
import argparse
import os.path
from mtk_structs.md1img import Md1img
from kaitaistruct import KaitaiStream
def ensure_dir(path):
if os.path.isfile(path):
raise FileExistsError()
return os.makedirs(path, exist_ok=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('img', type=argparse.FileType('rb'))
parser.add_argument('--outdir', type=str, default=None)
args = parser.parse_args()
# make sure output directory exists or create it
if args.outdir is not None:
args.outdir = os.path.abspath(args.outdir)
ensure_dir(args.outdir)
print(f'extracting files to: {args.outdir}')
md_img = Md1img(KaitaiStream(args.img))
# simple name deduplication
file_num = 0
for section in md_img.sections:
fname = section.sec_hdr.name
print(f'{fname}: addr={section.sec_hdr.maddr:#010x}, size={section.sec_hdr.dsize}')
if args.outdir is not None:
out_path = os.path.join(args.outdir, f'{file_num:03}_{os.path.basename(fname)}')
file_num += 1
with open(out_path, 'wb') as out_file:
out_file.write(section.sec_data)
print(f'\textracted to {os.path.basename(out_path)}')
if __name__ == '__main__':
main()