dvwa执行php,DVWA之Command Execution命令执行漏洞

Command Execution命令执行漏洞,或称为命令注入漏洞。

70eae84867913ec5699d480ff57a1fb7.png

安全级别为Low时,输入正确的IP地址,提交后返回ping 命令的结果

936dae97463f916818270712aef82294.png

如果输入的值是非IP地址的值,则返回的是不正常结果

9e6f4a40418d2ecb401ff21ea26034c6.png2f2c0870f2bd18733577b1ae05c38eaf.png

现在我们输入正确的ip地址以及连接命令,如输入

127.0.0.1 && net user

返回结果如下:

d447fe962fbf13825ae144472b75ea2e.png

说明除了执行ping 命令外,还执行了net user命令,造成任意命令执行漏洞。

查看漏洞源代码

(1)Low Level Sourcecode

//Low Security Level

if( isset( $_POST[ 'submit' ] ) ) {

$target = $_REQUEST[ 'ip' ];

// Determine OS and execute the ping command.

if (stristr(php_uname('s'), 'Windows NT')) {

$cmd = shell_exec( 'ping ' . $target );

echo '

'.$cmd.'

';

} else {

$cmd = shell_exec( 'ping -c 3 ' . $target );

echo '

'.$cmd.'

';

}

}

?>

(2)Medium Level Sourcecode

<?php //Medium Security Level if( isset( $_POST[ 'submit'] ) ) { $target = $_REQUEST[ 'ip' ]; // Remove any of the charactars in the array (blacklist). $substitutions = array( '&&' => '',

';' => '',

);

$target = str_replace( array_keys( $substitutions ), $substitutions, $target );

// Determine OS and execute the ping command.

if (stristr(php_uname('s'), 'Windows NT')) {

$cmd = shell_exec( 'ping ' . $target );

echo '

'.$cmd.'

';

} else {

$cmd = shell_exec( 'ping -c 3 ' . $target );

echo '

'.$cmd.'

';

}

}

?>

(3)High Level Sourcecode

?php

//High Security Level

if( isset( $_POST[ 'submit' ] ) ) {

$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')) {

$cmd = shell_exec( 'ping ' . $target );

echo '

'.$cmd.'

';

} else {

$cmd = shell_exec( 'ping -c 3 ' . $target );

echo '

'.$cmd.'

';

}

}

else {

echo '

ERROR: You have entered an invalid IP

';

}

}

?>

综合比较三个不用等级代码发现:

同样的处理操作:判断操作系统类型,若是windows类型,执行ping ip命令;若是非windows类型,执行ping -c 3 ip 命令

Low级别:输入的ip值不经任何处理,直接作为参数传入,造成的结果就是使用命令连接字符可以输入任意命令来执行

Medium级别:相当于加了一个黑名单,将输入ip值中的 &&  ;  变成空,然后作为参数输入,可以有效过滤&&连接字符,但是对于黑名单之外的例如 || 字符没有过滤,依然可以构造命令注入。|| 使用就是当前边的命令执行失败后执行后边的命令。

构造命令aaaa || net user 执行结果如图:

57f42cc49c328ac4c81069f9a567e11f.png

High级别:对获取的ip值,先去下划线处理,然后根据’.’来分成数组,判断是否分成四份且每一份是数字的,然后还原回去,对ip值进行ping操作,否则判定输入ip值为非法ip格式。经过这样的处理,输入的只能是ip格式的参数,确保了执行输入参数的安全性。

命令注入漏洞防御:

(1)谨慎使用像exec(),shell_exec(),system(),passthru()等命令

(2)确保输入的参数跟命令匹配,对参数进行转义、过滤、黑白盒限制等

(3)如果有必要,避免使用系统执行函数

(4)设置safe_mode = on

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值