firmex/python/main.py

89 lines
2.2 KiB
Python

import argparse
import os
import io
from pathlib import Path
import matcher
signatures = [
matcher.Zip,
matcher.Ambarella,
matcher.SquashFS,
matcher.RomFS,
matcher.CromFS,
matcher.FlattenDeviceTree,
matcher.ELF,
]
def detect(args):
print('detecting', args.file)
matches = []
# recursive?
for matcher in signatures:
m = matcher(args.file)
if m.is_valid():
matches += [m]
for filetype in matches:
print("detected", filetype.name)
for m in filetype.matches:
print(">", m)
filetype.view(m, None)
return matches
def extract(args):
file = Path(args.file)
folder = file.parent / (file.name + "_extracted")
folder.mkdir(exist_ok=True)
matches = detect(args)
for filetype in matches:
print("detected", filetype.name)
for m in filetype.matches:
print(">", m)
filetype.view(m, folder)
def main():
parser = argparse.ArgumentParser(description='Program for detecting or extracting data.')
subparsers = parser.add_subparsers(dest='command')
# Subparser for the 'detect' command
detect_parser = subparsers.add_parser('detect', help='Detect data in a file.')
detect_parser.add_argument('file', help='Input file')
detect_parser.add_argument('--isa', action='store_true', help='Perform ISA detection')
# Subparser for the 'extract' command
extract_parser = subparsers.add_parser('extract', help='Extract data from a file.')
extract_parser.add_argument('file', help='Input file')
extract_parser.add_argument('--dry', action='store_true', help='Perform a dry run without extracting')
args = parser.parse_args()
if args.command is None:
print('no command given')
exit(1)
if args.file is None:
print('require file')
exit(1)
if not os.path.exists(args.file) or os.path.isdir(args.file):
print('please provide an existing file')
exit(1)
if args.command == 'detect':
detect(args)
elif args.command == 'extract':
extract(args)
else:
parser.print_help()
if __name__ == '__main__':
main()