Last active
April 15, 2025 07:30
-
-
Save 00xc/600d8514b931dd6b99242336dcf42ab3 to your computer and use it in GitHub Desktop.
Extracting every embedded file with binwalk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import binwalk | |
import shlex | |
import subprocess as sp | |
def dump_file(file, offset, size, outfile): | |
cmd = "dd if={} of={} bs=1 skip={}".format(file, outfile, offset, size) | |
if size is not None: | |
cmd += " count={}".format(size) | |
s = sp.run(shlex.split(cmd)) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print("./{} <input_file> <output_dir>".format(sys.argv[0]), file=sys.stderr) | |
sys.exit() | |
FILE = sys.argv[1] | |
OUTDIR = sys.argv[2] | |
if not os.path.exists(OUTDIR): | |
os.mkdir(OUTDIR) | |
results = binwalk.scan(FILE, signature=True, quiet=True)[0] | |
offsets = [r.offset for r in results.results] | |
for i, off in enumerate(offsets): | |
size = None | |
if i < len(offsets)-1: | |
size = offsets[i+1] - off | |
dump_file(FILE, off, size, os.path.join(OUTDIR, "file{}".format(i))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment