1. 程式人生 > 實用技巧 >php偽協議

php偽協議

php偽協議

php學習

1. php中包含的偽協議

  1. file:// — 訪問本地檔案系統
  2. http:// — 訪問 HTTP(s) 網址
  3. ftp:// — 訪問 FTP(s) URLs
  4. php:// — 訪問各個輸入/輸出流(I/O streams)
  5. zlib:// — 壓縮流
  6. data:// — 資料(RFC 2397)
  7. glob:// — 查詢匹配的檔案路徑模式
  8. phar:// — PHP 歸檔
  9. ssh2:// — Secure Shell 2
  10. rar:// — RAR
  11. ogg:// — 音訊流
  12. expect:// — 處理互動式的流

2. file://協議

file://用於訪問本地的檔案系統,通常用來讀取本地檔案而不受allow_url_fopen

allow_url_include的控制。

3. php://協議

php:filter在雙off的情況下也可以使用:
不需要開啟allow_url_fopen,僅php://input、 php://stdin、 php://memory 和 php://temp 需要開啟allow_url_include
php://訪問各個輸入和輸出流,經常使用的是php://filter和php://input ,php://filter用來讀取原始碼,php://input 用來執行程式碼。

4. php://filter

php://filter 是一種元封裝器, 設計用於資料流開啟時的篩選過濾應用。 這對於一體式(all-in-one)的檔案函式非常有用,類似 readfile()、 file() 和 file_get_contents(), 在資料流內容讀取之前沒有機會應用其他過濾器。

1. resource=<要過濾的資料流>     這個引數是必須的。它指定了你要篩選過濾的資料流。
 2. read=<讀鏈的篩選列表>         該引數可選。可以設定一個或多個過濾器名稱,以管道符(|)分隔。
 3. write=<寫鏈的篩選列表>    該引數可選。可以設定一個或多個過濾器名稱,以管道符(|)分隔。
 4. <;兩個鏈的篩選列表>        任何沒有以 read=write= 作字首 的篩選器列表會視情況應用於讀或寫鏈。
可以運用多種過濾器(字串,轉換,壓縮,加密)
php://filter/read=convert
.base64-encode/resource=upload.php 這裡讀的過濾器為convert.base64-encode,就和字面上的意思一樣,把輸入流base64-encode。 resource=upload.php,代表讀取upload.php的內容

5. 過濾器

過濾器有很多種,字串過濾器,轉換過濾器,壓縮過濾器,加密過濾器
字串過濾器:

string.rot13
進行rot13轉換
string.toupper
將字元全部大寫
string.tolower
將字元全部小寫
string.strip_tags
去除空字元、HTML 和 PHP 標記後的結果。
功能類似於strip_tags()函式,若不想某些字元不被消除,後面跟上字元,可利用字串或是陣列兩種方式。
<?php
    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'string.rot13');
    echo "rot13:";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'string.toupper');
    echo "Upper:";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'string.tolower');
    echo "Lower:";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    echo "Del1:";
    stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE);
    fwrite($fp, "<b>This is a test.</b>!!!!<h1>~~~~</h1>\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    echo "Del2:";
    stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, "<b>");
    fwrite($fp, "<b>This is a test.</b>!!!!<h1>~~~~</h1>\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, array('b','h1'));
    echo "Del3:";
    fwrite($fp, "<b>This is a test.</b>!!!!<h1>~~~~</h1>\n");
    fclose($fp);
?>

<轉換過濾器>

<?php
    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'convert.base64-encode');
    echo "base64-encode:";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $param = array('line-length' => 8, 'line-break-chars' => "\n");
    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'convert.base64-encode', STREAM_FILTER_WRITE, $param);
    echo "\nbase64-encode-split:\n";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'convert.base64-decode');
    echo "\nbase64-decode:";
    fwrite($fp, "VGhpcyBpcyBhIHRlc3QuCg==\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'convert.quoted-printable-encode');
    echo "quoted-printable-encode:";
    fwrite($fp, "This is a test.\n");
    fclose($fp);
    echo "<br>";

    $fp = fopen('php://output', 'w');
    stream_filter_append($fp, 'convert.quoted-printable-decode');
    echo "\nquoted-printable-decode:";
    fwrite($fp, "This is a test.=0A");
    fclose($fp);
    echo "<br>";

?>

PHP file_put_contents() 函式:
payload=http://127.0.0.1/xxx.php?a=php://filter/write=string.tolower/resource=test.php
可以往伺服器中寫入一個檔案內容全為小寫且檔名為test.php的檔案

PHP file_get_contents() 函式
file_get_contents()的$filename引數不僅僅為檔案路徑,還可以是一個URL(偽協議)。
payload=http://127.0.0.1/xxx.php?a=php://filter/convert.base64-encode/resource=test.php
test.php的內容以base64編碼的方式顯示出來