DVWA--Command Injection(命令注入)(全难度)

DVWA–Command Injection(命令注入)

原理参考:网络安全笔记-99-渗透-OS命令注入

管道符

  • ”;“:分号。执行完前面的语句再执行后面的语句
  • ”|“:显示后面语句的执行结果,例如:ping 127.0.0.1| whoami
  • ”||“:当前面的语句执行出错时执行后面的语句。例如:ping 1 || whoami
  • ”&“:如果前面的语句为假则直接执行后面的语句,前面的语句可真可假
  • ”&&“:如果前面的语句为假则直接出错,也不执行后面的语句,前面的语句只能为真。

常见URL编码:

  • %21: 空格
  • %5c: \
  • %26: &
  • %7c: |

难度分级

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

?>
分析:

php_uname(”string“):返回运行PHP的系统的有关信息。

‘a’:此为默认。包含序列 “s n r v m” 里的所有模式。
‘s’:操作系统名称。例如: FreeBSD。
‘n’:主机名。例如: localhost.example.com。
‘r’:版本名称,例如: 5.1.2-RELEASE。
‘v’:版本信息。操作系统之间有很大的不同。
‘m’:机器类型。例如:i386。


shell_exec():通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回

判断系统类型,然后直接拼接用户输入$target,没有过滤。

利用:

image-20210204153234355

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

?>
分析:

将”&&“和”;“替换为空。其他管道符仍然可以使用。

利用:

与Low模式相同。

High

<?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
    echo "<pre>{$cmd}</pre>";
}

?> 
分析:

替换的字符变多了。但是“| ”后面有一个空格,所以我们依然可以用“|”进行绕过。

利用:

不变。

Impossible

核心代码:
<?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();

?> 
分析:

首先去除反斜线。

stripslashes():返回一个去除转义反斜线后的字符串(\’ 转换为 等等)。双反斜线(\\)被转换为单个反斜线(\)。

然后将IP地址用点号.分隔,返回一个字符数组。

explode():用字符串分隔另一个字符串,返回分隔后的字符数组。

然后再检查IP地址中的每8个比特位是否为数字,如果是则把IP地址重新组合起来。

Impossible级别并且加入了Anti-CSRF token与session Token。安全级别非常高。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值