PHP伪协议3

2024-08-09

进一步理解file_get_contents()函数,分析其与php://inputdata://text/plain之间的关系

前言

之前对file_get_contents()函数一直处于似懂非懂的状态,现在经过几次文件包含漏洞的刷题,对file_get_contents()函数和PHP伪协议有了一个更加深刻的理解

1.file_get_contents()函数

file_get_contents() 函数用于将文件的内容读入到一个字符串中

和file() 一样,不同的是 file_get_contents() 把文件读入到一个字符串

常常会用一个变量去接收file_get_contents()的内容,然后echo输出出来

<?php
    $a = file_get_contents(flag.php);
    echo $a;
?>

2.php://input结合file_get_contents()函数

php://input可以访问请求的原始数据的只读流,将post请求的数据当作php代码执行。当传入的参数作为文件名打开时,可以将参数设为php://input,同时post想设置的文件内容,php执行时会将post内容当作文件内容。从而导致任意代码执行。

当前file_get_contents()函数遇到php://input的时候,函数会把php://input看作一个文件,如file_get_contents(php://input) ,这时候,file_get_contents得到的内容,就是你post进入的参数

php://input只有一种使用方法:

  • file_get_contents(php://input) 然后利用工具传入一个post参数(比如hackbar工具)

所以我们遇到以下if判断语句时:

if(file_get_contents($file2) === "hello ctf")

我们就可以传入payload:file2=php://input ,再利用hackbar工具或是burpsuite传入post数据,post值为 hello ctf 即可,这样就能使得if语句条件成立

3.data://text/plain结合file_get_contents()函数

php://data能直接写入字符串到file_get_contents()函数中

有两种方法:

  • file_get_contents(data://text/plain,要写入的字符串)
  • file_get_contents(data://text/plain;base64,base64加密后的字符串)

注意在 data://text/plain 后面,第一种方式是以逗号 , 分隔,第二种方式是以分号 ; 分隔

效果和php://input差不多,好处就是不需要利用post传入另一个参数了

所以我们遇到以下if判断语句时:

if(file_get_contents($file2) === "hello ctf")

我们可以传入paylaod:file2=data://text/plain,hello ctf

这样就可以使得if语句条件成立

结语

这一次算是理清了file_get_contents()这个函数与php://inputdata://text/plain间的化学反应