Last active
March 7, 2022 21:21
-
-
Save solsticedhiver/4ffdd096682eccbb8d389b8a5aa53c33 to your computer and use it in GitHub Desktop.
Script to search offset of the LUKS header on a device file
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 threading | |
import queue | |
import argparse | |
import os.path | |
import sys | |
NUM_WORKER_THREADS = 16 | |
LUKS_HEADER = b'LUKS\xba\xbe' | |
BLOCK_SIZE = 1024 | |
# globals | |
q = queue.Queue() | |
found = False | |
def look_for_header(item): | |
global found | |
f, offset = item | |
f.seek(offset) | |
b = f.read(BLOCK_SIZE) | |
indx = b.find(LUKS_HEADER) | |
if indx != -1: | |
found = True | |
print(f'Found LUKS header at offset {offset+indx} (in bytes)') | |
def worker(): | |
global q | |
while True: | |
item = q.get() | |
if not found: | |
look_for_header(item) | |
q.task_done() | |
def main(args): | |
for i in range(NUM_WORKER_THREADS): | |
t = threading.Thread(target=worker) | |
t.daemon = True | |
t.start() | |
for offset in range(0, args.limit*1024*1024, BLOCK_SIZE): | |
item = (args.f, offset) | |
q.put(item) | |
q.join() # block until all tasks are done | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Look for LUKS header in device file') | |
parser.add_argument('-l', '--limit', default=2, type=int, help='limit in MB where to look for the header (default=2)') | |
parser.add_argument('device', help='the device where to look for LUKS header') | |
args = parser.parse_args() | |
if not os.path.isfile(args.device): | |
print(f'Error: device {args.device} not found or not readable', file=sys.stderr) | |
sys.exit(-1) | |
try: | |
args.f = open(args.device, 'rb') | |
main(args) | |
args.f.close() | |
except KeyboardInterrupt as k: | |
print('Aborted by user') | |
finally: | |
args.f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment