一、适用对象
----------------------------------------------------------------
被 BatchEncryption 一类加固器处理过的 .bat / .cmd 文件。典型特征:
1. 体积异常大(几十 KB 到几百 KB),逻辑却很简单;
2. 开头几行是可见的 @echo off / title / start / goto,后面一大段是由
%变量:~N,1% 切片拼起来的乱码;
二、用法
----------------------------------------------------------------
代码另存为:Decrypt-BatchEncryption.ps1
powershell -NoProfile -ExecutionPolicy Bypass -File Decrypt-BatchEncryption.ps1 -Path 样本.cmd -Out 输出.txt
不给 -Out 时直接打印到控制台。原样本只读,不会被修改。
三、代码
[ol]
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Path,
[string]$Out,
[switch]$All,
[switch]$NoGbk,
[switch]$NoFallback
)
$ErrorActionPreference = 'Stop'
$PC = [char]37
$CR = [char]13
$LF = [char]10
$script:Vars = @{}
$script:Tables = @{}
$script:LastTable = $null
$script:VarRx = [regex]('%([^%:~,]+)(?::~(-?\d+)(?:,(-?\d+))?)?%')
function ConvertTo-GbkText {
param([string]$Text)
if ($Text -notmatch '[^\x00-\x7F]') { return $Text }
try {
$bytes = [Text.Encoding]::GetEncoding(28591).GetBytes($Text)
$fixed = [Text.Encoding]::GetEncoding(936).GetString($bytes)
if ($fixed.IndexOf([char]0xFFFD) -ge 0) { return $Text }
return $fixed
} catch {
return $Text
}
}
function Expand-Slice {
param([string]$Fragment)
return [regex]::Replace($Fragment, $script:VarRx, {
param($m)
$name = $m.Groups[1].Value
if ($script:Vars.ContainsKey($name)) {
$val = $script:Vars[$name]
} else {
# Only a slice (%os:~6,1%) asks for a real environment value, and the
# packer uses these to build its variable names. A plain %SystemRoot%
# is payload text and must stay literal, or the output would depend
# on the machine that ran the decryption.
$val = $null
if ($m.Groups[2].Success) { $val = [Environment]::GetEnvironmentVariable($name) }
}
# A one-character value means the table was already released; only the
# quote-named table qualifies for the stage-one fallback.
if ($null -ne $val -and $val.Length -lt 2 -and $name -eq [string][char]39) { $val = $null }
if ($null -eq $val) {
# The packer releases its table with endlocal, yet the second stage
# still slices it. Fall back to the last table seen in stage one.
if (-not $NoFallback -and $name -eq [string][char]39 -and $null -ne $script:LastTable) {
$val = $script:LastTable
} else {
return $m.Value
}
}
if (-not $m.Groups[2].Success) { return $val }
$from = [int]$m.Groups[2].Value
$len = $val.Length
if ($from -lt 0) { $from = $len + $from }
if ($from -lt 0 -or $from -ge $len) { return '' }
$count = 1
if ($m.Groups[3].Success) { $count = [int]$m.Groups[3].Value }
if ($count -lt 0) { $count = $len - $from + $count }
$end = $from + $count
if ($end -gt $len) { $end = $len }
if ($end -le $from) { return '' }
return $val.Substring($from, $end - $from)
})
}
function Invoke-Unpack {
param([string]$Text)
$script:Vars = @{}
$script:LastTable = $null
$lines = New-Object System.Collections.Generic.List[string]
$i = 0
$n = $Text.Length
while ($i -lt $n) {
$buf = New-Object System.Text.StringBuilder
$inPct = $false
$start = 0
while ($i -lt $n) {
$ch = $Text[$i]
# line terminator: CRLF, or a lone CR (the packer emits bare CRs
# between the payload and its trailing junk)
if ($ch -eq $CR) {
if (($i + 1) -lt $n -and $Text[$i + 1] -eq $LF) { $i += 2 } else { $i++ }
break
}
if ($ch -eq $LF) { $i++; break }
if ($ch -eq $PC) {
if (-not $inPct) {
# %~dp0 %~f0 %~nx0 are argument modifiers, not variable
# expansions: copy them through so they do not steal the
# closing percent of the slice that follows.
if ($i + 1 -lt $n -and $Text[$i + 1] -eq [char]126) {
$k = $i + 2
while ($k -lt $n -and (($Text[$k] -ge [char]65 -and $Text[$k] -le [char]90) -or
($Text[$k] -ge [char]97 -and $Text[$k] -le [char]122))) { $k++ }
if ($k -lt $n -and $Text[$k] -ge [char]48 -and $Text[$k] -le [char]57) {
$null = $buf.Append($Text.Substring($i, $k - $i + 1))
$i = $k + 1
continue
}
}
$inPct = $true
$start = $i
} else {
$inPct = $false
$null = $buf.Append((Expand-Slice $Text.Substring($start, $i - $start + 1)))
}
$i++
continue
}
if (-not $inPct) {
$null = $buf.Append($ch)
$i++
continue
}
if (($start + 1) -eq $i -and ((($ch -ge [char]48) -and ($ch -le [char]57)) -or $ch -eq [char]42)) {
$inPct = $false
$null = $buf.Append($Text.Substring($start, $i - $start + 1))
}
$i++
}
$line = $buf.ToString().TrimEnd($CR)
if ($inPct -and $line -match '%[^%]+:~-?\d+,-?\d+$') {
$line = Expand-Slice ($line + $PC)
}
if ($line.Contains('set ')) {
foreach ($seg in ($line -split '&@')) {
$body = $seg
if ($body.StartsWith('@')) { $body = $body.Substring(1) }
if ($body.Length -gt 4 -and $body.Substring(0, 4) -eq 'set ') {
$kv = $body.Substring(4)
$eq = $kv.IndexOf('=')
if ($eq -gt 0) {
$nm = $kv.Substring(0, $eq)
$vl = $kv.Substring($eq + 1).Replace('^^^', '^')
$script:Vars[$nm] = $vl
if ($nm -eq [string][char]39 -and $vl.Length -ge 64) {
$script:LastTable = $vl
}
}
}
}
}
$lines.Add($line)
}
return $lines
}
if (-not (Test-Path -LiteralPath $Path)) {
throw "file not found: $Path"
}
$Path = (Resolve-Path -LiteralPath $Path).ProviderPath
$bytes = [IO.File]::ReadAllBytes($Path)
$buf = New-Object System.Collections.Generic.List[byte]
foreach ($b in $bytes) { if ($b -ne 0) { $buf.Add($b) } }
$text = [Text.Encoding]::GetEncoding(28591).GetString($buf.ToArray())
if ($text.Length -ge 2 -and [int][char]$text[0] -eq 255 -and [int][char]$text[1] -eq 254) {
$text = $text.Substring(2)
}
$bom = [string][char]255 + [string][char]254
$text = $text.Replace($bom, ([string]$CR + [string]$LF))
$decoded = Invoke-Unpack $text
if (-not $NoGbk) {
for ($k = 0; $k -lt $decoded.Count; $k++) {
$decoded[$k] = ConvertTo-GbkText $decoded[$k]
}
}
$cut = 0
for ($k = 0; $k -lt $decoded.Count; $k++) {
if ($decoded[$k] -match 'echo on' -and $decoded[$k] -match 'endlocal') { $cut = $k + 1 }
}
if ($All) {
$result = $decoded
} else {
$result = New-Object System.Collections.Generic.List[string]
for ($k = $cut; $k -lt $decoded.Count; $k++) {
$ln = $decoded[$k]
if ($ln.Trim().Length -eq 0) { continue }
if ($ln -match 'BatchEncryption Build') { continue }
if ($ln -match 'teRAr') { continue }
$result.Add($ln)
}
}
$sliceLeft = 0
foreach ($ln in $result) {
if ($ln -match '%[^%]+:~-?\d+,-?\d+%') { $sliceLeft++ }
}
Write-Host ''
Write-Host '=== Decrypt-BatchEncryption ===' -ForegroundColor Cyan
Write-Host ("File : {0}" -f $Path)
Write-Host ("Size : {0} B" -f $bytes.Length)
Write-Host ("Decoded : {0} lines, payload {1} lines starting at line {2}" -f $decoded.Count, $result.Count, ($cut + 1))
Write-Host ("Vars/Tabs : {0}" -f $script:Vars.Count)
Write-Host ("Leftovers : {0} line(s) still contain a slice" -f $sliceLeft)
Write-Host ''
foreach ($ln in $result) { Write-Host $ln }
if ($Out) {
$enc = New-Object Text.UTF8Encoding($true)
$eol = [string]$CR + [string]$LF
[IO.File]::WriteAllText($Out, ($result -join $eol) + $eol, $enc)
Write-Host ''
Write-Host ("written: {0}" -f $Out) -ForegroundColor Green
}
[/ol]复制代码
至于作用,懂得则懂吧!!!! 上一个贴子无故被删,也没有原因说明。

