進入題目后下載附件,發現是一個.pyc文件。
pyc是一種二進制文件,是由py文件經過編譯后,生成的文件,是一種byte code,py文件變成pyc文件后,運行加載的速度會有所提高;另一反面,把py文件編譯為pyc文件,從而可以實現部分的源碼隱藏,保證了python做商業化軟件時的安全性
用uncompyle6這個第三方python反編譯器來進行反編譯。
uncompyle6是一個原生python的跨版本反編譯器和fragment反編譯器,是decompyle、uncompyle、uncompyle2等的接替者。
uncompyle6可將python字節碼轉換回等效的python源代碼,它接受python 1.3版到3.8版的字節碼,這其中跨越了24年的python版本,此外還包括Dropbox的Python 2.5字節碼和一些PyPy字節碼。
github項目:https://github.com/rocky/python-uncompyle6
pip install uncompyle6
uncompyle6 -o . test.pyc
得到py代碼如下:
import base64 def encode1(ans): s = '' for i in ans: x = ord(i) ^ 36 x = x + 25 s += chr(x) return s def encode2(ans): s = '' for i in ans: x = ord(i) + 36 x = x ^ 36 s += chr(x) return s def encode3(ans): return base64.b32encode(ans) flag = ' ' print 'Please Input your flag:' flag = raw_input() final = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E===' if encode3(encode2(encode1(flag))) == final: print 'correct' else: print 'wrong'
代碼解析
encode1函數是把輸入的字符串分割成單獨的字符,先轉換為ascii數值,再與36異或,然后+25,接着轉回字符,最后拼接為新的字符串輸出。
因此對應的decode1函數應該是先-25,再與36異或(異或的逆操作還是異或)。
encode2函數是把輸入的字符串分割成單獨的字符,先轉換為ascii數值,再+36,然后與36異或,接着轉回字符,最后拼接為新的字符串輸出。
因此對應的decode2函數應該是先與36異或,再-36。
encode3函數是調用base64庫里的b32encode()函數進行base32運算。
因此對應的decode3函數應該是base64.b32decode()。
下面是對應的解密代碼
import base64 def decode1(ans): s = '' for i in ans: x = ord(i) - 25 x = x ^ 36 s += chr(x) return s def decode2(ans): s = '' for i in ans: x = i^ 36 x = x - 36 s += chr(x) return s def decode3(ans): return base64.b32decode(ans) final = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E===' flag=decode1(decode2(decode3(final))) print(flag)
得到對應的flag為cyberpeace{interestinghhhhh}
參考:https://www.jianshu.com/p/aafdedcbab4f