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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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()

运行效果: