feat(huya): 分身 CK 支持 cred0 解密补全,WSS 帧改为 JCE WebSocketCommand

- scripts/huya_clone_ck.py: root 读取小X分身/真身 WebView Cookie,默认
  用 AES-128-ECB(UDB 主密钥前 16B) 解 cred0 注入 udb_cred,新增
  --anon-device 补匿名设备字段、--no-cred0 退回旧行为
- core/huya/wss_client.py: 帧层改用浏览器 HUYA.WebSocketCommand(JCE)
  结构,vData 直接放 Wup 原包,按 WUP iRequestId 匹配响应,初始化顺序
  对齐 RegisterGroup→wsLaunch→VerifyCookie→getConfig→UpdateUserExps
- tests: 帧级断言改为新结构,假 WS 按 requestId 兑现
- docs: 新增分身 CK 提取、HAR 提取 CK 与积分中心说明

实测:四个分身 cred0 解出 udb_cred(39/43 字段),WSS
PlayMallUI.queryAllocation 返回 code=200、18 个商品;tests -k huya 394 passed
This commit is contained in:
yml2213
2026-10-26 11:23:29 +08:00
parent 6fc453017c
commit 03bc0818dd
9 changed files with 1188 additions and 156 deletions
+31 -2
View File
@@ -795,6 +795,7 @@ def points_center_blob(
product_id: str = "hy-1001",
points: int = 200,
stock: int = 39,
remaining: int | None = None,
category: str = "礼包",
) -> bytes:
"""构造 queryAllocation 的 ``tag5`` 最小商品列表(TAF LIST)。"""
@@ -817,6 +818,13 @@ def points_center_blob(
os.write_string(5, "")
os.write_int64(9, points * 10)
os.write_int64(13, stock)
if remaining is not None:
os.write_int64(14, remaining)
os.write_head(17, TafType.LIST)
os.write_int32(0, 1)
os.write_head(0, TafType.STRUCT_BEGIN)
os.write_int64(3, remaining)
os.write_struct_end()
# 积分 tag75.tag0.tag2
os.write_head(75, TafType.STRUCT_BEGIN)
os.write_head(0, TafType.STRUCT_BEGIN)
@@ -1295,8 +1303,9 @@ class TestCollabCkPreview(TestCollabBase):
"product_id": "hy-1001",
"name": "不朽伊人大礼包",
"price": 200,
"remain_text": "剩39",
"remain_text": "39",
"stock": 39,
"claimed": 39,
"score": 200,
"category_name": "礼包",
"icon": "",
@@ -1392,8 +1401,9 @@ class TestCollabGifts(TestCollabBase):
"product_id": "hy-1001",
"name": "不朽伊人大礼包",
"price": 200,
"remain_text": "剩39",
"remain_text": "39",
"stock": 39,
"claimed": 39,
"score": 200,
"category_name": "礼包",
"icon": "",
@@ -1440,6 +1450,25 @@ class TestCollabGifts(TestCollabBase):
assert gifts[0]["score"] == 888
assert gifts[0]["stock"] == 7
def test_points_center_parser_prefers_remaining_stock(self):
"""2026-10-25 抓包:tag13 是累计兑换,tag14 才是剩余库存。"""
from core.huya.shop_structs import parse_points_center_gifts
gifts = parse_points_center_gifts(
{
"tag2": [
{
"tag5": points_center_blob(
points=600, stock=43194, remaining=5054
)
}
]
}
)
assert gifts[0]["claimed"] == 43194
assert gifts[0]["stock"] == 5054
assert gifts[0]["remain_text"] == "5054"
def test_resolve_gifts_sid_prefers_workbench_config(self):
self.db.add(HuyaConfig(sid="2277"))
self.db.commit()
+7 -16
View File
@@ -11,7 +11,7 @@ from core.huya.shop_structs import (
ShopCodeRsp,
)
from core.huya.taf_protocol import TafInputStream, TafOutputStream, TafType
from core.huya.wss_client import TAIL_BYTES, HuyaWssClient, WssCommand, WssMessage
from core.huya.wss_client import HuyaWssClient, WssCommand, WssMessage
from web.backend.services.huya_runner_core import HuyaBatchRunnerCore
@@ -108,9 +108,8 @@ def test_wss_requires_web_device_cookie_fields():
def test_activity_auth_frame_matches_latest_capture_layout():
"""对齐 9.1 活动 AUTH(828 连接)逐字段结构。
9.1: tag3 ZERO 首字段 -> tag0 uid -> tag1 UA -> tag2 cookie(STRING4)
-> tag3 STRING1 guid(32hex) -> tag4 INT8=1 -> tag5 "HUYA&ZH&2052"
-> tag6 "" -> TAIL_BYTES
2026-10-25 对齐 huya-go:tag0 uid -> tag1 UA -> tag2 cookie(STRING4)
-> tag3 STRING1 guid(32hex) -> tag4 INT32=1 -> tag5 "HUYA&ZH&2052"
"""
async def build_frame():
@@ -126,12 +125,9 @@ def test_activity_auth_frame_matches_latest_capture_layout():
message = WssMessage.decode(asyncio.run(build_frame()))
assert message.command == WssCommand.AUTH
assert message.sequence == 0x1D000109
assert message.body.endswith(TAIL_BYTES)
assert message.sequence == 0
stream = TafInputStream(message.body[: -len(TAIL_BYTES)])
# 首字段:tag3 ZERO(9.1 活动帧的占位字段)
assert stream.read_head() == (3, TafType.ZERO)
stream = TafInputStream(message.body)
assert stream.read_int64(0) == 1199664135026
assert stream.read_string(1) == "webh5&0.0.1&websocket&&diypc_52775"
assert stream.read_head() == (2, TafType.STRING4)
@@ -141,12 +137,8 @@ def test_activity_auth_frame_matches_latest_capture_layout():
# tag3 = STRING1 guid(9.1 中为 Cookie 里的 32hex guid)
guid = stream.read_string(3)
assert guid == "0a" * 16
assert stream.read_int8(4) == 1
assert stream.read_int32(4) == 1
assert stream.read_string(5) == "HUYA&ZH&2052"
assert stream.read_string(6) == ""
# body 必须与 9.1 828 AUTH 同样以 TAIL_BYTES 收尾
assert message.body[-len(TAIL_BYTES) :] == TAIL_BYTES
def test_activity_cookie_guid_is_replaced_by_launch_guid():
@@ -173,8 +165,7 @@ def test_activity_auth_accepts_explicit_launch_guid_over_cookie_guid():
return client.ws.send.call_args.args[0]
message = WssMessage.decode(asyncio.run(build_frame()))
stream = TafInputStream(message.body[: -len(TAIL_BYTES)])
assert stream.read_head() == (3, TafType.ZERO)
stream = TafInputStream(message.body)
stream.read_int64(0)
stream.read_string(1)
assert stream.read_head() == (2, TafType.STRING4)
+15 -7
View File
@@ -11,10 +11,11 @@
import asyncio
from types import SimpleNamespace
from core.huya.frame_decoder import _decode_wup_body, decode_struct_bytes
from core.huya.frame_decoder import _decode_wup_body, _extract_wup, decode_struct_bytes
from core.huya.http_client import HuyaHttpClient
from core.huya.shop_structs import ShopRawRsp
from core.huya.wss_client import HuyaWssClient, WssMessage
from core.huya.wss_client import HuyaWssClient, WssCommand, WssMessage
from core.huya.wup_protocol import WupResponse
from web.backend.models import HuyaAccount
from web.backend.services.huya_runner import HuyaBatchRunner
@@ -41,10 +42,17 @@ class _FakeWs:
async def send(self, data: bytes):
self.sent.append(data)
while self.client.rpc_queue:
future = self.client.rpc_queue.popleft()
if not future.done():
future.set_result(b"")
message = WssMessage.decode(data)
if message.command != WssCommand.RPC_REQUEST:
return
try:
wup = WupResponse()
wup.decode(_extract_wup(message.body))
except Exception: # noqa: BLE001 测试替身:无法解析的帧不兑现
return
future = self.client._pending.pop(wup.iRequestId, None)
if future is not None and not future.done():
future.set_result(b"")
def last_request(self) -> dict:
return _decode_wup_body(WssMessage.decode(self.sent[-1]).body)
@@ -111,7 +119,7 @@ def test_wss_redeem_need_goods_request_matches_real_call():
message = WssMessage.decode(client.ws.sent[-1])
# 积分中心业务帧统一 0x1d000107(只有下单/支付用 0x1d000108)
assert message.sequence == 0x1D000107
assert message.sequence == 0
decoded = client.ws.last_request()
assert (decoded["servant"], decoded["func"]) == (
+27 -15
View File
@@ -29,12 +29,13 @@ COOKIE = f"udb_uid={UID}; yyuid={UID}; guid={GUID}"
PID = 1199650619883
# 抓包字面值(不要引用实现常量,否则断言会与实现一起漂移)
POINTS_UA_LITERAL = "webh5&0.0.1&websocket&&h5_/points.html"
# 旧版本在 vData 末尾拼的尾部字节;JCE 帧不需要,保留常量用于确认已移除
TAIL_LITERAL = bytes.fromhex("2c36004c5c6600")
BIZ_PARAMS_LITERAL = '{ "jifencenter": 1 }'
# 抓包实测:RegisterGroup(live:0) 与 UpdateUserExps(版本 map) 的 body 逐字节一致
HEARTBEAT_BODY = bytes.fromhex("09000106066c6976653a3016002c36004c5c6600")
UPDATE_EXPS_BODY = bytes.fromhex("0800010605353836363416063130373236332c36004c5c6600")
HEARTBEAT_BODY = bytes.fromhex("09000106066c6976653a301600")
UPDATE_EXPS_BODY = bytes.fromhex("080001060535383636341606313037323633")
CONFIG_VERSIONS = {"58664": "107263"}
@@ -48,10 +49,17 @@ class _FakeWs:
async def send(self, data: bytes):
self.sent.append(data)
while self.client.rpc_queue:
future = self.client.rpc_queue.popleft()
if not future.done():
future.set_result(self.response)
message = WssMessage.decode(data)
if message.command != WssCommand.RPC_REQUEST:
return
try:
wup = WupResponse()
wup.decode(_extract_wup(message.body))
except Exception: # noqa: BLE001 测试替身:无法解析的帧不兑现
return
future = self.client._pending.pop(wup.iRequestId, None)
if future is not None and not future.done():
future.set_result(self.response)
def last_body(self) -> bytes:
return WssMessage.decode(self.sent[-1]).body
@@ -106,8 +114,9 @@ def test_register_group_frame_matches_capture():
await client.send_heartbeat()
message = WssMessage.decode(client.ws.sent[-1])
assert message.command == WssCommand.HEARTBEAT_SEND
assert message.sequence == 0x1D00000D
assert message.sequence == 0
assert message.body == HEARTBEAT_BODY
assert not message.body.endswith(TAIL_LITERAL)
asyncio.run(run())
@@ -119,8 +128,8 @@ def test_shop_ws_launch_carries_real_identity_and_points_ua():
message = WssMessage.decode(client.ws.sent[-1])
assert message.command == WssCommand.RPC_REQUEST
# 抓包:wsLaunch 帧 seq=0x1d000100,iRequestId=-1
assert message.sequence == 0x1D000100
# JCE 帧 lRequestId 固定 0,真正的请求号是 WUP iRequestId=-1
assert message.sequence == 0
wup = WupResponse()
wup.decode(_extract_wup(message.body))
assert wup.iRequestId == -1
@@ -182,8 +191,7 @@ def test_create_order_v5_matches_points_capture():
)
message = WssMessage.decode(client.ws.sent[-1])
# 抓包:下单帧用 0x1d000108(其余业务帧 0x1d000107)
assert message.sequence == 0x1D000108
assert message.command == WssCommand.RPC_REQUEST
decoded = _decode_wup_body(message.body)
assert (decoded["servant"], decoded["func"]) == (
@@ -223,7 +231,7 @@ def test_pay_order_submit_v5_callback_points_page():
)
message = WssMessage.decode(client.ws.sent[-1])
assert message.sequence == 0x1D000108
assert message.command == WssCommand.RPC_REQUEST
decoded = _decode_wup_body(message.body)
assert (decoded["servant"], decoded["func"]) == (
@@ -338,11 +346,15 @@ def test_new_points_rpcs_match_capture():
asyncio.run(run())
def test_confirm_frame_ends_with_capture_tail():
def test_confirm_frame_carries_version_map_without_legacy_tail():
async def run():
client = _points_client()
await client.send_confirm({})
assert client.ws.last_body().endswith(TAIL_LITERAL)
await client.send_confirm(CONFIG_VERSIONS)
message = WssMessage.decode(client.ws.sent[-1])
assert message.command == WssCommand.CONFIRM_SEND
assert message.sequence == 0
assert message.body == UPDATE_EXPS_BODY
assert not message.body.endswith(TAIL_LITERAL)
asyncio.run(run())