萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

VBScript 加解密:Microsoft Script Encoder (.vbe) 完整實作

Microsoft Script Encoder (screnc.exe) 可將 VBScript/JScript 原始碼編碼為 .vbe/.jse,防止輕易閱讀。本文提供**加密腳本** (需 cscript 執行) 與**解密腳本** (逆向還原) 完整代碼。

一、加密腳本 (encode.vbs)

' 必須用 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

二、解密腳本 (decode.vbs)

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 內建或下載)。
  • 不支援 VBScript 以外的語言 (如 PowerShell、Python)。
  • 現代安全需求請改用:代碼簽章、編譯為 DLL、或使用商用保護工具。

理解此機制有助於維護舊系統、分析惡意腳本或學習編碼/解碼原理。

copyright © 萬盛學電腦網 all rights reserved