分析DVWA靶场高难度High和不可能难度Impossible的命令执行漏洞

目录

命令执行漏洞

High

查看源代码

Impossible

查看源代码


命令执行漏洞

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

1.第一行检查是否设置了“Submit”POST变量。如果设置了,将执行ping命令。

2.第三行获取用户输入的IP地址,并使用trim()函数删除任何前导或尾随空格。

   trim()函数

 

3.第六行定义了一个黑名单数组,其中包含一些特殊字符,如&、;、|、-、$、(、)、`和||。这些字符可能被用于注入攻击。

 4.第九行使用str_replace()函数将黑名单数组中的任何字符替换为空字符串。这将删除任何黑名单字符,从而防止注入攻击。

array_keys()函数 返回数组中部分或所有的键名

 

 str_replace()函数  子字符串替换 它用一个字符串替换另一个字符串中的所有匹配项

 

 5.第十二行使用php_uname()函数检测操作系统类型,并根据操作系统类型执行不同的ping命令。如果操作系统是Windows,则执行“ping IP地址”命令;如果操作系统是*nix,则执行“ping -c 4 IP地址”命令。

stristr()函数 查找字符串的首次出现

php_uname()函数 返回运行PHP的系统的有关信息

 

7.第十五行将ping命令的输出结果存储在$cmd变量中。
8.第十八行使用echo语句将$cmd变量的值输出给用户。

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();

?>

1.第一行检查是否设置了“Submit”POST变量。如果设置了,将执行ping命令。
2.第三行调用checkToken()函数,检查Anti-CSRF令牌是否有效。如果令牌无效,则脚本将重定向到“index.php”页面。判断输入的值是否为空,不为空,继续往下执行。


3.第六行获取用户输入的IP地址,并使用stripslashes()函数删除任何反斜杠。

stripslashes()函数 反引用一个引用字符串

4.第九行使用explode()函数将IP地址分割成4个八位字节。

explode()函数 使用一个字符串分割另一个字符串

5.第十二行使用is_numeric()函数检查每个八位字节是否为整数。

 is_numeric()检测变量是否为数字或者数字字符串

 

 


6.第十七行如果所有4个八位字节都是整数,则将它们重新组合成IP地址,并将其作为参数传递给ping命令。

7.第二十行使用php_uname()函数检测操作系统类型,并根据操作系统类型执行不同的ping命令。如果操作系统是Windows,则执行“ping IP地址”命令;如果操作系统是*nix,则执行“ping -c 4 IP地址”命令。


8.第二十三行将ping命令的输出结果存储在$cmd变量中。


9.第二十六行使用echo语句将$cmd变量的值输出给用户。

10.第二十九行如果IP地址无效,则输出错误消息。


11.第三十二行调用generateSessionToken()函数,生成新的Anti-CSRF令牌。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值