js逆向之python有道翻译接口JavaScript解密
- 工具:chrome
- 打开有道翻译http://fanyi.youdao.com/
- F12打开开发者工具或鼠标右击->检查,找到Network一栏
- 输入字符串进行抓包
- 在Search中输入第三步的字符串,进行数据包关键词的搜索
- 此时可以猜测Form Data中ts:是时间戳,salt:时间戳+随机一位数字,sign和bv有加密,其它为固定值
i: 中国
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 15916148182391
sign: e2a7986f3466d33c120c7e34c8d25659
ts: 1591614818239
bv: c74c03c52496795b65595fdc27140f0f
doctype: json
version: 2.1
keyfrom: fanyi.web
action: FY_BY_REALTlME
-
经过以下堆栈追踪调试,定位到JavaScript加密的关键代码
-
JavaScript加密的核心代码
var r = function(e) {
var t = n.md5(navigator.appVersion)
, r = "" + (new Date).getTime()
, i = r + parseInt(10 * Math.random(), 10);
return {
ts: r,
bv: t,
salt: i,
sign: n.md5("fanyideskweb" + e + i + "Nw(nmmbP%A-r6U3EUn]Aj")
}
};
- python 代码两种实现方式一:使用python模拟实现JavaScript加密的核心代码
import requests
from random import randint
import time
import hashlib
def youdaofanyi(word):
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
ts = format(time.time() , '.3f').replace('.', '')
salt = ts + str(randint(1, 10))
bv = hashlib.md5(ua.encode('utf-8'))
sign_temp = "fanyideskweb" + str(word) + str(salt) + "mmbP%A-r6U3Nw(n]BjuEU"
sign = hashlib.md5(sign_temp.encode('utf-8'))
header = {
'Cookie': 'OUTFOX_SEARCH_USER_ID=-1334003568@10.108.160.17; OUTFOX_SEARCH_USER_ID_NCOO=670129948.8810779; _ga=GA1.2.63385724.1582769633; JSESSIONID=aaaTqfgI67jLlcro_ztmx; ___rl__test__cookies=1593761131267',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': ua,
}
data = {
'i': f'{word}',
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': f'{salt}',
'sign': f'{sign.hexdigest()}',
'ts': f'{ts}',
'bv': f'{bv.hexdigest()}',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}
res = requests.post(url=url, data=data, headers=header).json()
if res['errorCode'] != 0:
return res['errorCode']
for i in res['translateResult'][0]:
return i['tgt']
if __name__ == '__main__':
data = youdaofanyi(input('Please input the content of the translation you need:'))
print('翻译结果为:' + data)
-
python 代码两种实现方式二:使用execjs执行JavaScript加密的核心代码
import requests import execjs def youdaofanyi(word): url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' header = { 'Cookie': 'OUTFOX_SEARCH_USER_ID=-1334003568@10.108.160.17; OUTFOX_SEARCH_USER_ID_NCOO=670129948.8810779; _ga=GA1.2.63385724.1582769633; JSESSIONID=aaaTqfgI67jLlcro_ztmx; ___rl__test__cookies=1593761131267', 'Referer': 'http://fanyi.youdao.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36', } js_code = ''' var md5 = function(content) { var crypto = require('crypto'); var result = crypto.createHash('md5').update(content).digest("hex") console.log(result); return result }; var r = function(e) { var t = md5("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36") , r = "" + (new Date).getTime() , i = r + parseInt(10 * Math.random(), 10); return { ts: r, bv: t, salt: i, sign: md5("fanyideskweb" + e + i + "Nw(nmmbP%A-r6U3EUn]Aj") } }; ''' ctx = execjs.compile(js_code) result = ctx.call('r', word) data = { 'i': f'{word}', 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': f'{result["salt"]}', 'sign': f'{result["sign"]}', 'ts': f'{result["ts"]}', 'bv': f'{result["bv"]}', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTlME', } res = requests.post(url=url, data=data, headers=header).json() if res['errorCode'] != 0: return res['errorCode'] for i in res['translateResult'][0]: return i['tgt'] if __name__ == '__main__': data = youdaofanyi(input('Please input the content of the translation you need:')) print('翻译结果为:' + data)