forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfDefendingTemplate.ts
More file actions
66 lines (57 loc) · 3.07 KB
/
Copy pathSelfDefendingTemplate.ts
File metadata and controls
66 lines (57 loc) · 3.07 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
import { IEscapeSequenceEncoder } from '../../../../interfaces/utils/IEscapeSequenceEncoder';
import { IRandomGenerator } from '../../../../interfaces/utils/IRandomGenerator';
/**
* @param {IRandomGenerator} randomGenerator
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
* @returns {string}
* @constructor
*/
export function SelfDefendingTemplate (
randomGenerator: IRandomGenerator,
escapeSequenceEncoder: IEscapeSequenceEncoder
): string {
const identifierLength: number = 6;
const rc4BytesIdentifier: string = randomGenerator.getRandomString(identifierLength);
const statesIdentifier: string = randomGenerator.getRandomString(identifierLength);
const newStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const firstStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const secondStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const checkStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const runStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const getStateIdentifier: string = randomGenerator.getRandomString(identifierLength);
const stateResultIdentifier: string = randomGenerator.getRandomString(identifierLength);
return `
const StatesClass = function (${rc4BytesIdentifier}) {
this.${rc4BytesIdentifier} = ${rc4BytesIdentifier};
this.${statesIdentifier} = [1, 0, 0];
this.${newStateIdentifier} = function(){return 'newState';};
this.${firstStateIdentifier} = '${
escapeSequenceEncoder.encode('\\w+ *\\(\\) *{\\w+ *', true)
}';
this.${secondStateIdentifier} = '${
escapeSequenceEncoder.encode('[\'|"].+[\'|"];? *}', true)
}';
};
StatesClass.prototype.${checkStateIdentifier} = function () {
const regExp = new RegExp(this.${firstStateIdentifier} + this.${secondStateIdentifier});
const expression = regExp.test(this.${newStateIdentifier}.toString())
? --this.${statesIdentifier}[1]
: --this.${statesIdentifier}[0];
return this.${runStateIdentifier}(expression);
};
StatesClass.prototype.${runStateIdentifier} = function (${stateResultIdentifier}) {
if (!Boolean(~${stateResultIdentifier})) {
return ${stateResultIdentifier};
}
return this.${getStateIdentifier}(this.${rc4BytesIdentifier});
};
StatesClass.prototype.${getStateIdentifier} = function (${rc4BytesIdentifier}) {
for (let i = 0, len = this.${statesIdentifier}.length; i < len; i++) {
this.${statesIdentifier}.push(Math.round(Math.random()));
len = this.${statesIdentifier}.length;
}
return ${rc4BytesIdentifier}(this.${statesIdentifier}[0]);
};
new StatesClass({stringArrayCallsWrapperName}).${checkStateIdentifier}();
`;
}