Skip to content

Commit

Permalink
add: JS Hook 2023.10.16
Browse files Browse the repository at this point in the history
  • Loading branch information
TRHX committed Oct 15, 2023
1 parent 6234187 commit bdc0b15
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 101 deletions.
7 changes: 4 additions & 3 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ module.exports = {
]
},
{
text: '框架接口', link: '/pages/re/', items: [
text: '框架接口', link: '/pages/selenium/', items: [
{
text: '自动化框架', items: [
{ text: 'selenium', link: '/pages/selenium/' },
Expand Down Expand Up @@ -169,7 +169,7 @@ module.exports = {
]
},
{
text: '脚本命令', link: '/pages/re/', items: [
text: '脚本命令', link: '/pages/js-hook/', items: [
{
text: '辅助脚本', items: [
{ text: 'JS Hook', link: '/pages/js-hook/' },
Expand Down Expand Up @@ -200,6 +200,7 @@ module.exports = {
},
]
},
{ text: '报错处理', link: '/pages/error-handling/' },
{ text: '赞助', link: '/pages/sponsor/' },
],
sidebarDepth: 2, // 侧边栏显示深度,默认1,最大2(显示到h3标题)
Expand Down Expand Up @@ -346,7 +347,7 @@ module.exports = {
],

markdown: {
// lineNumbers: true,
lineNumbers: true,
extractHeaders: ['h2', 'h3', 'h4', 'h5', 'h6'], // 提取标题到侧边栏的级别,默认['h2', 'h3']
},

Expand Down
Binary file added docs/.vuepress/public/img/geekbyte.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/01.目录/01.目录/01.目录.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: 目录
date: 2020-05-12 14:57:21
permalink: /pages/toc
article: false
sidebar: false
---

## 目录
Expand Down
349 changes: 349 additions & 0 deletions docs/05.脚本命令/01.辅助脚本/01.JS HOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,352 @@ date: 2020-05-14 11:39:45
permalink: /pages/js-hook
article: false
---

## Hook Cookie

::: danger 注意
不推荐方法二,部分特性可能正准备或者已经从相关的 web 标准中移除,参考文档:[MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__)
:::

<code-group>
<code-block title="方法一" active>
```javascript
(function () {
var cookieCache = "";
Object.defineProperty(document, "cookie", {
set: function (val) {
console.log("Hook set cookie => ", val);
if (val.indexOf("spiderapi.cn") !== -1) {
debugger;
}
cookieCache = val;
return val;
},
get: function () {
return cookieCache;
}
});
})();
```
</code-block>

<code-block title="方法二">
```javascript
(function () {
var cookieCache = document.cookie.__lookupSetter__("cookie");
document.__defineSetter__("cookie", function (val) {
console.log("Hook set cookie => ", val);
if (val.indexOf("spiderapi.cn") !== -1) {
debugger;
}
cookieCache = val;
});
document.__defineGetter__("cookie", function () {
return cookieCache;
});
})();
```
</code-block>
</code-group>

## Hook Request Header

```javascript
(function () {
var headerCache = window.XMLHttpRequest.prototype.setRequestHeader;
window.XMLHttpRequest.prototype.setRequestHeader = function (key, value) {
console.log("Hook set header %s => %s", key, value);
if (key === "spiderapi.cn") {
debugger;
}
return headerCache.apply(this, arguments);
};
})();
```

## Hook Debugger

<code-group>
<code-block title="方法一:constructor" active>
```javascript
(function () {
var constructorCache = Function.prototype.constructor;
Function.prototype.constructor = function (string) {
if (string === "debugger") {
console.log("Hook constructor debugger!")
return function () {};
}
return constructorCache(string);
};
})();
```
</code-block>

<code-block title="方法二:setInterval">
```javascript
(function () {
var setIntervalCache = setInterval
setInterval = function (func, delay) {
if (func.toString().indexOf("debugger") !== -1) {
console.log("Hook setInterval debugger!")
return function () {};
}
return setIntervalCache(func, delay)
};
})();
```
</code-block>

<code-block title="方法三:setTimeout">
```javascript
(function () {
var setTimeoutCache = setTimeout;
setTimeout = function (func, delay) {
if (func.toString().indexOf("debugger") !== -1) {
console.log("Hook setTimeout debugger!")
return function () {};
}
return setTimeoutCache(func, delay);
};
})();
```
</code-block>

<code-block title="方法四:eval">
```javascript
(function () {
var evalCache = window.eval;
window.eval = function (string) {
if(string.includes("debugger")){
console.log("Hook eval debugger!")
}
return evalCache(string.replace(/debugger\s*;?/g, ""));
};
window.eval.toString = function () {
return evalCache.toString();
};
})();
```
</code-block>
</code-group>

## Hook XHR

```javascript
(function () {
var openCache = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url) {
console.log("Hook xhr method => %s, url => %s", method, url);
if (url.indexOf("spiderapi.cn") !== -1) {
debugger;
}
return openCache.apply(this, arguments);
};
})();
```

## Hook fetch

```javascript
(function() {
var fetchCache = Object.getOwnPropertyDescriptor(window, "fetch")
Object.defineProperty(window, "fetch", {
value: function (url) {
console.log("Hook fetch url => ", url)
debugger;
return fetchCache.value.apply(this, arguments)
}
})
})();
```

## Hook JSON.stringify

```javascript
(function() {
var stringifyCache = JSON.stringify;
JSON.stringify = function(params) {
console.log("Hook JSON.stringify => ", params);
debugger;
return stringifyCache(params);
}
})();
```

## Hook JSON.parse

```javascript
(function() {
var parseCache = JSON.parse;
JSON.parse = function(params) {
console.log("Hook JSON.parse => ", params);
debugger;
return parseCache(params);
}
})();
```

## Hook Function

以下代码执行后,所有的函数操作都会在控制台打印输出将要执行的 JS 源码。

```javascript
(function() {
let FunctionCache = window.Function;
let newFunction = function() {
let src = arguments[arguments.length - 1];
console.log("Hook Function => ", src);
debugger;
return FunctionCache.apply(this, arguments);
};
newFunction.toString = function() {
return FunctionCache.toString();
};
})();
```

## Hook WebSocket

```javascript
(function() {
let sendCache = WebSocket.prototype.send;
WebSocket.prototype.send = function (data){
console.info("Hook WebSocket send => ", data);
return sendCache(data)
};
})();
```

## Hook eval

```javascript
(function () {
var evalCache = window.eval;
window.eval = function (string) {
console.log("Hook eval =>", string);
debugger;
return evalCache(string);
};
window.eval.toString = function () {
return evalCache.toString();
};
})();
```

## Hook setInterval

```javascript
(function () {
var setIntervalCache = setInterval;
setInterval = function (func, delay) {
console.log("Hook setInterval func => %s, delay => %s", func, delay);
debugger;
return setIntervalCache(func, delay);
};
})();
```

## Hook setTimeout

```javascript
(function () {
var setTimeoutCache = setTimeout;
setTimeout = function (func, delay) {
console.log("Hook setTimeout func => %s, delay => %s", func, delay);
debugger;
return setTimeoutCache(func, delay);
};
})();
```

## Hook RegExp

```javascript
(function () {
let RegExpCache = RegExp;
RegExp = function(pattern, flags) {
console.log("Hook RegExp pattern => %s, flags => %s", pattern, flags);
debugger;
return RegExpCache(pattern, flags);
};
})();
```

## Hook Canvas

```javascript
(function() {
let createElementCache = document.createElement;
document.createElement = function(tagName) {
console.info("Hook createElement tagName => ", tagName);
if(tagName === "canvas") {
debugger;
}
return createElementCache(tagName);
};
})();
```

## Hook createElement

```javascript
(function() {
var createElementCache = document.createElement;
document.createElement = function(tagName) {
console.info("Hook createElement tagName => ", tagName);
if(tagName === "div") {
debugger;
}
return createElementCache(tagName);
};
})();
```

## Hook getElementById

```javascript
(function() {
var getElementByIdCache = document.getElementById;
document.getElementById = function(id) {
console.info("Hook getElementById id => ", id);
if(id === "spiderapi") {
debugger;
}
return getElementByIdCache(id);
};
})();
```

## Hook setAttribute

```javascript
(function() {
var setAttributeCache = window.Element.prototype.setAttribute;
window.Element.prototype.setAttribute = function(name, value) {
console.info("Hook setAttribute name => %s, value => %s", name, value);
if(name === "spiderapi") {
debugger;
}
return setAttributeCache(name, value);
};
})();
```

## 清除定时器

```javascript
for (let i = 1; i < 99999; i++) window.clearInterval(i);
```


## Hook 通用模板

```javascript
(function () {
var oldFunc = func;
func = function(arguments){
console.log(arguments)
return oldFunc.apply(arguments)
};
})();
```
Loading

0 comments on commit bdc0b15

Please sign in to comment.