Sunday, May 22, 2022

[反爬蟲原理與繞過實戰] Cookie 與 JavaScript 結合

假设访问网站/verify/cookie/content.html必须携带一个符合规定的 cookie,如果cookie不符合规定,则会被重定向到/verify/cookie/index.html去设置一个cookie,有了cookie后才能继续访问content.html。

这种方法很常见,就好比淘宝,在访问其商品列表页面前,必须登录且信息无误后才能继续访问,这种方法不仅能增加用户数,还能实现反爬。

反爬

我起初猜测它可能是这样实现的:在content.html加入一段JavaScript代码,使其能获取请求头中的cookie并判断是否符合规定,如果不符合则重定向到index.html,然后在index.html内插入一些JavaScript算法来生成cookie。

content.html

========================================================================

<body>

    <h1>This is cookie/content.html</h1>

    <script>

        var reg = /auth=[0-9]{3}[A-Z]{5}[0-9]{6}[A-Z]{3}/

        // 如果Cookie不符合要求,则跳转到首页

        if (reg.test(document.cookie) != true) {

            location.href = './index.html';

        }

    </script>

</body>

========================================================================

用浏览器测试了一下,确实可以实现重定向的操作,但是还是会先访问content.html。这种做法只能起到增长用户的目的,并不能实现反爬虫,因为python是没有JavaScript解释器的,它不会解析js代码,对于这种页面还是照爬无误。

所以还是得靠nginx,继续修改一下配置

========================================================================

location /verify/cookie/index.html {

    root /home/pineapple/Code/WebProject/www;

    index index.html;

}


location /verify/cookie/content.html {

    if ($http_cookie !~* "auth=[0-9]{3}[A-Z]{5}[0-9]{6}[A-Z]{3}") {

        rewrite content.html ./index.html redirect;

    }

    root /home/pineapple/Code/WebProject/www;

    index content.html;

  }

========================================================================

為了在重定向后给客户端设置cookie,需要在index.html里加入JavaScript代码

進行測試會發現 Cookie 值是隨機變化的

========================================================================

<!--

 * @Date         : 2020-10-22 22:38:37

 * @LastEditors  : Pineapple

 * @LastEditTime : 2020-10-23 17:30:20

 * @FilePath     : /www/verify/cookie/index.html

 * @Blog         : https://blog.csdn.net/pineapple_C

 * @Github       : https://github.com/Pineapple666

-->

<!DOCTYPE html>

<html lang="en">


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>


<body>

    <h1>This is cookie/index.html</h1>

    <h1><a href="./content.html">Go to cookie/content.html</a></h1>

    <script id="random cookie">

        function randcookie() {

            // 生成随机字符串用作cookie值

            var header = randints(9, 3, 0);

            var middle = randstrs(5);

            var footer = randints(9, 6, 0);

            var pp = randstrs(3);

            var res = header + middle + footer + pp

            return res;

        }

        function randints(r, n, tof) {

            /* 生成随机数字,tof决定返回number类型或者字符串类型

               r 代表数字范围 n 代表数量

            */


            var result = [];

            if (tof) {

                return Math.floor(Math.random() * r);

            }

            for (var i = 0; i < n; i++) {

                s = Math.floor(Math.random() * r);

                result.push(s);

            }

            return result.join('');

        }


        function randstrs(n) {

            // 生成随机字母,n为随机字母的数量

            var result = [];

            for (var i = 0; i < n; i++) {

                s = String.fromCharCode(65 + randints(25, 1, 1));

                result.push(s);

            }

            return result.join('');

        }

        // 设置Cookie

        document.cookie = 'auth=' + randcookie();

    </script>

</body>


</html>

========================================================================


No comments:

Post a Comment

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能