2.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import sys,re
  3. import pyinstxtractor
  4. exe_file=r"D:/xx.exe"
  5. sys.argv = ['pyinstxtractor', exe_file]
  6. pyinstxtractor.main()
  7. # 2. 预处理pyc文件修护校验头
  8. def find_main(pyc_dir):
  9. for pyc_file in os.listdir(pyc_dir):
  10. if not pyc_file.startswith("pyi-") and pyc_file.endswith("manifest"):
  11. main_file = pyc_file.replace(".exe.manifest", "")
  12. result = f"{pyc_dir}/{main_file}"
  13. if os.path.exists(result):
  14. return main_file
  15. pyc_dir = os.path.basename(exe_file)+"_extracted"
  16. main_file = find_main(pyc_dir)
  17. main_file
  18. # 读取从pyz目录抽取的pyc文件的前4个字节作基准:
  19. pyz_dir = f"{pyc_dir}/PYZ-00.pyz_extracted"
  20. for pyc_file in os.listdir(pyz_dir):
  21. if pyc_file.endswith(".pyc"):
  22. file = f"{pyz_dir}/{pyc_file}"
  23. break
  24. with open(file, "rb") as f:
  25. head = f.read(4)
  26. list(map(hex, head))
  27. # ['0x42', '0xd', '0xd', '0xa']
  28. # 校准入口类:
  29. import shutil
  30. if os.path.exists("pycfile_tmp"):
  31. shutil.rmtree("pycfile_tmp")
  32. os.mkdir("pycfile_tmp")
  33. main_file_result = f"pycfile_tmp/{main_file}.pyc"
  34. with open(f"{pyc_dir}/{main_file}", "rb") as read, open(main_file_result, "wb") as write:
  35. write.write(head)
  36. write.write(b"\0"*12)
  37. write.write(read.read())
  38. # 校准子类:
  39. pyz_dir = f"{pyc_dir}/PYZ-00.pyz_extracted"
  40. for pyc_file in os.listdir(pyz_dir):
  41. pyc_file_src = f"{pyz_dir}/{pyc_file}"
  42. pyc_file_dest = f"pycfile_tmp/{pyc_file}"
  43. print(pyc_file_src, pyc_file_dest)
  44. with open(pyc_file_src, "rb") as read, open(pyc_file_dest, "wb") as write:
  45. write.write(read.read(12))
  46. write.write(b"\0"*4)
  47. write.write(read.read())
  48. # 3. 开始反编译
  49. from uncompyle6.bin import uncompile
  50. if not os.path.exists("py_result"):
  51. os.mkdir("py_result")
  52. for pyc_file in os.listdir("pycfile_tmp"):
  53. sys.argv = ['uncompyle6', '-o',
  54. f'py_result/{pyc_file[:-1]}', f'pycfile_tmp/{pyc_file}']
  55. uncompile.main_bin()