【DVWA命令执行(Command Injection)impossible难度代码解释】

DVWA Command Injection 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();

?> 

解析

checkToken

checkToken( $_REQUEST[ 'user_token' ],$_SESSION[ 'session_token' ], 'index.php' );

为了防csrf跨站请求伪造(伪造攻击),使用令牌的方式,如果user_token与session_token不匹配就会报错,如果匹配就返回到后面的index.php
在这里插入图片描述

stripslashes

$target = stripslashes( $target );

stripslashes()函数删除反斜杠。

举例:
在这里插入图片描述
在这里插入图片描述

explode

$octet = explode( ".", $target );

explode() 函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组。本函数返回由字符串组成的数组,由点“.”作为边界点分割出来的子字符串。

语法:explode(separator,string,limit)

参数描述
separator必需。规定在哪里分割字符串。
string必需。要分割的字符串。
limit可选。规定所返回的数组元素的数目。可能的值:大于 0 返回包含最多 limit 个元素的数组,小于 0返回包含除了最后的 -limit 个元素以外的所有元素的数组, 0会被当做 1, 返回包含一个元素的数组

举例:
在这里插入图片描述
在这里插入图片描述

is_numeric

if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) )

is_numeric()函数是检测变量是否为数字或数字字符串,这里是检查IP地址以点分割后的数组值是否为数字。

语法:bool is_numeric ( mixed v a r ) / / var ) // var)//var:要检测的变量。

拼接

$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];

重新将IP地址拼接。

剩下都在之前文章解释过,不再赘述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值