justinooo

Python - Mystipy (CLI Obfuscation Tool)

Jun 16th, 2024 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 KB | None | 0 0
  1. import os
  2. import argparse
  3. import base64
  4. import gzip
  5. from io import BytesIO
  6.  
  7. default_key = "hello world"
  8. default_salt_sz = 16
  9.  
  10. def xor(data, key):
  11.     return bytes(a ^ b for a, b in zip(data, key * (len(data) // len(key) + 1)))
  12.  
  13. def obfuscate_data(input_data, key, salt_sz = default_salt_sz, hex = True):
  14.     salt = os.urandom(salt_sz)
  15.     input_data = salt + input_data
  16.     encoded_content = base64.b64encode(input_data)
  17.     xored_content = xor(encoded_content, key)
  18.     reversed_content = xored_content[::-1]
  19.     buffer = BytesIO()
  20.     with gzip.GzipFile(fileobj=buffer, mode='wb') as gzipped_file:
  21.         gzipped_file.write(reversed_content)
  22.     gzipped_data = buffer.getvalue()
  23.     if hex:
  24.         return gzipped_data.hex().encode()
  25.     return gzipped_data
  26.  
  27. def deobfuscate_data(input_data, key, salt_sz = default_salt_sz, hex = True):
  28.     if hex:
  29.         input_data = bytes.fromhex(input_data.decode())
  30.     buffer = BytesIO(input_data)
  31.     with gzip.GzipFile(fileobj=buffer, mode='rb') as gzipped_file:
  32.         reversed_content = gzipped_file.read()
  33.     xored_content = reversed_content[::-1]
  34.     encoded_content = xor(xored_content, key)
  35.     decoded_data = base64.b64decode(encoded_content)
  36.     return decoded_data[salt_sz:]
  37.  
  38. def read_file(file_path):
  39.     with open(file_path, 'rb') as file:
  40.         return file.read()
  41.  
  42. def write_file(file_path, data):
  43.     with open(file_path, 'wb') as file:
  44.         file.write(data)
  45.  
  46. def main():
  47.     parser = argparse.ArgumentParser(description = "Obfuscate or deobfuscate the contents of the file in a very simple way.")
  48.     parser.add_argument("input", type = str, nargs = "?", help = "Input file path.")
  49.     parser.add_argument("output", type = str, nargs = "?", help = "Output file path.")
  50.     parser.add_argument("-p", "--prompt", action = "store_true", help = "Prompt mode. If provided, you will be prompted for the arguments you don't explicitly set.")
  51.     parser.add_argument("-r", "--reverse", action = "store_true", help = "If provided, the protection will be reversed. (Deobfuscate)")
  52.     parser.add_argument("-k", "--key", type = str, default = default_key, help = "XOR key for encryption/decryption. Must be a valid byte string.")
  53.     parser.add_argument("-s", "--salt", type = int, default = default_salt_sz, help = "Number of bytes to use in salt. (Default: 16)")
  54.     parser.add_argument("--binary", action = "store_true", help = "Disable hexadecimal encoding/decoding, meaning the data will be (or is) compressed binary data.")
  55.  
  56.     args = parser.parse_args()
  57.  
  58.     input_path, output_path = args.input, args.output
  59.     reverse, binary, key, = args.reverse, args.binary, args.key
  60.  
  61.     if input_path and not output_path and not args.prompt:
  62.         # only specified the input file, not in prompt mode
  63.         if reverse:
  64.             # they're deobfuscating it, so ask for output path
  65.             output_path = input("Output file path: ")
  66.         else:
  67.             # they're obfuscating it, just add an extension to the file
  68.             output_path = input_path + (".bin" if binary else ".hex")
  69.     elif not input_path and not output_path:
  70.         # they didn't specify either input/output path, so prompt for both
  71.         input_path = input("Input file path: ")
  72.         output_path = input("Output file path: ")
  73.  
  74.     if args.prompt:
  75.         # prompt mode - prompt for any values they didn't explicitly set
  76.         input_path = input_path if input_path else input("Input file path: ")
  77.         output_path = output_path if output_path else input("Output file path: ")
  78.         if not reverse:
  79.             _reverse = input("Are you reversing the obfuscation? [y/n, default = n]: ").lower()
  80.             reverse = len(_reverse) > 0 and _reverse[:1].lower() == "y"
  81.         if not binary:
  82.             _binary = input("Should the output be hex encoded? [y/n, default = y]: ").lower()
  83.             binary = len(_binary) > 0 and _binary[:1].lower() == "n"
  84.         if key == default_key:
  85.             _key = input("Enter a custom encryption key, or leave blank to use default: ")
  86.             key = _key if len(_key) else key
  87.    
  88.     key = key.encode()
  89.     input_data = read_file(input_path)
  90.  
  91.     verb = ("de" if reverse else "") + "obfuscate"
  92.     try:
  93.         if reverse:
  94.             output_data = deobfuscate_data(input_data, key, args.salt, not binary)
  95.         else:
  96.             output_data = obfuscate_data(input_data, key, args.salt, not binary)
  97.         write_file(output_path, output_data)
  98.         print(f"File has been {verb}d.")
  99.     except Exception as ex:
  100.         print(f"Failed to {verb} data.\n{ex}")
  101.  
  102. if __name__ == "__main__":
  103.     main()
Add Comment
Please, Sign In to add comment