master
Raw Download raw file
  1import os
  2import hashlib
  3import puremagic
  4import re
  5
  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
 31    if f_id == "A":
 32        return [f_string, f_id, f_ext]
 33    if f_id == "B": 
 34        # <string>,reverse
 35        return [f_string[::-1], f_id, f_ext]
 36    if f_id == "C":
 37        # should match md5sum <(echo -n f_string)
 38        return [hashlib.md5(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
 39    if f_id == "D":
 40        # should match sha1sum <(echo -n f_string)
 41        return [hashlib.sha1(f_string.encode("utf-8")).hexdigest(), f_id, f_ext]
 42    if f_id == "E":
 43        return ["''", f_id, f_ext]
 44
 45def un_7z(filename, password):
 46    import py7zr
 47    with py7zr.SevenZipFile(filename, password=password) as z:
 48        if len(z.getnames()) == 1:
 49            next_file = z.getnames()[0]
 50            z.extractall(path="tmp")
 51            return f"tmp/{next_file}"
 52        else:
 53            print(f"bad 7z {filename}")
 54
 55def un_zip(filename,password):
 56    import zipfile
 57    with zipfile.ZipFile(filename) as z: 
 58        if len(z.namelist()) == 1:
 59            next_file = z.namelist()[0]
 60            z.extract(next_file, path="tmp", pwd=bytes(password, 'utf-8'))
 61            return f"tmp/{next_file}"
 62        else:
 63            print(f"bad zip {filename}")
 64
 65def un_gz(filename,password):
 66    import gzip, zipfile, magic
 67    next_file = re.findall(r'["](.*?)["]',magic.from_file(filename))[0]
 68    with gzip.GzipFile(filename) as gz: 
 69        ungzip_data = gz.read()
 70        with open(f"tmp/{next_file}", 'wb') as out:
 71            out.write(ungzip_data)
 72        return f"tmp/{next_file}"
 73        
 74def un_tar(filename,password):
 75    import tarfile
 76    with tarfile.open(filename) as z: 
 77        if len(z.getnames()) == 1:
 78            next_file = z.getnames()[0]
 79            z.extractall(path="tmp")
 80            return f"tmp/{next_file}"
 81        else:
 82            print(f"bad tar {filename}")
 83
 84def unziption(filename):
 85    password, _, ext = decode_filename(filename)
 86    print(filename, password)
 87    ext = puremagic.from_file(filename)
 88    if ext == ".7z":
 89        return un_7z(filename, password)
 90    if ext == ".zip":
 91        return un_zip(filename, password)
 92    if ext == ".gz":
 93        return un_gz(filename, password)
 94    if ext == ".tar":
 95        return un_tar(filename, password)
 96
 97
 98zip_file = "jB1qLK53r.A.7z"
 99out = open("test", "wb")
100while True:
101    zip_file = unziption(zip_file)
102    split_name = os.path.basename(zip_file).split('.')[0]
103    out.write(split_name.encode('utf-8'))
104    out.flush()
105   
106  
107
108#print(decode_filename('ExAmPlE.A.zip'))
109#print(decode_filename('ExAmPlE.B.zip'))
110#print(decode_filename('ExAmPlE.C.zip'))
111#print(decode_filename('ExAmPlE.D.zip'))
112#print(decode_filename('ExAmPlE.E.zip'))