master
Raw Download raw file
  1#### WELCOME
  2import os
  3import hashlib
  4import puremagic
  5import re
  6
  7def decode_filename(filename):
  8    """
  9    Decodes a inziption filename
 10    Returns [password, id, extension]
 11
 12    Filename Format: <string>.<id>.<extension>
 13    Decoder ring:
 14    +----+------------------+
 15    | ID |     PASSWORD     |
 16    +====+==================+
 17    | A  | <string>         |
 18    | B  | <string>.reverse |
 19    | C  | md5(<string>)    |
 20    | D  | sha1(<string>)   |
 21    | E  | ''               |
 22    +----+------------------+
 23    """
 24    print(filename)    
 25    split_name = os.path.basename(filename).split('.')
 26    if len(split_name) == 3:
 27        f_string = split_name[0]
 28        f_id = split_name[1]
 29        f_ext = split_name[2]
 30    # TODO handle when len is not 3
 31
 32    if f_id == "A":
 33        return [f_string, f_id, f_ext]
 34    if f_id == "B": 
 35        # <string>,reverse
 36        return [f_string[::-1], f_id, f_ext]
 37    if f_id == "C":
 38        # should match md5sum <(echo -n f_string)
 39        return [hashlib.md5(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
 40    if f_id == "D":
 41        # should match sha1sum <(echo -n f_string)
 42        return [hashlib.sha1(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
 43    if f_id == "E":
 44        return ["''", f_id, f_ext]
 45
 46def un_7z(filename, password):
 47    import py7zr
 48    with py7zr.SevenZipFile(filename, password=password) as z:
 49        if len(z.getnames()) == 1:
 50            next_file = z.getnames()[0]
 51            z.extractall(path="tmp")
 52            return f"tmp/{next_file}"
 53        else:
 54            print(f"bad 7z {filename}")
 55
 56def un_zip(filename, password):
 57    import zipfile
 58    with zipfile.ZipFile(filename) as z: 
 59        if len(z.namelist()) == 1:
 60            next_file = z.namelist()[0]
 61            z.extract(next_file, path="tmp", pwd=bytes(password, 'utf-8'))
 62            return f"tmp/{next_file}"
 63        else:
 64            print(f"bad zip {filename}")
 65
 66def un_gz(filename, password):
 67    import gzip, zipfile, magic
 68    next_file = re.findall(r'["](.*?)["]',magic.from_file(filename))[0]
 69    with gzip.GzipFile(filename) as gz: 
 70        ungzip_data = gz.read()
 71        with open(f"tmp/{next_file}", 'wb') as out:
 72            out.write(ungzip_data)
 73        return f"tmp/{next_file}"
 74        
 75def un_tar(filename, password):
 76    import tarfile
 77    with tarfile.open(filename) as z: 
 78        if len(z.getnames()) == 1:
 79            next_file = z.getnames()[0]
 80            z.extractall(path="tmp")
 81            return f"tmp/{next_file}"
 82        else:
 83            print(f"bad tar {filename}")
 84
 85def unziption(filename):
 86    password, _, ext = decode_filename(filename)
 87    print(filename, password)
 88    ext = puremagic.from_file(filename)
 89    if ext == ".7z":
 90        return un_7z(filename, password)
 91    if ext == ".zip":
 92        return un_zip(filename, password)
 93    if ext == ".gz":
 94        return un_gz(filename, password)
 95    if ext == ".tar":
 96        return un_tar(filename, password)
 97
 98zip_file = "provided/jB1qLK53r.A.7z"
 99
100while True:
101    zip_file = unziption(zip_file)