第三题:protobuf混淆解析挑战

本文会使用codex ai的分析来对比分析此题

一:本地分析

1、抓包

image-20260427220822821

protobuf是什么:

1
2
3
4
5
6
7
8
9
10
11
Protobuf(Protocol Buffers)是 Google 开发的一种跨语言、跨平台的数据序列化格式。它的核心作用是高效、紧凑地结构化数据,用于网络传输或持久化存储。

主要作用:

代替XML/JSON:序列化后的二进制数据体积更小(通常减少3-10倍),解析速度更快。

定义数据契约:通过 .proto 文件定义消息结构,自动生成多种语言(Java/C++/Python/Go等)的读写代码,保证多服务间数据一致。

支持向后/向前兼容:老代码可读新字段,新代码可忽略旧字段,方便系统演进。

常用于RPC框架:如 gRPC 默认使用 Protobuf 进行高效的服务间通信。

查看堆栈

image-20260427224440967

image-20260428001009707

可以看到是有特殊字符以及混淆的js代码,此时可以直接格式化后放到ai里进行分析,不格式化也行

image-20260428001213084

可以看到他给我们还原的代码,还原后直接搜索堆栈里的 apiGetPageData函数,但是我试过这个还原还是不太理想,还是美化后再进行代码替换

美化网站:https://www.qianbo.com.cn/Tool/Beautify/Js-Formatter.html

image-20260428004907116

image-20260428004927087

根据打印的行为记录和代码,可以看出实际调用也就是apiGetPageData(page,challengeType)得出值

image-20260428005035588

尝试调用一致。

因为能直接在当前页面上执行函数获得到值,尝试了使用jsrpc直接调用,发现调用404。page值传进去没有生效,我后面查看apiGetPageData(page,challengeType)接口的返回值,current_page返回就是undefined的。那么实际上的逻辑就是请求proto文件,会触发xhr请求,携带者处理过的值,再次请求获得数据。

这是相对分析比较细致的一篇文章,可以参考https://blog.csdn.net/qq_63401240/article/details/154620743

二:AI的做法

既然有了更发达的AI,那么还是使用codex+mcp直接逆向当前题目,并生成能执行的js

这是codex给我输出的带代码版的报告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
目标页面:

https://spiderdemo.cn/authentication/protobuf_challenge/?challenge_type=protobuf_challenge

要求是脱离浏览器,在纯 node.js 中模拟页面里的 payload 参数,请求第一页并拿到第一页数字。

最终实现文件在:
protobuf_challenge_node.js

———

1. 先确定 payload 是在哪里生成的

通过浏览器运行态分析可以确认,分页请求不是普通 JSON,而是 protobuf 二进制。

前端核心流程可以概括成:

const ChallengeRequest = root.lookupType("authentication.ChallengeRequest");

const requestData = {
page: page,
challengetype: encryptType(type),
timestamp: timestamp,
signature: signature
};

const message = ChallengeRequest.create(requestData);
const finalBuffer = ChallengeRequest.encode(message).finish();

xhr.open("POST", "/authentication/api/protobuf_challenge/page/1/", true);
xhr.setRequestHeader("Content-Type", "application/x-protobuf");
xhr.send(finalBuffer);

也就是说,题目里的 payload 本质上就是:

- ChallengeRequest
- protobuf 编码后的二进制数据

———

2. 读取 proto 协议,确认请求结构

页面会加载:

/static/protos/challenge.proto

协议内容里,关键请求结构是:

message ChallengeRequest {
int32 page = 1;
string challengetype = 2;
int64 timestamp = 3;
string signature = 4;
}

响应结构是:

message ChallengeResponse {
repeated NumberData numbers = 1;
int32 total_pages = 2;
int32 current_page = 3;
int64 timestamp = 4;
string signature = 5;
}

message NumberData {
int32 id = 1;
int32 value = 2;
}

因此我们在 Node 里只要能手写 protobuf 编码/解码,就不需要额外依赖 protobufjs。

———

3. 还原 challengetype 的处理逻辑

前端不会直接把 protobuf_challenge 放进请求,而是先做变换。

运行态验证后,逻辑是每个字符 charCode + 3:

function encryptChallengeType(input) {
return Array.from(String(input), (char) =>
String.fromCharCode(char.charCodeAt(0) + 3)
).join("");
}

例如:

encryptChallengeType("protobuf_challenge")
// "surwrexibfkdoohqjh"

所以 protobuf 请求里的 challengetype 必须填这个值。

———

4. 先解决登录态

不登录直接请求分页接口,服务端会返回 401。

登录页在:

https://spiderdemo.cn/admin_I/

分析后发现,后端注册接口可直接调用:

