Node Security Shield (NSS) 是一个开源运行时应用自我保护(RASP)工具,旨在通过允许开发者和安全工程师声明应用可以访问哪些资源,来填补 NodeJS 全面安全性的空白。
受 Log4Shell(CVE-2021-44228)漏洞的启发(该漏洞因应用可发起任意网络调用而被利用),我们认为应用需要一种机制来自行声明允许的权限,通过实施额外的控制来增加利用此类漏洞的难度。
为了实现这一点,NSS (Node Security Shield) 提出了资源访问策略 (RAP)
资源访问策略 类似于 CSP(内容安全策略)。
它允许开发者/安全工程师声明应用应访问哪些资源,而 Node Security Shield 将强制执行该策略。
npm install nodesecurityshield
// 引入 Node Security Shield
let nodeSecurityShield = require('nodesecurityshield');
// 启用攻击监控和/或阻断
nodeSecurityShield.enableAttackMonitoring("Unique-App-Id",resourceAccessPolicy ,callbackFunction);
const resourceAccessPolicy = {
"outBoundRequest" : {
"blockedDomains" : ["compromised.domdog.io"],
"allowedDomains" : []
},
"executedCommand": {
"allowedCommands": ["pwd" , "(node)[](https://github.com/domdogsec/nodesecurityshield/blob/main/helper%5C)[a-z|0-9]*(.js)"]
}
};
outBoundRequest: 定义 出站请求 的接受行为
executedCommand: 定义 命令执行 的接受行为
allowedCommands 数组接受 String 类型。可以传入 RegEx 以允许某一模式的命令。executedCommand 属性。pwd 命令,helper 内的 .js 文件作为 Node 进程启动。.*,因为这允许在管道符 | 后执行任意命令。var callbackFunction = function (violationEvent,violations,violationLimitPerMinReached) {
console.log(JSON.stringify(violationEvent,null, 4));
}
violationEvent – 发生的 RAP 违规事件,以 CSP 违规格式呈现。
violations – RAP 违规计数,每分钟重置为零。
violationLimitPerMinReached – 如果 RAP 违规数量超过 'maxViolationsPerMinute'(RAP 中的一个选项),则为 true。
若要阻断攻击 – 抛出错误
throw new Error("请求被阻断。违反了声明的资源访问策略。")
{
"csp-report": {
"document-uri": "https://Unique-App-Id",
"blocked-uri": "https://compromised.domdog.io:443",
"violated-directive": "connect-src",
"effective-directive": "connect-src",
"original-policy": "{\"outBoundRequest\":{\"blockedDomains\":[\"compromised.domdog.io\"],\"allowedDomains\":[]}}",
"disposition": "report",
"status-code": 200,
"script-sample": "",
"source-file": "Error\n at TLSSocket.obj.<computed> [as connect] (/mnt/c/Ironwasp/Product/NodeSecurityShield/lib/hook.js:20:25)\n at Object.connect (_tls_wrap.js:1606:13)\n at Agent.createConnection (https.js:126:22)\n at Agent.createSocket (_http_agent.js:273:26)\n at Agent.addRequest (_http_agent.js:232:10)\n at new ClientRequest (_http_client.js:302:16)\n at request (https.js:310:10)\n at Object.get (https.js:314:15)\n at /mnt/c/Ironwasp/RD/Node/SimpleVulnerableNode/routes/ssrf.js:19:19\n at Layer.handle [as handle_request] (/mnt/c/Ironwasp/RD/Node/SimpleVulnerableNode/node_modules/express/lib/router/layer.js:95:5)"
}
}
document-uri: 包含初始化 NSS 时传入的 Unique-App-Idblocked-uri: 违反 RAP 的出站请求的域名violated-directive: connect-src 是 出站请求 的同义词,同理 script-src 是 命令执行 的同义词original-policy: 被违反的资源访问策略 (RAP)source-file: 该违规的堆栈跟踪const resourceAccessPolicy = {
"outBoundRequest" : {
"blockedDomains" : ["compromised.domdog.io"],
"allowedDomains" : []
},
"executedCommand": {
"allowedCommands": ["pwd" , "(node)[](https://github.com/domdogsec/nodesecurityshield/blob/main/helper%5C)[a-z|0-9]*(.js)"]
},
"reportUri": "https://ingest.sentry.io/api/6011856/security/?sentry_key=",
};
outBoundRequest: 定义 出站请求 的接受行为
executedCommand: 定义 命令执行 的接受行为
allowedCommands 数组接受 String 类型。可以传入 RegEx 以允许某一模式的命令。executedCommand 属性。pwd 命令,helper 内的 .js 文件作为 Node 进程启动。.*,因为这允许在管道符 | 后执行任意命令。reportUri : 将违规发送到指定端点。由于违规类似于内容安全策略违规,可以使用任何 CSP 监控解决方案。上述 RAP 中我们使用了 Sentry 端点。Sentry 仪表板截图

const resourceAccessPolicy = {
"outBoundRequest" : {
"blockedDomains" : ["compromised.domdog.io"],
"allowedDomains" : ["domdog.io","*.domdog.io",
{
"domains": [
"domgo.at",
],
"modules": [
{
"file": "\/routes\/ssrf.js",
},
{
"file": "\/node_modules\/axios\/",
}
]
},
{
"domains": [
"cluster0-shard-00-00.lb9jm.mongodb.net",
"cluster0-shard-00-01.lb9jm.mongodb.net",
"cluster0-shard-00-02.lb9jm.mongodb.net"
],
"modules": [
{
"file": "\/node_modules\/mongodb\/"
}
]
}
]
},
"executedCommand": {
"allowedCommands": ["pwd" , "(node)[](https://github.com/domdogsec/nodesecurityshield/blob/main/helper%5C)[a-z|0-9]*(.js)"]
},
"reportUri": "https://endpoint-to-send-violations",
"maxViolationsPerMinute": 50
}
outBoundRequest: 定义 出站请求 的接受行为
executedCommand: 定义 命令执行 的接受行为
allowedCommands 数组接受 String 类型。可以传入 RegEx 以允许某一模式的命令。executedCommand 属性。pwd 命令,helper 内的 .js 文件作为 Node 进程启动。.*,因为这允许在管道符 | 后执行任意命令。reportUri: 将违规发送到指定端点。由于违规类似于内容安全策略违规,可以使用任何 CSP 监控解决方案。上述 RAP 中我们使用了 Sentry 端点。allowedDomain 数组接受包含以下字段的对象
domain:允许提供文件访问的域名数组。modules:包含文件路径的对象数组。只有通过这些文件发出的出站请求到指定域名才被允许。maxViolationsPerMinute: 每分鐘发送到 reportUri 的最大违规数量。如果未指定,则使用默认值(100 次违规)。