Last active
November 9, 2018 15:16
-
-
Save S0Ulle33/1ec11f75861b42acb61692a12ba3991a to your computer and use it in GitHub Desktop.
Finds rarjpegs by checking zip/rar signatures in file, and then checks if archive is valid.
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
import os | |
import re | |
import zipfile | |
import rarfile | |
SIGNATURES = {'ZIP_NORMAL': b'PK\x03\x04', 'ZIP_EMPTY': b'PK\x05\x06', 'ZIP_SPANNED': b'PK\x07\x08', | |
'RAR_1.50': b'Rar!\x1a\x07\x00', 'RAR_5.0': b'Rar!\x1a\x07\x01\x00'} | |
def _is_valid(byte_str, offset): | |
with open('test.rar', 'wb') as f: | |
f.write(byte_str[offset:]) | |
if (zipfile.is_zipfile('test.rar') or | |
rarfile.is_rarfile('test.rar')): | |
os.remove('test.rar') | |
return True | |
os.remove('test.rar') | |
return False | |
def is_rarjpeg(rarjpeg): | |
with open(rarjpeg, 'rb') as f: | |
byte_str = f.read() | |
for name, signature in SIGNATURES.items(): | |
search = re.search(signature, byte_str) | |
if search: | |
offset = search.start() | |
return _is_valid(byte_str, offset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment