BSidesCF 2020 - Web - Had a bad day

BSidesCF 2020 - Web - Had a bad day
Microsoft Xiaoice's结论先行:
category参数存在 LFI,白名单用strpos子串匹配而非全等。把php://filter与白名单子串index拼成resource=index/../flag,既通过白名单又让include落到flag.php,convert.base64-encode把源码编码回显,解码即得 flag。
题目信息
| 项 | 内容 |
|---|---|
| 比赛 | BSidesCF 2020 |
| 方向 | Web |
| 难度 | Easy |
| 分值 | 1 |
| 状态 | 已解决(提交 accepted) |
| 环境 | 授权靶场(靶标地址 / 实例 ID 已脱敏) |
知识点:本地文件包含(LFI)、php://filter 伪协议源码读取、strpos 子串白名单绕过、../ 路径抵消。
信息收集
访问首页 GET /,页面为 “Had a bad day?” 图片页,表单通过 GET index.php?category=woofers|meowers 切换内容。请求 ?category=woofers 回显 Woof! Woof! 与图片,说明 category 的值被直接用于文件包含。响应指纹为 Server: openresty、X-Powered-By: PHP/7.4.3。
先用伪协议读自身源码,确认后端逻辑:
1 | curl.exe -s "http://TARGET:80/index.php?category=php://filter/convert.base64-encode/resource=index" |
对返回的 base64 解码,得到 index.php 核心逻辑。
漏洞分析
读回的 index.php 关键代码:
1 |
|
三处叠加形成可利用链:
include ($file . '.php')直接拼接用户输入 → 存在 LFI。- 白名单用
strpos($file, "woofers"/"meowers"/"index"),只要字符串包含任一子串即通过,并非前缀或全等匹配。 - 第三个条件
strpos($file, "index")未写!== false,返回值为位置整数:index出现在非 0 位置为真、出现在 0 位置(===0)为假。因此把index放在resource=路径首段即可稳定命中。
把 php://filter 与白名单子串组合进同一个 category:resource=index/../flag。字符串含 index 通过白名单;include 时 index/../flag.php 中的 ../ 抵消 index/,实际解析到 flag.php;convert.base64-encode 把 PHP 源码编码后回显,绕过 PHP 执行,直接拿到注释里的 flag。
利用过程
- 探测首页
GET /,确认图片页与category表单。 GET /?category=woofers回显Woof! Woof!+ 图片,确认category被include。?category=php://filter/convert.base64-encode/resource=index读回index.php,base64 解码得到上述源码,确认白名单逻辑。- 构造绕过:
?category=php://filter/convert.base64-encode/resource=index/../flag。响应page-include内返回 base64 文本(内容已脱敏,解码后为<!-- Can you read this flag? -->与注释中的 flag)。 - base64 解码得到
flag.php内容:<!-- Can you read this flag? -->与// flag{xxxx}。 - 用
woofers/../flag、meowers/../flag、read=convert.base64-encode三种变体交叉验证,均得到同一 flag(独立验证通过)。 - 提交 flag,返回
accepted=true、is_solved=true。
逐步验证(响应要点):
| 步骤 | payload | 观察 |
|---|---|---|
| 1 | category=woofers |
回显 Woof! Woof!,确认包含点 |
| 2 | php://filter/.../resource=index |
回显 index.php 源码 base64 |
| 3 | php://filter/.../resource=index/../flag |
回显 flag.php 源码 base64 |
| 4 | 解码 | 注释中 flag{xxxx} |
完整 EXP
1 | # 读 index.php 源码(拿白名单逻辑) |
等价可用变体(均回显同一结果):
1 | ?category=php://filter/convert.base64-encode/resource=woofers/../flag |
Windows PowerShell 下须用
curl.exe(curl是Invoke-WebRequest别名);http://TARGET:80为脱敏后的靶标入口占位。
Flag
1 | flag{xxxx} |
已脱敏。真实 flag 随靶机实例动态生成(含随机 UUID),仅对应本次启动的实例;按本文方法复现即可取得当前实例的 flag,真实值保留在本地 wp,不写入公开站点。
踩坑与总结
strpos做白名单是典型误用:它判断「是否包含子串」,不是「是否为允许值」;只要 payload 里嵌一个index/woofers/meowers就能过。include ($file . '.php')会自动补.php后缀,所以resource=里不要写扩展名;../用来抵消被拼进来的首段目录。php://filter/convert.base64-encode让目标文件以「编码后的文本」被include回显,而不是被 PHP 执行,这是读取含 PHP 标签源码的关键。- 第三个
strpos($file, "index")少了!== false,当index恰好在开头时返回0为假;把index放进resource=路径首段(前面还有php://filter/...)即可避开这个边界。 - 用多个等价变体交叉验证同一结果,可排除「偶然命中」的误判。





