针对suctf的Python大法好
一题,通过解析内容还原pyc的脚本
对于不同的解析内容需要另加修改,但原理一致–将元素按照 格式标识符-len-内容的形式递归填入即可
值得说明的一点是脚本无法还原出原来一模一样的pyc,但可以还原出相同的py文件
这是因为python在编译pyc的时候,会将一些同样的字符串,通过引用来使用,即格式标识符“R”;另外还有一些字符串使用的是Interend,格式标识符’t’
这些表现出来的形式与常规字符串’s’相同,因此不易区分。而pyc中用常规字符串来解析,在运行和反编译的时候也是没有区别的
原理是这样的:引用字符串只是为了压缩pyc的大小,使得同样的字符串不用出现多次。而未经优化的pyc也是可以正常使用的。
脚本如下:
from struct import pack
def p32(i):
return pack("<i", i)
def string(d, fout):
l = len(d)
fout.write(b"s")
if(type(d)==type("1")):
if(d!="" and d[0]=="'"):
d = d.strip("'")
l -= 2
d = d.encode()
l = p32(l)
fout.write(l)
fout.write(d)
def tumple(d, fout):
l = p32(len(d))
fout.write(b"(")
fout.write(l)
for s in d:
string(s, fout)
def tumple_line(i, l, fout):
fout.write(b'(')
fout.write(p32(l))
while(data[i][:5]!="names"):
# print(data[i])
if<