Python安全小工具之反编译pyc文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SKI_12/article/details/82079620

有时需要查看某个Python Web目录内Python源码,但dump下来只有pyc文件而没有py文件,这时需要我们反编译pyc文件为py文件。为了方便就写个脚本遍历目录内的pyc文件并进行反编译。

主要应用Python的第三方uncompyle6库,安装:pip install uncompyle6

#coding=utf-8
import os
import sys
import uncompyle6

def Decompile(path):
	if os.path.exists(path):
		for parent,dirs,files in os.walk(path):
			for file in files:
				file_name,ext = os.path.splitext(file)
				if ext == ".pyc":
					file_path = os.path.join(parent,file)
					print "[*]Decompiling:", file_path
					cmd = "uncompyle6 " + file_path + " > " + parent + "\\" + file_name + ".py"
					try:
						os.system(cmd)
						print "[+]Decompile successful.\n"
					except Exception as e:
						print e
		print "[*]Finished."
	else:
		print "[-]Wrong Directory Path."

def main():
	if len(sys.argv) != 2:
		print "[*]Usage: python decompile.py [Directory Path]"
	else:
		path = sys.argv[1]
		Decompile(path)

if __name__ == "__main__":
	main()

运行效果:

猜你喜欢

转载自blog.csdn.net/SKI_12/article/details/82079620