forked from cilame/any-whim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_native.js
72 lines (58 loc) · 2.46 KB
/
set_native.js
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// by Ossian
// 算是完美hook toString的一个方法 妙啊,当然要完美挂钩,这个函数必须最开始就运行,否则可能还是会被检测到。
;(function(){
"use strict";
const $toString = Function.toString;
const myFunction_toString_symbol = Symbol('('.concat('', ')_', (Math.random() + '').toString(36)));
const myToString = function() {
return typeof this == 'function' && this[myFunction_toString_symbol] || $toString.call(this);
};
function set_native(func, key, value) {
Object.defineProperty(func, key, {
"enumerable": false,
"configurable": true,
"writable": true,
"value": value
})
};
delete Function.prototype['toString'];
set_native(Function.prototype, "toString", myToString);
set_native(Function.prototype.toString, myFunction_toString_symbol, "function toString() { [native code] }");
(typeof global=='undefined'?window:global).func_set_natvie = function(func){
set_native(func, myFunction_toString_symbol, `function ${myFunction_toString_symbol,func.name || ''}() { [native code] }`);
};
})();
// 一个旧的检测 native 函数的方式,依赖于 Object.toString 方法是原始方法。若不是,则无法确认检测内容是否真实。
;(function() {
var toString = Object.prototype.toString;
var fnToString = Function.prototype.toString;
var reHostCtor = /^\[object .+?Constructor\]$/;
// 使用 `Object#toString` 是因为一般他不会被污染
var reNative = RegExp('^' +
String(toString)
.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$&')
.replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?')
+ '$');
function isNative(value) {
var type = typeof value;
if (type == 'function'){
return reNative.test(fnToString.call(value))
}else{
return (value && type == 'object' && reHostCtor.test(toString.call(value))) || false;
}
}
(typeof global=='undefined'?window:global).isNative = isNative;
}());
//调用方法:
function setTimeout() {};
func_set_natvie(setTimeout);
console.log(setTimeout.toString());
console.log(setTimeout.toString + '');
//单独创建 不做hook 则会返回本来的字符串:
function myfunction() {
123123123
};
console.log(myfunction.toString());
console.log(myfunction.toString + '');
console.log(isNative(myfunction))
console.log(isNative(setTimeout))