吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 600|回复: 3
收起左侧

[新手问题] 请教下hook cookie遇到的一个问题

[复制链接]
GAGA666 发表于 2024-11-10 22:24
我在调加速乐的时候hook cookie,加速乐需要发送两次请求,第一次请求返回一个js文件用于计算出一个cookie来;然后第二次请求的时候会带上第一次计算出来的cookie,并会返回一个新的js文件用于计算第二个cookie。
问题就出在第二次请求上,使用hook脚本之后会导致第二次请求的啥时候带不上第一次计算出来的cookie,就会导致服务器一直返回的是第一次请求的那个js文件。
我写的hook脚本贴在下面了,请教下佬们有什么好的意见吗,或者对于这种需要多次请求计算cookie的网站有什么好的处理办法吗?

(function () {
    var cookie_cache = document.cookie;
    Object.defineProperty(document, 'cookie', {
        set: function (val) {
            if (val.indexOf('jsl') != -1) {
                debugger;
            }

            console.log("hook cookie =>", val)
            var cookie = val.split(";")[0];
            var ncookie = cookie.split("=");
            var flag = false;
            var cache = cookie_cache.split("; ");
            cache = cache.map(function (a) {
                if (a.split("=")[0] === ncookie[0]) {
                    flag = true;
                    return cookie;
                }
                return a;
            })
            cookie_cache = cache.join("; ");
            if (!flag) {
                cookie_cache += cookie + "; ";
            }
            return cookie_cache;
        },
        get: function () {
            return cookie_cache;
        },
    });
})();

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

FitContent 发表于 2024-11-10 23:55
本帖最后由 FitContent 于 2024-11-10 23:59 编辑

可能是在 Set Cookie 时对设置的值进行了操作。最好不要修改网站设置的 cookie 值(除非清楚知道自己在干什么)

(function () {
    var cookie_cache = document.cookie;
    Object.defineProperty(document, 'cookie', {
        // set 操作时不需要修改 val 的值
        // 也不需要修改 cookie_cache
        set: function (val) {
            // 在这个函数里面进行条件断点等等
            // 也可以和 cookie_cache 对比知道修改了哪些地方
            process(val);
            cookie_cache = val;
            return val;
        },
        get: function () {
            return cookie_cache;
        },
    });

    /** 在这里操作 cookie */
    function process(cookie) {

    }
})();
18382747915 发表于 2024-11-11 11:20
立即更新 cookie_cache: 可以在设置 cookie 的时候立刻更新 document.cookie,而不仅是缓存到 cookie_cache 中。你可以通过直接调用 document.cookie = cookie_cache 来确保页面上能够读取到最新的 cookie 值。

使用 Set-Cookie 头:如果服务端是通过 Set-Cookie 头来返回新 cookie,那么你可以在 hook 中直接模拟该头的效果,将新的 cookie 添加到 cookie_cache。

确保 cookie 的同步:可以在 set 中调用原生的 document.cookie 方法来保证 cookie 是在页面中的最新值。你的代码中仅在 console.log 后缓存到 cookie_cache,可能需要再调用一次 document.cookie = cookie_cache。

增加对 Cookie 的直接读取:在 get 方法中可以尝试获取当前 document.cookie 值,并与 cookie_cache 合并,以确保读取到的 cookie 是最新的。

基于你的代码,我做出以下修改:

[JavaScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function () {
    var cookie_cache = document.cookie;
    Object.defineProperty(document, 'cookie', {
        set: function (val) {
            if (val.indexOf('jsl') != -1) {
                debugger;
            }
 
            console.log("hook cookie =>", val)
            var cookie = val.split(";")[0];
            var ncookie = cookie.split("=");
            var flag = false;
            var cache = cookie_cache.split("; ");
            cache = cache.map(function (a) {
                if (a.split("=")[0] === ncookie[0]) {
                    flag = true;
                    return cookie;
                }
                return a;
            });
            if (!flag) {
                cache.push(cookie);
            }
            cookie_cache = cache.join("; ");
            // 将cookie_cache立即同步到document.cookie
            document.__defineSetter__('cookie', function() {
                return cookie_cache;
            });
            return cookie_cache;
        },
        get: function () {
            return cookie_cache;
        },
    });
})();
ttdttt 发表于 2024-11-11 11:26
学习了学习了

免费评分

参与人数 1吾爱币 -50 收起 理由
爱飞的猫 -50 警告:本求助版块禁止回复『与主题无关非技术内容』,违者重罚!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-30 21:18

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表