Files
Nixevol cea741eead feat: DramaBox 全量走 App 接口,清单可停止并避免卡死
分类按 App 翻页,精选走推荐接口。清单顶部显示进度,停止后不再拉新剧,已拿到的封面继续加载。列表只绘制可见卡片,避免大量剧目占满主线程。
2026-10-24 03:03:27 +08:00

242 lines
8.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""阿里云 License 私有加密 MP4:从文件里的 AliyunPrivateKeyUri 解开样本后才能播。"""
import base64
import hashlib
import json
import struct
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
# DramaBox 播放器 LicenseKey(APK AndroidManifest com.aliyun.alivc_license.licensekey_SEA)
_LICENSE_KEY = b"8PfuCPCwtaTqTB00lca1b30cbaa074865b30efce2c7108498"
_KEY_URI = b"AliyunPrivateKeyUri"
_CONTAINERS = {b"moov", b"trak", b"mdia", b"minf", b"stbl", b"udta", b"meta"}
def is_aliyun_private_mp4(data: bytes) -> bool:
return bool(data) and data[4:8] == b"ftyp" and _KEY_URI in data
def decrypt_if_needed(data: bytes) -> bytes:
if not is_aliyun_private_mp4(data):
return data
return decrypt_mp4(data)
def decrypt_file_if_needed(path) -> bool:
target = Path(path)
data = target.read_bytes()
if not is_aliyun_private_mp4(data):
return False
decrypted = decrypt_mp4(data)
temporary = target.with_name(target.name + ".dec")
temporary.write_bytes(decrypted)
temporary.replace(target)
return True
def decrypt_mp4(data: bytes) -> bytes:
key = _content_key(data)
out = bytearray(data)
_decrypt_samples(out, key)
return bytes(out)
def _content_key(data: bytes) -> bytes:
uri = _aliyun_key_uri(data)
envelope = _aes_cbc(_md5_key(_LICENSE_KEY), _md5_key(_LICENSE_KEY), base64.b64decode(uri))
obj = json.loads(_pkcs7(envelope).decode("utf-8"))
client_rand = str(obj.get("ClientRand") or "").encode("utf-8")
server_b64 = str(obj.get("ServerRand") or "")
plain_b64 = str(obj.get("PlainText") or "")
if not client_rand or not server_b64 or not plain_b64:
raise RuntimeError("加密视频缺少密钥字段")
k1 = _md5_key(client_rand)
server = _pkcs7(_aes_cbc(k1, k1, base64.b64decode(server_b64)))
k2 = _md5_key(client_rand + server)
content = _pkcs7(_aes_cbc(k2, k1, base64.b64decode(plain_b64)))
key = base64.b64decode(content)
if len(key) != 16:
raise RuntimeError("加密视频密钥长度不对")
return key
def _aliyun_key_uri(data: bytes) -> str:
moov = _find_top(data, b"moov")
if moov is None:
raise RuntimeError("加密视频没有 moov")
start, size = moov
keys = _find_box(data, start + 8, start + size, b"keys")
ilst = _find_box(data, start + 8, start + size, b"ilst")
if not keys or not ilst:
raise RuntimeError("没有读到加密密钥")
index = _meta_key_index(data, keys, _KEY_URI)
if index <= 0:
raise RuntimeError("没有读到加密密钥")
value = _ilst_text(data, ilst, index)
if len(value) < 80:
raise RuntimeError("没有读到加密密钥")
return value
def _meta_key_index(data, box, name: bytes) -> int:
offset, size = box
count = struct.unpack(">I", data[offset + 12:offset + 16])[0]
pos = offset + 16
end = offset + size
for index in range(1, count + 1):
if pos + 8 > end:
break
key_size = struct.unpack(">I", data[pos:pos + 4])[0]
if key_size < 8 or pos + key_size > end:
break
if bytes(data[pos + 8:pos + key_size]) == name:
return index
pos += key_size
return 0
def _ilst_text(data, box, index: int) -> str:
offset, size = box
tag = struct.pack(">I", index)
for item_at, item_size, kind in _walk(data, offset + 8, offset + size):
if kind != tag:
continue
for data_at, data_size, data_kind in _walk(data, item_at + 8, item_at + item_size):
if data_kind != b"data" or data_size <= 16:
continue
return bytes(data[data_at + 16:data_at + data_size]).decode("ascii", "ignore")
return ""
def _md5_key(buf: bytes) -> bytes:
return hashlib.md5(buf).digest()[4:12].hex().encode("ascii")
def _aes_cbc(key: bytes, iv: bytes, data: bytes) -> bytes:
if len(data) < 16 or len(data) % 16:
data = data[: len(data) - (len(data) % 16)]
decryptor = Cipher(algorithms.AES(key), modes.CBC(iv)).decryptor()
return decryptor.update(data) + decryptor.finalize()
def _aes_ecb(key: bytes, data: bytes) -> bytes:
decryptor = Cipher(algorithms.AES(key), modes.ECB()).decryptor()
return decryptor.update(data) + decryptor.finalize()
def _pkcs7(data: bytes) -> bytes:
if not data:
return data
pad = data[-1]
if 1 <= pad <= 16 and data.endswith(bytes([pad]) * pad):
return data[:-pad]
return data.rstrip(b"\x00")
def _decrypt_samples(data: bytearray, key: bytes) -> None:
moov = _find_top(data, b"moov")
if moov is None:
raise RuntimeError("加密视频没有 moov")
start, size = moov
for offset, box_size, kind in _walk(data, start + 8, start + size):
if kind != b"trak":
continue
for sample_at, sample_size in _iter_samples(data, offset, box_size):
aligned = sample_size - (sample_size % 16)
if aligned < 16:
continue
data[sample_at:sample_at + aligned] = _aes_ecb(key, bytes(data[sample_at:sample_at + aligned]))
def _find_top(data: bytes, name: bytes):
for offset, size, kind in _walk(data, 0, len(data)):
if kind == name:
return offset, size
return None
def _walk(data, start: int, end: int):
index = start
while index + 8 <= end:
size = struct.unpack(">I", data[index:index + 4])[0]
kind = bytes(data[index + 4:index + 8])
header = 8
if size == 1:
if index + 16 > end:
break
size = struct.unpack(">Q", data[index + 8:index + 16])[0]
header = 16
if size < header or index + size > end:
break
yield index, size, kind
index += size
def _find_nested(data, start: int, end: int, name: bytes):
return _find_box(data, start, end, name, {b"mdia", b"minf", b"stbl", b"trak"})
def _find_box(data, start: int, end: int, name: bytes, containers=None):
containers = _CONTAINERS if containers is None else containers
for offset, size, kind in _walk(data, start, end):
if kind == name:
return offset, size
if kind in containers:
inner = offset + (12 if kind == b"meta" else 8)
found = _find_box(data, inner, offset + size, name, containers)
if found:
return found
return None
def _iter_samples(data, trak_at: int, trak_size: int):
start, end = trak_at + 8, trak_at + trak_size
stsz = _find_nested(data, start, end, b"stsz")
stsc = _find_nested(data, start, end, b"stsc")
stco = _find_nested(data, start, end, b"stco") or _find_nested(data, start, end, b"co64")
if not stsz or not stsc or not stco:
return
sizes = _stsz(data, stsz)
chunks = _stco(data, stco)
rows = _stsc(data, stsc)
sample = 0
for index, (first, per_chunk, _desc) in enumerate(rows):
last = rows[index + 1][0] - 1 if index + 1 < len(rows) else len(chunks)
for chunk in range(first - 1, last):
at = chunks[chunk]
for _ in range(per_chunk):
if sample >= len(sizes):
return
yield at, sizes[sample]
at += sizes[sample]
sample += 1
def _stsz(data, box):
offset, _size = box
default = struct.unpack(">I", data[offset + 12:offset + 16])[0]
count = struct.unpack(">I", data[offset + 16:offset + 20])[0]
if default:
return [default] * count
return [struct.unpack(">I", data[offset + 20 + 4 * i:offset + 24 + 4 * i])[0] for i in range(count)]
def _stco(data, box):
offset, _size = box
kind = bytes(data[offset + 4:offset + 8])
count = struct.unpack(">I", data[offset + 12:offset + 16])[0]
if kind == b"co64":
return [struct.unpack(">Q", data[offset + 16 + 8 * i:offset + 24 + 8 * i])[0] for i in range(count)]
return [struct.unpack(">I", data[offset + 16 + 4 * i:offset + 20 + 4 * i])[0] for i in range(count)]
def _stsc(data, box):
offset, _size = box
count = struct.unpack(">I", data[offset + 12:offset + 16])[0]
rows = []
for i in range(count):
first, per_chunk, desc = struct.unpack(">III", data[offset + 16 + 12 * i:offset + 28 + 12 * i])
rows.append((first, per_chunk, desc))
return rows