移除二维码信封历史数据依赖
This commit is contained in:
+21
-19
@@ -9,7 +9,8 @@
|
||||
6. POST /web/cookie/verify 兑换获取全套网页 Cookie
|
||||
|
||||
注册链不再重放旧 dfpReport 密文。登录帧的 32hex hdid 仍是服务端硬锚,
|
||||
当前继续使用已注册样本;新注册链动态更新的是 safedeviceid 和 device_id。
|
||||
登录帧中的 HDID32 是 app 版本级协议常量;二维码信封的 ACTION、设备字段、
|
||||
UID、证书和会话元数据均在本次登录中动态生成或由注册链签发。
|
||||
注册链(``core/huya/dfp_register``)每次登录前执行,失败即抛错终止(``HuyaAppLoginError``),
|
||||
不读取画像里的旧固定值,也不静默回退旧链。
|
||||
|
||||
@@ -35,7 +36,7 @@ from urllib.parse import parse_qs, quote, urlparse
|
||||
import requests
|
||||
from loguru import logger
|
||||
|
||||
from .cert_forge import build_p1, decrypt_cert, forge_cert, parse_p1
|
||||
from .cert_forge import build_p1, forge_cert
|
||||
from .cookie_utils import normalize_huya_cookie
|
||||
from .device_fingerprint import account_state_dir, get_huya_sdid, reset_account_state
|
||||
from .device_profile import get_profile, mobile_user_agent
|
||||
@@ -270,7 +271,8 @@ def login_cred_with_flow(
|
||||
max_rounds: int = 3,
|
||||
device_info: dict | None = None,
|
||||
proxies: dict | None = None,
|
||||
) -> tuple[bytes, int]:
|
||||
include_device_token: bool = False,
|
||||
) -> tuple[bytes, int] | tuple[bytes, int, str]:
|
||||
"""新注册设备后登录,返回 ``(新鲜 cred, 真实 uid)``。
|
||||
|
||||
注册只执行一次;safe_auth 通过后的重发继续使用同一组设备字段。
|
||||
@@ -305,6 +307,8 @@ def login_cred_with_flow(
|
||||
)
|
||||
if cred:
|
||||
uid = parse_real_uid(resp)
|
||||
if include_device_token:
|
||||
return cred, uid, safedeviceid
|
||||
return cred, uid
|
||||
if risk_url:
|
||||
kind = (
|
||||
@@ -462,11 +466,12 @@ class HuyaAppPasswordLogin:
|
||||
|
||||
# 1) 获取新鲜 cred 与 真实 uid (自动过 safe_auth 滑块)
|
||||
try:
|
||||
cred, uid = login_cred_with_flow(
|
||||
cred, uid, safedeviceid = login_cred_with_flow(
|
||||
acct,
|
||||
self.password,
|
||||
device_info=self.device_info,
|
||||
proxies=self.proxies,
|
||||
include_device_token=True,
|
||||
)
|
||||
except HuyaAppQrAuthRequiredError as exc:
|
||||
return HuyaLoginResult(
|
||||
@@ -485,30 +490,27 @@ class HuyaAppPasswordLogin:
|
||||
|
||||
# 2) 本地生成 nonce 铸造证书 (P1 指纹与该账号设备画像一致)
|
||||
try:
|
||||
env = Envelope.load()
|
||||
orig = base64.b64decode(env.cert_b64)
|
||||
f = parse_p1(decrypt_cert(orig))
|
||||
st = int(time.time() * 1000)
|
||||
rnd = gen_nonce(uid, K1_DEFAULT, service_time_ms=st, counter=0)
|
||||
fp_bytes = self.device_info["fingerprint"].encode("ascii")
|
||||
p1 = build_p1(f["app_id"], fp_bytes, cred, rnd=rnd)
|
||||
cert = base64.b64encode(forge_cert(p1, key_idx=orig[1])).decode()
|
||||
p1 = build_p1(b"5008", fp_bytes, cred, rnd=rnd)
|
||||
# 证书头和 app_id 是协议版本常量,不从抓包信封读取。
|
||||
cert = base64.b64encode(forge_cert(p1, key_idx=0x20)).decode()
|
||||
|
||||
# 3) 信封补丁
|
||||
raw = bytearray(env.raw)
|
||||
if env.cert_off is None or env.uid_off is None:
|
||||
raise ValueError("信封缺少证书或 uid 偏移")
|
||||
raw[env.cert_off : env.cert_off + env.cert_len] = cert.encode("ascii")
|
||||
if env.uid != uid:
|
||||
struct.pack_into(">Q", raw, env.uid_off, uid)
|
||||
# QR 信封只保留协议结构;不要重放抓包里的旧会话值。
|
||||
# 3) 按当前账号画像和本次注册令牌重编码二维码信封。
|
||||
qr_session = random.randint(1_000_000, 9_999_999)
|
||||
qr_trace = (
|
||||
f"{uuid.uuid4().hex[:16]}-{random.randint(10000, 99999)}-"
|
||||
f"{time.time_ns():020d}"
|
||||
)
|
||||
env.raw = raw
|
||||
env.patch_session(qr_session).patch_meta(qr_session, qr_trace)
|
||||
env = Envelope.build_qr(
|
||||
uid=uid,
|
||||
cert_b64=cert,
|
||||
safedeviceid=safedeviceid,
|
||||
session=qr_session,
|
||||
trace_id=qr_trace,
|
||||
device_info=self.device_info,
|
||||
)
|
||||
wup = base64.b64encode(bytes(env.raw)).decode("ascii")
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return HuyaLoginResult(
|
||||
|
||||
+98
-14
@@ -1,6 +1,7 @@
|
||||
"""wupData 信封构造与补丁工具。
|
||||
|
||||
解析与改写 WUP 信封中的 cert、uid、session 等字段。
|
||||
生产二维码信封由 :meth:`Envelope.build_qr` 按当前账号状态编码;
|
||||
:meth:`Envelope.load` 仅保留协议结构模板兼容测试和离线分析。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -10,7 +11,8 @@ import json
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
from loguru import logger
|
||||
from .taf_protocol import TafOutputStream
|
||||
from .wup_protocol import WupRequest
|
||||
|
||||
INT8, INT16, INT32, INT64 = 0x00, 0x01, 0x02, 0x03
|
||||
STRING1, STRING4 = 0x06, 0x07
|
||||
@@ -119,24 +121,106 @@ class Envelope:
|
||||
|
||||
@classmethod
|
||||
def load(cls, path: str | Path | None = None) -> Envelope:
|
||||
"""加载信封模板,支持从文件加载或使用内嵌金样本。"""
|
||||
"""加载显式模板,或使用内嵌协议结构模板。
|
||||
|
||||
不自动扫描 ``evidence/``,避免历史抓包成为隐式生产输入。
|
||||
"""
|
||||
if path:
|
||||
p = Path(path)
|
||||
if p.exists():
|
||||
return cls._load_from_path(p)
|
||||
# 尝试查找 evidence/cert_keycap.json
|
||||
candidate = (
|
||||
Path(__file__).resolve().parent.parent.parent
|
||||
/ "evidence"
|
||||
/ "cert_keycap.json"
|
||||
)
|
||||
if candidate.exists():
|
||||
try:
|
||||
return cls._load_from_path(candidate)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"加载证书信封候选文件失败: {candidate}: {exc}")
|
||||
return cls(base64.b64decode(PROTOCOL_QURL_TEMPLATE_B64))
|
||||
|
||||
@classmethod
|
||||
def build_qr(
|
||||
cls,
|
||||
*,
|
||||
uid: int,
|
||||
cert_b64: str,
|
||||
safedeviceid: str,
|
||||
session: int,
|
||||
trace_id: str,
|
||||
device_info: dict[str, str],
|
||||
) -> Envelope:
|
||||
"""按当前账号状态编码二维码绑定信封。
|
||||
|
||||
生产二维码请求不应从抓包信封复制字段。这里仅复用已知的 TAF/WUP
|
||||
字段布局;证书、ACTION、UID、会话和全部设备画像都由本次登录提供。
|
||||
"""
|
||||
if len(cert_b64) != 260:
|
||||
raise ValueError(f"证书 base64 长度异常: {len(cert_b64)} != 260")
|
||||
if len(safedeviceid) != 180:
|
||||
raise ValueError(f"safedeviceid 长度异常: {len(safedeviceid)} != 180")
|
||||
dev = {k: str(v) for k, v in (device_info or {}).items()}
|
||||
meta = json.dumps(
|
||||
{
|
||||
"associationId": 184549392,
|
||||
"funcName": "",
|
||||
"group": 0,
|
||||
"id": 184549392,
|
||||
"session": int(session),
|
||||
"step": 0,
|
||||
"stillLogin": False,
|
||||
"traceId": str(trace_id),
|
||||
"type": 2,
|
||||
"uid": 0,
|
||||
"userContext": "",
|
||||
},
|
||||
ensure_ascii=False,
|
||||
separators=(",", ":"),
|
||||
)
|
||||
body = TafOutputStream()
|
||||
body.write_struct_begin(0)
|
||||
body.write_struct_begin(0)
|
||||
body.write_int8(0, 0)
|
||||
body.write_string(1, "1.0")
|
||||
body.write_string(2, meta)
|
||||
body.write_string(3, "5008")
|
||||
body.write_int8(4, 3)
|
||||
body.write_string(5, safedeviceid)
|
||||
body.write_string(6, "")
|
||||
body.write_string(7, "")
|
||||
body.write_string(8, "")
|
||||
body.write_string(9, "")
|
||||
body.write_struct_end()
|
||||
|
||||
body.write_struct_begin(1)
|
||||
body.write_string(0, dev.get("hdid", ""))
|
||||
body.write_string(1, dev.get("app_version", "13.4.22"))
|
||||
body.write_string(2, dev.get("sdk_version", "1.0.80138"))
|
||||
body.write_string(3, "")
|
||||
body.write_string(4, dev.get("ip", "127.0.0.1"))
|
||||
body.write_string(5, dev.get("vendor", "android"))
|
||||
body.write_string(6, "")
|
||||
body.write_struct_end()
|
||||
|
||||
body.write_struct_begin(2)
|
||||
body.write_int8(0, 1)
|
||||
body.write_string(1, dev.get("model", ""))
|
||||
body.write_string(2, dev.get("fingerprint", ""))
|
||||
body.write_string(3, dev.get("os", "android"))
|
||||
body.write_string(4, dev.get("screen", ""))
|
||||
body.write_string(6, dev.get("width", "1080"))
|
||||
body.write_string(7, dev.get("height", "2120"))
|
||||
body.write_string(8, dev.get("device_id", ""))
|
||||
body.write_struct_end()
|
||||
body.write_uint64(3, int(uid))
|
||||
body.write_string(4, cert_b64)
|
||||
body.write_string(5, "")
|
||||
body.write_string(6, "")
|
||||
body.write_struct_end()
|
||||
|
||||
req = TafOutputStream()
|
||||
req.write_int32(0, int(session))
|
||||
wup = WupRequest()
|
||||
wup.iTimeout = 0
|
||||
wup.setRequestId(int(session))
|
||||
wup.setServant("huyaudbwebui")
|
||||
wup.setFunc("default")
|
||||
wup.newdata["_wup_data"] = body.get_bytes()
|
||||
wup.newdata["wupudbrequest_v0"] = req.get_bytes()
|
||||
return cls(wup.encode())
|
||||
|
||||
@classmethod
|
||||
def _load_from_path(cls, p: Path) -> Envelope:
|
||||
if p.suffix == ".json":
|
||||
|
||||
Reference in New Issue
Block a user