二阶命令注入<a class="headerlink" href="#second-order-command-injection" title="Link to this heading">¶</a>
ID: js/second-order-command-line-injection
Kind: path-problem
Security severity: 7.0
Severity: error
Precision: high
Tags:
- correctness
- security
- external/cwe/cwe-078
- external/cwe/cwe-088
Query suites:
- javascript-code-scanning.qls
- javascript-security-extended.qls
- javascript-security-and-quality.qls
某些 shell 命令(例如<code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">ls-remote</span></code>)如果用户提供以<code class="docutils literal notranslate"><span class="pre">--upload-pack</span></code>开头的恶意 URL,则可以执行任意命令。这可用于在服务器上执行任意代码。
建议<a class="headerlink" href="#recommendation" title="Link to this heading">¶</a>
在将用户输入传递给 shell 命令之前清理用户输入。例如,确保 URL 有效且不包含恶意命令。
示例<a class="headerlink" href="#example" title="Link to this heading">¶</a>
以下示例显示了在可由恶意用户控制的 URL 上执行<code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">ls-remote</span></code> 的代码。
const express = require("express");
const app = express();
const cp = require("child_process");
app.get("/ls-remote", (req, res) => {
const remote = req.query.remote;
cp.execFile("git", ["ls-remote", remote]); // NOT OK
});
在下面的代码段中,问题已修复,其中 URL 在传递给 shell 命令之前经过验证。
const express = require("express");
const app = express();
const cp = require("child_process");
app.get("/ls-remote", (req, res) => {
const remote = req.query.remote;
if (!(remote.startsWith("git@") || remote.startsWith("https://"))) {
throw new Error("Invalid remote: " + remote);
}
cp.execFile("git", ["ls-remote", remote]); // OK
});
参考<a class="headerlink" href="#references" title="Link to this heading">¶</a>
Max Justicz:<a class="reference external" href="https://justi.cz/security/2021/04/20/cocoapods-rce.html">通过 CocoaPods 同时入侵 3,000,000 个应用程序</a>。
Git:<a class="reference external" href="https://git-scm.cn/docs/git-ls-remote/2.22.0#Documentation/git-ls-remote.txt---upload-packltexecgt">Git - git-ls-remote 文档</a>。
OWASP:<a class="reference external" href="https://www.owasp.org/index.php/Command_Injection">命令注入</a>。
常见弱点枚举:<a class="reference external" href="https://cwe.mitre.org/data/definitions/78.html">CWE-78</a>。
常见弱点枚举:<a class="reference external" href="https://cwe.mitre.org/data/definitions/88.html">CWE-88</a>。