Skip to content

Instantly share code, notes, and snippets.

@nstarke
Created September 24, 2019 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nstarke/04a11dfa36d4d42bfb723d576336fa7b to your computer and use it in GitHub Desktop.
Save nstarke/04a11dfa36d4d42bfb723d576336fa7b to your computer and use it in GitHub Desktop.
Split Binary File on Zero'd Memory Regions
#!/usr/bin/env python
# The idea behind this file is that many times firmware images are constructed with zero'd out address regions as the delimiter.
# This script will split files in a firmware image when the embedded files are $MAX zero'd bytes in distance from each other.
import sys
import copy
FILE=sys.argv[1]
MAX=512
print ("Analyzing File: " + FILE)
with open(FILE) as f:
counter = 0
data = f.read()
start = 0
tripped = False
for byte in range(len(data)):
if data[byte] == '\x00':
if not tripped:
counter = counter + 1
if counter == MAX:
tripped = True
with open('splitter-%08d-%08d.bin' % (start, byte - start - MAX), 'w') as wf:
wf.write(data[start:(byte - MAX)])
wf.close()
print('wrote file. start: %08d. length was %08d' % (start, byte - start - MAX))
start = byte + 1
counter = 0
else:
start = byte + 1
else:
tripped = False
counter = 0
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment