Created
November 12, 2015 17:23
-
-
Save luca020400/c9d9be594dfae7eb89eb to your computer and use it in GitHub Desktop.
This file contains 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/python2 | |
import sys | |
import mmap | |
ext4Magic = '\x53\xEF' | |
ext4MagicOffset = 1080 | |
if len(sys.argv) < 3: | |
print("No I/O supplied") | |
exit(1) | |
def copyExt4Image(origin, dest): | |
with open(origin, 'rb') as input: | |
with open(dest, 'wb') as output: | |
input.seek(offset - ext4MagicOffset) | |
output.write(input.read()) | |
def findMagic(input): | |
with open(input, 'r+b') as f: | |
mm = mmap.mmap(f.fileno(), 0) | |
return mm.find(ext4Magic, ext4MagicOffset) | |
systemimg = sys.argv[1] | |
systemimgout = sys.argv[2] | |
offset = findMagic(systemimg) | |
if offset == ext4MagicOffset: | |
print('The given file is a proper ext4 image') | |
elif offset != -1: | |
print('ext4 magic number found at byte %d' % offset) | |
print('Creating %s...' % systemimgout) | |
copyExt4Image(systemimg, systemimgout) | |
else: | |
print("ext4 magic number not found") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment