背景:
- 有的反爬虫策略采用js对需要传输的数据进行加密处理(通常是取MD5值)
- 经过加密传输的就是密文
- 但是加密函数或者过程一定是在浏览器中完成,也就一定会把代码(js代码)暴露给客户端的使用者
- 通过阅读加密算法,就能模拟出加密过程,从而破解加密
以下代码为模拟破解有道辞典加密算法过程(本代码仅供业内学习交流使用,禁止违背道德谋取不当利益,做损人利己的事)
def getsalt():
import time,random #导入会用到的模块
salt = int(time.time()*1000)+random.randint(0,10)
return salt
def getMD5(v):
import hashlib
md5 = hashlib.md5()
md5.update(v.encode("utf-8"))
sign = md5.hexdigest()
return sign
def getsign(key ,salt):
sign = "fanyieskweb" + key + str(salt) + "n%A-rKaT5fb[Gy?;N5@Tj" #通过F12查看有道网站对salt的使用算法
sign = getMD5(sign)
return sign
from urllib import request,parse
def youdao(key):
#此处的链接本来为http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule,但是加了_o会导致报错,无法显示翻译结果,去掉后可正常显示,还不清楚原因
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
salt = getsalt()
data = {
#以下内容从F12页面调试所得,为Headers界面FromData,注意需要修改格式
"i": key,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": str(salt),
"sign": getsign(key ,salt),
#"ts": "1571214795919",
#"bv": "33a62fdcf6913d2da91495dad54778d1",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTlME"
}
print(data)
#参数需要时byte格式
data = parse.urlencode(data).encode()
headers = {
"Accept": "application / json, text / javascript, * / *; q = 0.01",
"Accept-Language":"zh - CN, zh;q = 0.9",
"Connection": "keep - alive",
"Content - Length": len(data),
"Content - Type": "application / x - www - form - urlencoded;charset = UTF - 8",
"Cookie": "OUTFOX_SEARCH_USER_ID=-656507629@220.163.12.3; OUTFOX_SEARCH_USER_ID_NCOO=1053588685.4351896; _ntes_nnid=6295da8a4a28ea20e17e82c4e6fbea37,1550836235897; JSESSIONID=aaa8Aah8aJGAngOTZQs3w; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcbl7CvU03mJdkZQJt3w; ___rl__test__cookies=1571214795917",
"Host": "fanyi.youdao.com",
"Origin": "http: // fanyi.youdao.com",
"Referer": "http: // fanyi.youdao.com /",
"User - Agent": "Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 72.03626.121Safari / 537.36",
"X - Requested - With": "XMLHttpRequest"
}
req = request.Request(url = url , data=data, headers=headers)
rsp = request.urlopen(req)
html = rsp.read().decode()
print(html)
if __name__ == "__main__":
word = input("请输入您要翻译的关键字:")
#youdao("boy")
youdao(word)