• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

爬虫——某翻译网站参数sign的构造

武飞扬头像
S. 先生
帮助1

1.网页分析

该翻译网站为进行Ajax加载的网站,针对这种网页的爬取,一般有两种方式:

①使用Selenium等模拟浏览器的方式进行爬取,这种方式实现起来较为简单,但是爬取速度相对较慢。

②直接对网站的接口进行请求,爬取速度相对较快。但是某些网站的请求表单里会附带一些例如token、sign等字段的加密参数,针对这种网站,需要使用js逆向对加密参数进行解密,弄清楚加密参数如何构造,实现难度较大。

本案例中的请求接口中就存在一些加密参数,这里重点模拟构造其中的sign参数。

首先进入网站,打开浏览器开发者工具(F12),接着输入待翻译的内容,点击翻译按钮

学新通

进入浏览器开发者工具的Network面板,开始抓包分析。选中XHR,逐个查看请求,发现一个名为v2transapi?from=en&to=zh的请求,进入该请求查看详细内容。

学新通

该请求为POST请求,查看响应内容,发现响应内容中包含了我们想要获取的已翻译的内容。

学新通

查看表单数据,分析请求该接口的表单内容包含哪些数据

学新通

其中,from是待翻译内容语言,to是需要翻译成的语言,sign、token是一个加密参数,query是待翻译内容,多次请求实验后可以发现,token参数短期内保持不变,sign参数随着query字段的变化而变化。下面目标就明确了,重点找出sign参数是如何构造,即可完成模拟请求,爬取数据。

 2.寻找加密入口

使用搜索功能,搜索关键字sign。

学新通

可以看到查找出5个有关结果,逐个进入查找到的文件,在文件内搜索sign关键字

学新通 在第三个搜索结果的内部,找到了疑似表单数据构造的方法,再这里下一个断点,再次点击翻译按钮,刷新页面。

学新通

可以看到在这里成功断住了,然后在右侧watch面板做添加变量w,进行单步调试,查看变量w的内容。可以发现,w中的sign与之前表单中的一致,说明加密入口就在这里。鼠标放到sign的构造方法b(e)上,进入方法内部。

学新通

3.扣取js代码

进入sign的构造方法内部,查看加密过程:

学新通

可以看到整体加密过程比较复杂,使用python重写整个加密逻辑难度较大。这里将这段js代码截取出来保存到本地,使用pyexecjs库对js进行运行。

截取出的js代码:

  1.  
    sign = function(t) {
  2.  
    var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
  3.  
    if (null === i) {
  4.  
    var a = t.length;
  5.  
    a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
  6.  
    } else {
  7.  
    for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, l = s.length, u = []; c < l; c )
  8.  
    "" !== s[c] && u.push.apply(u, function(t) {
  9.  
    if (Array.isArray(t))
  10.  
    return e(t)
  11.  
    }(o = s[c].split("")) || function(t) {
  12.  
    if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
  13.  
    return Array.from(t)
  14.  
    }(o) || function(t, n) {
  15.  
    if (t) {
  16.  
    if ("string" == typeof t)
  17.  
    return e(t, n);
  18.  
    var r = Object.prototype.toString.call(t).slice(8, -1);
  19.  
    return "Object" === r && t.constructor && (r = t.constructor.name),
  20.  
    "Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
  21.  
    }
  22.  
    }(o) || function() {
  23.  
    throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  24.  
    }()),
  25.  
    c !== l - 1 && u.push(i[c]);
  26.  
    var p = u.length;
  27.  
    p > 30 && (t = u.slice(0, 10).join("") u.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) 5).join("") u.slice(-10).join(""))
  28.  
    }
  29.  
    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 ) {
  30.  
    var _ = t.charCodeAt(v);
  31.  
    _ < 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)),
  32.  
    g[y ] = _ >> 18 | 240,
  33.  
    g[y ] = _ >> 12 & 63 | 128) : g[y ] = _ >> 12 | 224,
  34.  
    g[y ] = _ >> 6 & 63 | 128),
  35.  
    g[y ] = 63 & _ | 128)
  36.  
    }
  37.  
    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 )
  38.  
    b = n(b = g[x], w);
  39.  
    return b = n(b, k),
  40.  
    (b ^= m) < 0 && (b = 2147483648 (2147483647 & b)),
  41.  
    "".concat((b %= 1e6).toString(), ".").concat(b ^ f)
  42.  
    }
学新通

