1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import argparse
- import urllib.request
- from Crypto.Cipher import AES
- from os import remove
- DESCRIPTION = f"{__file__} - decrypt AES-256-GCM URL"
- def read_file(file_path: str):
- try:
- file = open(file_path, "rb+")
- return file.read()
- except FileNotFoundError:
- print("File not found")
- def write_file(file_path: str, data):
- try:
- file = open(file_path, "wb+")
- file.write(data)
- except Exception:
- print(f"Cannot write file: {file_path}")
- def encrypt(key, data):
- cipher = AES.new(key, AES.MODE_GCM)
- encrypted = cipher.encrypt(data)
- return encrypted, cipher.nonce
- def decrypt(key, iv, data):
- cipher = AES.new(key, AES.MODE_GCM, nonce=iv)
- decrypted = cipher.decrypt(data)
- return decrypted
- def main():
- parser = argparse.ArgumentParser(description=DESCRIPTION)
- parser.add_argument("url", help="URL with file to decrypt, e.g. aesgcm://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg#8c3d050e9386ec173861778f68e9af38a97aaf82faa4063b4d0878a61261534410c8a84331eaac851759f587")
- args = parser.parse_args()
- splited_url = args.url.split("#")
- download_url = splited_url[0]
- download_url = download_url.replace('aesgcm://', 'https://')
- splited_download_url = download_url.split('/')
- file_name = splited_download_url[len(splited_download_url) - 1]
- file_name_enc = f'{file_name}.enc'
- # print(f'Downloading {download_url} to {file_name_enc}')
- urllib.request.urlretrieve(download_url, file_name_enc)
- iv_key = splited_url[1]
- iv = iv_key[:24]
- key = iv_key[24:]
- encrypted_data = read_file(file_name_enc)
- iv_b = bytearray.fromhex(iv)
- key_b = bytearray.fromhex(key)
- decrypted = decrypt(key_b, iv_b, encrypted_data)
- write_file(file_name, decrypted)
- remove(file_name_enc)
- print(file_name)
- if __name__ == "__main__":
- main()
|