DVWA-2.4 Command Injection(命令注入)-Impossible-安全的白名单

Impossible Level

查看源码

<?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 );  //stripslashes()删除反斜杠

    // Split the IP into 4 octects
    $octet = explode( ".", $target );   //explode() 函数把字符串打散为数组

    // 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.---如果都为整数,则将其重新组合为ip
        $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
        $html .= "<pre>{$cmd}</pre>";
    }
    else {
        // Ops. Let the user name theres a mistake
        $html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?>

可以看到,本等级的服务器源代码已被重写,只允许非常严格的输入。 如果不匹配并且没有产生特定结果,则将不允许执行它。 相比于前面2个等级的“黑名单”过滤(允许任何输入并删除不需要的内容),使用“白名单”(仅允许输入ip地址)更加安全。

posted @ 2020-05-06 16:30  zhengna  阅读(831)  评论(0编辑  收藏  举报