Skip to content

Instantly share code, notes, and snippets.

@f0rki
Created August 28, 2013 10:30
Show Gist options
  • Save f0rki/6364596 to your computer and use it in GitHub Desktop.
Save f0rki/6364596 to your computer and use it in GitHub Desktop.
quick and dirty python script to check how much of the original data is left if a partition or a image is formatted using ext4. Hint: it's none ;)
#!/usr/bin/python
"""
quick test to check how much of the original data is left if a partition or a
image is formatted using ext4.
"""
from __future__ import print_function
import os
import sys
import subprocess
magic = "\xF5\xF6\xF7\xF8"
magicstr = magic * (512/4)
size = 512 * 1024 * 1024
imgfile = "./test.img"
print("[+] creating test image at {}".format(imgfile))
with open(imgfile, "wb") as f:
print("[+] writing pattern \"{}\" to image".format(repr(magic)))
written = 0
while written < size:
f.write(magicstr)
written += len(magicstr)
print("[+] calling mkfs.ext4")
print(subprocess.check_output(["mkfs.ext4", "-F", imgfile],
stderr=sys.stderr))
print("[+] reading data an searching for pattern")
fsize = os.stat(imgfile).st_size
bs = len(magicstr) * 100
blocks = fsize / bs
curblock = 0
count = 0
with open(imgfile, "rb") as f:
while f.tell() < fsize:
s = f.read(bs)
count += s.count(magic)
if curblock % 17 == 0: # this is pretty random, just for less output
print(("[+] block {} of {}, byte {} of {} \n" +
"currently magic was found {} times")
.format(curblock, blocks, f.tell(), fsize, count))
curblock += 1
print("[+] found magic pattern {} times".format(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment