爬虫js逆向2519/20期-学习笔记

课前必备

说明:一共35节课,每节课约2h。前1/3由顾安老师讲解,后面2/3由柏汌老师讲解。

1.课程地址:https://vip.tulingpyton.cn/p/t_pc/course_pc_detail/column/p_681b49fae4b0694c5aceaa9a?product_id=p_681b49fae4b0694c5aceaa9a

2.课件地址:
(1)顾安:https://www.yuque.com/tuling_python/js_reverse/ 密码:vx6c
(2)柏汌:https://www.yuque.com/buziran/cbopse 密码:ghzd

 

课堂笔记

2026.1.28 js调试技巧

1.学会用浏览器中的“XMLHttpRequest”,主要了解其open/setRequestHeader/onreadystatechange/send方法。相关文档:https://developer.mozilla.org/zh-CN/docs/Web/API 

2.拦截器。大致写法如下:

axios = require('axios') //引入axios

// 请求拦截器
axios.interceptors.request.use(function(config){
    console.log('请求拦截器执行success,下面可写拦截后想执行的逻辑代码……')
    config.headers['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36'
    return config
}, function(error){
    console.log('请求拦截器执行error,下面可写逻辑代码……')
    return Promise.reject(error)
})

//响应拦截器
axios.interceptors.response.use(function (response){
    console.log('响应拦截器success,下面可写处理响应的代码,如解密、格式化……')
    console.log(response.data)
    return response;
},function(error){
    console.log('响应拦截器error,后面可展示错误信息……')
    return Promise.reject(error)
})

//发送请求
axios.get('https://httpbin.org')

3.在Python中执行js代码的三种方式:(1)使用“pyexecjs/pyexecjs2”库直接调用,缺点是不支持异步函数。(2)用“subprocess”库,原理:控制“终端”,间接用node执行js代码,支持异步。(3)使用“Express”Web框架,启动web服务器,返回api供调用,支持异步。

4.了解一下js中的“逗号表达式”,形如:(0,函数名)(参数1,参数2,...)。如:函数function sum(a,b){return a+b},正常调用为sum(1,2),逗号表达式调用为(0,sum)(1,2)

2025.11.26 HOOK技术

1.学会用HOOK代码XHR断点定位前端解密函数位置。HOOK定位速度快,但代码难写;XHR定位速度慢,但无需写代码。

大部分情况下,你从别人网站请求到的数据是加密的,这些数据能在别人网站上正常显示,而你获取到也是一串看不懂的字符串。这是因为别人的服务器会把这些数据先加密再传输到浏览器,浏览器再通过别人网站上的解密函数解析这些数据,因此就能正常显示了。而我要做的就是在浏览器中找到这个解密函数(这个过程很耗时),再将它保存到本地,就能用这个解密函数来破解别人的加密数据。

练习任务:定位到解密函数的位置。网站:https://www.hanghangcha.com/hhcreport(需登录)。

(1)HOOK方法:①控制台 > 源代码/来源 > 代码段,新建一个自执行函数。②翻页以触发。

// 用hook脚本使网站解密脚本debugger暂停
// 自执行函数,将其放在浏览器控制台的 “代码段 > 新代码段” 中。
(function (){
    var _parse = JSON.parse;
    JSON.parse = function (value){
        debugger;
        return _parse(value)
    }
})()

(2)XHR断点方法:①控制台 > 网络 > 筛选“Fetch/XHR”,复制api请求网址。②控制台 > 源代码/来源 > 右侧“XHR/提取断点”。③翻页以触发。

2025.9.3 浏览器调试技巧

1.JavaScript常用的Hook脚本:https://www.cnblogs.com/xiaoweigege/p/14954648.html

2.了解浏览器开发者工具的基本作用。如:元素,控制台,源代码/来源(XHR/提取断点最常用),网络,性能,内存,应用。

3.学会定位JS加密的位置,掌握了强制关闭debug模式的方法(如:右键一律不在此处暂停,js本地替换,置空方法,HOOK方法)。

2025.9.2 JS基础

1.返回多个值时,返回的是最后一个值

function return_attr(){
return 1,2,3;
console.log(return_attr()) //结果是3
}

2.重点:自执行函数,是一个没有名字的函数

// 写法一:以!开头,()结尾。
!function(){
console.log('自执行函数-1')
}();
// 写法二:直接在()里面写无名函数,再以()结尾。
(function(){
console.log('自执行函数-2')
})()

// 自执行函数模拟python中的装饰器,也可叫“闭包”
var inner_func;
!function (){
function _inner_func(){
console.log('这是一个内部函数!!')
}
inner_func = _inner_func()
}()

3.创建对象的3种方式

// 方法一:通过字典创建对象
var obj_1 = {
    "name":'对象1的名字',
    'age':18,
    'func_':function(){
        console.log(this.name,this.age)
    }
}
console.log(obj_1, obj_1.age)


// 方法二:构造函数创建对象
function Person(name,age){
    this.name = name
    this.age= age
    this.print_info = function (){
        console.log(this.name,this.age)
    }
}
var obj_2 = new Person('对象2的名字',11)
console.log(obj_2, obj_2.name)


// 方法三:直接用Object创建对象
var obj_3 = new Object()
obj_3.name = '对象3的名字'
obj_3.age = 18
obj_3.func_info = function (){
    console.log('obj_3的方法被调用了')
}
console.log(obj_3)
obj_3.func_info()

4.定时器

// 延迟执行 setTimeout(函数,时间),
// 说明:这一个异步方法,也就是说setTimeout()不会按上下文代码顺序执行,如果延迟较长,会先执行它下面的代码。
setTimeout(function (){
    console.log('i love u')
},3000)


// 周期执行 setInterval(函数,时间)
var hate = setInterval(function (){
    console.log('i hate u')
},1000)

// 关闭定时器
clearInterval(hate)

5.实现异步

//实现异步:1.Promise基本使用:
const p = new Promise((resolve,reject)=>{
 setTimeout(()=>{
  reject('error message')
  },1000)
 }).then(res=>{
  console.log(res) //Promise 状态变为 fulfilled(即resolve) ,会执行 .then 方法
 }).catch(err=>{
  console.log('err',err) //Promise 状态从 pending 变为 rejected(即reject),会执行 .catch 方法
})


//实现异步:2.async的使用:
async function a(){
 console.log('异步函数被调用了')
}
a()

6.string与json互相转换

// string转为json
var str = '{"name":"佳航","age":18}'
var json_new = JSON.parse(str)
console.log(json_new)

// json转为string
var json_raw = {"name":"佳航","age":18}
var str_new = JSON.stringify(json_raw)
console.log(str_new)

7.ajax演示,基于jquery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajax请求示例</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <style>
        .post {
            border: 1px solid darkgrey;
            margin: 10px;
            padding: 10px;
            border-radius: 5px;
        }
    </style>
</head>

<body>
<button id="get-posts-btn" onclick="getPosts()">获取文章列表</button>
<div id="postList"></div>
<div id="error"></div>

<script>
    function getPosts() {
        $.ajax({
            // 请求的URL(有些网站带cors跨域验证会导致请求失败)
            url: 'https://jsonplaceholder.typicode.com/posts',
            // 请求的方法
            method: 'GET',
            // 指定数据类型为JSON
            dataType: 'json',
            // 是否异步请求
            async: true,
            // 请求的参数, 一般发送post请求时会使用
            data: {
                // 例如:{ id: 1, name: 'John' }
            },

            // 请求成功时的回调函数
            success: function (response) {
                console.log('请求成功:', response);
                displayPosts(response);
            },
            // 请求失败时的回调函数
            error: function (xhr, status, error) {
                console.error('请求失败:', error);
                handleError(error);
            }
        });
    }

    function displayPosts(posts) {
        const postList = document.getElementById('postList');
        if (postList) {
            postList.innerHTML = posts
                .slice(0, 10) // 只显示前10篇文章
                .map(post => `
                        <div class="post">
                            <h3>${post.title}</h3>
                            <p>${post.body}</p>
                        </div>
                    `)
                .join('');  //去除默认的逗号分隔符
        }
    }

    function handleError(error) {
        const errorDiv = document.getElementById('error');
        if (errorDiv) {
            errorDiv.textContent = `获取数据失败:${error}`;
        }
    }
</script>
</body>
</html>

8.window 对象属性

// console.log(window)  在node环境中运行会报错, 因为node没有window对象(浏览器环境中的全局对象)

console.log(global);  // node环境中的全局对象

/*
* 在window对象中比较重要的几个方法
*   1.document: 文档对象, 用于操作HTML元素
*   2.navigator: 导航对象, 用于获取浏览器信息
*   3.location: 位置对象, 用于获取当前页面的URL信息
*   3.frames: 框架对象, 用于操作框架和查询框架
*   4.history: 历史对象, 用于操作浏览器的历史记录
*   5.screen: 屏幕对象, 用于获取屏幕信息
*
* 以上几个对象需要在浏览器控制台执行
* */
© 注意事项
THE END
点赞1
评论 抢沙发

请登录后发表评论

    暂无评论内容