Command Injection

Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

下面对四种级别的代码进行分析。

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

} 

?> 

相关函数介绍

stristr(string,search,before_search)

stristr函数搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回FALSE。参数string规定被搜索的字符串,参数search规定要搜索的字符串(如果该参数是数字,则搜索匹配该数字对应的ASCII值的字符),可选参数before_true为布尔型,默认为“false”,如果设置为“true”,函数将返回search参数第一次出现之前的字符串部分。

php_uname(mode)

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

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

漏洞利用

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

127.0.0.1&&net user

Linux下输入127.0.0.1&&cat /etc/shadow甚至可以读取shadow文件,可见危害之大。

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

} 

?>

可以看到,相比Low级别的代码,服务器端对ip参数做了一定过滤,即把”&&” 、”;”删除,本质上采用的是黑名单机制,因此依旧存在安全问题。

漏洞利用

1、127.0.0.1&net user

因为被过滤的只有”&&”与” ;”,所以”&”不会受影响。

这里需要注意的是”&&”与” &”的区别:

Command 1&&Command 2

先执行Command 1,执行成功后执行Command 2,否则不执行Command 2

Command 1&Command 2

先执行Command 1,不管是否成功,都会执行Command 2

2、由于使用的是str_replace把”&&” 、”;”替换为空字符,因此可以采用以下方式绕过:

127.0.0.1&;&ipconfig

这是因为”127.0.0.1&;&ipconfig”中的” ;”会被替换为空字符,这样一来就变成了”127.0.0.1&& ipconfig” ,会成功执行。

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

} 

?> 

相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。

漏洞利用

黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,于是 ”|”成了“漏网之鱼”。

127.0.0.1|net user

Command 1 | Command 2

“|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。

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)

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

explode(separator,string,limit)

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

is_numeric(string)

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

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

本文原创作者:lonehand

转自:http://www.freebuf.com/articles/web/116714.html

DVWA之Command Injection的更多相关文章

  1. 【DVWA】Command Injection(命令注入)通关教程

    日期:2019-08-01 16:05:34 更新: 作者:Bay0net 介绍:利用命令注入,来复习了一下绕过过滤的方法,还可以写一个字典来 fuzz 命令注入的点. 0x01. 漏洞介绍 仅仅需要 ...

  2. DVWA之Command injection(命令执行漏洞)

    目录 Low Medium Middle Impossible 命令执行漏洞的原理:在操作系统中, &  .&& .|  . ||   都可以作为命令连接符使用,用户通过浏览器 ...

  3. 安全性测试入门:DVWA系列研究(二):Command Injection命令行注入攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. Command Injection:命令注入攻击. 1. Command Injection命令注入 命令注入是通过在应用中执行宿主操作系统的命令, ...

  4. DVWA Command Injection 通关教程

    Command Injection 介绍 命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻 ...

  5. DVWA之命令注入(command injection)

    Command injection就是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的 LOW 无论是Windows还是Linux,都可以使用&&连接多个命令 执行 ...

  6. DVWA Command Injection 解析

    命令注入,即 Command Injection.是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的. 在Web应用中,有时候会用到一些命令执行的函数,如php中system.ex ...

  7. DVWA靶场练习-Command Injection命令注入

    Command Injection 原理 攻击者通过构造恶意参数,破坏命令的语句结构,从而达到执行恶意命令的目的.

  8. DVWA(四):Command Injection 全等级命令注入

    Command Injection : 命令注入(Command Injection),对一些函数的参数没有做好过滤而导致用户可以控制输入的参数,使其恶意执行系统命令或这cmd.bash指令的一种注入 ...

  9. DVWA靶场之Command Injection(命令行注入)通关

    Command Injection Low: <?php if( isset( $_POST[ 'Submit' ]  ) ) { // Get input $target = $_REQUES ...

随机推荐

  1. 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理

    在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...

  2. System.Net.Mail邮件发送抄送附件(多个)

    /// <summary> /// 邮件发送抄送附件 /// </summary> /// <param name="mailTo">收件人(可 ...

  3. 华为OD机试题

    """最长回文字符串问题"""# 说明:方法很多,这个是最简单,也是最容易理解的一个,利用了动态规化.# 先确定回文串的右边界i,然后以右边 ...

  4. FPGA的开发板

    板卡架构 板载FPGA(K7-325T)处理24端口10/100/1000M以太网数据: FPGA外挂4Gbit的DDR3颗粒,最大支持800MHz: 板载CPU进行系统配置.管理,并与客户端软件通信 ...

  5. BeetleX使用bootstrap5开发SPA应用

        在早期版本BeetleX.WebFamily只提供了vuejs+element的集成,由于element只适合PC管理应用开发相对于移动应用适配则没这么方便.在新版本组件集成了bootstra ...

  6. 超详细Linux新手快速入门(一)——Linux的介绍安装以及虚拟机的介绍安装

    一.Linux的介绍 1.Linux和Windows的比较  Linux是一款操作系统,其性能稳定,因其防火墙组件高效安全.简单易配置,所以获得了追求速度和安全的一些企业和人群的青睐.与我们日常所熟知 ...

  7. PriorityQueue 是线性结构吗?90%的人都搞错了!

    文章首发于「陈树义」公众号及个人博客 shuyi.tech 其实这个问题的完整描述是:Java 中的 PriorityQueue 实现,其数据的逻辑结构是线性结构吗?其数据的物理结构又是什么? 估计很 ...

  8. gtk中构件添加背景图

    在gtk中我们总想要去给构件添加背景图,具体函数代码如下 void chang_background(GtkWidget *widget, int w, int h, const gcha r *pa ...

  9. POJ1562_Oil Deposits(JAVA语言)

    思路:bfs.水题,标记下计数就完了. Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22928 ...

  10. 【数据结构与算法笔记04】对图搜索策略的一些思考(包括DFS和BFS)

    图搜索策略 这里的"图搜索策略"应该怎么理解呢? 首先,是"图搜索",所谓图无非就是由节点和边组成的,那么图搜索也就是将这个图中所有的节点和边都访问一遍. 其次 ...