0
点赞
收藏
分享

微信扫一扫

DVWA : Command Injection

三次方 2022-03-30 阅读 47
phpweb安全

难度:Low

代码解析

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_REQUEST[ 'ip' ];

    // 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.stristr() PHP stristr() 函数

stristr() 函数搜索字符串在另一字符串中的第一次出现。

语法:stristr(string,search,before_search)

参数描述
string必需。规定被搜索的字符串。
search

必需。规定要搜索的字符串。

如果该参数是数字,则搜索匹配该数字对应的 ASCII 值的字符。

before_search

可选。默认值为 "false" 的布尔值。

如果设置为 "true",它将返回 search 参数第一次出现之前的字符串部分。

 返回字符串的剩余部分(从匹配点)。如果未找到所搜索的字符串,则返回 FALSE。

2.php_uname() PHP中php_uname的用法详解

返回运行 PHP 的系统的有关信息。

参数

mode 是单个字符,用于定义要返回什么信息:

'a':此为默认。包含序列 "s n r v m" 里的所有模式。

's':操作系统名称。例如: FreeBSD。

'n':主机名。例如: localhost.example.com。

'r':版本名称,例如: 5.1.2-RELEASE。

'v':版本信息。操作系统之间有很大的不同。

'm':机器类型。例如:i386。

3.shell_exec() php shell_exec() 命令用法_PHP教程

shell_exec ()函数 是PHP中的一个内置函数,用于通过shell执行命令并以字符串的形式返回完整的输出。

感觉跟反引号一样是用来执行命令的。

查看代码后发现没有对输入的数据作出任何过滤,可以直接针对操作系统类型进行ping。

命令注入

命令注入通常因为指Web应用在服务器上拼接系统命令而造成的漏洞。

操作

直接输入 ip && 系统命令

提交后先执行ping ip再执行系统命令,就可以得到你想要的信息。

输入 ip & 系统命令 

提交后限制性系统命令再执行ping ip.但是显示的结果都是先显示ping的结果再显示系统命令的结果。

输入 ip | 系统命令 

只执行系统命令。

等级:Medium

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_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.str_replace() PHP str_replace() 函数

替换字符串中的一些字符(区分大小写)。

语法:str_replace(find,replace,string,count)

参数描述
find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。一个变量,对替换数进行计数。

返回带有替换值的字符串或数组。

2.array_keys() PHP array_keys() 函数

 返回包含数组中所有键名的一个新数组。

语法:array_keys(array,value,strict)

参数描述
array必需。规定数组。
value可选。您可以指定键值,然后只有该键值对应的键名会被返回。
strict可选。与 value 参数一起使用。可能的值:
  • true - 返回带有指定键值的键名。依赖类型,数字 5 与字符串 "5" 是不同的。
  • false - 默认值。不依赖类型,数字 5 与字符串 "5" 是相同的。

返回包含数组中所有键名的一个新数组。

操作

Medium将"&&"和";"过滤了,但任然可以使用"&"和"|"执行系统命令。

等级: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>";
}

?> 

High相比Medium过滤了更多的特殊字符,但是可以看到'| '的|后是有空格的,所以可以输入 ip |系统命令来绕过过滤的到想要的信息。

难度: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();

?> 

Impossible增加了token,防止csrf攻击。

1. expload() PHP explode() 函数

把字符串打散为数组。

使用一个字符串分割另一个字符串,并返回由字符串组成的数组。

语法:explode(separator,string,limit)

参数描述
separator必需。规定在哪里分割字符串。
string必需。要分割的字符串。
limit

可选。规定所返回的数组元素的数目。

可能的值:

  • 大于 0 - 返回包含最多 limit 个元素的数组
  • 小于 0 - 返回包含除了最后的 -limit 个元素以外的所有元素的数组
  • 0 - 返回包含一个元素的数组

 返回字符串的数组。

验证了ip的每个部分均为数字后再合并,过滤了字符串。

举报

相关推荐

0 条评论