DVWA Command Injection 攻略

DVWA Command Injection 攻略

●初级

进行黑盒测试一下:
127.0.0.1
在这里插入图片描述

返回了ping命令结果,从TTL值来看是windows系统。
尝试执行多条命令
127.0.0.1&whoami
在这里插入图片描述

查看源代码:

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

代码中存在PHP危险函数shell_exec(),且代码中没有进行任何的过滤,存在RCE漏洞

●中级

127.0.0.1&whoami
在这里插入图片描述

查看源代码:

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

?>

代码中存在PHP危险函数shell_exec(),代码中又设置了黑名单,过滤了&&和;,但是可以绕过黑名单,依旧存在RCE漏洞

●高级

127.0.0.1|whoami
在这里插入图片描述

查看源代码:

<?php
if( isset( $_POST[ 'Submit' ]  ) ) {
// Get input
$target = trim($_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
$html .= "<pre>{$cmd}</pre>";
}
?>

代码中存在PHP危险函数shell_exec(),代码中又设置了黑名单,过滤了很多符号,但是还可以使用|绕过黑名单,依旧存在RCE漏洞

●完全防御难度分析

代码分析:

<?php
if( isset( $_POST[ 'Submit' ]  ) ) {
// 检查防范CSRF的令牌
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target ); //去除反斜杠

$octet = explode( ".", $target );
//explode()函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组
// 将IP分割为4个八进制

// 检查每个八进制是否为整数
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {

//is_numeric()函数用于判断括号内的值是否为数字或数字字符串,如果是返回true否则返回false
//同时sizeof()函数判断分割的数组是否为4
// 如果4个八进制都是整数,把IP放回去
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];

// 判断操作系统,执行ping命令
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. 让用户知道有个错误
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// 生成防范CSRF的令牌
generateSessionToken();
?>

代码审计:
1、这里增加了令牌检查,防范CSRF(跨站请求伪造),同时对参数使用了stripslashes( )函数去除了反斜杠
2、使用explode()函数将IP根据"."分块,然后is_numeric()函数检查每一块是否为数字或数字字符串,限制了其他字符,同时还有sizeof()函数确定是否分割的数组只有4块,导致注入的不可能性
相关函数说明:
stripslashes( ), 删除反斜杠
addslashes() 函数,返回在预定义字符(单引号,双引号,反斜杠)之前添加反斜杠的字符串。
explode()函数,把字符串分割为数组。
is_numeric()函数,用于检测变量是否为数字或数字字符串。返回值为布尔类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gaynell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值