PHP僞協議探究php
php中支持的僞協議有下面這麼多python
file:// — 訪問本地文件系統
http:// — 訪問 HTTP(s) 網址
ftp:// — 訪問 FTP(s) URLs
php:// — 訪問各個輸入/輸出流(I/O streams)
zlib:// — 壓縮流
data:// — 數據(RFC 2397)
glob:// — 查找匹配的文件路徑模式
phar:// — PHP 歸檔
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — 音頻流
expect:// — 處理交互式的流
今天着重研究php://shell
首先先把官方文檔貼上來
http://php.net/manual/zh/wrappers.php.php數組
有兩個比較重要的配置在php.ini中,allow_url_fopen 和allow_url_include會影響到fopen等等和include等等函數對於僞協議的支持,而allow_url_include依賴allow_url_fopen,因此allow_url_fopen不開啓的話,allow_url_include也是沒法使用的。app
php://是用來訪問各個輸入、輸出流的,除了php://stdin, php://stdout 和 php://stderr
ssh
php://input表明能夠訪問請求的原始數據,簡單來講POST請求的狀況下,php://input能夠獲取到post的數據。函數
比較特殊的一點,enctype=」multipart/form-data」 的時候 php://input 是無效的。post
php://output 是一個只寫的數據流, 容許你以 print 和 echo 同樣的方式 寫入到輸出緩衝區。this
這篇文章的關鍵在於討論php://filter,事實上,這也是咱們經常使用的一個僞協議,在任意文件讀取,甚至getshell的時候都有利用的機會。編碼
php://filter
是一種元封裝器, 設計用於數據流打開時的篩選過濾應用。 這對於一體式(all-in-one)的文件函數很是有用,相似 readfile()、 file() 和 file_get_contents(), 在數據流內容讀取以前沒有機會應用其餘過濾器。
事實上,在include函數的使用上,常常會形成任意文件讀取漏洞,而file_get_contents()和file_put_contents()這樣函數下,經常會構成getshell等更嚴重的漏洞。
php://filter 目標使用如下的參數做爲它路徑的一部分。 複合過濾鏈可以在一個路徑上指定。詳細使用這些參數能夠參考具體範例。
文檔裏是這麼寫的
名稱 描述 resource=<要過濾的數據流> 這個參數是必須的。它指定了你要篩選過濾的數據流。 read=<讀鏈的篩選列表> 該參數可選。能夠設定一個或多個過濾器名稱,以管道符(|)分隔。 write=<寫鏈的篩選列表> 該參數可選。能夠設定一個或多個過濾器名稱,以管道符(|)分隔。 <;兩個鏈的篩選列表> 任何沒有以 read= 或 write= 做前綴 的篩選器列表會視狀況應用於讀或寫鏈。
咱們舉一個例子,這是平時咱們用來任意文件讀取的payload
php://filter/read=convert.base64-encode/resource=upload.php
這裏讀的過濾器爲convert.base64-encode,就和字面上的意思同樣,把輸入流base64-encode。
resource=upload.php,表明讀取upload.php的內容
下面仔細研究下關於過濾器的問題
先貼文檔,不由於本身的翻譯小問題接鍋 (・ω・)ノ
http://php.net/manual/zh/filters.php
http://php.net/manual/zh/filters.convert.php
convert.* 過濾器是php5.0.0之後添加的。
convert.base64-encode和 convert.base64-decode使用這兩個過濾器等同於分別用 base64_encode()和 base64_decode()函數處理全部的流數據。 convert.base64-encode支持以一個關聯數組給出的參數。若是給出了 line-length,base64 輸出將被用 line-length個字符爲 長度而截成塊。若是給出了 line-break-chars,每塊將被用給出的字符隔開。這些參數的效果和用 base64_encode()再加上 chunk_split()相同。
固然,這裏的過濾器不止運用於php://filter,因此文檔中給出的例子是這樣的
<?php $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'convert.base64-encode'); fwrite($fp, "This is a test.\n"); fclose($fp); /* Outputs: VGhpcyBpcyBhIHRlc3QuCg== */ $param = array('line-length' => 8, 'line-break-chars' => "\r\n"); $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'convert.base64-encode', STREAM_FILTER_WRITE, $param); fwrite($fp, "This is a test.\n"); fclose($fp); /* Outputs: VGhpcyBp : cyBhIHRl : c3QuCg== */ $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'convert.base64-decode'); fwrite($fp, "VGhpcyBpcyBhIHRlc3QuCg=="); fclose($fp); /* Outputs: This is a test. */ ?>
convert.quoted-printable-encode和 convert.quoted-printable-decode使用此過濾器的 decode 版本等同於用 quoted_printable_decode()函數處理全部的流數據。沒有和 convert.quoted-printable-encode相對應的函數。 convert.quoted-printable-encode支持以一個關聯數組給出的參數。除了支持和 convert.base64-encode同樣的附加參數外, convert.quoted-printable-encode還支持布爾參數 binary和 force-encode-first。 convert.base64-decode只支持 line-break-chars參數做爲從編碼載荷中剝離的類型提示。
關於quoted_printable_decode()在php.net上的解釋是將 quoted-printable 字符串轉換爲 8-bit 字符串,原諒我沒怎麼看懂
string.*是用來處理各個字符串的,比較像python的string模塊
rot13,很好理解
<?php $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'string.rot13'); fwrite($fp, "This is a test.\n"); /* Outputs: Guvf vf n grfg. */ ?>
變大寫,也一樣很好理解
<?php $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'string.toupper'); fwrite($fp, "This is a test.\n"); /* Outputs: THIS IS A TEST. */ ?>
這回是小寫
<?php $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'string.tolower'); fwrite($fp, "This is a test.\n"); /* Outputs: this is a test. */ ?>
string.strip_tags(自 PHP 5.0.0 起)使用此過濾器等同於用 strip_tags()函數處理全部的流數據。能夠用兩種格式接收參數:一種是和 strip_tags()函數第二個參數類似的一個包含有標記列表的字符串,一種是一個包含有標記名的數組。
strip_tags()返回給定的字符串 str 去除空字符、HTML 和 PHP 標記後的結果。
<?php $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, "<b><i><u>"); fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n"); fclose($fp); /* Outputs: <b>bolded text</b> enlarged to a level 1 heading */ $fp = fopen('php://output', 'w'); stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, array('b','i','u')); fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n"); fclose($fp); /* Outputs: <b>bolded text</b> enlarged to a level 1 heading */ ?>
zlib.* 壓縮過濾器自 PHP 版本 5.1.0起可用,在激活 zlib的前提下。也能夠經過安裝來自 » PECL的 » zlib_filter包做爲一個後門在 5.0.x版中使用。此過濾器在 PHP 4 中 不可用。
zlib.deflate和 zlib.inflate是主要的兩個用法
<?php $params = array('level' => 6, 'window' => 15, 'memory' => 9); $original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n"; echo "The original text is " . strlen($original_text) . " characters long.\n"; $fp = fopen('test.deflated', 'w'); stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params); fwrite($fp, $original_text); fclose($fp); echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n"; echo "The original text was:\n"; /* Use readfile and zlib.inflate to decompress on the fly */ readfile('php://filter/zlib.inflate/resource=test.deflated'); /* Generates output: The original text is 70 characters long. The compressed file is 56 bytes long. The original text was: This is a test. This is only a test. This is not an important string. */ ?>
mcrypt.和 mdecrypt.使用 libmcrypt 提供了對稱的加密和解密。
格式爲 mcrypt.ciphername,其中 ciphername是密碼的名字,將被傳遞給 mcrypt_module_open()。有如下五個過濾器參數可用:
參數 是否必須 默認值 取值舉例
mode 可選 cbc cbc, cfb, ecb, nofb, ofb, stream
algorithms_dir 可選 ini_get(‘mcrypt.algorithms_dir’) a lgorithms 模塊的目錄
modes_dir 可選 ini_get(‘mcrypt.modes_dir’) modes 模塊的目錄
iv 必須 N/A 典型爲 8,16 或 32 字節的二進制數據。根據密碼而定
key 必須 N/A 典型爲 8,16 或 32 字節的二進制數據。根據密碼而定
細節研究文檔吧
http://php.net/manual/zh/filters.encryption.php
from: LoRexxar https://lorexxar.cn/2016/09/14/php-wei/