爬虫逆向破解翻译接口参数

Python 请求baidu翻译接口:https://fanyi.baidu.com/v2transapi?from=zh&to=en

步骤一: 查找构建请求参数

JS 断点发现如下参数:

w = {
      from: _.fromLang,
      to: _.toLang,
      query: e,
      transtype: i,
      simple_means_flag: 3,
      sign: b(e),
      token: window.common.token,
      domain: k.getCurDomain(),
      ts: +new Date
},

 如图所示参数解析出来对应得值。 

from:翻译文字对应语言代码

to:需要翻译语言代码

query:翻译文字

transtype:固定值【realtime】

sign:根据翻译文字设定得动态加密值

simple_means_flag:固定值【3】

token:固定值【85ecac1256c1bc754ad63419393fecbc】

domain:固定值【common】

ts: 时间戳

步骤二: 断点验证请求种加密参数 并找到加密函数

主要逆向解析值为:sign

js文件查找定位到:b函数

            var r = null;
            t.exports = function(t) {
            var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
            if (null === i) {
                var a = t.length;
                a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
            } else {
                for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
                    "" !== s[c] && l.push.apply(l, function(t) {
                        if (Array.isArray(t))
                            return e(t)
                    }(o = s[c].split("")) || function(t) {
                        if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
                            return Array.from(t)
                    }(o) || function(t, n) {
                        if (t) {
                            if ("string" == typeof t)
                                return e(t, n);
                            var r = Object.prototype.toString.call(t).slice(8, -1);
                            return "Object" === r && t.constructor && (r = t.constructor.name),
                            "Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
                        }
                    }(o) || function() {
                        throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
                    }()),
                    c !== u - 1 && l.push(i[c]);
                var p = l.length;
                p > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) + 5).join("") + l.slice(-10).join(""))
            }
            for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = (null !== r ? r : (r = window[d] || "") || "").split("."), f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
                var _ = t.charCodeAt(v);
                _ < 128 ? g[y++] = _ : (_ < 2048 ? g[y++] = _ >> 6 | 192 : (55296 == (64512 & _) && v + 1 < t.length && 56320 == (64512 & t.charCodeAt(v + 1)) ? (_ = 65536 + ((1023 & _) << 10) + (1023 & t.charCodeAt(++v)),
                g[y++] = _ >> 18 | 240,
                g[y++] = _ >> 12 & 63 | 128) : g[y++] = _ >> 12 | 224,
                g[y++] = _ >> 6 & 63 | 128),
                g[y++] = 63 & _ | 128)
            }
            for (var b = f, w = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), x = 0; x < g.length; x++)
                b = n(b += g[x], w);
            return b = n(b, k),
            (b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
            "".concat((b %= 1e6).toString(), ".").concat(b ^ f)
        }
    }

步骤三: 补充执行js环境参数

进行JS修补,补充环境之后得到如下代码:


var r = null;
var window = {}
window.gtk = '320305.131321201';

function e(t, e) {
	(null == e || e > t.length) && (e = t.length);
	for (var n = 0, r = new Array(e); n < e; n++)
		r[n] = t[n];
	return r
}

function n(t, e) {
	for (var n = 0; n < e.length - 2; n += 3) {
		var r = e.charAt(n + 2);
		r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
		r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
		t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
	}
	return t
}

function a( t ){
	
	var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
	if (null === i) {
		var a = t.length;
		a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
	} else {
		for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
			"" !== s[c] && l.push.apply(l, function(t) {
				if (Array.isArray(t))
					return e(t)
			}(o = s[c].split("")) || function(t) {
				if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
					return Array.from(t)
			}(o) || function(t, n) {
				if (t) {
					if ("string" == typeof t)
						return e(t, n);
					var r = Object.prototype.toString.call(t).slice(8, -1);
					return "Object" === r && t.constructor && (r = t.constructor.name),
					"Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
				}
			}(o) || function() {
				throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
			}()),
			c !== u - 1 && l.push(i[c]);
		var p = l.length;
		p > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) + 5).join("") + l.slice(-10).join(""))
	}
	
	for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = (null !== r ? r : (r = window[d] || "") || "").split("."), f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
		var _ = t.charCodeAt(v);
		_ < 128 ? g[y++] = _ : (_ < 2048 ? g[y++] = _ >> 6 | 192 : (55296 == (64512 & _) && v + 1 < t.length && 56320 == (64512 & t.charCodeAt(v + 1)) ? (_ = 65536 + ((1023 & _) << 10) + (1023 & t.charCodeAt(++v)),
		g[y++] = _ >> 18 | 240,
		g[y++] = _ >> 12 & 63 | 128) : g[y++] = _ >> 12 | 224,
		g[y++] = _ >> 6 & 63 | 128),
		g[y++] = 63 & _ | 128)
	}
	
	for (var b = f, w = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), x = 0; x < g.length; x++)
	{
		b = n(b += g[x], w); 
	}
	
	return b = n(b, k),
	(b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
	"".concat((b %= 1e6).toString(), ".").concat(b ^ f)
}

