-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathset_native.js
More file actions
103 lines (85 loc) · 3.29 KB
/
Copy pathset_native.js
File metadata and controls
103 lines (85 loc) · 3.29 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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) {
return 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){
return set_native(func, myFunction_toString_symbol, `function ${myFunction_toString_symbol,func.name || ''}() { [native code] }`);
};
})();
// 根据上面的原理改造的 toString 保护函数,这样不用入侵每一个函数体,给那个函数对象增加东西
var saf;
;(function(){
var $toString = Function.toString
, cacheI = []
, cacheS = []
, idxI = [].indexOf.bind(cacheI)
, pushI = [].push.bind(cacheI)
, pushS = [].push.bind(cacheS)
Object.defineProperty(Function.prototype, 'toString', {
"enumerable": !1,
"configurable": !0,
"writable": !0,
"value": function toString() {
return typeof this == 'function' && cacheS[idxI(this)] || $toString.call(this);
}
})
function safe_func(func, name){
if (-1 == idxI(func)){
pushI(func)
pushS(`function ${name || func.name || ''}() { [native code] }`)
}
return func
};
safe_func(Function.prototype.toString)
saf = safe_func
})();
// 一个旧的检测 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))