-
Notifications
You must be signed in to change notification settings - Fork 132
/
Invoke-DOSfuscation.psm1
7725 lines (6231 loc) · 433 KB
/
Invoke-DOSfuscation.psm1
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Invoke-DOSfuscation.
#
# Copyright 2018 Daniel Bohannon <@danielhbohannon>
# while at Mandiant <http://www.mandiant.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function Get-ObfuscatedCmd
{
<#
.SYNOPSIS
Get-ObfuscatedCmd returns properly escaped intricate syntax that resolves to "cmd" in memory but not on the command line (except for "env" option). It relies on numerous methods supported by cmd.exe including:
1) substring capabilities in the context of environment variables
2) SET, ASSOC and FTYPE native commands for producing output containing "cmd"
3) FIND and FINDSTR for selecting line in output containing "cmd"
4) FOR loop for setting native command output as an environment variable
5) FOR loop delims and tokens arguments for extracting "cmd" from output
6) optional randomized casing
7) optional whitespace obfuscation
8) optional caret (and double-caret) obfuscation
Invoke-DOSfuscation Function: Get-ObfuscatedCmd
Author: Daniel Bohannon (@danielhbohannon)
License: Apache License, Version 2.0
Required Dependencies: Out-RandomCase, Out-ObfuscatedCaret, Get-RandomWhitespaceAndRandomChar, Out-EnvVarEncodedCommand (all located in Invoke-DOSfuscation.psm1)
Optional Dependencies: None
.DESCRIPTION
Get-ObfuscatedCmd returns properly escaped intricate syntax that resolves to "cmd" in memory but not on the command line (except for "env" option)
.PARAMETER ObfuscationLevel
(Optional) Specifies the preset obfuscation "profile" of all below parameters. This is to simplify general usage of this function without becoming overwhelmed by all of the options.
.PARAMETER ObfuscationType
(Optional) Specifies the obfuscation type to produce "cmd":
1) env (environment variable substring encoding)
2) assoc (FOR loop + assoc w/optional FIND/FINDSTR)
3) ftype (FOR loop + ftype w/optional FIND/FINDSTR)
4) set (FOR loop + set w/optional FIND/FINDSTR)
.PARAMETER VarName
(Optional) Specifies the single alphanumeric character for the FOR loop variable name (not used in the "env" option).
.PARAMETER RandomCase
(Optional) Specifies that random casing be used wherever possible.
.PARAMETER RandomSpace
(Optional) Specifies that random whitespace be input wherever possible.
.PARAMETER RandomSpaceRange
(Optional) Specifies the range of the length of each randomly-selected whitespace if -RandomSpace is also selected.
.PARAMETER RandomChar
(Optional) Specifies that random commas and semicolons be input wherever possible in the command.
.PARAMETER RandomCharRange
(Optional) Specifies the range of the count of commas and semicolons to be input wherever possible in the command if -RandomChar is also selected.
.PARAMETER RandomCharArray
(Optional) Specifies the character or array of characters (only comma and semicolon) to use if -RandomChar is also selected.
.PARAMETER RandomCaret
(Optional) Specifies that random carets be added before non-escapable characters in syntax components not affected by caret escape characters.
.PARAMETER RandomCaretPercent
(Optional) Specifies the percentage of characters to obfuscate with caret escape characters if -RandomCaret is also selected.
.PARAMETER DoubleEscape
(Optional) Specifies that double caret escaping occur for eligible components of the FOR loop sub-command so one layer of caret escapes will persist into the execution of the child process(es) of the sub-command.
.EXAMPLE
C:\PS> Get-ObfuscatedCmd
FOR /F "delims=lc tokens=5" %p IN ('assoc^|findstr lCmd')DO %p
.EXAMPLE
C:\PS> Get-ObfuscatedCmd -ObfuscationLevel 3
^F^o^r , , ; , /^F ; ; , ; ; ; , " tokens= 2 delims=j1AfJ=" ; ; , ; %j , ; ; ; ^iN ; , ; , ; , ( ; ; , ; ; , ' , ; ; ^^A^^S^^So^^C ; ; , , ; , ; ^^.^^c^^m^^d ' , , ; , , ; ) , , ; , , ; ; ^D^O ; ; , , , , ; %j
.EXAMPLE
C:\PS> Get-ObfuscatedCmd -ObfuscationType env -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomCaret -RandomCaretPercent 25
C%progRAMW6432:~ 9, -6%^D
.EXAMPLE
C:\PS> Get-ObfuscatedCmd -ObfuscationType assoc -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomChar -RandomCharRange @(2..5) -RandomCaret -RandomCaretPercent 75 -DoubleEscape
^f^o^r , , /^F , , , , " delims=lb tokens= 3 " , , , , , %F , , ^in , , , , , ( , , , , , ' , , , , ^^A^^s^^s^^O^^C , , , , , ^^.^^c^^d^^x^^m^^l ' , , ) , , , , , ^D^O , , , , %F
.EXAMPLE
C:\PS> Get-ObfuscatedCmd -ObfuscationType ftype -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomChar -RandomCharRange @(2..5) -RandomCaret -RandomCaretPercent 75 -DoubleEscape
F^o^r ; ; ; /^F ; ; ; ; ; " delims=unfR tokens= 1 " ; ; ; %b ; ; ; ; IN ; ; ; ; ; ( ; ; ; ' ; ; ; ; ; ^^Ft^^y^^P^^e ; ; ; ^| ; ; ; ; ^^F^^IN^^d ; ; ; ; "cm" ; ; ; ; ' ; ; ) ; ; ; ; ^D^O ; ; ; %b
.EXAMPLE
C:\PS> Get-ObfuscatedCmd -ObfuscationType set -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomChar -RandomCharRange @(2..5) -RandomCaret -RandomCaretPercent 75 -DoubleEscape
f^o^r , , , , , /^F , , , " tokens= 4 delims=.\Rb" , , , , , %y , , , , , ^In , , , ( , , ' , , , , , ^^s^^eT , , , , , ComSp ' , , ) , , ^d^O , , %y
.NOTES
This is a personal project developed by Daniel Bohannon while an employee at MANDIANT, A FireEye Company.
.LINK
http://www.danielbohannon.com
#>
[CmdletBinding()]
[OutputType('System.String')]
param (
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet(1,2,3)]
[System.Int16]
$ObfuscationLevel,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet('env','assoc','ftype','set')]
[System.String]
$ObfuscationType = (Get-Random -InputObject @('assoc','ftype')),
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9')]
[System.Char]
$VarName = (Get-Random -InputObject ([System.Char[]] (@(48..57) + @(65..90) + @(97..122)))),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomCase,
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomSpace,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | sort-object | select-object -First 1) -ge 0 } )]
[System.Object[]]
$RandomSpaceRange = @(0..4),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomChar,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | sort-object | select-object -First 1) -ge 0 } )]
[System.Object[]]
$RandomCharRange = @(1..5),
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | where-object { @(',',';','(',')') -contains $_ }) -or ($_ | where-object { ($_.Count -eq 2) -and (@(',',';') -contains $_[0]) -and (@(',',';') -contains $_[1]) }) } )]
[System.Object[]]
$RandomCharArray = (Get-Random -InputObject @(@(','),@(';'),@(',',';'))),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomCaret,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ -ge 0) -and ($_ -le 100) } )]
[System.Int16]
$RandomCaretPercent = 50,
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$DoubleEscape
)
# Create "profiles" depending on -ObfuscationLevel value. This is to simplify general usage of this function without becoming overwhelmed by all of the options.
if ($ObfuscationLevel)
{
switch ($ObfuscationLevel)
{
'1' {
$ObfuscationType = 'env'
$RandomCase = $false
$RandomSpace = $false
$RandomChar = $false
$RandomCaret = $false
$DoubleEscape = $false
}
'2' {
$ObfuscationType = Get-Random -InputObject @('assoc','ftype')
$RandomCase = $false
$RandomSpace = $false
$RandomChar = $false
$RandomCaret = $false
$DoubleEscape = $false
}
'3' {
$ObfuscationType = Get-Random -InputObject @('assoc','ftype')
$RandomCase = $true
$RandomSpace = $true
$RandomSpaceRange = @(3..7)
$RandomChar = $true
$RandomCharRange = @(3..7)
$RandomCharArray = @(',',';')
$RandomCaret = $true
$RandomCaretPercent = Get-Random -InputObject @(65..85)
$DoubleEscape = $true
}
}
}
# Set random case values if -RandomCase switch is set.
$for = 'FOR'
$f = 'F'
$tokens = 'tokens'
$delims = 'delims'
$in = 'IN'
$do = 'DO'
$find = 'find'
$findstr = 'findstr'
$assoc = 'assoc'
$ftype = 'ftype'
$set = 'set'
if ($RandomCase.IsPresent)
{
$for = Out-RandomCase $for
$f = Out-RandomCase $f
$tokens = Out-RandomCase $tokens
$delims = Out-RandomCase $delims
$in = Out-RandomCase $in
$do = Out-RandomCase $do
$find = Out-RandomCase $find
$findstr = Out-RandomCase $findstr
$assoc = Out-RandomCase $assoc
$ftype = Out-RandomCase $ftype
$set = Out-RandomCase $set
}
# Add random carets if -RandomCaret switch is set.
if ($RandomCaret.IsPresent)
{
$for = Out-ObfuscatedCaret -StringToObfuscate $for -RandomCaretPercent:$RandomCaretPercent
$f = Out-ObfuscatedCaret -StringToObfuscate $f -RandomCaretPercent:$RandomCaretPercent
$in = Out-ObfuscatedCaret -StringToObfuscate $in -RandomCaretPercent:$RandomCaretPercent
$do = Out-ObfuscatedCaret -StringToObfuscate $do -RandomCaretPercent:$RandomCaretPercent
}
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
$find = Out-ObfuscatedCaret -StringToObfuscate $find -RandomCaretPercent:$RandomCaretPercent
$findstr = Out-ObfuscatedCaret -StringToObfuscate $findstr -RandomCaretPercent:$RandomCaretPercent
$assoc = Out-ObfuscatedCaret -StringToObfuscate $assoc -RandomCaretPercent:$RandomCaretPercent
$ftype = Out-ObfuscatedCaret -StringToObfuscate $ftype -RandomCaretPercent:$RandomCaretPercent
$set = Out-ObfuscatedCaret -StringToObfuscate $set -RandomCaretPercent:$RandomCaretPercent
}
# Double escape caret characters in sub-command components if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$find = $find.Replace('^','^^')
$findstr = $findstr.Replace('^','^^')
$assoc = $assoc.Replace('^','^^')
$ftype = $ftype.Replace('^','^^')
$set = $set.Replace('^','^^')
}
# Set random whitespace values if -RandomSpace switch is set.
$randomSpaceA = ''
$randomSpace1 = ''
$randomSpace2 = ''
$randomSpace3 = ''
$randomSpace4 = ''
if ($RandomSpace.IsPresent)
{
$randomSpaceA = ' ' * (Get-Random -InputObject $RandomSpaceRange)
$randomSpace1 = ' ' * (Get-Random -InputObject $RandomSpaceRange)
$randomSpace2 = ' ' * (Get-Random -InputObject $RandomSpaceRange)
$randomSpace3 = ' ' * (Get-Random -InputObject $RandomSpaceRange)
$randomSpace4 = ' ' * (Get-Random -InputObject $RandomSpaceRange)
}
# Get random commas and/or semicolons (and whitespace mixed in if -RandomSpace is also selected).'
$randomCharA = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomCharB = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomCharC = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomCharD = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomCharE = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar1 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar2 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar3 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar4 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar5 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar6 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar7 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar8 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
$randomChar9 = Get-RandomWhitespaceAndRandomChar -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomChar:$RandomChar -RandomCharRange:$RandomCharRange -RandomCharArray:$RandomCharArray
# Ensure specific $randomChar* variables are at least one whitespace if they are not defined.
if (-not $randomSpace2) { $randomSpace2 = ' ' }
if (-not $randomCharD) { $randomCharD = ' ' }
if (-not $randomChar1) { $randomChar1 = ' ' }
if (-not $randomChar2) { $randomChar2 = ' ' }
if (-not $randomChar3) { $randomChar3 = ' ' }
if (-not $randomChar4) { $randomChar4 = ' ' }
if (-not $randomChar5) { $randomChar5 = ' ' }
if (-not $randomChar9) { $randomChar9 = ' ' }
# Randomly select between FINDSTR and FIND (adding double quotes around $subFindVal later via $quotes variable if FIND is selected).
if (Get-Random -InputObject @(0..1))
{
$subFind = $findstr
$quotes = ''
}
else
{
$subFind = $find
$quotes = '"'
}
# Set core components of intricate syntax per -ObfuscationType option.
$tokenValues = @()
$delimValues = @()
switch ($ObfuscationType)
{
'env' {
# Calculate percentage of characters to substitute with environment variable substring syntax.
$envVarPercent = $RandomCaretPercent
# Retrieve environment variable encoded version of $stringToEncode below calling Out-EnvVarEncodedCommand up to $tryLimit times to increases the likelihood that some encoding actually takes place, particularly for low -EnvVarPercent values.
$stringToEncode = 'cmd'
$tryLimit = 5
$tryCount = 0
do
{
$finalResult = Out-EnvVarEncodedCommand -StringToEncode $stringToEncode -EnvVarPercent $envVarPercent -RandomCase:$RandomCase -RandomSpace:$RandomSpace -RandomSpaceRange:$RandomSpaceRange -RandomCaret:$RandomCaret -RandomCaretPercent:$RandomCaretPercent -DoubleEscape:$DoubleEscape
$tryCount++
}
while (($finalResult -eq $stringToEncode) -and ($tryCount -lt $tryLimit))
# Since there is no FOR LOOP syntax required for the env option of this function we will return the result from this switch statement instead of proceeding like the rest of the options necessitate.
return $finalResult
}
'assoc' {
# Randomly choose between assoc+file extension and assoc+find/findstr syntaxes.
if (Get-Random -InputObject @(0..1))
{
# Set value pairings for retrieving "cmd" with assoc command.
$valuePairings = @()
$valuePairings += @{ Extension = '.cmd'; RequiredDelims = @('=','f'); SearchTerm = 'cmd'; Output = '.cmd=cmdfile' }
# .cdxml value is not present in assoc command on Win7 so commenting out.
# $valuePairings += @{ Extension = '.cdxml'; RequiredDelims = @('l'); SearchTerm = 'Cmd'; Output = '.cdxml=Microsoft.PowerShellCmdletDefinitionXML.1' }
# Select random pairing values from above.
$valuePairing = Get-Random -InputObject $valuePairings
# Retrieve all alpha-numeric characters that are case-sensitive unique to the characters found in $valuePairings.SearchTerm and then add random characters to $delimValues mix for more randomization of delim values and potentially resultant tokens value.
$nonConflictingChars = [System.Char[]] (@(48..57) + @(65..90) + @(97..122)) | where-object { [System.Char[]] ($valuePairing.SearchTerm + $valuePairing.RequiredDelims) -cnotcontains $_ }
$delimValues = $valuePairing.RequiredDelims + (Get-Random -InputObject $nonConflictingChars -Count (Get-Random -InputObject @(1..4)))
# Randomize order of $delimValues.
$delimValues = Get-Random -InputObject $delimValues -Count $delimValues.Count
# Since the search term (file extension) used by assoc imposes its case on the output result we will perform this case-insensitive substitution since it will affect the tokens index value(s).
$valuePairing.Output = $valuePairing.Output -ireplace [Regex]::Escape($valuePairing.Extension),$valuePairing.Extension
# Retrieve all matching tokens index values from assoc output, required+randomly-selected delim values and search term.
$tokenValues = Get-Index -Delims $delimValues -Output $valuePairing.Output -SearchTerm $valuePairing.SearchTerm
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
# FIND interprets carets as escapes in a regex term, so only add caret obfuscation if $subFind is FINDSTR.
if ($subFind.Replace('^','') -ieq 'findstr')
{
# Randomly decide to escape the '=' character if it is present since Out-ObfuscatedCaret will not escape this character.
if ((Get-Random -InputObject @(0..1)) -and -not $valuePairing.Extension.Contains('^='))
{
$valuePairing.Extension = $valuePairing.Extension.Replace('=','^=')
}
$valuePairing.Extension = Out-ObfuscatedCaret -StringToObfuscate $valuePairing.Extension -RandomCaretPercent:$RandomCaretPercent
}
}
# Double escape caret characters in $valuePairing.Extension if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$valuePairing.Extension = $valuePairing.Extension.Replace('^','^^').Replace('^^=','^^^=')
}
# Assemble the sub-command that will produce output in cmd.exe that contains "cmd".
$subCommand = "$randomCharA$assoc$randomCharB$($valuePairing.Extension)$randomSpace4"
}
else
{
# Randomly select from find/findstr query values that return specific output containing "cmd".
$findValCmd = Get-Random -InputObject @('md=','md=c','d=c','d=cm','=cm','mdf','mdfi')
$findValCdxml = Get-Random -InputObject @('cdxm','cdxml','llCm','lCm','lCmd','Cmdl','onX','nX','nXM','onXM')
# cmdfile has "cmd" (case-sensitive) twice in its result, so randomly select required delimiter values for each instance.
$cmdfileRequiredDelims = Get-Random -InputObject @(@('.','='),@('=','f'))
# Set value pairings for retrieving "cmd" with assoc+find/findstr command.
$valuePairings = @()
$valuePairings += @{ FindVal = $findValCmd; RequiredDelims = $cmdfileRequiredDelims; SearchTerm = 'cmd'; Output = '.cmd=cmdfile' }
$valuePairings += @{ FindVal = $findValCdxml; RequiredDelims = @('l'); SearchTerm = 'Cmd'; Output = '.cdxml=Microsoft.PowerShellCmdletDefinitionXML.1' }
# Select random pairing values from above.
$valuePairing = Get-Random -InputObject $valuePairings
# Retrieve all alpha-numeric characters that are case-sensitive unique to the characters found in $valuePairings.SearchTerm and then add random characters to $delimValues mix for more randomization of delim values and potentially resultant tokens value.
$nonConflictingChars = [System.Char[]] (@(48..57) + @(65..90) + @(97..122)) | where-object { [System.Char[]] ($valuePairing.SearchTerm + $valuePairing.RequiredDelims) -cnotcontains $_ }
$delimValues = $valuePairing.RequiredDelims + (Get-Random -InputObject $nonConflictingChars -Count (Get-Random -InputObject @(1..4)))
# Randomize order of $delimValues.
$delimValues = Get-Random -InputObject $delimValues -Count $delimValues.Count
# Retrieve all matching tokens index values from assoc output, required+randomly-selected delim values and search term.
$tokenValues = Get-Index -Delims $delimValues -Output $valuePairing.Output -SearchTerm $valuePairing.SearchTerm
# If FIND is being used versus FINDSTR then add random characters at the end instead of whitespace.
if ($subFind.Replace('^','') -ieq 'find')
{
$randomSpace4 = $randomCharE
}
else
{
# Equal characters in the FINDSTR argument need to be escaped with a caret.
if ($valuePairing.FindVal.Contains('='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
}
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
# FIND interprets carets as escapes in a regex term, so only add caret obfuscation if $subFind is FINDSTR.
if ($subFind.Replace('^','') -ieq 'findstr')
{
# Randomly decide to escape the '=' character if it is present since Out-ObfuscatedCaret will not escape this character.
if ((Get-Random -InputObject @(0..1)) -and -not $valuePairing.FindVal.Contains('^='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
$valuePairing.FindVal = Out-ObfuscatedCaret -StringToObfuscate $valuePairing.FindVal -RandomCaretPercent:$RandomCaretPercent
}
}
# Double escape caret characters in $valuePairing.FindVal if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('^','^^').Replace('^^=','^^^=')
}
# Assemble the sub-command that will produce output in cmd.exe that contains "cmd".
$subCommand = "$randomCharA$assoc$randomCharB^|$randomCharC$subFind$randomCharD$quotes$($valuePairing.FindVal)$quotes$randomSpace4"
}
}
'ftype' {
# Randomly select from find/findstr query values that return specific output containing "cmd".
$findValCmdfile = Get-Random -InputObject @('cm','mdf','mdfi','dfi','dfil')
$findValSHCmdFile = Get-Random -InputObject @('SHC','SHCm','HC','HCm','Cm','mdF','mdFi')
# Set value pairings for retrieving "cmd" with ftype+find/findstr command.
$valuePairings = @()
$valuePairings += @{ FindVal = $findValCmdfile; RequiredDelims = @('f'); SearchTerm = 'cmd'; Output = 'cmdfile="%1" %*' }
$valuePairings += @{ FindVal = $findValSHCmdFile; RequiredDelims = @('H','F'); SearchTerm = 'Cmd'; Output = 'SHCmdFile=%SystemRoot%\explorer.exe' }
# Select random pairing values from above.
$valuePairing = Get-Random -InputObject $valuePairings
# Retrieve all alpha-numeric characters that are case-sensitive unique to the characters found in $valuePairings.SearchTerm and then add random characters to $delimValues mix for more randomization of delim values and potentially resultant tokens value.
$nonConflictingChars = [System.Char[]] (@(48..57) + @(65..90) + @(97..122)) | where-object { [System.Char[]] ($valuePairing.SearchTerm + $valuePairing.RequiredDelims) -cnotcontains $_ }
$delimValues = $valuePairing.RequiredDelims + (Get-Random -InputObject $nonConflictingChars -Count (Get-Random -InputObject @(1..4)))
# Randomize order of $delimValues.
$delimValues = Get-Random -InputObject $delimValues -Count $delimValues.Count
# Retrieve all matching tokens index values from ftype output, required+randomly-selected delim values and search term.
$tokenValues = Get-Index -Delims $delimValues -Output $valuePairing.Output -SearchTerm $valuePairing.SearchTerm
# If FIND is being used versus FINDSTR then add random characters at the end instead of whitespace.
if ($subFind.Replace('^','') -ieq 'find')
{
$randomSpace4 = $randomCharE
}
else
{
# Equal characters in the FINDSTR argument need to be escaped with a caret.
if ($valuePairing.FindVal.Contains('='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
}
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
# FIND interprets carets as escapes in a regex term, so only add caret obfuscation if $subFind is FINDSTR.
if ($subFind.Replace('^','') -ieq 'findstr')
{
# Randomly decide to escape the '=' character if it is present since Out-ObfuscatedCaret will not escape this character.
if ((Get-Random -InputObject @(0..1)) -and -not $valuePairing.FindVal.Contains('^='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
$valuePairing.FindVal = Out-ObfuscatedCaret -StringToObfuscate $valuePairing.FindVal -RandomCaretPercent:$RandomCaretPercent
}
}
# Double escape caret characters in $valuePairing.FindVal if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('^','^^').Replace('^^=','^^^=')
}
# Assemble the sub-command that will produce output in cmd.exe that contains "cmd".
$subCommand = "$randomCharA$ftype$randomCharB^|$randomCharC$subFind$randomCharD$quotes$($valuePairing.FindVal)$quotes$randomSpace4"
}
'set' {
Write-Warning "Using SET intricate syntax for Cmd will produce an incorrect result if launched from PowerShell or PowerShell_ISE since these binaries append an additional path to `$env:PSModule not found when running from other binaries (like cmd.exe)."
# Randomly choose between set+environment variable name and set+find/findstr syntaxes.
if (Get-Random -InputObject @(0..1))
{
# Randomly select from environment variable values that return specific output containing "cmd".
$setValComSpec = Get-Random -InputObject @('ComS','ComSp','ComSpe')
# Set value pairings for retrieving "cmd" with set command.
$valuePairings = @()
$valuePairings += @{ EnvVarSubstring = $setValComSpec; RequiredDelims = @('\','.'); SearchTerm = 'cmd'; Output = 'ComSpec=C:\Windows\system32\cmd.exe' }
# Select random pairing values from above.
$valuePairing = Get-Random -InputObject $valuePairings
# Retrieve all alpha-numeric characters that are case-sensitive unique to the characters found in $valuePairings.SearchTerm and then add random characters to $delimValues mix for more randomization of delim values and potentially resultant tokens value.
$nonConflictingChars = [System.Char[]] (@(48..57) + @(65..90) + @(97..122)) | where-object { [System.Char[]] ($valuePairing.SearchTerm + $valuePairing.RequiredDelims) -cnotcontains $_ }
$delimValues = $valuePairing.RequiredDelims + (Get-Random -InputObject $nonConflictingChars -Count (Get-Random -InputObject @(1..4)))
# Randomize order of $delimValues.
$delimValues = Get-Random -InputObject $delimValues -Count $delimValues.Count
# Retrieve all matching tokens index values from set output, required+randomly-selected delim values and search term.
$tokenValues = Get-Index -Delims $delimValues -Output $valuePairing.Output -SearchTerm $valuePairing.SearchTerm
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
# FIND interprets carets as escapes in a regex term, so only add caret obfuscation if $subFind is FINDSTR.
if ($subFind.Replace('^','') -ieq 'findstr')
{
# Randomly decide to escape the '=' character if it is present since Out-ObfuscatedCaret will not escape this character.
if ((Get-Random -InputObject @(0..1)) -and -not $valuePairing.EnvVarSubstring.Contains('^='))
{
$valuePairing.EnvVarSubstring = $valuePairing.EnvVarSubstring.Replace('=','^=')
}
$valuePairing.EnvVarSubstring = Out-ObfuscatedCaret -StringToObfuscate $valuePairing.EnvVarSubstring -RandomCaretPercent:$RandomCaretPercent
}
}
# Double escape caret characters in $valuePairing.EnvVarSubstring if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$valuePairing.EnvVarSubstring = $valuePairing.EnvVarSubstring.Replace('^','^^').Replace('^^=','^^^=')
}
# Assemble the sub-command that will produce output in cmd.exe that contains "cmd".
$subCommand = "$randomCharA$set$randomCharD$($valuePairing.EnvVarSubstring)$randomSpace4"
}
else
{
# Randomly select from find/findstr query values that return specific output containing "cmd".
$findValComspec = Get-Random -InputObject @('ComS','omS','mS','mSp','Sp','Spe','pec','md.e')
# Set value pairings for retrieving "cmd" with set+find/findstr command.
$valuePairings = @()
$valuePairings += @{ FindVal = $findValComspec; RequiredDelims = @('\','.'); SearchTerm = 'cmd'; Output = 'ComSpec=C:\Windows\system32\cmd.exe' }
# Select random pairing values from above.
$valuePairing = Get-Random -InputObject $valuePairings
# Retrieve all alpha-numeric characters that are case-sensitive unique to the characters found in $valuePairings.SearchTerm and then add random characters to $delimValues mix for more randomization of delim values and potentially resultant tokens value.
$nonConflictingChars = [System.Char[]] (@(48..57) + @(65..90) + @(97..122)) | where-object { [System.Char[]] ($valuePairing.SearchTerm + $valuePairing.RequiredDelims) -cnotcontains $_ }
$delimValues = $valuePairing.RequiredDelims + (Get-Random -InputObject $nonConflictingChars -Count (Get-Random -InputObject @(1..4)))
# Randomize order of $delimValues.
$delimValues = Get-Random -InputObject $delimValues -Count $delimValues.Count
# Retrieve all matching tokens index values from set output, required+randomly-selected delim values and search term.
$tokenValues = Get-Index -Delims $delimValues -Output $valuePairing.Output -SearchTerm $valuePairing.SearchTerm
# If FIND is being used versus FINDSTR then add random characters at the end instead of whitespace.
if ($subFind.Replace('^','') -ieq 'find')
{
$randomSpace4 = $randomCharE
}
else
{
# Equal characters in the FINDSTR argument need to be escaped with a caret.
if ($valuePairing.FindVal.Contains('='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
}
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
# FIND interprets carets as escapes in a regex term, so only add caret obfuscation if $subFind is FINDSTR.
if ($subFind.Replace('^','') -ieq 'findstr')
{
# Randomly decide to escape the '=' character if it is present since Out-ObfuscatedCaret will not escape this character.
if ((Get-Random -InputObject @(0..1)) -and -not $valuePairing.FindVal.Contains('^='))
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('=','^=')
}
$valuePairing.FindVal = Out-ObfuscatedCaret -StringToObfuscate $valuePairing.FindVal -RandomCaretPercent:$RandomCaretPercent
}
}
# Double escape caret characters in $valuePairing.FindVal if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$valuePairing.FindVal = $valuePairing.FindVal.Replace('^','^^').Replace('^^=','^^^=')
}
# Assemble the sub-command that will produce output in cmd.exe that contains "cmd".
$subCommand = "$randomCharA$set$randomCharB^|$randomCharC$subFind$randomCharD$quotes$($valuePairing.FindVal)$quotes$randomSpace4"
}
}
default {
Write-Warning "Undefined `$ObfuscationType ($ObfuscationType) in switch statement."
return $null
}
}
# Select random token value.
$randomTokenValue = Get-Random -InputObject $tokenValues
# Randomly add explicit '+' or '-' sign to positive tokens value if -RandomChar is selected.
$randomPlusOrMinusSign = ''
if ($RandomChar.IsPresent -and ((Get-Random -InputObject @(1..100)) -le $RandomCharPercent))
{
if ($randomTokenValue -eq 0)
{
$randomPlusOrMinusSign = Get-Random -InputObject @('-','+')
}
elseif ($randomTokenValue -gt 0)
{
$randomPlusOrMinusSign = '+'
}
}
# Add tokens and delims placeholder value names with randomly-selected values generated in above switch block.
$tokenValue = "tokens=$randomSpaceA$randomPlusOrMinusSign$randomTokenValue"
$delimValue = "delims=" + (-join (Get-Random -InputObject $delimValues -Count $delimValues.Count))
# Rejoin above delim and token values with random whitespace in between and encapsulating if -RandomSpace argument is selected.
$tokenDelimRandomStr = (Get-Random -InputObject @($tokenValue,$delimValue) -Count 2) -join ' '
if ($RandomSpace.IsPresent)
{
# Do not add extra whitespace after $delimValue if it is listed after $tokenValue as this whitespace will then be part of the delim value(s) and cause errors depending on the token value(s).
if (Get-Random -InputObject @(0..1))
{
# Whitespace is not necessary between $tokenValue and $delimValue but the inverse is not true. To maintain more normal appearances default to single space if -RandomSpace argument is not selected.
if (-not $RandomSpace.IsPresent)
{
$randomSpace3 = ' '
}
$tokenDelimRandomStr = "$randomSpace1$tokenValue$randomSpace3$delimValue"
}
else
{
$tokenDelimRandomStr = "$randomSpace1$delimValue$randomSpace2$tokenValue$randomSpace3"
}
}
# Assemble all components into final obfuscated syntax.
$obfuscatedCmd = "$for$randomChar1/$f$randomChar2`"$tokenDelimRandomStr`"$randomChar3%$VarName$randomChar4$in$randomChar5($randomChar6'$subCommand'$randomChar7)$randomChar8$do$randomChar9%$VarName"
# Return final result.
return $obfuscatedCmd
}
function Get-ObfuscatedPowerShell
{
<#
.SYNOPSIS
Get-ObfuscatedPowerShell returns properly escaped intricate syntax that resolves to "PowerShell" in memory but not on the command line (except for "env" option). It relies on numerous methods supported by cmd.exe including:
1) substring capabilities in the context of environment variables
2) SET, ASSOC and FTYPE native commands for producing output containing "PowerShell"
3) FIND and FINDSTR for selecting line in output containing "PowerShell"
4) FOR loop for setting native command output as an environment variable
5) FOR loop delims and tokens arguments for extracting "PowerShell" from output
6) optional randomized casing
7) optional whitespace obfuscation
8) optional caret (and double-caret) obfuscation
Invoke-DOSfuscation Function: Get-ObfuscatedPowerShell
Author: Daniel Bohannon (@danielhbohannon)
License: Apache License, Version 2.0
Required Dependencies: Out-RandomCase, Out-ObfuscatedCaret, Get-RandomWhitespaceAndRandomChar, Out-EnvVarEncodedCommand (all located in Invoke-DOSfuscation.psm1)
Optional Dependencies: None
.DESCRIPTION
Get-ObfuscatedPowerShell returns properly escaped intricate syntax that resolves to "PowerShell" in memory but not on the command line (except for "env" option)
.PARAMETER ObfuscationLevel
(Optional) Specifies the preset obfuscation "profile" of all below parameters. This is to simplify general usage of this function without becoming overwhelmed by all of the options.
.PARAMETER ObfuscationType
(Optional) Specifies the obfuscation type to produce "PowerShell":
1) env (environment variable substring encoding)
2) assoc (FOR loop + assoc w/optional FIND/FINDSTR)
3) ftype (FOR loop + ftype w/optional FIND/FINDSTR)
4) set (FOR loop + set w/optional FIND/FINDSTR)
.PARAMETER VarName
(Optional) Specifies the single alphanumeric character for the FOR loop variable name (not used in the "env" option).
.PARAMETER RandomCase
(Optional) Specifies that random casing be used wherever possible.
.PARAMETER RandomSpace
(Optional) Specifies that random whitespace be input wherever possible.
.PARAMETER RandomSpaceRange
(Optional) Specifies the range of the length of each randomly-selected whitespace if -RandomSpace is also selected.
.PARAMETER RandomChar
(Optional) Specifies that random commas and semicolons be input wherever possible in the command.
.PARAMETER RandomCharRange
(Optional) Specifies the range of the count of commas and semicolons to be input wherever possible in the command if -RandomChar is also selected.
.PARAMETER RandomCharArray
(Optional) Specifies the character or array of characters (only comma and semicolon) to use if -RandomChar is also selected.
.PARAMETER RandomCaret
(Optional) Specifies that random carets be added before non-escapable characters in syntax components not affected by caret escape characters.
.PARAMETER RandomCaretPercent
(Optional) Specifies the percentage of characters to obfuscate with caret escape characters if -RandomCaret is also selected.
.PARAMETER DoubleEscape
(Optional) Specifies that double caret escaping occur for eligible components of the FOR loop sub-command so one layer of caret escapes will persist into the execution of the child process(es) of the sub-command.
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell
FOR /F "delims=N8WLC. tokens=2" %6 IN ('ftype^|find "lCo"')DO %6
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell -ObfuscationLevel 3
F^O^R ; ; , ; /^f ; ; , , , , " delims=F.C tokens= 2 " , ; ; , %j ; ; ; ; , ^in ; , , ; ; , ( , ; ; , ; ; , ' , ; ; ; ; , , ^^A^^s^^S^^O^^C ; , ; ^^.c^^dx^^m^^l ' ; , , , ) ; ; ; ; , ^D^O ; , ; %j
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell -ObfuscationType env -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomCaret -RandomCaretPercent 25
P^O%winDir:~ 8, -1%e^RS^H%prOGRAmfilES(x86):~ 14, -7%%SESSiONnaMe:~ -2, -1%^L
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell -ObfuscationType assoc -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomCaret -RandomCaretPercent 75 -RandomChar -RandomCharRange @(2..5) -DoubleEscape
^f^oR ; ; ; ; /^f ; ; ; ; " tokens= 3 delims=xD5d." ; ; %o ; ; ^in ; ; ( ; ; ; ; ' ; ; ; ; ^^a^^s^^S^^o^^C ; ; ; ; ; ^^.^^p^^s^^d^^1 ' ; ; ; ; ) ; ; ^d^O ; ; ; ; %o
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell -ObfuscationType ftype -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomCaret -RandomCaretPercent 75 -RandomChar -RandomCharRange @(2..5) -DoubleEscape
^F^o^r ; ; ; ; ; /^F ; ; ; ; ; " delims=sum\i tokens= 12 " ; ; %k ; ; ; ; ; ^IN ; ; ; ; ; ( ; ; ' ; ; ; ^^F^^T^^Y^^p^^e ; ; ^| ; ; ; ^^f^^iN^^D^^STR ; ; ; l^^C^^o ' ; ; ) ; ; ; ^d^O ; ; ; ; %k
.EXAMPLE
C:\PS> Get-ObfuscatedPowerShell -ObfuscationType set -RandomCase -RandomSpace -RandomSpaceRange @(2..5) -RandomCaret -RandomCaretPercent 75 -RandomChar -RandomCharRange @(2..5) -DoubleEscape
^F^O^r ; ; /^F ; ; ; " delims=siQOd\ tokens= 8 " ; ; ; ; %f ; ; ; ^in ; ; ; ( ; ; ; ; ' ; ; ; ; ; ^^s^^e^^T ; ; PSM ' ; ; ; ; ; ) ; ; ; ; ; ^d^o ; ; ; ; %f
.NOTES
This is a personal project developed by Daniel Bohannon while an employee at MANDIANT, A FireEye Company.
.LINK
http://www.danielbohannon.com
#>
[CmdletBinding()]
[OutputType('System.String')]
param (
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet(1,2,3)]
[System.Int16]
$ObfuscationLevel,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet('env','assoc','ftype','set')]
[System.String]
$ObfuscationType = (Get-Random -InputObject @('assoc','ftype')),
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9')]
[System.Char]
$VarName = (Get-Random -InputObject ([System.Char[]] (@(48..57) + @(65..90) + @(97..122)))),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomCase,
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomSpace,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | sort-object | select-object -First 1) -ge 0 } )]
[System.Object[]]
$RandomSpaceRange = @(0..4),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomChar,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | sort-object | select-object -First 1) -ge 0 } )]
[System.Object[]]
$RandomCharRange = @(1..5),
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ | where-object { @(',',';','(',')') -contains $_ }) -or ($_ | where-object { ($_.Count -eq 2) -and (@(',',';') -contains $_[0]) -and (@(',',';') -contains $_[1]) }) } )]
[System.Object[]]
$RandomCharArray = (Get-Random -InputObject @(@(','),@(';'),@(',',';'))),
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$RandomCaret,
[Parameter(Position = 0, Mandatory = $false)]
[ValidateScript( { ($_ -ge 0) -and ($_ -le 100) } )]
[System.Int16]
$RandomCaretPercent = 50,
[Parameter(Position = 0, Mandatory = $false)]
[Switch]
$DoubleEscape
)
# Create "profiles" depending on -ObfuscationLevel value. This is to simplify general usage of this function without becoming overwhelmed by all of the options.
if ($ObfuscationLevel)
{
switch ($ObfuscationLevel)
{
'1' {
$ObfuscationType = 'env'
$RandomCase = $false
$RandomSpace = $false
$RandomChar = $false
$RandomCaret = $false
$DoubleEscape = $false
}
'2' {
$ObfuscationType = Get-Random -InputObject @('assoc','ftype')
$RandomCase = $false
$RandomSpace = $false
$RandomChar = $false
$RandomCaret = $false
$DoubleEscape = $false
}
'3' {
$ObfuscationType = Get-Random -InputObject @('assoc','ftype')
$RandomCase = $true
$RandomSpace = $true
$RandomSpaceRange = @(3..7)
$RandomChar = $true
$RandomCharRange = @(3..7)
$RandomCharArray = @(',',';')
$RandomCaret = $true
$RandomCaretPercent = Get-Random -InputObject @(65..85)
$DoubleEscape = $true
}
}
}
# Set random case values if -RandomCase switch is set.
$for = 'FOR'
$f = 'F'
$tokens = 'tokens'
$delims = 'delims'
$in = 'IN'
$do = 'DO'
$find = 'find'
$findstr = 'findstr'
$assoc = 'assoc'
$ftype = 'ftype'
$set = 'set'
if ($RandomCase.IsPresent)
{
$for = Out-RandomCase $for
$f = Out-RandomCase $f
$tokens = Out-RandomCase $tokens
$delims = Out-RandomCase $delims
$in = Out-RandomCase $in
$do = Out-RandomCase $do
$find = Out-RandomCase $find
$findstr = Out-RandomCase $findstr
$assoc = Out-RandomCase $assoc
$ftype = Out-RandomCase $ftype
$set = Out-RandomCase $set
}
# Add random carets if -RandomCaret switch is set.
if ($RandomCaret.IsPresent)
{
$for = Out-ObfuscatedCaret -StringToObfuscate $for -RandomCaretPercent:$RandomCaretPercent
$f = Out-ObfuscatedCaret -StringToObfuscate $f -RandomCaretPercent:$RandomCaretPercent
$in = Out-ObfuscatedCaret -StringToObfuscate $in -RandomCaretPercent:$RandomCaretPercent
$do = Out-ObfuscatedCaret -StringToObfuscate $do -RandomCaretPercent:$RandomCaretPercent
}
# Add random carets if -RandomCaret or -DoubleEscape switch is set.
if ($RandomCaret.IsPresent -or $DoubleEscape.IsPresent)
{
$find = Out-ObfuscatedCaret -StringToObfuscate $find -RandomCaretPercent:$RandomCaretPercent
$findstr = Out-ObfuscatedCaret -StringToObfuscate $findstr -RandomCaretPercent:$RandomCaretPercent
$assoc = Out-ObfuscatedCaret -StringToObfuscate $assoc -RandomCaretPercent:$RandomCaretPercent
$ftype = Out-ObfuscatedCaret -StringToObfuscate $ftype -RandomCaretPercent:$RandomCaretPercent
$set = Out-ObfuscatedCaret -StringToObfuscate $set -RandomCaretPercent:$RandomCaretPercent
}
# Double escape caret characters in sub-command components if -DoubleEscape switch is set.
if ($DoubleEscape.IsPresent)
{
$find = $find.Replace('^','^^')
$findstr = $findstr.Replace('^','^^')
$assoc = $assoc.Replace('^','^^')
$ftype = $ftype.Replace('^','^^')
$set = $set.Replace('^','^^')
}
# Set random whitespace values if -RandomSpace switch is set.
$randomSpaceA = ''
$randomSpace1 = ''
$randomSpace2 = ''
$randomSpace3 = ''
$randomSpace4 = ''
if ($RandomSpace.IsPresent)
{
$randomSpaceA = ' ' * (Get-Random -InputObject $RandomSpaceRange)
$randomSpace1 = ' ' * (Get-Random -InputObject $RandomSpaceRange)