Command Injection(命令注入)

Command Injection(命令注入)

通过构造特殊命令字符串,输入到编辑域中,来改变网页动态生成的内容。Shell命令注入。

在操作系统中, & 、&& 、| 、 || 都可以作为命令连接符使用,用户通过浏览器提交执行命令,由于服务器端没有对执行函数进行过滤,从而造成可以执行危险命令

command1 & command2 :不管command1执行成功与否,都会执行command2(将上一个命令的输出作为下一个命令的输入)
command1 && command2 :先执行command1执行成功后才会执行command2 ,若command1执行失败,则不执行command2
command1 | command2:只执行command2
command1 || command2:command1执行失败,再执行command2(若command1执行成功,就不再执行command2)

  • ipconfig 查看本地网络
  • net user 查看系统用户
  • dir 查看当前目录
  • find 查找包含指定字符的行
  • whoami 查看系统当前有效用户名
  • ![image-20210510213429976](Command Injection/image-20210510213429976.png)

【low】

Command Injection Source
vulnerabilities/exec/source/low.php
<?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>";
}
?>

$_REQUEST php中$_REQUEST可以获取以POST方法和GET方法提交的数据

$_POST get方式在url后面拼接参数

**stristr(string,search,before_search) **搜索字符串的第一次出现,(从匹配点)返回字符串得剩余部分

php_uname('s') 返回运行php的操作系统的描述。参数mode可取值”a” (此为默认,包含序列”s n r v m”里的所有模式),”s ”(返回操作系统名称),”n”(返回主机名),” r”(返回版本名称),”v”(返回版本信息), ”m”(返回机器类型)。

shell_exec() 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回。

【漏洞原理】

可以看到,服务器通过stristr 与 php_uname判断操作系统是否为windows NT,执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。

window和linux系统都可以用&&来执行多条命令

127.0.0.1&&net user

Linux下输入127.0.0.1&&cat /etc/shadow甚至可以读取shadow文件(存储 Linux 系统中用户的密码信息,又称为“影子文件”)

对目标进行ping测试,我们在ping完后再执行ipconfig 命令查看ip信息,比如,我们执行 61.135.169.125 & net user xie /add ,尝试ping完后新建一个用户。

127.0.0.1

image-20210427210726500

127.0.0.1 &&ipconfig 和127.0.0.1 &ipconfig

image-20210427210921293

127.0.0.1| calc 打开计算器

image-20210427211156373

127.0.0.1| netstat -ano 查看本机端口占用情况

image-20210427211235259

系统有效用户名

![image-20210511144320885](Command Injection/image-20210511144320885.png)

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

?>

多了

// Set blacklist
    $substitutions = array(
        '&&' => '',
        ';'  => '',
    );

    // Remove any of the charactars in the array (blacklist).
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );

采用黑名单机制,过滤了 ‘&&’ ,但未过滤 '&';过滤了 ';',但未过滤 ‘|;’

【绕过方式】

&&&

&;&

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

?>
  '| ' => '',

可以用"|"

posted @ 2021-07-03 16:11  dem0n小姐姐  阅读(712)  评论(0)    收藏  举报