DVWA–Command Injection(命令注入)
原理参考:网络安全笔记-99-渗透-OS命令注入
管道符
- ”;“:分号。执行完前面的语句再执行后面的语句
- ”|“:显示后面语句的执行结果,例如:ping 127.0.0.1| whoami
- ”||“:当前面的语句执行出错时执行后面的语句。例如:ping 1 || whoami
- ”&“:如果前面的语句为假则直接执行后面的语句,前面的语句可真可假
- ”&&“:如果前面的语句为假则直接出错,也不执行后面的语句,前面的语句只能为真。
常见URL编码:
- %21: 空格
- %5c: \
- %26: &
- %7c: |
难度分级
Low
核心代码
{$cmd}";
}?>
分析:
php_uname(”string“):返回运行PHP的系统的有关信息。
‘a’:此为默认。包含序列 “s n r v m” 里的所有模式。
‘s’:操作系统名称。例如: FreeBSD。
‘n’:主机名。例如: localhost.example.com。
‘r’:版本名称,例如: 5.1.2-RELEASE。
‘v’:版本信息。操作系统之间有很大的不同。
‘m’:机器类型。例如:i386。
shell_exec():通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回
判断系统类型,然后直接拼接用户输入$target,没有过滤。
利用:
Medium
核心代码
if( isset( $_POST[ 'Submit' ] ) ) {$target = $_REQUEST[ 'ip' ];$substitutions = array('&&' => '',';' => '',);$target = str_replace( array_keys( $substitutions ), $substitutions, $target );if( stristr( php_uname( 's' ), 'Windows NT' ) ) {$cmd = shell_exec( 'ping ' . $target );}else {$cmd = shell_exec( 'ping -c 4 ' . $target );}echo "{$cmd}
";
}?>
分析:
将”&&“和”;“替换为空。其他管道符仍然可以使用。
利用:
与Low模式相同。
High
'',';' => '','| ' => '','-' => '','$' => '','(' => '',')' => '','`' => '','||' => '',);// Remove any of the charactars in the array (blacklist).$target = str_replace( array_keys( $substitutions ), $substitutions, $target );// 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 userecho "{$cmd}
";
}?>
分析:
替换的字符变多了。但是“| ”后面有一个空格,所以我们依然可以用“|”进行绕过。
利用:
不变。
Impossible
核心代码:
{$cmd}";}else {// Ops. Let the user name theres a mistakeecho 'ERROR: You have entered an invalid IP.
';}
}// Generate Anti-CSRF token
generateSessionToken();?>
分析:
首先去除反斜线。
stripslashes():返回一个去除转义反斜线后的字符串(\’ 转换为 ’ 等等)。双反斜线(\\)被转换为单个反斜线(\)。
然后将IP地址用点号.分隔,返回一个字符数组。
explode():用字符串分隔另一个字符串,返回分隔后的字符数组。
然后再检查IP地址中的每8个比特位是否为数字,如果是则把IP地址重新组合起来。
Impossible级别并且加入了Anti-CSRF token与session Token。安全级别非常高。