3.Command Injection(命令注入)

 命令注入攻击的常见模式为:仅仅需要输入数据的场合,却伴随着数据同时输入了恶意代码,

装载数据的系统对此并未设计良好的过滤过程,导致恶意代码也一并执行,最终导致信息泄露或者正常数据的破坏。

   PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

现在开始我们的 Command Injection

首先打开 Command Injection,发现是一个执行 ping 的界面,推测若没有做好过滤,则很有可能执行其他 DOS 命令

image

DOS 在一行中执行多条命令需使用的符号:

&&:只有当前面的命令执行成功才执行后面的命令

&:无论怎样执行后面的命令

||:只有当前面的命令执行失败才执行后面的命令

|:将前面命令执行的输出作为后面命令执行的输入

Low

1.Low 级别的源码,未对用户输入 ip 做任何过滤

image

2.可以看到,low 级别的代码接收了用户输入的 ip,然后根据服务器是否是 Windows NT 系统,对目标 ip 进行不同的 ping 测试。

但是这里对用户输入的 ip 并没有进行任何的过滤,所以我们可以进行命令执行漏洞

我们 ping 一个 ip(随便一个能 ping 得通的就行,比如本机 ip 127.0.0.1)

可以看到能 ping 通

image

3.我们尝试输入 127.0.0.1 & ipconfig ,在操作系统中," & 、&& 、| 、 || "都可以作为命令连接符使用,我们在 ping 完后再执行 ipconfig 命令查看 ip 信息

可以看到,成功执行。然后我们就可以继续执行我们的命令了。把 ipconfig 换成其他的系统命令

image

比如,我们执行 127.0.0.1 & net user xie /add ,尝试 ping 完后新建一个用户,我们就可以成功创建用户了。

image

使用&,查看系统用户

image

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

可以看到,medium 级别的代码在 low 级别的代码上增加量了对 && 和 ;的过滤,

但并未过滤&,|,||

使用&,查看系统用户

image

输入” 127.0.0.1&;& net view ”时,也是可以的,因为过滤一次后相当于”127.0.0.1 && net view

image

High

image

仔细查看过滤代码发现 ”|”后面有个空格,因此当输入”127.0.0.1 |net view” ,一样可以攻击,”|”是管道符,意思是将前者处理后的结果作为参数传给后者。

image

也可以使用一个不会执行的命令和 | 来执行命令

image

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

?> 

stripslashes(string) : 该函数会删除字符串 string 中的反斜杠,返回已剥离反斜杠的字符串。

explode(separator,string,limit): 该函数把字符串打散为数组,返回字符串的数组。参数 separator 规定在哪里分割字符串,参数 string 是要分割的字符串,可选参数 limit 规定所返回的数组元素的数目。

is_numeric(string): 该检测 string 是否为数字或数字字符串,如果是返回 TRUE,否则返回 FALSE。

可以看到,Impossible 级别的代码加入了 Anti-CSRF token,同时对参数 ip 进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

  • 安全

    安全永远都不是一个小问题。

    189 引用 • 813 回帖 • 1 关注
  • PHP

    PHP(Hypertext Preprocessor)是一种开源脚本语言。语法吸收了 C 语言、 Java 和 Perl 的特点,主要适用于 Web 开发领域,据说是世界上最好的编程语言。

    164 引用 • 407 回帖 • 528 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...