将其另存为 getSignEncrypt.js 在node环境下运行可得到加密后参数值【sign】

步骤四: Python 执行js动态得到加密参数

python种执行js需导入模块【execjs】

js_file = "D:/Projects/abc.js"
with open(js_file, "r")as f:
    js_tamp = f.read()

jsDrive = execjs.compile( js_tamp )
sign = jsDrive.call( 'a', trans )

步骤五:发送请求得到翻译后结果集合

 完整请求代码:

import time
import execjs
import requests


trans = input( '请输入要翻译得文案:' )

js_file = "./getSignEncrypt.js"
with open(js_file, "r")as f:
    js_tamp = f.read()

jsDrive = execjs.compile( js_tamp )
sign = jsDrive.call( 'a', trans )

data = {
    "from": "zh",
    "to": "zh",
    "query": trans,
    "simple_means_flag": 3,
    "sign": sign,
    "token": "85ecac1256c1bc754ad63419393fecbc",
    "domain": "common",
    "ts": int( time.time() * 1000 )
}

header = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en,zh-CN;q=0.9,zh;q=0.8,ja;q=0.7",
    "Acs-Token": "1704798513422_1704883339706_WWWhQaSrjFtjvNWfXZYiy0mm0b94KOtCcvgpLipfwBwvOR3ETXvhJu4VUqdUbHqNk+wCNxdB7rnwbmJ7lc0uadQCtTsFWetHBUm7VUo71qkSo2G21OFmpjbZ9rhGL4hYgy3PTGUj/ErSbsyYOpl6OQE8PaAC3Sv2HipfGQ6B5z82IO5s9i3sejx6YKrG6ErNb+Uv019WU3giQFegmYk7IUm3CvnhN8nFvPaZwOyoXJzQ8W5JkZyMtFQVX7uoMHchTUvEMmsxDcb/ShDNWVWa+++L2xp+vCAW2+OR8Y2p3xpWDdhp3KEhC/kpGX75VkJARgZ3a4x2IvFWJrDhGtB/KwaPVqxSXr5DgLR9VpTDxKnS2zY/eOfOztMp/HEn1jf4d1LYkYwe01/MGac/mPINHW+N2kTHRpzsnA8u32kNcRsm1+ZGrh2m9tWaDebPF/Njf7yZHm5nwb/eKe0hiv6oBclv3+GXmznmG8Cc+U000Aoq8Zk+R9Ht0Ow7iaUM8RFD",
    "Host": "fanyi.baidu.com",
    "Origin": "https://fanyi.baidu.com",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Referer": "https://fanyi.baidu.com",
    "X-Requested-With": "XMLHttpRequest",
    "Cookie": "REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BIDUPSID=8CD7BC00DC822E5612A2EF9AB4566896; PSTM=1693744950; BAIDUID=8CD7BC00DC822E56741E842C9C5EB981:SL=0:NR=10:FG=1; MCITY=-233%3A; H_WISE_SIDS_BFESS=39712_39841_39902_39819_39909_39934_39936_39933_39943_39941_39938_39930_39732_39997_40009; H_PS_PSSID=39997_40076; H_WISE_SIDS=39997_40076; BAIDUID_BFESS=8CD7BC00DC822E56741E842C9C5EB981:SL=0:NR=10:FG=1; ZFY=haZlWdnMqSuVP8UY3jzqH9yRVGZ9jtY4HRxDOliN:Azw:C; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=0k2l85852la120alal20ah8hg1uvbt1ipslhu1s; delPer=0; PSINO=7; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1704247282,1704711563,1704787335,1704883262; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1704883271; ab_sr=1.0.1_ZmVmZmQ1YzkwMTViMzBmNDFlMzgxNzNjZGIxZmUxYjRlNjE5MzZkNjllOWYzMzE5NjRlNGFlMDg3YWRmYjAyYzk0MGNjN2RjOTViOTU3ZTA3OTlkNWRjYTIyMDNhNzEwYTczYWQ3ZTA4MmE2ZTcxNjEyNDNmODgwZmM2ZDA0YzVhNDU5MDMyODg2YWJhYmE5OGEwMzU3MDQyN2QxNzVjYQ==",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'

print( data )
print( header )

resp = requests.post( url, data = data, headers = header )
retData =  resp.json()

print( retData[ 'trans_result'][ 'data' ][0] )

执行代码输出如下:

方式补充:

 接口:https://fanyi.baidu.com/basetrans   

cookie : 必须  -可固定

User-Agent: 必须 -可固定

参数:sign  -动态

