PHP伪协议学习笔记

PHP伪协议学习笔记!

GET和POST

GET和POST都使用HTTP协议,GET可以在请求体中携带数据,POST也可以在URL中携带数据。

GET请求,浏览器会把http header和data一同发送出去;POST请求,浏览器会先发送header,服务器回复100 continue状态码,浏览器再发送数据data,服务器回复200 ok

PHP伪协议

file:// 访问本地文件系统
http:// 访问HTTP(S)网址
ftp:// 访问FTP(S)网址
php:// 访问各个输入/输出流(I/O streams)
zlib:// 压缩流
data:// 数据
glob:// 查找匹配的文件路径模式
phar:// PHP归档
ssh2:// Secure Shell 2
rar:// RAR
ogg:// 音频流
expect:// 处理交互式的流

php://伪协议

php.ini中,allow_url_fopen和allow_url_include会影响fopen和include等函数对伪协议的支持,而allow_url_include依赖allow_url_fopen,所以allow_url_fopen不开启,allow_url_include是没有作用的。

php://input

php://input代表可以访问请求的原始数据,简单来说,php://input可以获得post(以及Get中请求体的数据)的数据并将其作为PHP代码执行。但是表单编码为enctype=”multipart/form-data”时,该方法无效。

必须要求allow_url_include打开




CTF中的应用


题目:welcome to bugkuctf
源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--  
$user = $_GET["txt"];
$file = $_GET["file"];
$pass = $_GET["password"];

if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){
echo "hello admin!<br>";
include($file); //hint.php
}
else{
echo "you are not admin ! ";
}
-->

可以看到:
1.get方式传参
2.get参数中需要包含txt,file,password三个参数
3.$user存在且该文件内容为welcome to the bugkuctf
4.$file需要为hint.php,即file为hint.php

思路:利用php://filter和php://input
解题:http://www.xx.com/index.php?txt=php://input&file=php://filter/read=convert.base64-encode/resource=hint.php&password=1 同时Post数据中写入welcome to the bugkuctf
解读:
txt=php://input,表示将post传递过来的数据赋值给txt。file=php://filter/read=convert.base64-encode/resource=hint.php,表示将hint.php传递给include进行执行的过程中,同时通过base64加密输出hint.php的源代码。

php://output

php://output是一个只写的数据流,允许你以print和echo一样的方式写入到输出缓冲区。

php://filter

php://filter是一种元封装器,设计用于在数据流打开时的筛选过滤应用。在读取源代码后会进行base64编码输出(不编码会使得其当作PHP代码执行,无法查看源代码内容)

与allow_url_fopen和allow_url_include开启状态无关。
resource=要过滤的数据流 read=读数据的筛选列表

例如:php://filter/read=convert.base64-encode/resource=upload.php
resource=upload.php,代表读取upload.php的内容 read=convert.base64-encode,代表将数据流进行base64加密。

CTF中的应用


题目:flag在index里


根据题目已知flag在index.php文件里,需要使用伪协议
解题:http://www.xx.com/post/index.php?file=php://filter/read=convert.base64-encode/resource=index.php
将输出内容进行BASE64解码,即可得到flag。