在Python编写调用执行js的代码:

  1.  
     
  2.  
    #百度翻译JS逆向
  3.  
    import execjs
  4.  
     
  5.  
     
  6.  
    word = 'who are you'
  7.  
     
  8.  
     
  9.  
    with open(r'/home/kylin/PycharmProjects/untitled/百度翻译sign加密.js', encoding='utf-8') as js:
  10.  
    js_content = js.read()
  11.  
    compile = execjs.compile(js_content)
  12.  
    sign = compile.call('sign', word)
  13.  
    print(sign)

运行代码,发现报错

学新通

提示变量r没有定义,返回网站,改变翻译内容进行多次调试,可以发现变量r是个定值,因此直接在js代码里定义变量r即可。再次运行python代码,发现依然报错

学新通

这里的h实际上也是一个固定的列表,实际上列表的第一个元素就是r的整数部分,列表的第二个元素就是r的小数部分,这里为了方便直接将h后的语句替换成列表即可。修改完成后继续调试

学新通

 发现提示n没有定义,回去翻看js代码发现n是一个方法,在网页的js代码中查找,发现方法n的定义就在我们sign加密方法的上方,将其扣取下来放到我们的js文件里学新通

再次运行python代码,发现成功输出了sign的值,与网页中的sign完全一致,到此就完成的sign加密参数的构造。

本地完整js代码

  1.  
    var r = 320305.131321201
  2.  
    sign = function(t) {
  3.  
    var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
  4.  
    if (null === i) {
  5.  
    var a = t.length;
  6.  
    a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
  7.  
    } else {
  8.  
    for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, l = s.length, u = []; c < l; c )
  9.  
    "" !== s[c] && u.push.apply(u, function(t) {
  10.  
    if (Array.isArray(t))
  11.  
    return e(t)
  12.  
    }(o = s[c].split("")) || function(t) {
  13.  
    if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
  14.  
    return Array.from(t)
  15.  
    }(o) || function(t, n) {
  16.  
    if (t) {
  17.  
    if ("string" == typeof t)
  18.  
    return e(t, n);
  19.  
    var r = Object.prototype.toString.call(t).slice(8, -1);
  20.  
    return "Object" === r && t.constructor && (r = t.constructor.name),
  21.  
    "Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
  22.  
    }
  23.  
    }(o) || function() {
  24.  
    throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  25.  
    }()),
  26.  
    c !== l - 1 && u.push(i[c]);
  27.  
    var p = u.length;
  28.  
    p > 30 && (t = u.slice(0, 10).join("") u.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) 5).join("") u.slice(-10).join(""))
  29.  
    }
  30.  
    for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h =[320305,131321201], f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v ) {
  31.  
    var _ = t.charCodeAt(v);
  32.  
    _ < 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)),
  33.  
    g[y ] = _ >> 18 | 240,
  34.  
    g[y ] = _ >> 12 & 63 | 128) : g[y ] = _ >> 12 | 224,
  35.  
    g[y ] = _ >> 6 & 63 | 128),
  36.  
    g[y ] = 63 & _ | 128)
  37.  
    }
  38.  
    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 )
  39.  
    b = n(b = g[x], w);
  40.  
    return b = n(b, k),
  41.  
    (b ^= m) < 0 && (b = 2147483648 (2147483647 & b)),
  42.  
    "".concat((b %= 1e6).toString(), ".").concat(b ^ f)
  43.  
    }
  44.  
    function n(t, e) {
  45.  
    for (var n = 0; n < e.length - 2; n = 3) {
  46.  
    var r = e.charAt(n 2);
  47.  
    r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
  48.  
    r = " " === e.charAt(n 1) ? t >>> r : t << r,
  49.  
    t = " " === e.charAt(n) ? t r & 4294967295 : t ^ r
  50.  
    }
  51.  
    return t
  52.  
    }
学新通

 Python代码:

  1.  
     
  2.  
    #百度翻译JS逆向
  3.  
    import execjs
  4.  
     
  5.  
     
  6.  
    word = 'who are you'
  7.  
     
  8.  
     
  9.  
    with open(r'/home/kylin/PycharmProjects/untitled/百度翻译sign加密.js', encoding='utf-8') as js:
  10.  
    js_content = js.read()
  11.  
    compile = execjs.compile(js_content)
  12.  
    sign = compile.call('sign', word)
  13.  
    print(sign)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcjekf
系列文章
更多 icon
同类精品
更多 icon
继续加载