POST /admin_I/api/auth/register

请求体就是普通 JSON:

{
username,
email,
password,
confirmPassword
}

Node 端注册逻辑:

async function registerAccount() {
const seed = Date.now();
const username = `codex_${seed}`;
const password = "codex123";
const email = `${username}@example.com`;

const response = await fetch(`${BASE_URL}/admin_I/api/auth/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
username,
email,
password,
confirmPassword: password,
}),
});

const body = await response.json();
const cookie = extractCookies(response);
return { username, password, email, cookie, body };
}

从响应头提取 sessionid:

function extractCookies(response) {
const setCookies =
typeof response.headers.getSetCookie === "function"
? response.headers.getSetCookie()
: [];

return setCookies
.map((cookie) => cookie.split(";", 1)[0])
.filter(Boolean)
.join("; ");
}

这样就拿到了后续请求必须携带的:

sessionid=...

———

5. 发现必须先调用初始化接口

即使已经登录,直接打分页接口仍然会失败。

继续分析页面逻辑后确认,请求顺序必须是:

1. 先调初始化接口
2. 再调分页接口

初始化接口是:

GET /authentication/api/protobuf_challenge/init/?challenge_type=protobuf_challenge

Node 里对应实现:

async function initChallenge(cookie) {
const response = await fetch(
`${BASE_URL}/authentication/api/protobuf_challenge/init/?challenge_type=${encodeURIComponent(
CHALLENGE_TYPE
)}`,
{
headers: {
Cookie: cookie,
},
}
);

const body = await response.json();
return body;
}

这一步跑完,服务端才会建立当前挑战会话。

———

6. 还原 signature 算法

这一步最关键。

一开始如果误以为签名是普通 MD5:

const signature = md5(timestamp.toString());

服务端会返回:

403 Signature validation failed

继续分析 md_sign 后发现:

- 它不是普通 MD5
- 它的轮函数结构是 MD4
- 但它又不是标准 MD4
- 它有前端实现缺陷,必须按缺陷复现

最终确认它的行为是:

- 输入 "177727..." 时,不按 UTF-8 字节算
- 而是按数字数组 [1,7,7,7,...] 算
- 补位长度也沿用了前端那个有 bug 的写法

Node 里复现后的核心代码:

function leftRotate(value, shift) {
return (value << shift) | (value >>> (32 - shift));
}

function md4(input) {
const source = Array.from(String(input), (char) => {
const numeric = Number(char);
return Number.isFinite(numeric) ? numeric & 0xff : 0;
});
const bitLength = source.length * 8;

const withPadding = [];
for (const byte of source) {
withPadding.push(byte);
}
withPadding.push(0x80);

while ((withPadding.length % 64) !== 56) {
withPadding.push(0);
}

for (let i = 0; i < 8; i += 1) {
withPadding.push((bitLength >>> (i * 8)) & 0xff);
}

let A = 0x67452301;
let B = 0xefcdab89;
let C = 0x98badcfe;
let D = 0x10325476;

const F = (x, y, z) => (~x & z) | (y & x);
const G = (x, y, z) => (x & y) | (x & z) | (y & z);
const H = (x, y, z) => x ^ y ^ z;

const round1 = (a, b, c, d, k, s) => leftRotate(a + F(b, c, d) + k, s);
const round2 = (a, b, c, d, k, s) =>
leftRotate(a + G(b, c, d) + k + 0x5a827999, s);
const round3 = (a, b, c, d, k, s) =>
leftRotate(a + H(b, c, d) + k + 0x6ed9eba1, s);

// 中间 3 轮计算省略,脚本里是完整版本

const output = Buffer.allocUnsafe(16);
output.writeUInt32LE(A, 0);
output.writeUInt32LE(B, 4);
output.writeUInt32LE(C, 8);
output.writeUInt32LE(D, 12);
return output.toString("hex");
}

最终 signature 的生成:

const signature = md4(String(timestamp));

———

7. 手写 protobuf 编码,生成真正的 payload

因为请求体很简单,所以直接手写 protobuf 足够了。

先实现 varint:

function encodeVarint(value) {
let current = BigInt(value);
const bytes = [];

while (current >= 0x80n) {
bytes.push(Number((current & 0x7fn) | 0x80n));
current >>= 7n;
}
bytes.push(Number(current));
return Buffer.from(bytes);
}

编码字符串字段:

function encodeStringField(fieldNumber, value) {
const body = Buffer.from(String(value), "utf8");
return Buffer.concat([
encodeVarint((fieldNumber << 3) | 2),
encodeVarint(body.length),
body,
]);
}

编码整数:

function encodeIntField(fieldNumber, value) {
return Buffer.concat([
encodeVarint((fieldNumber << 3) | 0),
encodeVarint(value),
]);
}

拼出 ChallengeRequest:

function buildChallengeRequest(page, challengeType, timestamp) {
const encryptedType = encryptChallengeType(challengeType);
const signature = md4(String(timestamp));

return Buffer.concat([
encodeIntField(1, page),
encodeStringField(2, encryptedType),
encodeIntField(3, timestamp),
encodeStringField(4, signature),
]);
}

这一步输出的二进制 buffer,就是最终要发给接口的 payload。

———

8. 发送分页请求

分页请求实现如下:

async function fetchPage(cookie, page) {
const timestamp = Date.now();
const payload = buildChallengeRequest(page, CHALLENGE_TYPE, timestamp);

const response = await fetch(
`${BASE_URL}/authentication/api/protobuf_challenge/page/${page}/`,
{
method: "POST",
headers: {
Cookie: cookie,
"Content-Type": "application/x-protobuf",
},
body: payload,
}
);

const bodyBuffer = Buffer.from(await response.arrayBuffer());

return {
timestamp,
payloadHex: payload.toString("hex"),
responseHex: bodyBuffer.toString("hex"),
decoded: decodeChallengeResponse(bodyBuffer),
};
}

实际跑通后抓到的 payload_hex 类似这样:

08011212737572777265786962666b646f6f68716a6818d8a8e5eedc33222065653735333530336335383233633265633432363162303831323965
66633236

———

9. 手写 protobuf 解码响应

先解 varint:

function decodeVarint(buffer, offset) {
let result = 0n;
let shift = 0n;
let cursor = offset;

while (cursor < buffer.length) {
const byte = BigInt(buffer[cursor]);
result |= (byte & 0x7fn) << shift;
cursor += 1;
if ((byte & 0x80n) === 0n) {
return { value: result, offset: cursor };
}
shift += 7n;
}

throw new Error("unexpected end of buffer");
}

解 NumberData:

function decodeNumberData(buffer) {
const item = { id: 0, value: 0 };
let offset = 0;

while (offset < buffer.length) {
const tag = decodeVarint(buffer, offset);
offset = tag.offset;
const fieldNumber = Number(tag.value >> 3n);

const data = decodeVarint(buffer, offset);
offset = data.offset;

if (fieldNumber === 1) item.id = Number(data.value);
if (fieldNumber === 2) item.value = Number(data.value);
}

return item;
}

解整个 ChallengeResponse:

function decodeChallengeResponse(buffer) {
const response = {
numbers: [],
total_pages: 0,
current_page: 0,
timestamp: "0",
signature: "",
};

let offset = 0;
while (offset < buffer.length) {
const tag = decodeVarint(buffer, offset);
offset = tag.offset;
const fieldNumber = Number(tag.value >> 3n);
const wireType = Number(tag.value & 0x7n);

if (fieldNumber === 1 && wireType === 2) {
const length = decodeVarint(buffer, offset);
offset = length.offset;
const end = offset + Number(length.value);
response.numbers.push(decodeNumberData(buffer.subarray(offset, end)));
offset = end;
continue;
}

if (wireType === 0) {
const data = decodeVarint(buffer, offset);
offset = data.offset;

if (fieldNumber === 2) response.total_pages = Number(data.value);
if (fieldNumber === 3) response.current_page = Number(data.value);
if (fieldNumber === 4) response.timestamp = data.value.toString();
continue;
}

if (fieldNumber === 5 && wireType === 2) {
const length = decodeVarint(buffer, offset);
offset = length.offset;
const end = offset + Number(length.value);
response.signature = buffer.subarray(offset, end).toString("utf8");
offset = end;
continue;
}
}

return response;
}

———

10. 最终主流程

完整主流程就是:

async function main() {
const account = await registerAccount();
const init = await initChallenge(account.cookie);
const page1 = await fetchPage(account.cookie, 1);

console.log(
JSON.stringify(
{
account: {
username: account.username,
email: account.email,
cookie: account.cookie,
},
init_page_1_numbers: init.page_data,
payload_hex: page1.payloadHex,
page_response: page1.decoded,
page_1_values: page1.decoded.numbers.map((item) => item.value),
},
null,
2
)
);
}

———

11. 实际结果

脚本已经纯 Node 跑通。第 1 页数字是:

6485, 2863, 7292, 4190, 5768, 8403, 7125, 2747, 7212, 7790

对应输出里的数组是:

[
6485,
2863,
7292,
4190,
5768,
8403,
7125,
2747,
7212,
7790
]

———

最终执行js代码也是能直接拿到结果

image-20260429013954409

后续调用代码略

更多内容也在公众号更新:码字的秃猴

tuhou