Created
June 23, 2021 10:05
-
-
Save lheckemann/5f59185dd34df70b5a5b14de679a589d to your computer and use it in GitHub Desktop.
Accidentally wipefs'd a bit too much? Fear not! As long as you kept its output.
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/env python3 | |
import os | |
import sys | |
import re | |
import codecs | |
import subprocess | |
parse = re.compile(r'^(?P<device>.+): (?P<count>[0-9]+) bytes were erased at offset (?P<offset>0x[0-9a-f]+) \((?P<type>.+)\):(?P<bytes>( [0-9a-f][0-9a-f])+)$') | |
def undo_command(parsed): | |
device = parsed['device'] | |
count = int(parsed['count']) | |
offset = int(parsed['offset'].removeprefix('0x'), 16) | |
data = codecs.decode(parsed['bytes'].replace(' ', ''), 'hex') | |
assert len(data) == count | |
return (['dd', 'bs=1', f'of={device}', f'seek={offset}', f'count={count}'], data) | |
for line in sys.stdin.readlines(): | |
match = parse.match(line) | |
if match: | |
raw = match.groupdict() | |
cmd, data = undo_command(raw) | |
if not os.path.exists(raw['device']): | |
print(f'{raw["device"]} does not exist') | |
continue | |
print(cmd) | |
# Check the output before running with this line uncommented! | |
# subprocess.run(cmd, input=data) | |
elif 'ioctl' not in line: | |
print(f"Could not parse line '{line}'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment