基于requests\hashlib模块实现md5加密方式的有道翻译接口破解
第一步打开控制台查询任意一个单词进行抓包,获取请求方式和查看所需的查询参数(salt,ts,sign)
第二部,根据所需查询参数在控制台中搜索参数所在的 js文件的位置,打断点调试,查看各个参数在js文件中对应函数的实现过程;经调试发现ts是一个时间戳,salt是0-9和时间戳拼接成的一个字符串,sign是所查询单词和salt以及两个固定字符串的一个拼接.
第三步,用python实现Form Data中查询参数的动态生成.
第四步,发请求,传入第三步实现的参数,获取相应结果
源码如下 : 看不懂的同学,b站搜 一只会唱歌的程序狗,里面有一期讲述了整个分析过程,每一步打印非常详细。
import requests
import time
import random
from hashlib import md5
class YdSpider():
def init(self):
self.url = “http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule”
self.headers = {
“Accept”: “application/json, text/javascript, /; q=0.01”,
“Accept-Encoding”: “gzip, deflate”,
“Accept-Language”: “zh-CN,zh;q=0.9,en;q=0.8”,
“Cache-Control”: “no-cache”,
“Connection”: “keep-alive”,
“Content-Length”: “239”,
“Content-Type”: “application/x-www-form-urlencoded; charset=UTF-8”,
“Cookie”: “[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=146889837.6278171; _ga=GA1.2.828762415.1557487039; P_INFO=18203678715|1557487117|1|youdaonote|00&99|null&null&null#hen&410100#10#0|&0||18203678715; _ntes_nnid=66628fdd0933b91b9f3b3f4e77e6ace3,1557741619397; UM_distinctid=16c2c70da34500-02a227a759081c-15231708-1fa400-16c2c70da35658; JSESSIONID=aaaXbo2PVTqwr6xtLBiYw; ___rl__test__cookies=1565689925539”,
“Host”: “fanyi.youdao.com”,
“Origin”: “http://fanyi.youdao.com”,
“Pragma”: “no-cache”,
“Referer”: “http://fanyi.youdao.com/”,
“User-Agent”: “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36”,
“X-Requested-With”: “XMLHttpRequest”
}
def get_salt_sign_ts(self,word):
# ts
ts = str(int(time.time() * 1000))
# salt
salt = ts + str(random.randint(0, 9))
# sign
string = "fanyideskweb" + word + salt + "n%A-rKaT5fb[Gy?;N5@Tj"
s = md5()
s.update(string.encode())
sign = s.hexdigest()
return salt, sign, ts
def attack_yd(self,word):
#1.先拿到salt,sign,ts
salt, sign, ts = self.get_salt_sign_ts(word)
#2.定义form表单数据为字典:data={}
data = {
'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'ts': ts,
'bv': 'cf156b581152bd0b259b90070b1120e6',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
#3.直接发送请求:request.post(url,data=data,headers=headers)
res = requests.post(url=self.url,data=data,headers=self.headers)
html = res.json()
result = html["translateResult"][0][0]["tgt"]
#4.获取相应内容
print(result)
def main(self):
#输入翻译单词
word = input("input your word:")
self.attack_yd(word)
if name == “main”:
spider = YdSpider()
spider.main()