js逆向爬虫3

目标:百度翻译百度翻译-200种语言互译、沟通全世界! (baidu.com)

image-20221228120613218.png

定位需要的返回数据如上图,查看它的请求参数,api为v2transapi开头

image-20221228120722856

搜索上面几个参数,其中最容易定位的是simple_means_flag,只搜索到一个结果

image-20221228120942042.png

在源代码中打开,搜索simple_means_flag得到两个结果,都打上断点刷新调试;发现跳转到了第二个,说明这个才是关键加密函数

image-20221228121417038.png

通过多次刷新发现,除了sign其他参数均为固定值,单步进入b函数,将整个函数复制下来

image-20221228121604066.png

image-20221228121648441.png

创建了个获取请求参数的函数,返回结果为一个字典变量

image-20221228121741269

运行查看结果:41行的r未定义

image-20221228121842501.png

找到源码中对应的位置打断点,查看r值,发现r值为空

image-20221228121942114.png

故在41行前添加如下代码后再次运行

var r = null;

image-20221228122102259.png

报错说window变量未定义,光标移动到window变量上,在弹窗中找到gtk的值(d变量的值为gtk)

image-20221228143133495

在40行前添加如下代码:

var window = {'gtk': '320305.131321201'}

如图,获取了全部参数

image-20221228143315650

image-20221228144403210.png

完整代码:

baidu.py

import requests
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import execjs

translate_words = input('请输入要翻译的内容:')
ctx = execjs.compile(open('baidu.js', encoding='utf-8').read())
params = ctx.call('get_req_data', translate_words)
# 获取输入语言类型
detect_lang = requests.post('https://fanyi.baidu.com/langdetect', data={'query': translate_words}).json()
if detect_lang['lan'] == 'en':
    params['from'] = 'en'
    params['to'] = 'zh'
headers = {
    'Cookie': 'your cookie',
}
results = requests.post('https://fanyi.baidu.com/v2transapi?from=zh&to=en', data=params, headers=headers).json()
print(results['trans_result']['data'][0]['dst'])

baidu.js

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 b(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(""))
    }
    var window = {'gtk': '320305.131321201'}
    var r = null;
    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)
}
function get_req_data(query){
    const w = {
        from: 'zh',
        to: 'en',
        query: query,
        transtype: '',
        simple_means_flag: 3,
        sign: b(query),
        token: "d21ee97178386951469ceda3f0d7caf7",
        domain: 'common'
    }
    return w;
}
console.log(get_req_data('今天是周三'))

小结:

  • 百度翻译接口破解也很简单,和有道翻译相比要简单些,大部分参数都是固定的,只加密了一个;
  • 翻译上来看貌似有道更生动些:smile:,百度甚至把must be直译成了必须是
最后修改:2022 年 12 月 29 日
如果觉得我的文章对你有用,请随意赞赏