DVWA靶场命令执行代码审计

该文章讨论了PHP代码中防止命令注入的措施,包括使用trim和str_replace函数过滤特殊字符,以及通过检查IP地址的每个八位段是否为数字来验证输入。同时,提到了对操作系统检测以执行不同ping命令的处理,并强调了原代码中可能存在的安全漏洞。
摘要由CSDN通过智能技术生成

high

源码


Command Injection Source
<?php

if( isset( $_POST[ 'Submit' ]  ) ) {//isset函数判断内容是否为空
    // 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>";
}

?>

相关函数解析

trim函数——去除字符串首尾处的空白字符(或者其他字符)

首位都存在空白字符
在这里插入图片描述
查看输出结果
在这里插入图片描述

arry_keys函数——返回数组中部分的或所有的键名

在这里插入图片描述
for循环,输出数组内的键名
在这里插入图片描述

str_replace函数——子字符串替换

如果传递过来的$target参数中含有 $substitutions 中的内容,那么会根据内容来进行替换

当没用添加str_replace函数时,$target字符串中的内容为正常输出的内容
在这里插入图片描述
在这里插入图片描述
添加了str_replace函数之后,输出的内容会过滤掉数组中定义的内容
在这里插入图片描述
在这里插入图片描述
输出的内容已经过滤掉了ipconfig等一些命令

stristr函数——查看字符是否首次出现并忽略大小写

这里stristr函数的作用是判断系统是否为windows nt 不区分大小写,如果是则执行命令

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

在这里插入图片描述
在这里插入图片描述

代码分析

设置了黑名单$ ; |+ - ( ) 反引号 ||
黑名单中漏掉了关键字符|

impossible

源码

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

?>

相关函数解析

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

在这里插入图片描述
用.将$target的内容分割
在这里插入图片描述

is_numeric函数——检查内容是否为数字或字符串

如果是纯数字在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

if判断语句
  if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {

这里需要判断由.分割得到的$octet数组的内容是否都为数字,且一共只有四个,如果都成立的话那么执行命令
因为 $target通过post传递的参数应该是一个ip地址,ip地址由.分割得到四个纯数字数组

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值