2FA二维码token改v1_前缀+查询参数名t换token:规避WAF对32位以上纯hex查询串的404拦截

This commit is contained in:
ypsdf1
2026-10-01 10:37:59 +08:00
parent b86d29b34a
commit 1eb898adea
2 changed files with 10 additions and 6 deletions
+5 -3
View File
@@ -49,9 +49,10 @@ function twofaRequireSecret() {
}
}
/** token 形态:64 位十六进制(Java SecureRandom 256bit) */
/** token 形态:v1_ 前缀 + 64 位十六进制(Java SecureRandom 256bit)。
* 前缀不是装饰:线上 WAF 会拦「查询参数=32位以上纯hex」形态,实测带前缀可过。 */
function twofaValidToken($t) {
return is_string($t) && preg_match('/^[a-f0-9]{32,128}$/', $t) === 1;
return is_string($t) && preg_match('/^v1_[a-f0-9]{32,128}$/', $t) === 1;
}
$action = getParam('action', '');
@@ -90,7 +91,8 @@ try {
// ===== 玩家浏览器:取二维码载荷 =====
case 'get': {
$token = (string) getParam('t', '');
// 参数名用 token 而非 t:WAF 规则匹配「t=<长hex>」,换名+前缀双保险
$token = (string) getParam('token', '');
if (!twofaValidToken($token)) error('参数错误', 400);
$db = getDB();
+5 -3
View File
@@ -5,8 +5,10 @@
* 页面凭据 = URL 里的随机 token(Java 端 SecureRandom 生成,10 分钟有效),
* 取回 otpauth 载荷后由内联二维码库就地渲染;不做任何 2FA 校验逻辑。
*/
$t = isset($_GET['t']) ? strtolower(preg_replace('/[^a-f0-9]/', '', (string) $_GET['t'])) : '';
$bad = (strlen($t) < 32 || strlen($t) > 128);
$raw = isset($_GET['t']) ? (string) $_GET['t'] : '';
$t = $raw;
// v1_ 前缀 + 64hex:线上 WAF 拦「t=纯32+位hex」,前缀/换名双保险(与 api/twofa.php 对齐)
$bad = preg_match('/^v1_[a-f0-9]{32,128}$/', $raw) !== 1;
?>
<!DOCTYPE html>
<html lang="zh-CN">
@@ -2368,7 +2370,7 @@ var qrcode = function() {
box.innerHTML = '<div class="fail">链接参数无效,请回游戏重新执行 /2fa add</div>';
return;
}
fetch('api/twofa.php?action=get&t=' + encodeURIComponent(t), { credentials: 'same-origin' })
fetch('api/twofa.php?action=get&token=' + encodeURIComponent(t), { credentials: 'same-origin' })
.then(function (r) { return r.json(); })
.then(function (j) {
if (!j.success) {