PHP伪协议

[Web安全]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:// — 处理交互式的流

file协议

file:// — 访问本地文件系统

说明


文件系统 是 PHP 使用的默认封装协议,展现了本地文件系统。 当指定了一个相对路径(不以/、\、\\或 Windows 盘符开头的路径)提供的路径将基于当前的工作目录。 在很多情况下是脚本所在的目录,除非被修改了。 使用 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

 

http://协议

http:// -- https:// — 访问 HTTP(s) 网址

说明

允许通过 HTTP 1.0 的 GET方法,以只读访问文件或资源。 HTTP 请求会附带一个 Host: 头,用于兼容基于域名的虚拟主机。 如果在你的 php.ini 文件中或字节流上下文(context)配置了 user_agent 字符串,它也会被包含在请求之中。

数据流允许读取资源的 body,而 headers 则储存在了 $http_response_header 变量里。

如果需要知道文档资源来自哪个 URL(经过所有重定向的处理后), 需要处理数据流返回的系列响应报头(response headers)。

The from directive will be used for the From: header if set and not overwritten by the 上下文(Context)选项和参数.

用法
  • http://example.com
  • http://example.com/file.php?var1=val1&var2=val2
  • http://user:password@example.com
  • https://example.com
  • https://example.com/file.php?var1=val1&var2=val2
  • https://user:password@example.com

ftp://协议

ftp:// -- ftps:// — 访问 FTP(s) URLs

说明

允许通过 FTP 读取存在的文件,以及创建新文件。 如果服务器不支持被动(passive)模式的 FTP,连接会失败。

打开文件后你既可以读也可以写,但是不能同时进行。 当远程文件已经存在于 ftp 服务器上,如果尝试打开并写入文件的时候, 未指定上下文(context)选项 overwrite,连接会失败。 如果要通过 FTP 覆盖存在的文件, 指定上下文(context)的 overwrite 选项来打开、写入。 另外可使用 FTP 扩展来代替。

如果你设置了 php.ini 中的 from 指令, 这个值会作为匿名(anonymous)ftp 的密码。

用法
  • ftp://example.com/pub/file.txt
  • ftp://user:password@example.com/pub/file.txt
  • ftps://example.com/pub/file.txt
  • ftps://user:password@example.com/pub/file.txt

php://协议(这是重点)

php:// — 访问各个输入/输出流(I/O streams)

说明

PHP 提供了一些杂项输入/输出(IO)流,允许访问 PHP 的输入输出流、标准输入输出和错误描述符, 内存中、磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器。

php://stdin, php://stdout 和 php://stderr

php://stdin、php://stdout 和 php://stderr 允许直接访问 PHP 进程相应的输入或者输出流。 数据流引用了复制的文件描述符,所以如果你打开 php://stdin 并在之后关了它, 仅是关闭了复制品,真正被引用的 STDIN 并不受影响。 注意 PHP 在这方面的行为有很多 BUG 直到 PHP 5.2.1。 推荐你简单使用常量 STDINSTDOUTSTDERR 来代替手工打开这些封装器。

php://stdin 是只读的, php://stdout 和 php://stderr 是只写的。

php://input

php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令。 而且,这样的情况下 $HTTP_RAW_POST_DATA 默认没有填充, 比激活 always_populate_raw_post_data 潜在需要更少的内存。 enctype="multipart/form-data" 的时候 php://input 是无效的。

Note: 在 PHP 5.6 之前 php://input 打开的数据流只能读取一次; 数据流不支持 seek 操作。 不过,依赖于 SAPI 的实现,请求体数据被保存的时候, 它可以打开另一个 php://input 数据流并重新读取。 通常情况下,这种情况只是针对 POST 请求,而不是其他请求方式,比如 PUT 或者 PROPFIND。

php://output

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

php://fd

php://fd 允许直接访问指定的文件描述符。 例如 php://fd/3 引用了文件描述符 3。

php://memory 和 php://temp

php://memory 和 php://temp 是一个类似文件 包装器的数据流,允许读写临时数据。 两者的唯一区别是 php://memory 总是把数据储存在内存中, 而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中。 临时文件位置的决定和 sys_get_temp_dir()的方式一致。

php://temp 的内存限制可通过添加 /maxmemory:NN 来控制,NN 是以字节为单位、保留在内存的最大数据量,超过则使用临时文件。

php://filter

php://filter 是一种元封装器, 设计用于数据流打开时的筛选过滤应用。 这对于一体式(all-in-one)的文件函数非常有用,类似 readfile()file()file_get_contents(), 在数据流内容读取之前没有机会应用其他过滤器。

php://filter 目标使用以下的参数作为它路径的一部分。 复合过滤链能够在一个路径上指定。详细使用这些参数可以参考具体范例。

 

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

payload:http://43.143.7.127:28706/?file=php://filter/read=convert.base64-encode/resource=flag

zlib://协议

zlib:// -- bzip2:// -- zip:// — 压缩流

说明

compress.zlib:// and compress.bzip2://

zlib: 的功能类似 gzopen(),但是 其数据流还能被 fread() 和其他文件系统函数使用。 自 PHP 4.3.0 后这个不建议被使用,因为会和其他带“:”字符的文件名混淆; 请使用 compress.zlib:// 作为替代。

compress.zlib://、 compress.bzip2:// 和 gzopen()bzopen()是相等的。并且可以在不支持 fopencookie 的系统中使用。

ZIP 扩展 注册了 zip: 封装器。 自 PHP 7.2.0 和 libzip 1.2.0+ 起,加密归档开始支持密码,允许数据流中使用密码。 字节流上下文(stream contexts)中使用 'password' 选项设置密码。

data://协议

data:// — 数据(RFC 2397)

说明

自 PHP 5.2.0 起 data:(» RFC 2397)数据流封装器开始有效。

CTF 用法
  • data://text/plain;base64,<text-base64>
  • data://text/plain,<text>

glob://协议

glob:// — 查找匹配的文件路径模式

说明

glob: 数据流包装器自 PHP 5.3.0 起开始有效。

用法
  • glob://

phar://协议

phar:// — PHP 归档

说明

phar:// 数据流包装器自 PHP 5.3.0 起开始有效。详细的描述可参见 Phar 数据流包装器

用法
  • phar://

ssh2://协议

ssh2:// — Secure Shell 2

说明

ssh2.shell:// ssh2.exec:// ssh2.tunnel:// ssh2.sftp:// ssh2.scp:// PHP 4.3.0 and up (PECL)

Note: 该封装器默认没有激活 
为了使用 ssh2.*:// 封装协议, 你必须安装来自 » PECL» SSH2扩展。

除了支持传统的 URI 登录信息,ssh2 封装协议也支持通过 URL 的主机(host)部分来复用打开连接。

用法
  • ssh2.shell://user:pass@example.com:22/xterm
  • ssh2.exec://user:pass@example.com:22/usr/local/bin/somecmd
  • ssh2.tunnel://user:pass@example.com:22/192.168.0.1:14
  • ssh2.sftp://user:pass@example.com:22/path/to/filename

rar://协议

rar:// — RAR

说明

The wrapper takes the url encoded path to the RAR archive (relative or absolute), an optional asterik (*), an optional number sign (#) and an optional url encoded entry name, as stored in the archive. Specifying an entry name requires the number sign; a leading forward slash in the entry name is optional.

This wrapper can open both files and directories. When opening directories, the asterisk sign forces the directory entries names to be returned unencoded. If it's not specified, they will be returned url encoded – the reason for this is to allow the wrapper to be correctly used with built-in functionality like the RecursiveDirectoryIterator in the presence of file names that seem like url encoded data.

If the pound sign and the entry name part are not included, the root of the archive will be displayed. This differs from regular directories in that the resulting stream will not contain information such as the modification time, as the root directory is not stored in an individual entry in the archive. The usage of the wrapper with RecursiveDirectoryIterator requires the number sign to be included in the URL when accessing the root, so that the URLs of the children may be constructed correctly.

Note: This wrapper is not enabled by default 
In order to use the rar:// wrapper, you must install the » rar extension available from » PECL.

rar:// Available since PECL rar 3.0.0

用法
  • rar://<url encoded archive name>[*][#[<url encoded entry name>]]

ogg://协议

ogg:// — 音频流

说明

通过包装器 ogg:// 读取的文件, 是作为 OGG/Vorbis 格式的压缩音频编码。 同样,通过包装器 ogg:// 写入或追加的数据格式也是压缩音频。 当 stream_get_meta_data() 用于一个打开读取的 OGG/Vorbis 文件时,会返回关于数据流的详细信息,包含了 vendor 标签、任何内含的 commentschannels 数字、采样率(rate),以及 用 bitrate_lowerbitrate_upperbitrate_nominalbitrate_window 描述的可变比特率范围。

ogg:// PHP 4.3.0 及以上(PECL)

Note: 该封装器默认未激活 
要使用 ogg:// 封装器,您必须安装 » OGG/Vorbis 扩展。 可以在 » PECL 上找到。

用法
  • ogg://soundfile.ogg
  • ogg:///path/to/soundfile.ogg
  • ogg://http://www.example.com/path/to/soundstream.ogg

expect://协议

expect:// — 处理交互式的流

说明

由 expect:// 封装协议打开的数据流 PTY 通过提供了对进程 stdio、stdout 和 stderr 的访问。

Note: 该封装协议默认未开启 
为了使用 expect:// 封装器,你必须安装 » PECL 上的 » Expect扩展。

expect:// PHP 4.3.0 and up (PECL)

用法
  • expect://command