php伪协议总结 作者: ynnddddd 时间: 2024-09-28 分类: 网络安全,RCE php支持的伪协议: 1 file:// — 访问本地文件系统 2 http:// — 访问 HTTP(s) 网址 3 ftp:// — 访问 FTP(s) URLs 4 php:// — 访问各个输入/输出流(I/O streams) 5 zlib:// — 压缩流 6 data:// — 数据(RFC 2397) 7 glob:// — 查找匹配的文件路径模式 8 phar:// — PHP 归档 9 ssh2:// — Secure Shell 2 10 rar:// — RAR 11 ogg:// — 音频流 12 expect:// — 处理交互式的流 https://blog.csdn.net/IAMAASTUDENT/article/details/136753142 https://blog.csdn.net/cosmoslin/article/details/120695429 1 php://filter php://filter 是一种元封装器, 设计用于数据流打开时的筛选过滤应用。 这对于一体式(all-in-one)的文件函数非常有用,类似 readfile()、 file() 和 file_get_contents(), 在数据流内容读取之前没有机会应用其他过滤器。 简单通俗的说,这是一个中间件,在读入或写入数据的时候对数据进行处理后输出的一个过程。 php://filter可以获取指定文件源码。当它与包含函数结合时,php://filter流会被当作php文件执行。所以我们一般对其进行编码,让其不执行。从而导致 任意文件读取。 示例:**php://filter/convert.base64-encode/resource=index.php** 2 data:// 数据流封装器,以传递相应格式的数据。可以让用户来控制输入流,当它与包含函数结合时,用户输入的data://流会被当作php文件执行。 例如: data://text/plain, 注意: 当enctype="multipart/form-data"的时候 php://input` 是无效的 遇到file_get_contents()要想到用php://input绕过 6 zip:// zip:// 可以访问压缩包里面的文件。当它与包含函数结合时,zip://流会被当作php文件执行。从而实现任意代码执行。 ---------- PHP中,有许多函数可以接受伪协议(如 `php://`、`http://`、`ftp://` 等)作为参数,用于读取或写入数据。伪协议本质上是文件包装器,因此任何与文件系统交互的函数基本上都可以接受伪协议作为参数。 以下是一些常用的函数,它们可以接受伪协议作为参数: ### 1. **`file_get_contents()`** - 读取文件或数据流的内容,可以是本地文件或远程资源,也可以是伪协议。 ```php $data = file_get_contents('php://input'); ``` ### 2. **`file_put_contents()`** - 写入数据到文件或流,可以使用伪协议来指定目标(如 `php://output`)。 ```php file_put_contents('php://output', 'Hello World'); ``` ### 3. **`fopen()`** - 打开一个文件或数据流,并返回文件指针。伪协议可用于打开内存流、网络资源或其他特殊流。 ```php $handle = fopen('php://memory', 'r+'); ``` ### 4. **`fgets()`** - 从打开的文件指针中读取一行,通常与 `fopen()` 一起使用。 ```php $handle = fopen('php://input', 'r'); $line = fgets($handle); ``` ### 5. **`fwrite()`** - 向文件指针写入数据。可以向伪协议流(如 `php://output`)写入内容。 ```php $handle = fopen('php://output', 'w'); fwrite($handle, 'Hello World'); ``` ### 6. **`include()` / `require()`** - 这些函数也可以使用伪协议读取脚本内容,尽管通常用于本地文件。 ```php include 'phar://archive.phar/script.php'; ``` ### 7. **`readfile()`** - 直接读取并输出文件内容,也可以是伪协议内容。 ```php readfile('php://input'); ``` ### 8. **`file()`** - 将整个文件读取到一个数组中,每一行作为数组的一个元素。伪协议也可用于读取数据流。 ```php $lines = file('php://input'); ``` ### 9. **`copy()`** - 复制文件,可以从伪协议读取并写入到另一个文件。 ```php copy('http://example.com/file.txt', 'local_copy.txt'); ``` ### 10. **`parse_ini_file()`** - 读取INI文件并解析,可以从伪协议中读取内容。 ```php $config = parse_ini_file('php://memory'); ``` ### 11. **`parse_ini_string()`** - 从字符串(可以是伪协议返回的数据)中解析INI格式的数据。 ```php $ini_string = file_get_contents('data://text/plain;base64,aW5pX2tleT12YWx1ZQ=='); $config = parse_ini_string($ini_string); ``` ### 12. **`unlink()`** - 删除文件。虽然通常用于本地文件,但可以使用某些伪协议删除文件,例如 `ftp://`。 ```php unlink('ftp://username:password@ftp.example.com/file.txt'); ``` ### 13. **`rmdir()`** - 删除目录,类似于 `unlink()`,也可以用于FTP等伪协议。 ```php rmdir('ftp://username:password@ftp.example.com/directory'); ``` ### 14. **`rename()`** - 重命名或移动文件,支持本地文件和通过伪协议访问的文件。 ```php rename('ftp://example.com/oldfile.txt', 'ftp://example.com/newfile.txt'); ``` ### 15. **`mkdir()`** - 创建目录,也可用于通过伪协议(如 `ftp://`)在远程服务器上创建目录。 ```php mkdir('ftp://username:password@ftp.example.com/new_directory'); ``` ### 16. **`is_readable()` / `is_writable()`** - 检查文件是否可读或可写,伪协议也可用于此检查。 ```php if (is_readable('php://input')) { // Do something } ``` ### 17. **`is_file()` / `is_dir()`** - 判断给定的路径是否是文件或目录,同样支持伪协议。 ```php if (is_file('ftp://example.com/file.txt')) { // Do something } ``` ### 18. **`stream_context_create()`** - 用于创建和修改流的上下文设置,常与伪协议一起使用,特别是HTTP、FTP等。 ```php $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => 'User-Agent: PHP' ] ]); $data = file_get_contents('http://example.com', false, $context); ``` ### 19. **`stream_get_contents()`** - 从文件指针中读取内容,可以用于伪协议流。 ```php $handle = fopen('php://input', 'r'); $content = stream_get_contents($handle); ``` ### 20. **`stream_copy_to_stream()`** - 将数据从一个流复制到另一个流。可以用于伪协议流之间的数据传输。 ```php $input = fopen('php://input', 'r'); $output = fopen('php://output', 'w'); stream_copy_to_stream($input, $output); ``` ### 21. **`stream_filter_append()` / `stream_filter_prepend()`** - 为流添加过滤器,适用于伪协议流。 ```php $handle = fopen('php://memory', 'r+'); stream_filter_append($handle, 'string.toupper'); fwrite($handle, 'hello world'); rewind($handle); echo fread($handle, 1024); // 输出: "HELLO WORLD" ``` ### 总结 PHP中任何能够处理文件或数据流的函数,几乎都可以接受伪协议作为参数。常见的伪协议包括 `php://`、`file://`、`http://`、`ftp://`、`data://` 等。这使得PHP具备了强大的灵活性,可以处理各种数据源,同时也带来了一定的安全风险。在使用伪协议处理外部输入时,务必注意安全性,尤其是防范任意文件读取或代码注入等安全问题。 标签: none