import requests
import execjs


trans = input( '请输入要翻译得文案:' )

js_file = "./translate.js"
with open(js_file, "r")as f:
    js_tamp = f.read()

jsDrive = execjs.compile( js_tamp )
sign = jsDrive.call( 'get_trans_sign', trans )


headers = {
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1 wechatdevtools/1.06.2401020 MicroMessenger/8.0.5 webview/17070335943581780 webdebugger port/59477 token/b6009ff5e5640758ee10863aa84bc513',
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': '*/*',
    'Origin': 'https://fanyi.baidu.com',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://fanyi.baidu.com/',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cookie': 'BAIDUID=795A1E35B30501910CDC9C7A7B0A852E:FG=1; H_WISE_SIDS=282631_110085_281879_287977_256739_292057_292326_292355_292363_292509_292345_292708_292823_292893_284553_290995_287175_293589_292962_287701_279490_293494_290399_293960_292169_275097_294047_288665_291032_287932_294107_294296_294314_294310_293629_294508_292246_294544_294542_293518_294630_294708_294739_294746_294754_294796_294396_294492_293365_295129_294625_292241_291191_295170_290425_289026_295291_294859_295285_295402_295376_295487_295460_295502_295509_295567_295631_295772_295763_295854_277936_295909_295927_295725; rsv_i=c224u1OthIt1cTgRwYCpIb+WvQJMYXSCYhW1+hrnreWW4GkHmUT6MFZq+9AUi32zYPWKMzQnkTXyyklCLboxSZD0T0UGL+U; BA_HECTOR=82016k80u58l848k9kah2k00oea2ub1iruh6l1t; ZFY=XLFkL6ZmB4lz0zXJcNOawcGN3ekSnKW7cHXnnbkcqWQ:C; BDORZ=AE84CDB3A529C0F8A2B9DCDD1D18B695; bd_af=1; SE_LAUNCH=5%3A28450563; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1707033823; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1707033823; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1707033979; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1707033979; ab_sr=1.0.1_YTk2OTJlMDQwZDkwM2M3ZGQyMmJlOTc5NWQ3NDcxNDY5NmJlZjI2NGUzODZiMDFiNjcyZjgzOThmMDg2MmFjNDk3NjNiOWE1ZWRiMDM3NjRlYjBjNmQ2MTlmYTJhMzUxYTQ1NjI4NzExZmM4Y2M3MTQ0N2E0NGVjYjE5NTYxMWM0NWZmMjFkMmE0ZDZkMjlkYmYzMjk4NmViYWIzNGVkZQ==',
}

data = {
    'query': trans,
    'from': 'zh',
    'to': 'en',
    'token': 'c7224d9b7d3e40bda79b5813d31473aa',
    'sign': sign,
}

response = requests.post('https://fanyi.baidu.com/basetrans', headers=headers, data=data)
print( response.json() )
var window = {}
window.gtk = '320305.131321201';

function n(t, e) {
	for (var n = 0; n < e.length - 2; n += 3) {
		var r = e.charAt(n + 2);
		r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
		r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
		t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
	}
	return t
}


function get_trans_sign(r) {

	var t = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
	if (null === t) {
		var a = r.length;
		a > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(a / 2) - 5, 10) + r.substr(-10, 10))
	} else {
		for (var C = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), h = 0, f = C.length, u = []; f > h; h++)
			"" !== C[h] && u.push.apply(u, e(C[h].split(""))),
			h !== f - 1 && u.push(t[h]);
		var g = u.length;
		g > 30 && (r = u.slice(0, 10).join("") + u.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + u.slice(-10).join(""))
	}
	var l = void 0
	  , d = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
	l = window.gtk;
	for (var m = l.split("."), S = Number(m[0]) || 0, s = Number(m[1]) || 0, c = [], v = 0, F = 0; F < r.length; F++) {
		var p = r.charCodeAt(F);
		128 > p ? c[v++] = p : (2048 > p ? c[v++] = p >> 6 | 192 : (55296 === (64512 & p) && F + 1 < r.length && 56320 === (64512 & r.charCodeAt(F + 1)) ? (p = 65536 + ((1023 & p) << 10) + (1023 & r.charCodeAt(++F)),
		c[v++] = p >> 18 | 240,
		c[v++] = p >> 12 & 63 | 128) : c[v++] = p >> 12 | 224,
		c[v++] = p >> 6 & 63 | 128),
		c[v++] = 63 & p | 128)
	}
	for (var w = S, A = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), b = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), D = 0; D < c.length; D++)
		w += c[D],
		w = n(w, A);
	return w = n(w, b),
	w ^= s,
	0 > w && (w = (2147483647 & w) + 2147483648),
	w %= 1e6,
	w.toString() + "." + (w ^ S)
}

结果如下:

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值