Last active
January 25, 2024 21:19
-
-
Save nstarke/e1ba019b3b62de47e79519d5deed9ea7 to your computer and use it in GitHub Desktop.
Python script to bruteforce gzip data
This file contains hidden or 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 | |
# | |
# find-data.py | |
# | |
# A small script to bruteforce embedded compressed data that might not have a header | |
# Useful for raw binary firmware images that do not contain a standard | |
# binary header (ELF, PE, MACH-O). | |
# | |
# Usage: python find-data.py "filename.bin" | |
# | |
import zlib, sys, lzma, bz2, threading, copy | |
DECOMPRESSION_CHAMBER_PATH = "/tmp/decompression-chamber" | |
DIVISOR = 8 | |
def do_bz2(compressed_data): | |
data_length = len(compressed_data) | |
for i in range(int(data_length)): | |
try: | |
unzipped = bz2.decompress(compressed_data[i:]) | |
except Exception as ex: | |
continue | |
if len(unzipped) > int((data_length / DIVISOR)): | |
print ('BZ2: Offset Found', i) | |
with open(DECOMPRESSION_CHAMBER_PATH + '/result-bz2-' + str(i) + '.bin', 'wb') as result: | |
result.write(unzipped); | |
result.close() | |
break | |
def do_lzma(compressed_data): | |
data_length = len(compressed_data) | |
for i in range(int(data_length)): | |
try: | |
unzipped = lzma.decompress(compressed_data[i:]) | |
except Exception as ex: | |
continue | |
if len(unzipped) > int((data_length / DIVISOR)): | |
print ('LZMA: Offset Found', i) | |
with open(DECOMPRESSION_CHAMBER_PATH + '/result-lzma-' + str(i) + '.bin', 'wb') as result: | |
result.write(unzipped); | |
result.close() | |
break | |
def do_zlib(compressed_data): | |
data_length = len(compressed_data) | |
for i in range(int(data_length)): | |
try: | |
unzipped = zlib.decompress(compressed_data[i:], -zlib.MAX_WBITS) | |
except Exception as ex: | |
continue | |
if len(unzipped) > int((data_length / DIVISOR)): | |
print ('GZIP: Offset found', i) | |
with open(DECOMPRESSION_CHAMBER_PATH + '/result-gz-' + str(i) + '.bin', 'wb') as result: | |
result.write(unzipped); | |
result.close() | |
break | |
fname = sys.argv[1] | |
with open(fname, 'rb') as compressed_data: | |
compressed_data = compressed_data.read() | |
thread_zlib = threading.Thread(target=do_zlib, args=(copy.copy(compressed_data),)) | |
thread_lzma = threading.Thread(target=do_lzma, args=(copy.copy(compressed_data),)) | |
thread_bz2 = threading.Thread(target=do_bz2, args=(copy.copy(compressed_data),)) | |
thread_zlib.start() | |
thread_lzma.start() | |
thread_bz2.start() | |
thread_zlib.join() | |
thread_lzma.join() | |
thread_bz2.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment