Skip to content

Instantly share code, notes, and snippets.

@lheckemann
Created June 23, 2021 10:05
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 lheckemann/5f59185dd34df70b5a5b14de679a589d to your computer and use it in GitHub Desktop.
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.
#!/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