返回提交历史
Modified
README.md
+1
-0
Modified
docs/audio/rank.md
+1
-1
Added
docs/login/cookie_refresh.md
+407
-0
XFEstudio/bilibili-API-collect
add【web端Cookie刷新】APIDoc
a46f140
代码差异
3 个文件
+409
-1
@@ -73,6 +73,7 @@ B站 API 采用 C/S 结构,大多数接口为 REST API 和 gRPC,少部分接
73
73
- [ ] [个人中心](docs/login/member_center.md)
74
74
- [ ] [注销登录](docs/login/exit.md)
75
75
- [x] [登录记录](docs/login/login_notice.md)
76
- [x] [Web 端 Coookie 刷新](docs/login/cookie_refresh.md)
76
77
- [ ] [消息中心](docs/message)
77
78
- [ ] [通知类消息](docs/message/msg.md)
78
79
- [ ] [私信](docs/message/private_msg.md)
@@ -1118,7 +1118,7 @@ curl -G 'https://api.bilibili.com/x/copyright-music-publicity/toplist/music_list
1118
1118
1119
1119
认证方式:Cookie(SESSDATA)
1120
1120
1121
**正文参数:**
1121
**正文参数(application/x-www-form-urlencoded):**
1122
1122
1123
1123
| 参数名 | 类型 | 内容 | 必要性 | 备注 |
1124
1124
| ------- | ---- | ------------------------ | --------------- | ------------------------------------------------ |
@@ -0,0 +1,407 @@
1
# Web端Cookie刷新
2
3
自从 2023 以来,社区反馈似乎 Web 端的 Cookie 会随着一些敏感接口的访问逐渐失效,而在 Web 页面上会判断 Cookie是否需要刷新,如需刷新则会以动态加载 iframe 方式实现,同时登录(二维码 / 密码 / 短信验证码等)接口会返回`refresh_token`字段,用以持久化保存,是一种官方的风控机制实现
4
5
感谢 [#524](https://github.com/SocialSisterYi/bilibili-API-collect/issues/524) 提供相关研究报告以及逆向工程结果
6
7
> 编辑到最前面:cookie 不会主动刷新的,只要他没有调用下面的刷新接口就不会刷新。也就是说,你只要不再打开浏览器,或者直接把 localStorage 的 ac_time_value 字段删除了。那么 cookie 在真的失效前(登录过期、账号风控等强制下线)都是不变化的。
8
9
## 刷新步骤(伪代码)
10
11
```python
12
cookie, refresh_token = 进行登录操作() # can be 二维码 / 密码 / 短信验证码
13
14
while True:
15
if 每日第一次访问接口:
16
if 检查是否需要刷新(cookie):
17
CorrespondPath = 生成CorrespondPath(当前毫秒时间戳)
18
refresh_csrf = 获取refresh_csrf(CorrespondPath, cookie)
19
refresh_token_old = refresh_token # 这一步必须保存旧的 refresh_token 备用
20
cookie, refresh_token = 刷新Cookie(refresh_token, refresh_csrf, cookie)
21
确认更新(refresh_token_old, cookie) # 这一步需要新的 Cookie 以及旧的 refresh_token
22
SSO站点跨域登录(cookie)
23
do_somethings(cookie) # 其他业务逻辑处理
24
```
25
26
## 检查是否需要刷新
27
28
> https://passport.bilibili.com/x/passport-login/web/cookie/info
29
30
*请求方式:GET*
31
32
鉴权方式:Cookie
33
34
**url 参数:**
35
36
| 参数名 | 类型 | 内容 | 必要性 | 备注 |
37
| ------ | ---- | ------------------------- | ------ | ---- |
38
| csrf | str | CSRF Token(位于 cookie) | 非必要 | |
39
40
**json 回复:**
41
42
根对象:
43
44
| 字段 | 类型 | 内容 | 备注 |
45
| ------- | ---- | -------- | ----------------------------- |
46
| code | num | 返回值 | 0:成功<br />-101:账号未登录 |
47
| message | str | 错误信息 | 默认为 0 |
48
| ttl | num | 1 | |
49
| data | obj | 信息本体 | |
50
51
`data`对象:
52
53
| 字段 | 类型 | 内容 | 备注 |
54
| --------- | ---- | ------------------- | ----------------------------------------------------- |
55
| refresh | bool | 是否应该刷新 cookie | `true`:需要刷新 Cookie<br />`false`:无需刷新 Cookie |
56
| timestamp | num | 当前毫秒时间戳 | 用于获取 refresh_csrf |
57
58
**示例:**
59
60
```bash
61
curl -G 'https://passport.bilibili.com/x/passport-login/web/cookie/info' \
62
--data-urlencode 'csrf=xxx' \
63
-b 'SESSDATA=xxx'
64
```
65
66
<details>
67
<summary>查看响应示例:</summary>
68
69
```json
70
{
71
"code": 0,
72
"message": "0",
73
"ttl": 1,
74
"data": {
75
"refresh": false,
76
"timestamp": 1684466082562
77
}
78
}
79
```
80
81
</details>
82
83
## 生成CorrespondPath算法
84
85
该算法逆向于以下 wasm 以及 JavaScript bind 接口,抓取于官方 web 首页中,感谢 [#524](https://github.com/SocialSisterYi/bilibili-API-collect/issues/524) 提供
86
87
https://s1.hdslb.com/bfs/static/jinkela/long/wasm/wasm_rsa_encrypt_bg.wasm
88
89
https://s1.hdslb.com/bfs/static/jinkela/long/wasm/wasm_ras_umd.js
90
91
### 算法细节
92
93
将`refresh_${timestamp}`作为消息体(`timestamp`为当前毫秒时间戳),用以下方 PubKey 进行 [RSA-OAEP](https://datatracker.ietf.org/doc/html/rfc3447#section-7.1) 算法加密,之后密文通过 Base16 小写编码为字符串
94
95
JWK 格式:
96
97
> {
98
> "kty": "RSA",
99
> "n": "y4HdjgJHBlbaBN04VERG4qNBIFHP6a3GozCl75AihQloSWCXC5HDNgyinEnhaQ_4-gaMud_GF50elYXLlCToR9se9Z8z433U3KjM-3Yx7ptKkmQNAMggQwAVKgq3zYAoidNEWuxpkY_mAitTSRLnsJW-NCTa0bqBFF6Wm1MxgfE",
100
> "e": "AQAB"
101
> }
102
103
PEM 格式:
104
105
> -----BEGIN PUBLIC KEY-----
106
> MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLgd2OAkcGVtoE3ThUREbio0Eg
107
> Uc/prcajMKXvkCKFCWhJYJcLkcM2DKKcSeFpD/j6Boy538YXnR6VhcuUJOhH2x71
108
> nzPjfdTcqMz7djHum0qSZA0AyCBDABUqCrfNgCiJ00Ra7GmRj+YCK1NJEuewlb40
109
> JNrRuoEUXpabUzGB8QIDAQAB
110
> -----END PUBLIC KEY-----
111
112
### 相关Demo
113
114
该 Demo 提供 JavaScript、Python 以及 vercel 云函数,感谢 [#524](https://github.com/SocialSisterYi/bilibili-API-collect/issues/524) 提供
115
116
#### JavaScript
117
118
```javascript
119
const publicKey = await crypto.subtle.importKey(
120
"jwk",
121
{
122
kty: "RSA",
123
n: "y4HdjgJHBlbaBN04VERG4qNBIFHP6a3GozCl75AihQloSWCXC5HDNgyinEnhaQ_4-gaMud_GF50elYXLlCToR9se9Z8z433U3KjM-3Yx7ptKkmQNAMggQwAVKgq3zYAoidNEWuxpkY_mAitTSRLnsJW-NCTa0bqBFF6Wm1MxgfE",
124
e: "AQAB",
125
},
126
{ name: "RSA-OAEP", hash: "SHA-256" },
127
true,
128
["encrypt"],
129
)
130
131
async function getCorrespondPath(timestamp) {
132
const data = new TextEncoder().encode(`refresh_${timestamp}`);
133
const encrypted = new Uint8Array(await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data))
134
return encrypted.reduce((str, c) => str + c.toString(16).padStart(2, "0"), "")
135
}
136
137
const ts = Date.now()
138
console.log(await getCorrespondPath(ts))
139
```
140
141
```
142
b77f21ab5b7ce7879c410b2311dd6e7ea1a2cd1cd941073db067f4c3279fdabca3a06dfa744168ee14ad050b9f4889bd4edb8e76eb597fdd18c16804d82566b55c6dba8e225d838aa93d8e5b31cf7c56720db8244d92373f4944e0561f6ca5bf721a36ac079786060fc853605ccd1ddcb33f54617de6aedd44e3b9850d13b45f
143
```
144
145
#### Python
146
147
依赖`pycryptodome`
148
149
```python
150
from Crypto.Cipher import PKCS1_OAEP
151
from Crypto.PublicKey import RSA
152
from Crypto.Hash import SHA256
153
import binascii
154
import time
155
156
key = RSA.importKey('''\
157
-----BEGIN PUBLIC KEY-----
158
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLgd2OAkcGVtoE3ThUREbio0Eg
159
Uc/prcajMKXvkCKFCWhJYJcLkcM2DKKcSeFpD/j6Boy538YXnR6VhcuUJOhH2x71
160
nzPjfdTcqMz7djHum0qSZA0AyCBDABUqCrfNgCiJ00Ra7GmRj+YCK1NJEuewlb40
161
JNrRuoEUXpabUzGB8QIDAQAB
162
-----END PUBLIC KEY-----''')
163
164
def getCorrespondPath(ts):
165
cipher = PKCS1_OAEP.new(key, SHA256)
166
encrypted = cipher.encrypt(f'refresh_{ts}'.encode())
167
return binascii.b2a_hex(encrypted).decode()
168
169
ts = round(time.time() * 1000)
170
print(getCorrespondPath(ts))
171
```
172
173
```
174
47bbd615f333d6a2c597bbb46ad47a6e59752a305a2f545d3ba5d49ca055309347796f80d257613696d36170c57443a0e9dea2b47f83b0b4224d431e46124fadd9a24c8fa468147e8bf2d2501eaacae43310e19bf58fc4a728d80c90b9401afcfc1536ba9a2f6438ea53c0b2652f8b8d01c87355dd5a5da51de998b1a35d519a
175
```
176
177
#### vercel云函数
178
179
```bash
180
curl -G 'https://wasm-rsa.vercel.app/api/rsa' \
181
--data-urlencode "t=$((`date '+%s'`*1000+`date '+%N'`/1000000))"
182
```
183
184
```json
185
{
186
"timestamp": "1684468084078",
187
"hash": "a768efe5114ef8610f9ed9ebc28c00827375f4a3448ec4ab17958cacc4fde9898e5b7aa27f649426bba1acae4aa222aafaff7d528669b15249de0b2b60d86618557d8dc90684db4ec68e8d98e41d94f1c97d1d431c288e595ceb522d033822442a9e1ee150b32771a83fbf65c13329e9fda080fbe3bc85c49c1de7ab148d228f",
188
"code": 0
189
}
190
```
191
192
## 获取refresh_csrf
193
194
> https://www.bilibili.com/correspond/1/{correspondPath}
195
196
*请求方式:GET*
197
198
鉴权方式:Cookie
199
200
**path 参数:**
201
202
| 参数名 | 类型 | 内容 | 必要性 | 备注 |
203
| -------------- | ---- | ---------------------------- | ------ | --------------------------------------------------------- |
204
| correspondPath | str | 使用当前毫秒时间戳生成的密文 | 必要 | 由 [生成CorrespondPath算法](#生成CorrespondPath算法) 获得 |
205
206
将参数`correspondPath`拼接在 https://www.bilibili.com/correspond/1/ 后进行请求,例如
207
208
> https://www.bilibili.com/correspond/1/0248397e5139a8b878894cae46f8d6742ef7c728e46403706452b5dda90fe248e58e73bd6c2da0dba515c53af107dc1ecda757ce843579bcf197fcd7800586126e9b896b646cc94c23183a5a067642e96f7b6e803880e1d3cceabc9f1dc52a121b5e3ba5619e008f6b6dcb65a09d7864084ac114f4ec9ccf6218776fe4f2fa95
209
210
请求该 url 会返回一个 html 页面,通常由 iframe 方式加载,它通过 SSR 范式返回刷新一个实时刷新口令`refresh_csrf`,并在 Client 端通过 js 请求 RestAPI 完成一些列的提交刷新、确认、SSO 站点登录等操作
211
212
如果参数`correspondPath`错误或过期,则返回一个 404 Page
213
214
以下为返回的参数:
215
216
| 标签 id | 内容 | xpath | 备注 |
217
| ------- | ------------ | ------------------------- | --------------------------------- |
218
| 1-name | refresh_csrf | //div[id='1-name']/text() | 实时刷新口令<br />用于更新 Cookie |
219
220
**示例:**
221
222
```bash
223
correspondPath='0248397e5139a8b878894cae46f8d6742ef7c728e46403706452b5dda90fe248e58e73bd6c2da0dba515c53af107dc1ecda757ce843579bcf197fcd7800586126e9b896b646cc94c23183a5a067642e96f7b6e803880e1d3cceabc9f1dc52a121b5e3ba5619e008f6b6dcb65a09d7864084ac114f4ec9ccf6218776fe4f2fa95'
224
225
curl -G "https://www.bilibili.com/correspond/1/$correspondPath" \
226
-b 'SESSDATA=xxx'
227
```
228
229
```html
230
<!DOCTYPE html>
231
<html lang="zh-Hans">
232
233
<head>
234
<meta name="spm_prefix" content="333.1193">
235
<link
236
href="//s1.hdslb.com/bfs/static/jinkela/token-iframe/css/token-iframe.1.a035e81c3bee5fa1a05633ad534ad1f44b05e54d.css"
237
rel="stylesheet">
238
</head>
239
<title>Correspond</title>
240
<script type="text/javascript"
241
src="//www.bilibili.com/gentleman/polyfill.js?features=Promise%2CObject.assign%2CString.prototype.includes%2CNumber.isNaN2%CglobalThis"></script>
242
243
<body>
244
<div id="1-name">b0cc8411ded2f9db2cff2edb3123acac</div>
245
<div id="token-iframe-app"></div>
246
<script type="text/javascript"
247
src="//s1.hdslb.com/bfs/static/jinkela/token-iframe/2.token-iframe.a035e81c3bee5fa1a05633ad534ad1f44b05e54d.js"></script>
248
<script type="text/javascript"
249
src="//s1.hdslb.com/bfs/static/jinkela/token-iframe/token-iframe.a035e81c3bee5fa1a05633ad534ad1f44b05e54d.js"></script>
250
</body>
251
<script type="text/javascript">window.reportMsgObj = {};
252
window.reportConfig = {
253
sample: 1,
254
scrollTracker: true,
255
msgObjects: 'reportMsgObj',
256
};
257
258
let reportScript = document.createElement('script');
259
reportScript.src = '//s1.hdslb.com/bfs/seed/log/report/log-reporter.js';
260
document.getElementsByTagName('body')[0].appendChild(reportScript);</script>
261
262
</html>
263
```
264
265
所以当前账号的实时刷新口令`refresh_csrf`为`b0cc8411ded2f9db2cff2edb3123acac`
266
267
## 刷新Cookie
268
269
> https://passport.bilibili.com/x/passport-login/web/cookie/refresh
270
271
*请求方式:POST*
272
273
鉴权方式:Cookie
274
275
刷新成功后会设置以下 Cookie 项:
276
277
`sid`、`DedeUserID`、`DedeUserID__ckMd5`、`SESSDATA`、`bili_jct`
278
279
**正文参数 (application/x-www-form-urlencoded)或 url 参数:**
280
281
| 参数名 | 类型 | 内容 | 必要性 | 备注 |
282
| ------------- | ---- | -------------- | ------ | ------------------------------------------------------------ |
283
| csrf | str | CSRF Token | 必要 | 位于 Cookie 中的`bili_jct`字段 |
284
| refresh_csrf | str | 实时刷新口令 | 必要 | 通过 [获取refresh_csrf](#获取refresh_csrf) 获得 |
285
| source | str | 访问来源? | 必要 | 一般为`main_web` |
286
| refresh_token | str | 持久化刷新口令 | 必要 | localStorage 中的`ac_time_value`字段,在登录成功后返回并保存 |
287
288
**json 回复:**
289
290
根对象:
291
292
| 字段 | 类型 | 内容 | 备注 |
293
| ------- | ---- | -------- | ------------------------------------------------------------ |
294
| code | num | 返回值 | 0:成功<br />-101:账号未登录<br />-111:csrf 校验失败<br />86095:refresh_csrf 错误或 refresh_token 与 cookie 不匹配 |
295
| message | str | 错误信息 | 默认为 0 |
296
| ttl | num | 1 | |
297
| data | obj | 信息本体 | |
298
299
`data`对象:
300
301
| 字段 | 类型 | 内容 | 备注 |
302
| ------------- | ---- | ------------------ | ----------------------------------------------------------- |
303
| status | num | 0 | |
304
| message | str | 空 | |
305
| refresh_token | str | 新的持久化刷新口令 | 将存储于 localStorage 中的`ac_time_value`字段,以便下次使用 |
306
307
**示例:**
308
309
```bash
310
curl -i 'https://passport.bilibili.com/x/passport-login/web/cookie/refresh' \
311
--data-urlencode 'csrf=f610640a37f51f6266f6b83cfc5eedbb' \
312
--data-urlencode 'refresh_csrf=b0cc8411ded2f9db2cff2edb3123acac' \
313
--data-urlencode 'source=main_web' \
314
--data-urlencode 'refresh_token=45240a041836905fe953e3b98b83d751' \
315
-b 'SESSDATA=xxx'
316
```
317
318
<details>
319
<summary>查看响应示例:</summary>
320
321
http 响应(关键信息已做脱敏处理):
322
323
```http
324
HTTP/2 200
325
date: Fri, 19 May 2023 07:34:11 GMT
326
content-type: application/json; charset=utf-8
327
content-length: 116
328
bili-status-code: 0
329
bili-trace-id: 17f4251365646726
330
set-cookie: SESSDATA=***; Path=/; Domain=bilibili.com; Expires=Wed, 15 Nov 2023 07:34:10 GMT; HttpOnly; Secure
331
set-cookie: bili_jct=***; Path=/; Domain=bilibili.com; Expires=Wed, 15 Nov 2023 07:34:10 GMT
332
set-cookie: DedeUserID=***; Path=/; Domain=bilibili.com; Expires=Wed, 15 Nov 2023 07:34:10 GMT
333
set-cookie: DedeUserID__ckMd5=***; Path=/; Domain=bilibili.com; Expires=Wed, 15 Nov 2023 07:34:10 GMT
334
set-cookie: sid=***; Path=/; Domain=bilibili.com; Expires=Wed, 15 Nov 2023 07:34:10 GMT
335
x-bili-trace-id: 3f6f6174aaa087b517f4251365646726
336
expires: Fri, 19 May 2023 07:34:10 GMT
337
cache-control: no-cache
338
x-cache-webcdn: BYPASS from blzone03
339
340
{"code":0,"message":"0","ttl":1,"data":{"status":0,"message":"","refresh_token":"ae1bd1149b56af9743ffe7bbbeff3e51"}}
341
```
342
343
JSON Payload:
344
345
```json
346
{
347
"code": 0,
348
"message": "0",
349
"ttl": 1,
350
"data": {
351
"status": 0,
352
"message": "",
353
"refresh_token": "ae1bd1149b56af9743ffe7bbbeff3e51"
354
}
355
}
356
```
357
358
</details>
359
360
## 确认更新
361
362
> https://passport.bilibili.com/x/passport-login/web/confirm/refresh
363
364
*请求方式:POST*
365
366
鉴权方式:Cookie
367
368
该步操作将让旧的`refresh_token`对应的 Cookie 失效
369
370
**正文参数 (application/x-www-form-urlencoded)或 url 参数:**
371
372
| 参数名 | 类型 | 内容 | 必要性 | 备注 |
373
| ------------- | ---- | ------------------------- | ------ | ------------------------------------------------------------ |
374
| csrf | str | CSRF Token(位于 cookie) | 必要 | 从新的 cookie 中获取 |
375
| refresh_token | str | 旧的持久化刷新口令 | 必要 | 在刷新前 localStorage 中的`ac_time_value`获取,**并非刷新后返回的值** |
376
377
**json 回复:**
378
379
根对象:
380
381
| 字段 | 类型 | 内容 | 备注 |
382
| ------- | ---- | -------- | ------------------------------------------------------------ |
383
| code | num | 返回值 | 0:成功<br />-101:账号未登录<br />-111:csrf 校验失败<br />-400:请求错误 |
384
| message | str | 错误信息 | 默认为 0 |
385
| ttl | num | 1 | |
386
387
**示例:**
388
389
```bash
390
curl 'https://passport.bilibili.com/x/passport-login/web/confirm/refresh' \
391
--data-urlencode 'csrf=1e9658858e6da76be64bd92cdc0fa324' \
392
--data-urlencode 'refresh_token=45240a041836905fe953e3b98b83d751' \
393
-b 'SESSDATA=xxx'
394
```
395
396
<details>
397
<summary>查看响应示例:</summary>
398
399
```json
400
{
401
"code": 0,
402
"message": "0",
403
"ttl": 1
404
}
405
```
406
407
</details>