由于工作紧张的原因很久没更新博客,实在对不住,说声抱歉大家了!!!之前立了个flag说要闯一下pythonchallenge看能到哪,后面几关真是越来越摸不着头脑了,好久才搞定了第19关,记录如下:
首先是查看源代码,发现里面有超长的一段注释,且告诉了我们使用base64编码,是一个wav文件,因此,首先我们拿到注释,代码中的get_comment函数;
接着将其写入wav文件,parse_data函数,形成一个wav文件,打开后就听到一个单词sorry,将sorry填入url,之后就问你为什么道歉,
这里卡住了很久,查看源代码、请求头、cookie等都没看到接近的信息,直到后来返回去看图,发现海的颜色与陆地的颜色是反过来的,是否音频也需要反过来,首先尝试了将注视中的代码反过来,再生成wav文件,提示不对,再尝试将之前的wav文件按帧反转,parse_wav函数,播放之,得到you’re an idiot!,将idiot填入url,bingo!!
import requests
import re
import base64
import wave
# from lxml import etree
def get_comment(href):
"""
:return: comments in the page
"""
resp = requests.get(href).text
text = re.findall('(UklGR.*?QBA=)', resp, re.S)[0]
return text
def parse_data(document, string):
"""
:param string: the obtained base64 codes.
:param document: the source file.
:return: get the decoded document
"""
with open(document, 'wb') as wav:
wav.write(base64.b64decode(string))
def parse_wav(document):
"""
Considering the reverse color of the sea(yellow) and land(blue) in the map,
maybe a reversion of the audio frames is needed.
"""
with wave.open(document, 'rb') as wi:
with wave.open('indian_out.wav', 'wb') as wo:
wo.setparams(wi.getparams())
for i in range(wi.getnframes()):
wo.writeframes(wi.readframes(1)[::-1])
if __name__ == '__main__':
url = 'http://butter:fly@www.pythonchallenge.com/pc/hex/bin.html'
file = 'indian.wav'
data = get_comment(url) # 获取评论中字符串
print(data)
parse_data(file, data)
parse_wav(file)