Skip to content

Commit dc90e86

Browse files
committed
added estemplate usage for more complex AST structures
1 parent 9d6a03a commit dc90e86

8 files changed

+95
-648
lines changed

build/transformations/codeEncryption.js

+19-244
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var expressionObfuscation_1 = __importDefault(require("./expressionObfuscation")
3333
var literalObfuscation_1 = __importDefault(require("./literalObfuscation"));
3434
var numberObfuscation_1 = __importDefault(require("./numberObfuscation"));
3535
var unicodeLiteral_1 = __importDefault(require("./unicodeLiteral"));
36+
var estemplate = require('estemplate');
3637
var CodeEncryption = /** @class */ (function (_super) {
3738
__extends(CodeEncryption, _super);
3839
function CodeEncryption() {
@@ -165,136 +166,14 @@ var CodeEncryption = /** @class */ (function (_super) {
165166
* @memberof CodeEncryption
166167
*/
167168
CodeEncryption.prototype.generateClientKeyDecl = function (keyIdent, keyFuncDecl) {
168-
var resultIdent = identifiers_1.Identifiers.generate();
169-
var numIdent = identifiers_1.Identifiers.generate();
170-
var indexIdent = identifiers_1.Identifiers.generate();
171-
return {
172-
type: 'VariableDeclaration',
173-
declarations: [
174-
{
175-
type: 'VariableDeclarator',
176-
id: {
177-
type: 'Identifier',
178-
name: keyIdent
179-
},
180-
init: {
181-
type: 'CallExpression',
182-
callee: {
183-
type: 'MemberExpression',
184-
object: {
185-
type: 'CallExpression',
186-
callee: {
187-
type: 'MemberExpression',
188-
object: {
189-
type: 'CallExpression',
190-
callee: {
191-
type: 'MemberExpression',
192-
object: {
193-
type: 'Identifier',
194-
name: keyFuncDecl.id ? keyFuncDecl.id.name : ''
195-
},
196-
property: {
197-
type: 'Literal',
198-
value: 'toString'
199-
},
200-
computed: true
201-
},
202-
arguments: []
203-
},
204-
property: {
205-
type: 'Literal',
206-
value: 'split'
207-
},
208-
computed: true
209-
},
210-
arguments: [
211-
{
212-
type: 'Literal',
213-
value: ''
214-
}
215-
]
216-
},
217-
property: {
218-
type: 'Literal',
219-
value: 'reduce'
220-
},
221-
computed: true
222-
},
223-
arguments: [
224-
{
225-
type: 'FunctionExpression',
226-
id: null,
227-
generator: false,
228-
params: [
229-
{
230-
type: 'Identifier',
231-
name: resultIdent
232-
},
233-
{
234-
type: 'Identifier',
235-
name: numIdent
236-
},
237-
{
238-
type: 'Identifier',
239-
name: indexIdent
240-
}
241-
],
242-
body: {
243-
type: 'BlockStatement',
244-
body: [
245-
{
246-
type: 'ReturnStatement',
247-
argument: {
248-
type: 'BinaryExpression',
249-
left: {
250-
type: 'Identifier',
251-
name: resultIdent
252-
},
253-
operator: '^',
254-
right: {
255-
type: 'BinaryExpression',
256-
operator: '+',
257-
left: {
258-
type: 'CallExpression',
259-
callee: {
260-
type: 'MemberExpression',
261-
object: {
262-
type: 'Identifier',
263-
name: numIdent
264-
},
265-
property: {
266-
type: 'Literal',
267-
value: 'charCodeAt'
268-
},
269-
computed: true
270-
},
271-
arguments: [
272-
{
273-
type: 'Literal',
274-
value: 0
275-
}
276-
]
277-
},
278-
right: {
279-
type: 'Identifier',
280-
name: indexIdent
281-
}
282-
}
283-
}
284-
}
285-
]
286-
}
287-
},
288-
{
289-
type: 'Literal',
290-
value: 0
291-
}
292-
]
293-
}
294-
}
295-
],
296-
kind: 'var'
297-
};
169+
var template = "var <%= varIdent %> = <%= funcDecl %>['toString']()['split']('')['reduce'](function(<%= resultIdent %>, <%= numIdent %>, <%= indexIdent %>) {return <%= resultIdent %> ^ <%= numIdent %>['charCodeAt'](0) + <%= indexIdent %>}, 0);";
170+
return estemplate(template, {
171+
varIdent: { type: 'Identifier', name: keyIdent },
172+
funcDecl: keyFuncDecl.id,
173+
resultIdent: { type: 'Identifier', name: identifiers_1.Identifiers.generate() },
174+
numIdent: { type: 'Identifier', name: identifiers_1.Identifiers.generate() },
175+
indexIdent: { type: 'Identifier', name: identifiers_1.Identifiers.generate() }
176+
}).body[0];
298177
};
299178
/**
300179
* @protected
@@ -318,90 +197,12 @@ var CodeEncryption = /** @class */ (function (_super) {
318197
value: value.charCodeAt(0) ^ key
319198
});
320199
});
321-
var encryptedArrayExpression = {
322-
type: 'ArrayExpression',
323-
elements: encrypted
324-
};
325-
var valueIdent = identifiers_1.Identifiers.generate();
326-
return {
327-
type: 'CallExpression',
328-
callee: {
329-
type: 'MemberExpression',
330-
object: {
331-
type: 'CallExpression',
332-
callee: {
333-
type: 'MemberExpression',
334-
object: encryptedArrayExpression,
335-
property: {
336-
type: 'Literal',
337-
value: 'map'
338-
},
339-
computed: true
340-
},
341-
arguments: [
342-
{
343-
type: 'FunctionExpression',
344-
id: null,
345-
generator: false,
346-
params: [
347-
{
348-
type: 'Identifier',
349-
name: valueIdent
350-
}
351-
],
352-
body: {
353-
type: 'BlockStatement',
354-
body: [
355-
{
356-
type: 'ReturnStatement',
357-
argument: {
358-
type: 'CallExpression',
359-
callee: {
360-
type: 'MemberExpression',
361-
object: {
362-
type: 'Identifier',
363-
name: 'String'
364-
},
365-
property: {
366-
type: 'Literal',
367-
value: 'fromCharCode'
368-
},
369-
computed: true
370-
},
371-
arguments: [
372-
{
373-
type: 'BinaryExpression',
374-
left: {
375-
type: 'Identifier',
376-
name: valueIdent
377-
},
378-
operator: '^',
379-
right: {
380-
type: 'Identifier',
381-
name: keyIdent
382-
}
383-
}
384-
]
385-
}
386-
}
387-
]
388-
}
389-
}
390-
]
391-
},
392-
property: {
393-
type: 'Literal',
394-
value: 'join'
395-
},
396-
computed: true
397-
},
398-
arguments: [
399-
{
400-
type: 'Literal',
401-
value: ''
402-
}
403-
]
404-
};
200+
var template = "[%= elements %]['map'](function(<%= valueIdent %>) {return String['fromCharCode'](<%= valueIdent %> ^ <%= key %>)})['join']('')";
201+
return estemplate(template, {
202+
elements: encrypted,
203+
valueIdent: { type: 'Identifier', name: identifiers_1.Identifiers.generate() },
204+
key: { type: 'Identifier', name: keyIdent }
205+
}).body[0].expression;
405206
};
406207
/**
407208
* @protected
@@ -410,36 +211,10 @@ var CodeEncryption = /** @class */ (function (_super) {
410211
* @memberof CodeEncryption
411212
*/
412213
CodeEncryption.prototype.generateTryCatchExpression = function (blockCodeDecryptExpr) {
413-
return {
414-
type: 'TryStatement',
415-
block: {
416-
type: 'BlockStatement',
417-
body: [
418-
{
419-
type: 'ExpressionStatement',
420-
expression: {
421-
type: 'CallExpression',
422-
callee: {
423-
type: 'Identifier',
424-
name: 'eval'
425-
},
426-
arguments: [blockCodeDecryptExpr]
427-
}
428-
}
429-
]
430-
},
431-
handler: {
432-
type: 'CatchClause',
433-
param: {
434-
type: 'Identifier',
435-
name: 'e'
436-
},
437-
body: {
438-
type: 'BlockStatement',
439-
body: []
440-
}
441-
}
442-
};
214+
var template = 'try {eval(<%= block %>)} catch (e){}';
215+
return estemplate(template, {
216+
block: blockCodeDecryptExpr
217+
}).body[0];
443218
};
444219
return CodeEncryption;
445220
}(transformations_1.BaseTransformation));

build/transformations/debuggerBreakpointLoop.js

+9-49
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var __extends = (this && this.__extends) || (function () {
1515
var identifiers_1 = require("../identifiers");
1616
var insertPosition_1 = require("../insertPosition");
1717
var transformations_1 = require("../transformations");
18+
var estemplate = require('estemplate');
1819
var DebuggerBreakpointLoop = /** @class */ (function (_super) {
1920
__extends(DebuggerBreakpointLoop, _super);
2021
function DebuggerBreakpointLoop() {
@@ -26,55 +27,14 @@ var DebuggerBreakpointLoop = /** @class */ (function (_super) {
2627
*/
2728
DebuggerBreakpointLoop.prototype.apply = function () {
2829
var funcDeclIdent = identifiers_1.Identifiers.generate();
29-
var loopFuncDecl = {
30-
type: 'FunctionDeclaration',
31-
id: {
32-
type: 'Identifier',
33-
name: funcDeclIdent
34-
},
35-
params: [],
36-
body: {
37-
type: 'BlockStatement',
38-
body: [
39-
{
40-
type: 'ExpressionStatement',
41-
expression: {
42-
type: 'CallExpression',
43-
callee: {
44-
type: 'Identifier',
45-
name: 'eval'
46-
},
47-
arguments: [
48-
{
49-
type: 'Literal',
50-
value: 'debugger'
51-
}
52-
]
53-
}
54-
}
55-
]
56-
}
57-
};
58-
var loopExpr = {
59-
type: 'ExpressionStatement',
60-
expression: {
61-
type: 'CallExpression',
62-
callee: {
63-
type: 'Identifier',
64-
name: 'setInterval'
65-
},
66-
arguments: [
67-
{
68-
type: 'Identifier',
69-
name: funcDeclIdent
70-
},
71-
{
72-
type: 'Literal',
73-
value: 500
74-
}
75-
]
76-
}
77-
};
30+
var funcTemplate = 'function <%= funcIdent %>(){eval("debugger");}';
31+
var loopFuncDecl = estemplate(funcTemplate, {
32+
funcIdent: { type: 'Identifier', name: funcDeclIdent }
33+
}).body[0];
34+
var loopTemplate = 'setInterval(<%= funcIdent %>, 500);';
35+
var loopExpr = estemplate(loopTemplate, {
36+
funcIdent: { type: 'Identifier', name: funcDeclIdent }
37+
}).body[0];
7838
this.ast.body.splice(insertPosition_1.InsertPosition.get(), 0, loopFuncDecl);
7939
this.ast.body.splice(insertPosition_1.InsertPosition.get(), 0, loopExpr);
8040
return this.ast;

0 commit comments

Comments
 (0)