@@ -11,17 +11,12 @@ import {
11
11
Literal ,
12
12
UnaryExpression ,
13
13
NewExpression ,
14
- FunctionDeclaration ,
15
- ReturnStatement ,
16
14
VariableDeclaration ,
17
- ObjectExpression ,
18
- Property ,
19
- ArrayExpression ,
20
- FunctionExpression ,
21
15
ThisExpression ,
22
16
VariableDeclarator ,
23
17
Location ,
24
18
LogicalExpression ,
19
+ SequenceExpression ,
25
20
} from "../../util/gen" ;
26
21
import traverse , { getBlock , isBlock } from "../../traverse" ;
27
22
import { choice , getRandomInteger } from "../../util/random" ;
@@ -47,6 +42,12 @@ export default class Lock extends Transform {
47
42
counterMeasuresNode : Location ;
48
43
iosDetectFn : string ;
49
44
45
+ /**
46
+ * This is a boolean variable injected into the source code determining wether the countermeasures function has been called.
47
+ * This is used to prevent infinite loops from happening
48
+ */
49
+ counterMeasuresActivated : string ;
50
+
50
51
made : number ;
51
52
52
53
constructor ( o ) {
@@ -73,36 +74,32 @@ export default class Lock extends Transform {
73
74
typeof this . options . lock . countermeasures === "string" &&
74
75
isValidIdentifier ( this . options . lock . countermeasures )
75
76
) {
76
- var defined = new Set < string > ( ) ;
77
77
traverse ( tree , ( object , parents ) => {
78
- if ( object . type == "Identifier" ) {
78
+ if (
79
+ object . type == "Identifier" &&
80
+ object . name === this . options . lock . countermeasures
81
+ ) {
79
82
var info = getIdentifierInfo ( object , parents ) ;
80
83
if ( info . spec . isDefined ) {
81
- defined . add ( object . name ) ;
82
- if ( object . name === this . options . lock . countermeasures ) {
83
- if ( this . counterMeasuresNode ) {
84
+ if ( this . counterMeasuresNode ) {
85
+ throw new Error (
86
+ "Countermeasures function was already defined, it must have a unique name from the rest of your code"
87
+ ) ;
88
+ } else {
89
+ var definingContext = getVarContext ( parents [ 0 ] , parents . slice ( 1 ) ) ;
90
+ if ( definingContext != tree ) {
84
91
throw new Error (
85
- "Countermeasures function was already defined, it must have a unique name from the rest of your code"
86
- ) ;
87
- } else {
88
- var definingContext = getVarContext (
89
- parents [ 0 ] ,
90
- parents . slice ( 1 )
92
+ "Countermeasures function must be defined at the global level"
91
93
) ;
92
- if ( definingContext != tree ) {
93
- throw new Error (
94
- "Countermeasures function must be defined at the global level"
95
- ) ;
96
- }
97
- var chain : Location = [ object , parents ] ;
98
- if ( info . isFunctionDeclaration ) {
99
- chain = [ parents [ 0 ] , parents . slice ( 1 ) ] ;
100
- } else if ( info . isVariableDeclaration ) {
101
- chain = [ parents [ 1 ] , parents . slice ( 2 ) ] ;
102
- }
103
-
104
- this . counterMeasuresNode = chain ;
105
94
}
95
+ var chain : Location = [ object , parents ] ;
96
+ if ( info . isFunctionDeclaration ) {
97
+ chain = [ parents [ 0 ] , parents . slice ( 1 ) ] ;
98
+ } else if ( info . isVariableDeclaration ) {
99
+ chain = [ parents [ 1 ] , parents . slice ( 2 ) ] ;
100
+ }
101
+
102
+ this . counterMeasuresNode = chain ;
106
103
}
107
104
}
108
105
}
@@ -120,7 +117,7 @@ export default class Lock extends Transform {
120
117
super . apply ( tree ) ;
121
118
}
122
119
123
- getCounterMeasuresCode ( ) : Node [ ] {
120
+ getCounterMeasuresCode ( object : Node , parents : Node [ ] ) : Node [ ] {
124
121
var opt = this . options . lock . countermeasures ;
125
122
126
123
if ( opt === false ) {
@@ -129,10 +126,30 @@ export default class Lock extends Transform {
129
126
130
127
// Call function
131
128
if ( typeof opt === "string" ) {
129
+ if ( ! this . counterMeasuresActivated ) {
130
+ this . counterMeasuresActivated = this . getPlaceholder ( ) ;
131
+
132
+ prepend (
133
+ parents [ parents . length - 1 ] || object ,
134
+ VariableDeclaration ( VariableDeclarator ( this . counterMeasuresActivated ) )
135
+ ) ;
136
+ }
137
+
132
138
// Since Lock occurs before variable renaming, we are using the pre-obfuscated function name
133
139
return [
134
140
ExpressionStatement (
135
- CallExpression ( Template ( opt ) . single ( ) . expression , [ ] )
141
+ LogicalExpression (
142
+ "||" ,
143
+ Identifier ( this . counterMeasuresActivated ) ,
144
+ SequenceExpression ( [
145
+ AssignmentExpression (
146
+ "=" ,
147
+ Identifier ( this . counterMeasuresActivated ) ,
148
+ Literal ( true )
149
+ ) ,
150
+ CallExpression ( Template ( opt ) . single ( ) . expression , [ ] ) ,
151
+ ] )
152
+ )
136
153
) ,
137
154
] ;
138
155
}
@@ -288,7 +305,7 @@ export default class Lock extends Transform {
288
305
nodes . push (
289
306
IfStatement (
290
307
callExpression ,
291
- this . getCounterMeasuresCode ( ) || [ ] ,
308
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
292
309
null
293
310
)
294
311
) ;
@@ -324,7 +341,11 @@ export default class Lock extends Transform {
324
341
}
325
342
326
343
nodes . push (
327
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
344
+ IfStatement (
345
+ test ,
346
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
347
+ null
348
+ )
328
349
) ;
329
350
}
330
351
@@ -338,7 +359,11 @@ export default class Lock extends Transform {
338
359
) ;
339
360
340
361
nodes . push (
341
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
362
+ IfStatement (
363
+ test ,
364
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
365
+ null
366
+ )
342
367
) ;
343
368
344
369
break ;
@@ -351,14 +376,20 @@ export default class Lock extends Transform {
351
376
) ;
352
377
353
378
nodes . push (
354
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
379
+ IfStatement (
380
+ test ,
381
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
382
+ null
383
+ )
355
384
) ;
356
385
357
386
break ;
358
387
359
388
case "context" :
360
389
var prop = choice ( this . options . lock . context ) ;
361
390
391
+ var code = this . getCounterMeasuresCode ( object , parents ) || [ ] ;
392
+
362
393
// Todo: Alternative to `this`
363
394
if ( ! this . globalVar ) {
364
395
offset = 1 ;
@@ -384,9 +415,7 @@ export default class Lock extends Transform {
384
415
"!" ,
385
416
MemberExpression ( Identifier ( this . globalVar ) , Literal ( prop ) , true )
386
417
) ;
387
- nodes . push (
388
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
389
- ) ;
418
+ nodes . push ( IfStatement ( test , code , null ) ) ;
390
419
391
420
break ;
392
421
@@ -397,6 +426,8 @@ export default class Lock extends Transform {
397
426
398
427
ok ( this . options . lock . osLock ) ;
399
428
429
+ var code = this . getCounterMeasuresCode ( object , parents ) || [ ] ;
430
+
400
431
this . options . lock . osLock . forEach ( ( osName ) => {
401
432
var agentMatcher = {
402
433
windows : "Win" ,
@@ -449,9 +480,7 @@ export default class Lock extends Transform {
449
480
} ) ;
450
481
451
482
test = UnaryExpression ( "!" , { ...test } ) ;
452
- nodes . push (
453
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
454
- ) ;
483
+ nodes . push ( IfStatement ( test , code , null ) ) ;
455
484
break ;
456
485
457
486
case "browserLock" :
@@ -488,7 +517,11 @@ export default class Lock extends Transform {
488
517
489
518
test = UnaryExpression ( "!" , { ...test } ) ;
490
519
nodes . push (
491
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
520
+ IfStatement (
521
+ test ,
522
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
523
+ null
524
+ )
492
525
) ;
493
526
break ;
494
527
@@ -540,7 +573,11 @@ export default class Lock extends Transform {
540
573
) ;
541
574
}
542
575
nodes . push (
543
- IfStatement ( test , this . getCounterMeasuresCode ( ) || [ ] , null )
576
+ IfStatement (
577
+ test ,
578
+ this . getCounterMeasuresCode ( object , parents ) || [ ] ,
579
+ null
580
+ )
544
581
) ;
545
582
}
546
583
0 commit comments