代码混淆

使用https://pyob.oxyry.com/进行代码的混淆(找不到什么可用的离线混淆库)
image.png
抓取真实api后

  1. def obfuscation(py_file, save_path):
  2. print("读取文件:", py_file)
  3. with open(py_file, "r", encoding="utf-8") as f:
  4. py_content = f.read()
  5. print("进行混淆中...")
  6. url = "https://pyob.oxyry.com/obfuscate"
  7. headers = {
  8. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
  9. "Referer": "http://pyob.oxyry.com/",
  10. "content-type": "application/json",
  11. "cookie": "_ga=GA1.2.1306886713.1588752647; _gid=GA1.2.46944674.1588899118"
  12. }
  13. data = json.dumps({
  14. "append_source": "false",
  15. "preserve": "",
  16. "remove_docstrings": "true",
  17. "rename_default_parameters": "false",
  18. "rename_nondefault_parameters": "true",
  19. "source": py_content
  20. })
  21. result = json.loads(requests.post(url, data=data, headers=headers).text)["dest"]
  22. result = "# cython: language_level=3\n" + result
  23. print("混淆成功...")
  24. with open(save_path, "w", encoding="utf-8") as f:
  25. f.write(result)
  26. print("混淆文件已写入{}\n".format(save_path))
  27. if __name__ == '__main__':
  28. obfuscation("my.py", "../混淆/my.py")
  29. obfuscation("approach.py", "../混淆/approach.py")

编译pyd

build_pyd.py

  1. from distutils.core import setup
  2. from Cython.Build import cythonize
  3. setup(
  4. name='any words.....',
  5. ext_modules=cythonize(["my.py","approach.py" ])
  6. )

执行打包

  1. import json
  2. import os
  3. # 清理旧pyd文件
  4. import uuid
  5. import requests
  6. def clearPyd():
  7. for file in os.listdir():
  8. if ".pyd" in file:
  9. print("删除.pyd:", file)
  10. os.remove(file)
  11. print("***********************************************************************")
  12. # 构建pyd文件
  13. def buildPyd():
  14. os.system("python build_pyd.py build_ext --inplace")
  15. # 重命名pyd文件
  16. def renamePyd():
  17. print("***********************************************************************")
  18. for file in os.listdir():
  19. if ".pyd" in file:
  20. print("重新命名pyd:", file)
  21. os.rename(file, file[:file.find(".")] + ".pyd")
  22. for file in os.listdir():
  23. if ".c" in file:
  24. print("删除.c文件:", file)
  25. os.remove(file)
  26. print("***********************************************************************")
  27. # 执行打包
  28. def pyinstaller(key, ico):
  29. os.system("pyinstaller -F --key {} -i {} main.py".format(key, ico))
  30. # 删除bulid和spec文件
  31. def clearBuildAndSpec():
  32. import shutil
  33. shutil.rmtree('build')
  34. print("删除bulid文件夹")
  35. os.remove("main.spec")
  36. print("删除spec文件")
  37. if __name__ == '__main__':
  38. clearPyd() # 清理旧pyd文件
  39. buildPyd() # 构建pyd文件
  40. renamePyd() # 重命名pyd文件
  41. pyinstaller(uuid.uuid4()[0:16], "1.ico") # 执行打包
  42. clearPyd() # 清理pyd文件
  43. clearBuildAndSpec() # 删除bulid和spec文件