生活随笔
收集整理的這篇文章主要介紹了
XCTF-高手进阶区:ics-07
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
發現view-source
<!DOCTYPE html
>
<html
><head
><meta charset
="utf-8"><title
>cetc7
</title
></head
><body
><?php
session_start();if (!isset($_GET[page
])) {show_source(__FILE__);die();}if (isset($_GET[page
]) && $_GET[page
] != 'index.php') {include('flag.php');}else {header('Location: ?page=flag.php');}?><form action
="#" method
="get">page : <input type
="text" name
="page" value
="">id : <input type
="text" name
="id" value
=""><input type
="submit" name
="submit" value
="submit"></form
><br
/><a href
="index.phps">view
-source
</a
><?php
if ($_SESSION['admin']) {$con = $_POST['con'];$file = $_POST['file'];$filename = "backup/".$file;if(preg_match('/.+\.ph(p[3457]?|t|tml)$/i', $filename)){die("Bad file extension");}else{chdir('uploaded');$f = fopen($filename, 'w');fwrite($f, $con);fclose($f);}}?><?php
if (isset($_GET[id
]) && floatval($_GET[id
]) !== '1' && substr($_GET[id
], -1) === '9') {include 'config.php';$id = mysql_real_escape_string($_GET[id
]);$sql="select * from cetc007.user where id='$id'";$result = mysql_query($sql);$result = mysql_fetch_object($result);} else {$result = False;die();}if(!$result)die("<br >something wae wrong ! <br>");if($result){echo "id: ".$result->id."</br>";echo "name:".$result->user."</br>";$_SESSION['admin'] = True;}?></body
>
</html
>
第一段分析
參數page存在且其值不等于index.php,才會包含flag.php
<?phpsession_start();if (!isset($_GET[page
])) {show_source(__FILE__);die();}if (isset($_GET[page
]) && $_GET[page
] != 'index.php') {include('flag.php');}else {header('Location: ?page=flag.php');}?>
第二段分析
首先$_SESSION['admin'],將$con的內容寫入到$file,但文件后綴名不可以為.php3/4/5/6/7/t/html
<?phpif ($_SESSION['admin']) {$con = $_POST['con'];$file = $_POST['file'];$filename = "backup/".$file;if(preg_match('/.+\.ph(p[3457]?|t|tml)$/i', $filename)){die("Bad file extension");}else{chdir('uploaded');$f = fopen($filename, 'w');fwrite($f, $con);fclose($f);}}?>
第三段分析
$_SESSION['admin'] = True可以滿足第二段所需
該段需要滿足參數id存在,id的浮點值不為'1',id參數的最后一個數值是9
<?phpif (isset($_GET[id
]) && floatval($_GET[id
]) !== '1' && substr($_GET[id
], -1) === '9') {include 'config.php';$id = mysql_real_escape_string($_GET[id
]);$sql="select * from cetc007.user where id='$id'";$result = mysql_query($sql);$result = mysql_fetch_object($result);} else {$result = False;die();}if(!$result)die("<br >something wae wrong ! <br>");if($result){echo "id: ".$result->id."</br>";echo "name:".$result->user."</br>";$_SESSION['admin'] = True;}?>
總結
1、第三段
可以使用id=1+9來繞過
payload:
http://159.138.137.79:56884//index.php?page=flag.php&id=1+9
2、第二段
獲得了admin,$con等于文件的內容,$file為文件名,$filename = "backup/".$file,因為preg_match('/.+\.ph(p[3457]?|t|tml)$/i', $filename是判斷.之后的值,那么可以使用例如peak.php/.繞過,(/.表示在peak.php文件所在目錄加一個新的空目錄,相當于沒加,例如在一個網站后面加/.,測試后即可了解),繞過正則后,還有一個問題,它改變了當前目錄,這就是chdir搞得鬼,那,何為chdir?
chdir
(PHP 4, PHP 5, PHP 7)
chdir — 改變目錄說明
chdir( string $directory) : bool
將
PHP 的當前目錄改為 directory。 參數
directory
新的當前目錄 返回值
成功時返回
TRUE, 或者在失敗時返回
FALSE。 錯誤/異常
Throws an error of level
E_WARNING on failure
. 范例
Example
<?php
echo getcwd() . "\n";chdir('public_html');
echo getcwd() . "\n";?> 以上例程的輸出類似于:
/home
/vincent
/home
/vincent
/public_html
相當于,將當前所在目錄后面添加一個新的目錄,該文件即在新添的這個目錄下,根據代碼所知,原本是在根目錄下,現在在根目錄后面添加一個uploaded目錄,$filename就在根目錄/uploaded下,所以,第二段的payload為:
總結
以上是生活随笔為你收集整理的XCTF-高手进阶区:ics-07的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。