混淆 Python 代码

Fariba Laiq 2023年10月10日
  1. 使用 Base64 编码来混淆 Python 代码
  2. 使用 PyArmor 混淆 Python 代码
混淆 Python 代码

代码混淆是指将代码加密或转换成人类难以理解的格式。我们在知情的情况下执行此任务是为了避免攻击者的逆向工程并保护知识产权和商业机密。

Python 提供了多种混淆代码的方法。

使用 Base64 编码来混淆 Python 代码

base64 是 Python 中的一个模块,一旦将数据转换为类似字节的对象,它就会对数据进行编码和解码。对 Python 的代码进行编码可能是一种有用的技巧,可以混淆代码以防止其被人类阅读和理解以确保安全。

在下面的代码中,我们导入了 base64 模块并使用方法 b64encode() 对语句 print('Delftstack') 进行了编码。我们必须将 UTF-8 格式的字符串传递给方法。

我们还可以使用方法 b64.decode() 通过将编码文本传递给此方法来解密代码。最后,我们可以使用 eval()compile() 方法运行解密后的代码。

示例代码:

# Python 3.x
import base64

code = "print('Delftstack')"
print("Code:", code)
encrypted_code = base64.b64encode(code.encode("utf-8"))
print("Obfuscated code:", encrypted_code)
mydecode = base64.b64decode(encrypted_code)
print("Decrypted Code:")
eval(compile(mydecode, "<string>", "exec"))

输出:

#Python 3.x
Code: print('Delftstack')
Obfuscated code: b'cHJpbnQoJ0RlbGZ0c3RhY2snKQ=='
Decrypted Code:
Delftstack

我们还可以导入包含代码的完整 .py 文件,并使用 base64 技术按以下方式对其进行编码。My_Script.py 文件包含代码 print('hello world')

# Python 3.x
import base64

file = open("My_Script.py")
encrypted_code = base64.b64encode(file.read().encode("utf-8"))
print("Encrypted Code:", encrypted_code)

输出:

#Python 3.x
Encrypted Code: b'cHJpbnQoJ2hlbGxvIHdvcmxkJyk='

使用 PyArmor 混淆 Python 代码

Python 程序也可以用 PyArmor 加密。它是一个命令行工具,可以对代码进行混淆,将混淆脚本绑定到固定机器,甚至可以使混淆脚本过期。

要使用 PyArmor,我们应该首先使用以下命令安装它。

#Python 3.x
pip install pyarmor

在这里,我们将在名为 MyScript.py 的文件中混淆以下代码。我们必须将 .py 文件放在一个单独的文件夹中。

# Python 3.x
print("Hello World")

命令 pyarmor obfuscate MyFolder/MyScript.py 将混淆上述 .py 文件。我们可以在 dist 文件夹中看到具有相同文件名的输出文件。

输出:

#Python 3.x
from pytransform import pyarmor_runtime
pyarmor_runtime()
__pyarmor__(__name__, __file__, b'\x50\x59\x41\x52\x4d\x4f\x52\x00\x00\x03\x06\x00\x33\x0d\x0d\x0a\x09\x30\xe0\x02\x00\x00\x00\x00\x01\x00\x00\x00\x40\x00\x00\x00\xc2\x00\x00\x00\x00\x00\x00\x18\x58\xc1\xa9\xb3\xd3\xa5\x4a\x59\xc7\xef\x90\xbd\x03\x26\xa0\x44\x00\x00\x00\x00\x00\x00\x00\x00\x75\x29\x30\x1b\x83\xa8\x1a\x8c\x19\x5d\xf5\xfb\x82\xd2\xde\xf9\x3e\x55\x2a\x65\x05\x6c\xd5\x40\xf1\x45\xaf\x5a\x5e\xce\x5b\x94\xe6\xb5\x2d\xbf\x91\x08\x47\x5a\x41\x2a\x8e\x86\x28\x08\x76\x35\x77\xbf\x3f\x52\x28\x03\xbb\xcf\xd4\xac\xa5\x1e\x8a\x4d\xfe\xc4\x48\x04\xb7\x8f\xfb\x10\xa7\x25\x05\x97\x80\xfa\x74\x96\x4b\x61\x88\x8f\x98\x96\x1e\xa5\x3d\x98\x2e\xe0\x1e\x19\xa2\x15\x94\x8b\xc2\x5c\x2a\x1e\x2c\x6f\x46\x28\x08\x85\x3f\x0d\x8a\xd8\xb5\x9c\xcc\xee\x8e\xff\x6f\x31\xb3\x02\x53\xbd\x88\x4a\x98\x84\x61\xd5\xe9\xea\x66\x75\x69\x1c\xd2\x5d\x47\x5e\x5b\xc8\x2d\x5f\x01\x74\xaa\xf6\x3b\xfc\xd0\x9f\xfd\x9c\x27\x35\x2b\xbe\x41\xa6\xc1\x88\x79\xfc\xb3\xe8\xa7\x65\x19\xed\x8d\x85\xb1\x07\x35\x96\x4d\xea\x32\x71\xba\x63\xb0\x11\xaf\x31\x05\xe9\xa0\xba\xaa\x77\x96\x43\xeb\x4d', 2)
作者: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn