Impossible防御能破吗

发布时间 2023-11-10 23:40:15作者: 风香客
  • 先来看他的源代码
<?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();

?>

如何看懂代码

  • 不需要看的懂,知道他的意思就行
  • 以上源代码,最重要的几句。提取出来
$target = $_REQUEST[ 'ip' ];
    $target = stripslashes( $target );  // 这两行就是赋值操作

    // Split the IP into 4 octects
    $octet = explode( ".", $target );

    // if判断这里才重要
    // 这一行就是把你输入的东西拆分。他还有一个功能就是,只要你输入数字以为的东西,比如:/ & 还有其他符号就会直接报错。就凭这一功能就锁死攻击的可能
    if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
        // 这一行就是,把拆分的数字重新组合起来。只要看得懂这几行就行
        $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];