最近被编译原理搞得神魂颠倒,没时间更新dvwa…
今天写dvwa的第二篇:command injection命令注入。
在开始之前,先对centos进行设置:关闭selinux。
(我看到很多教程好像都没有提到关闭selinux,实测在没有关闭的情况下,命令注入后没有反馈,centos7给出警告如下:
因此首先需要关闭selinux,具体操作如下:
centos 7 终端输入vim /etc/sysconfig/selinux,将SELINUX=enforcing改为SELINUX=disabled,保存后重启centos。
回到dvwa,先把dvwa安全系数调至low,用连接符连接个简单的:127.0.0.1&whoami:
这里需要学习连接符的使用:
& 前面的语句为假则执行后面的语句,前面为真则都执行
&& 前面的语句为假则出错且后面语句不执行,前面为真则都执行
| 前面的语句为假则出错且后面语句不执行,前面为真则执行后面语句
|| 前面的语句为假则两个语句都执行,前面为真则只执行前面语句
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
echo "<pre>{$cmd}</pre>";
}
?>
可以看到,并没有对任何符号进行过滤。
下面看看Medium防护等级的代码:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Set blacklist
$substitutions = array(
'&&' => '',
';' => '',
);
// 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 user
echo "<pre>{$cmd}</pre>";
}
?>
可以看到,输入框过滤了&&和;,那么这次用||进行命令注入,输入test|echo ‘helloworld’执行:
此外,dvwa还提供了high和impossible两种防御方案,high的代码原理差不多,通过设置黑名单过滤的方式阻止命令注入,impossible的php还没读懂,查资料得知Impossible防御中加入了Anti-CSRF token,也只允许数字和’.‘输入,因此不存在命令注入漏洞。①
参考资料:
①:https://www.freebuf.com/articles/web/116714.html