DVWA之命令注入
DVWA之命令注入
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>";
}
?>
ip参数没有做任何的过滤就直接放在shell_exec函数中执行了,执行语句为shell_exec( 'ping 127.0.0.1 && whami ')。(linux下使用whoami查看用户命令,windows使用net user)
这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞
由于是在windows系统下的cmd中进行,所以我们一开始便用dir列出所有文件
利用| ipconfig获得本机的IP地址
利用dir列出所有文件
tips:
win10不支持通过ping用net user来查看用户
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>";
}
?>
查看源码,发现把”&&” 、”;”转为空,即删除
”&&”与” &”的区别:
Command 1&&Command 2
先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
Command 1&Command 2
先执行Command 1,不管是否成功,都会执行Command 2
输入” 192.168.2.3& dir”时,同样可以攻击,表明没有对”&”过滤,”&&”和”&”是有区别的,”&&”是短路运算符,只有前一步执行成功才会执行后一步,而”&”则两个表达式都会执行。
由以上waf可知,此waf只是过滤了 “&&”和”;”这两个特殊字符,所以,可以通过使用”&“,”|”,”||”绕过
可以用命令&dir查看
可以用命令&;&dir查看
原理:在经过对;的过滤后,自动拼接成&&
High
先看源码
1 <?php
2
3 if( isset( $_POST[ 'Submit' ] ) ) {
4 // Get input
5 $target = trim($_REQUEST[ 'ip' ]);
6
7 // Set blacklist
8 $substitutions = array(
9 '&' => '',
10 ';' => '',
11 '| ' => '', //仔细看|后有空字符,过滤不完全
12 '-' => '',
13 '$' => '',
14 '(' => '',
15 ')' => '',
16 '`' => '',
17 '||' => '',
18 );
19
20 // Remove any of the characters in the array (blacklist).
21 $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
22
23 // Determine OS and execute the ping command.
24 if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
25 // Windows
26 $cmd = shell_exec( 'ping ' . $target );
27 }
28 else {
29 // *nix
30 $cmd = shell_exec( 'ping -c 4 ' . $target );
31 }
32
33 // Feedback for the end user
34 echo "<pre>{$cmd}</pre>";
35 }
36
37 ?>
此waf将”&”,”;”,”| ”,”-”,”$”,”(”,”)”,”`”,”||”这些字符直接全部转换成空格
仔细观察,可以发现”| ”中,| 后面有一个空字符,因此,可以使用”|”进行绕过
用命令“|dir"查询文件
想要对管道符有更多了解,请看这篇博客:管道符_夜飛雪的博客-CSDN博客_管道符
Impossible
先看源码
1 <?php
2
3 if( isset( $_POST[ 'Submit' ] ) ) {
4 // Check Anti-CSRF token
5 checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
6
7 // Get input
8 $target = $_REQUEST[ 'ip' ];
9 $target = stripslashes( $target );
10
11 // Split the IP into 4 octects
12 $octet = explode( ".", $target );
13
14 // Check IF each octet is an integer
15 if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
16 // If all 4 octets are int's put the IP back together.
17 $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
18
19 // Determine OS and execute the ping command.
20 if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
21 // Windows
22 $cmd = shell_exec( 'ping ' . $target );
23 }
24 else {
25 // *nix
26 $cmd = shell_exec( 'ping -c 4 ' . $target );
27 }
28
29 // Feedback for the end user
30 echo "<pre>{$cmd}</pre>";
31 }
32 else {
33 // Ops. Let the user name theres a mistake
34 echo '<pre>ERROR: You have entered an invalid IP.</pre>';
35 }
36 }
37
38 // Generate Anti-CSRF token
39 generateSessionToken();
40
41 ?>
stripslashes(string) : 该函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。
explode(separator,string,limit): 该函数把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。
is_numeric(string): 该检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。
可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。