js逆向解加密-百度翻译

523 阅读7分钟

今天尝试爬取百度翻译的翻译内容

1.需求:

手动输入单词,抓取红色框框内的数据 image.png

2.找到数据位置

这种数据一般都在json文件中

image.png

走一遍流程抓下来看看

import requests
url='https://fanyi.baidu.com/v2transapi?from=en&to=zh'
head={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
    'Cookie':'',
    'Acs-token':''
}
data={
'from':'en',
'to':'zh',
'query':'world',
'transtype':'realtime',
'simple_means_flag':'3',
'sign':'335290.130699',
'token':'7bee190dd488c11f98c03fc84ff6c2a0',
'domain':'common',
'ts':'1735817783445',
}
res=requests.post(url=url,headers=head,data=data)
print(res.json())

数据为:

image.png

观察data中的数据,query就是要翻译的单词,再重新输入query

image.png 报错了说明参数不完全。

3.找到加密参数

image.png 观察hello与world载荷的区别,发现除了需要翻译的对象和时间戳(ts)变化的只有sign,说明出问题的就是它。下一步就是搜索哪个js文件使用到它了。

搜索出现这个参数的几个文件

image.png

如图一共是有5个,排除css样式文件,就还有四个。

image.png

发现这个文件的这一部分使用的sign参数,且方框内的参数跟载荷参数大致一致,基本确定就是这个文件了。

4.js逆向解加密

逆向说白了就是什么没有补什么,去哪补呢?去含加密参数的js文件里扣

4.1 创建js文件将载荷参数都放进去。

function w(){
    return {
        from: 'en',
        to: 'zh',
        query: 'hello',
        transtype: 'realtime',
        simple_means_flag: 3,
        sign: b('hello'),//参数就是翻译的内容
        token: '79182e31123c8dcc92d0c684b2c7bf1c',
        domain: 'common',
        ts: Date.now()
    }
}
res=w()
console.log(res)

运行代码 ReferenceError: b is not defined 报错信息为这个,说明少了一个b方法

4.2 找到b方法

在调试代码的时候,选中b会出现一个弹窗

调试代码就是在sign参数前面加一个断点,再重新输一个单词 image.png 如图上面有一个链接,点进去就可以跳转b方法的位置

image.png

点一下就跳到这里来了,说明这一个就是b方法,将它复制到js文件中,并且改一下开头,因为这里只需要b方法

//开头改成这样
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(""))
    }
    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 w(){
    return {
        from: 'en',
        to: 'zh',
        query: 'hello',
        transtype: 'realtime',
        simple_means_flag: 3,
        sign: b('hello'),//参数就是翻译的内容
        token: '79182e31123c8dcc92d0c684b2c7bf1c',
        domain: 'common',
        ts: Date.now()
    }
}
res=w()
console.log(res)

再运行,报错为ReferenceError: r is not defined说明少了一个r参数

就是这一段代码

image.png

4.3 找到r参数

一般情况,这种参数是很少变的,除非是关键的参数,也可以试试,再调试几次代码,看看r有没有改变,如果会改变,就要去分析这个r是怎么生成的,也就是要找到一个方法了,这里没有变。

image.png

将window[d]换成"320305.131321201" 将r定义成null,再运行n is not defined报错为这个,在40行

image.png

可以发现n是一个方法,那么下一步就是找到n方法了

4.4 找到n方法

同上面一样,选中,点击链接就跳转到n方法了

image.png 复制到代码中,再运行,就完成了

image.png

在python中运行js代码,传入需要翻译的单词。

运行python代码

import requests
import execjs
url='https://fanyi.baidu.com/v2transapi?from=en&to=zh'
head={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'Cookie':'',
    'Acs-token':''
}
trs = input('请输入你翻译的内容:')
# 读取js文件
with open(r'baidu.js', 'r', encoding='utf-8')as f:
    js_code = f.read()
# 编译
js_code = execjs.compile(js_code)
data = js_code.call('w', trs)
# print(data)
res = requests.post(url, data=data, headers=head)
# print(res.text)
print(res.json()['trans_result']['data'][0]['dst'])

输出内容

image.png 当然,也可以将得到json数据中的完整翻译数据拿出来,但是要进行解码,详细的可以看我以前的文章,文字加密,里面有类似的例子。

好了完结撒花,我也是新手,如果有哪有问题,请大家指教(抱拳)