def pyherion(code):
"""
Generates a crypted hyperion'esque version of python code using
base64 and AES with a random key, wrapped in an exec() dynamic launcher.
code = the python source code to encrypt
Returns the encrypted python code as a string.
"""
imports = list()
codebase = list()
# strip out all imports from the code so pyinstaller can properly
# launch the code by preimporting everything at compiletime
for line in code.split("\n"):
if not line.startswith("#"): # ignore commented imports...
if "import" in line:
imports.append(line)
else:
codebase.append(line)
# encrypt the input file (less the imports)
encrypted_code, key, iv = aes_encryption("\n".join(codebase), encryption_pad='{')
encrypted_code = encrypted_code.decode('ascii')
# some random variable names
b64var = helpers.randomString()
aesvar = helpers.randomString()
# randomize our base64 and AES importing variable
imports.append("from base64 import b64decode as " + b64var)
imports.append("from Crypto.Cipher import AES as " + aesvar)
# shuffle up our imports
random.shuffle(imports)
# add in the AES imports and any imports found in the file
crypted = ";".join(imports) + "\n"
# the exec() launcher for our base64'ed encrypted string
to_be_encoded = "exec(" + aesvar + ".new(\"" + key + "\", " + aesvar + ".MODE_CBC, \"" + iv + "\").decrypt(" + b64var + "(\"" + encrypted_code + "\")).rstrip(b'{'))\n"
to_be_encoded = to_be_encoded.encode()
encoded_script = base64.b64encode(to_be_encoded).decode('ascii')
crypted += "exec(" + b64var + "(\"" + encoded_script + "\"))"
return crypted
评论列表
文章目录