给出GitHub - BSidesSF/ctf-2020-release: BSidesSF CTF 2020 release 解决方案给出:
## Solution
Read flag.php using php filters and LFI. Can also leak the source code of the current page if they are stuck.
```?category=php://filter/convert.base64-encode/resource=index```
Trying to read the flag directly will fail, because the code does a basic check for presence of "woofers" / "index" / "meowers".
``` Won't work: ?category=php://filter/convert.base64-encode/resource=flag```
```Will work: ?category=php://filter/convert.base64-encode/write=woofers/resource=flag```
我们还是分析一下:
页面显示两个按钮,一个WOOFERS,一个MEOWERS,任选一个出来相应图片,对应的url为/index.php?category=woofers(或index.php?category=meowers),我们试试能不能通过get方式传参,发现可以,猜测可能是SQL注入,进行单引号测试:
/index.php?category=woofers'
出现include(woofers'.php),说明使用了include函数,并且自动添加php后缀,使用include函数,所以我们使用php伪协议来查看源码:(将flag文件参数传入include的参数中,这样利用文件漏洞,他在执行include包含文件语句时,就会引用flag文件,我们就可以查看flag文件了)(伪协议常常用于文件包含漏洞中,在php中能够造成文件包含的函数有include,require,include_once,require_once,highlight_file,show_source,file_get_contents,fopen,file,readfile)
#在index.php中发现以下代码:
<div class="page-include">
<?php
$file = $_GET['category'];
if(isset($file)){
if( strpos( $file, "woofers" ) !== false || strpos( $file, "meowers" ) !== false || strpos( $file, "index")){
include ($file . '.php');
}else{
echo "Sorry, we currently only support woofers and meowers.";
}
}
?>
</div>
可以看到通过GET方式传入category参数,进行判断和字符过滤,传入的字符串必须有woofers,meowers,index中的其中一个,于是通过传入的woofers查看flag,构造payload:
/index.php?category=php://filter/read=convert.base64-encode/woofers/resource=flag
出来的字符串发现很像base64编码后的字符串,于是解码得flag。
插入知识点:include函数为什么要用php://filter伪协议: PHP中include函数使用php://filter伪协议是为了实现文件包含漏洞的利用,特别是在安全领域,这种技术被用于测试或攻击存在文件包含漏洞的系统。当include函数加载的参数没有经过适当的过滤或定义,攻击者可以通过控制这些参数来包含其他恶意文件,从而执行非预期的代码。这种技术被称为文件包含漏洞。 具体来说,php://filter伪协议允许通过过滤器链对数据进行处理,当它与包含函数结合使用时,php://filter流会被当作PHP文件执行。为了防止文件内容被直接执行,通常需要对文件内容进行编码,如使用read=convert.base64-encode来对文件内容进行base64编码。这样,攻击者可以构造特定的payload,如:file=php://filter/read=convert.base64-encode/resource=flag.php来获取目标文件的编码内容,进而进行进一步的分析或攻击。