Microsoft Script Encoder (screnc.exe) 可將 VBScript/JScript 原始碼編碼為 .vbe/.jse,防止輕易閱讀。本文提供**加密腳本** (需 cscript 執行) 與**解密腳本** (逆向還原) 完整代碼。
' 必須用 cscript.exe 執行
If LCase(Right(WScript.FullName, 11)) = "wscript.exe" Then WScript.Quit(0)
If WScript.Arguments.Count < 2 Then
WScript.Echo "VBS Code Encoder v1.0"
WScript.Echo "Usage: cscript //nologo encode.vbs source.vbs target.vbe"
WScript.Quit(0)
End If
sFile = WScript.Arguments(0)
dFile = WScript.Arguments(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set src = fso.OpenTextFile(sFile, 1)
content = src.ReadAll
src.Close
' 核心:Scripting.Encoder COM 元件
encoded = CreateObject("Scripting.Encoder").EncodeScriptFile(".vbs", content, 0, "VBScript")
fso.OpenTextFile(dFile, 2, True).Write encoded
WScript.Echo "Encoded: " & dFile
cscript //nologo encode.vbs myscript.vbs myscript.vbe
Option Explicit
Dim args, srcFile
Set args = WScript.Arguments
If args.Count = 0 Then
WScript.Echo "Usage: cscript //nologo decode.vbs encoded.vbe"
WScript.Quit(0)
End If
srcFile = args(0)
Dim fso, f, content
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(srcFile) Then
WScript.Echo "File not found: " & srcFile
WScript.Quit(1)
End If
Set f = fso.OpenTextFile(srcFile, 1)
content = f.ReadAll
f.Close
Const TagBegin = "#@~^"
Const TagEnd = "==^#~@"
Dim posB, posE
Do
posE = 0
posB = InStr(content, TagBegin)
If posB > 0 Then
If InStr(posB, content, "==") - posB = 10 Then
posE = InStr(posB, content, TagEnd)
If posE > 0 Then
Dim encodedPart, decodedPart
encodedPart = Mid(content, posB + 12, posE - posB - 12 - 6)
decodedPart = Decode(encodedPart)
content = Left(content, posB - 1) & decodedPart & Mid(content, posE + 6)
End If
End If
End If
Loop Until posE = 0
WScript.Echo content
' ===== 核心解碼演算法 =====
Function Decode(str)
Dim encoder, map(127), i, j, c, temp, idx, comb
comb = "1231232332321323132311233213233211323231311231321323112331123132"
Set encoder = CreateObject("Scripting.Encoder")
' 建立字元對應表
For i = 9 To 127: map(i) = "JLA": Next
For i = 9 To 127
temp = Mid(encoder.EncodeScriptFile(".vbs", String(3, Chr(i)), 0, ""), 13, 3)
For j = 1 To 3
c = Asc(Mid(temp, j, 1))
map(c) = Left(map(c), j-1) & Chr(i) & Mid(map(c), j+1)
Next
Next
' 修正 bug: ')' 被誤解碼為 '>'
map(42) = Left(map(42), 1) & ")" & Right(map(42), 1)
Set encoder = Nothing
' 還原特殊逸出序列
str = Replace(Replace(str, "@&", Chr(10)), "@#", Chr(13))
str = Replace(Replace(str, "@*", ">"), "@!", "<")
str = Replace(str, "@$", "@")
idx = -1
For i = 1 To Len(str)
c = Asc(Mid(str, i, 1))
If c < 128 Then idx = idx + 1
If (c = 9) Or (c > 31 And c < 128) Then
If c <> 60 And c <> 62 And c <> 64 Then
str = Left(str, i-1) & Mid(map(c), Mid(comb, (idx Mod 64) + 1, 1), 1) & Mid(str, i+1)
End If
End If
Next
Decode = str
End Function
#@~^...==^#~@ 包裹 Base64-like 編碼資料。Scripting.Encoder 反推每個 ASCII 字元的映射表。@&→LF、@#→CR、@*→>、@!→<、@$→@。comb 決定每個位置使用映射表的哪個候選字元。screnc.exe (Windows 內建或下載)。理解此機制有助於維護舊系統、分析惡意腳本或學習編碼/解碼原理。