wxexport 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wxexport/__init__.py +4 -0
- wxexport/__main__.py +6 -0
- wxexport/cli.py +181 -0
- wxexport/config.py +40 -0
- wxexport/crypto/__init__.py +23 -0
- wxexport/crypto/profiles.py +215 -0
- wxexport/export/__init__.py +3 -0
- wxexport/export/exporter.py +155 -0
- wxexport/key/__init__.py +3 -0
- wxexport/key/memory.py +280 -0
- wxexport/key/offsets.json +68 -0
- wxexport/key/store.py +65 -0
- wxexport/media/__init__.py +3 -0
- wxexport/media/resolver.py +346 -0
- wxexport/model/__init__.py +3 -0
- wxexport/model/entities.py +117 -0
- wxexport/model/normalizer.py +325 -0
- wxexport/ops/__init__.py +3 -0
- wxexport/ops/pipeline.py +149 -0
- wxexport/preview/__init__.py +34 -0
- wxexport/preview/index.html +265 -0
- wxexport/schema/__init__.py +3 -0
- wxexport/schema/codec.py +130 -0
- wxexport/schema/wechat3.py +252 -0
- wxexport/source/__init__.py +3 -0
- wxexport/source/discovery.py +154 -0
- wxexport-0.1.0.dist-info/METADATA +238 -0
- wxexport-0.1.0.dist-info/RECORD +31 -0
- wxexport-0.1.0.dist-info/WHEEL +5 -0
- wxexport-0.1.0.dist-info/entry_points.txt +2 -0
- wxexport-0.1.0.dist-info/top_level.txt +1 -0
wxexport/__init__.py
ADDED
wxexport/__main__.py
ADDED
wxexport/cli.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from .config import Settings, default_export_dir, default_wechat_root, default_work_dir
|
|
10
|
+
from .export import Exporter
|
|
11
|
+
from .media import MediaResolver
|
|
12
|
+
from .ops.pipeline import prepare, resolve_account
|
|
13
|
+
from .preview import serve, write_preview
|
|
14
|
+
from .source.discovery import discover
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _common() -> argparse.ArgumentParser:
|
|
18
|
+
parser = argparse.ArgumentParser(add_help=False)
|
|
19
|
+
parser.add_argument("--wechat-root", type=Path, default=default_wechat_root(),
|
|
20
|
+
help="微信数据根目录(默认 Documents/WeChat Files)")
|
|
21
|
+
parser.add_argument("--account", help="账号 wxid,多个账号时必填")
|
|
22
|
+
parser.add_argument("--work-dir", type=Path, default=default_work_dir(),
|
|
23
|
+
help="工作目录(密钥缓存、明文库)")
|
|
24
|
+
parser.add_argument("--out", type=Path, default=default_export_dir(),
|
|
25
|
+
help="导出目录")
|
|
26
|
+
parser.add_argument("--key", dest="key_hex", help="手工指定 32/64 位 hex 密钥")
|
|
27
|
+
parser.add_argument("--db-dir", type=Path, help="已解密数据库目录(相对账号根结构)")
|
|
28
|
+
return parser
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
32
|
+
parser = argparse.ArgumentParser(
|
|
33
|
+
prog="wxexport", description="本地导出微信 3.x 聊天记录为 JSON"
|
|
34
|
+
)
|
|
35
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
36
|
+
|
|
37
|
+
sub.add_parser("detect", parents=[_common()], help="发现账号与数据库")
|
|
38
|
+
|
|
39
|
+
keys = sub.add_parser("keys", parents=[_common()], help="从微信进程内存获取密钥")
|
|
40
|
+
keys.add_argument("--no-persist", action="store_true", help="不写入 DPAPI 缓存")
|
|
41
|
+
|
|
42
|
+
sub.add_parser("decrypt", parents=[_common()], help="解密数据库到工作目录")
|
|
43
|
+
|
|
44
|
+
export = sub.add_parser("export", parents=[_common()], help="导出为 JSON/JSONL")
|
|
45
|
+
export.add_argument("--chat", help="只导出指定会话(名称 / wxid / 会话 ID)")
|
|
46
|
+
export.add_argument("--since", help="起始日期,如 2024-01-01")
|
|
47
|
+
export.add_argument("--no-media", action="store_true", help="不导出附件")
|
|
48
|
+
export.add_argument("--incremental", action="store_true", help="增量导出")
|
|
49
|
+
|
|
50
|
+
preview = sub.add_parser("preview", parents=[_common()], help="本地预览已导出数据")
|
|
51
|
+
preview.add_argument("--port", type=int, default=8765)
|
|
52
|
+
preview.add_argument("--no-browser", action="store_true")
|
|
53
|
+
return parser
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _settings(args) -> Settings:
|
|
57
|
+
return Settings(
|
|
58
|
+
wechat_root=args.wechat_root,
|
|
59
|
+
work_dir=args.work_dir,
|
|
60
|
+
export_dir=args.out,
|
|
61
|
+
account=args.account,
|
|
62
|
+
key_hex=args.key_hex,
|
|
63
|
+
db_dir=args.db_dir,
|
|
64
|
+
only_chat=getattr(args, "chat", None),
|
|
65
|
+
since=_parse_since(getattr(args, "since", None)),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _parse_since(value: str | None) -> int | None:
|
|
70
|
+
if not value:
|
|
71
|
+
return None
|
|
72
|
+
if value.isdigit():
|
|
73
|
+
return int(value)
|
|
74
|
+
try:
|
|
75
|
+
return int(datetime.fromisoformat(value).timestamp())
|
|
76
|
+
except ValueError:
|
|
77
|
+
raise SystemExit(f"无法解析日期:{value}")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def cmd_detect(args) -> int:
|
|
81
|
+
accounts = discover(args.wechat_root)
|
|
82
|
+
if not accounts:
|
|
83
|
+
print(f"未在 {args.wechat_root} 发现微信数据")
|
|
84
|
+
return 1
|
|
85
|
+
for account in accounts:
|
|
86
|
+
info = account.summary()
|
|
87
|
+
print(json.dumps(info, ensure_ascii=False, indent=2))
|
|
88
|
+
return 0
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def cmd_keys(args) -> int:
|
|
92
|
+
settings = _settings(args)
|
|
93
|
+
settings.ensure_dirs()
|
|
94
|
+
account = resolve_account(settings)
|
|
95
|
+
prepared = prepare(settings, persist_key=not args.no_persist)
|
|
96
|
+
print(json.dumps(
|
|
97
|
+
{"wxid": account.wxid, "profile": prepared.profile, "key": prepared.key_hex},
|
|
98
|
+
ensure_ascii=False, indent=2,
|
|
99
|
+
))
|
|
100
|
+
print("提示:密钥已写入 DPAPI 缓存,仅当前 Windows 用户可解")
|
|
101
|
+
prepared.schema.close()
|
|
102
|
+
return 0
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def cmd_decrypt(args) -> int:
|
|
106
|
+
settings = _settings(args)
|
|
107
|
+
prepared = prepare(settings)
|
|
108
|
+
print(f"明文库目录:{prepared.plain_root}")
|
|
109
|
+
prepared.schema.close()
|
|
110
|
+
return 0
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def cmd_export(args) -> int:
|
|
114
|
+
settings = _settings(args)
|
|
115
|
+
settings.ensure_dirs()
|
|
116
|
+
prepared = prepare(settings)
|
|
117
|
+
account = prepared.account
|
|
118
|
+
schema = prepared.schema
|
|
119
|
+
|
|
120
|
+
print("读取会话列表……")
|
|
121
|
+
conversations = schema.conversations(since=settings.since)
|
|
122
|
+
if settings.only_chat:
|
|
123
|
+
needle = settings.only_chat.lower()
|
|
124
|
+
conversations = [
|
|
125
|
+
c for c in conversations
|
|
126
|
+
if needle in {c.name.lower(), c.username.lower(), c.id.lower()}
|
|
127
|
+
]
|
|
128
|
+
if not conversations:
|
|
129
|
+
raise SystemExit(f"未找到会话:{settings.only_chat}")
|
|
130
|
+
print(f"待导出会话:{len(conversations)}")
|
|
131
|
+
|
|
132
|
+
resolver = MediaResolver(
|
|
133
|
+
account.root, settings.export_dir, schema, enabled=not args.no_media
|
|
134
|
+
)
|
|
135
|
+
exporter = Exporter(
|
|
136
|
+
account, schema, settings.export_dir, resolver,
|
|
137
|
+
settings.work_dir / "state.json", incremental=args.incremental,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
def progress(index: int, total: int, conv, count: int) -> None:
|
|
141
|
+
print(f"[{index}/{total}] {conv.name}:{count} 条")
|
|
142
|
+
|
|
143
|
+
manifest = exporter.run(conversations, progress=progress)
|
|
144
|
+
write_preview(settings.export_dir)
|
|
145
|
+
schema.close()
|
|
146
|
+
|
|
147
|
+
stats = manifest["stats"]
|
|
148
|
+
print(json.dumps({
|
|
149
|
+
"export_dir": str(settings.export_dir),
|
|
150
|
+
"conversations": stats["conversations"],
|
|
151
|
+
"messages": stats["messages"],
|
|
152
|
+
"attachments_ok": stats["attachments_ok"],
|
|
153
|
+
"attachments_unresolved": stats["attachments_unresolved"],
|
|
154
|
+
}, ensure_ascii=False, indent=2))
|
|
155
|
+
print("完成。运行 `wxexport preview` 打开本地预览。")
|
|
156
|
+
return 0
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def cmd_preview(args) -> int:
|
|
160
|
+
export_root = args.out
|
|
161
|
+
if not (export_root / "index.json").exists():
|
|
162
|
+
raise SystemExit(f"{export_root} 中没有导出数据,请先运行 export")
|
|
163
|
+
write_preview(export_root)
|
|
164
|
+
serve(export_root, port=args.port, open_browser=not args.no_browser)
|
|
165
|
+
return 0
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def main(argv: list[str] | None = None) -> int:
|
|
169
|
+
args = build_parser().parse_args(argv)
|
|
170
|
+
handlers = {
|
|
171
|
+
"detect": cmd_detect,
|
|
172
|
+
"keys": cmd_keys,
|
|
173
|
+
"decrypt": cmd_decrypt,
|
|
174
|
+
"export": cmd_export,
|
|
175
|
+
"preview": cmd_preview,
|
|
176
|
+
}
|
|
177
|
+
return handlers[args.command](args)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
if __name__ == "__main__":
|
|
181
|
+
sys.exit(main())
|
wxexport/config.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def default_wechat_root() -> Path:
|
|
9
|
+
"""微信 3.x 默认数据根目录。"""
|
|
10
|
+
return Path(os.environ.get("USERPROFILE", Path.home())) / "Documents" / "WeChat Files"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def default_work_dir() -> Path:
|
|
14
|
+
return Path.cwd() / ".wxexport"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def default_export_dir() -> Path:
|
|
18
|
+
return Path.cwd() / "export"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class Settings:
|
|
23
|
+
wechat_root: Path = field(default_factory=default_wechat_root)
|
|
24
|
+
work_dir: Path = field(default_factory=default_work_dir)
|
|
25
|
+
export_dir: Path = field(default_factory=default_export_dir)
|
|
26
|
+
account: str | None = None
|
|
27
|
+
key_hex: str | None = None
|
|
28
|
+
db_dir: Path | None = None
|
|
29
|
+
with_media: bool = True
|
|
30
|
+
only_chat: str | None = None
|
|
31
|
+
since: int | None = None
|
|
32
|
+
|
|
33
|
+
def ensure_dirs(self) -> None:
|
|
34
|
+
self.work_dir.mkdir(parents=True, exist_ok=True)
|
|
35
|
+
self.export_dir.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def plain_dir(self) -> Path:
|
|
39
|
+
"""解密后的明文库目录。"""
|
|
40
|
+
return self.work_dir / "plain"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from .profiles import (
|
|
2
|
+
PROFILES,
|
|
3
|
+
SQLCIPHER4,
|
|
4
|
+
WECHAT3,
|
|
5
|
+
CipherProfile,
|
|
6
|
+
DecryptError,
|
|
7
|
+
decrypt_database,
|
|
8
|
+
detect_profile,
|
|
9
|
+
parse_key,
|
|
10
|
+
probe,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"PROFILES",
|
|
15
|
+
"SQLCIPHER4",
|
|
16
|
+
"WECHAT3",
|
|
17
|
+
"CipherProfile",
|
|
18
|
+
"DecryptError",
|
|
19
|
+
"decrypt_database",
|
|
20
|
+
"detect_profile",
|
|
21
|
+
"parse_key",
|
|
22
|
+
"probe",
|
|
23
|
+
]
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"""SQLCipher 页级解密。
|
|
2
|
+
|
|
3
|
+
微信 3.x 使用 SQLCipher(page_size=4096,reserve=48,PBKDF2-HMAC-SHA1 64000 轮):
|
|
4
|
+
- 首页前 16 字节为 salt(明文),其后为密文;
|
|
5
|
+
- 每页末尾 48 字节 = 16 字节 IV + 20 字节 HMAC-SHA1 + 12 字节填充。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import hashlib
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from Crypto.Cipher import AES
|
|
15
|
+
from Crypto.Util.Padding import unpad
|
|
16
|
+
|
|
17
|
+
SQLITE_MAGIC = b"SQLite format 3\x00"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class CipherProfile:
|
|
22
|
+
name: str
|
|
23
|
+
page_size: int
|
|
24
|
+
reserve: int
|
|
25
|
+
kdf_iter: int
|
|
26
|
+
kdf_digest: str
|
|
27
|
+
mac_mode: str # "pbkdf2_2" | "split_kdf"
|
|
28
|
+
hmac_digest: str | None
|
|
29
|
+
hmac_size: int
|
|
30
|
+
|
|
31
|
+
def derive(self, passphrase: bytes, salt: bytes) -> tuple[bytes, bytes]:
|
|
32
|
+
if self.mac_mode == "split_kdf":
|
|
33
|
+
material = hashlib.pbkdf2_hmac(
|
|
34
|
+
self.kdf_digest, passphrase, salt, self.kdf_iter, 64
|
|
35
|
+
)
|
|
36
|
+
return material[:32], material[32:64]
|
|
37
|
+
|
|
38
|
+
enc_key = hashlib.pbkdf2_hmac(
|
|
39
|
+
self.kdf_digest, passphrase, salt, self.kdf_iter, 32
|
|
40
|
+
)
|
|
41
|
+
mac_salt = bytes(b ^ 0x3A for b in salt)
|
|
42
|
+
mac_key = hashlib.pbkdf2_hmac("sha1", enc_key, mac_salt, 2, 32)
|
|
43
|
+
return enc_key, mac_key
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def iv_offset(self) -> int:
|
|
47
|
+
return self.hmac_size + 12 if self.reserve == 48 else self.reserve - 16
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# 微信 3.x(SQLCipher 3 参数)
|
|
51
|
+
WECHAT3 = CipherProfile(
|
|
52
|
+
name="wechat3-sqlcipher3",
|
|
53
|
+
page_size=4096,
|
|
54
|
+
reserve=48,
|
|
55
|
+
kdf_iter=64000,
|
|
56
|
+
kdf_digest="sha1",
|
|
57
|
+
mac_mode="pbkdf2_2",
|
|
58
|
+
hmac_digest="sha1",
|
|
59
|
+
hmac_size=20,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# 标准 SQLCipher 4 参数,作为兜底
|
|
63
|
+
SQLCIPHER4 = CipherProfile(
|
|
64
|
+
name="sqlcipher4",
|
|
65
|
+
page_size=4096,
|
|
66
|
+
reserve=80,
|
|
67
|
+
kdf_iter=256000,
|
|
68
|
+
kdf_digest="sha512",
|
|
69
|
+
mac_mode="split_kdf",
|
|
70
|
+
hmac_digest="sha512",
|
|
71
|
+
hmac_size=64,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
PROFILES: tuple[CipherProfile, ...] = (WECHAT3, SQLCIPHER4)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class DecryptError(Exception):
|
|
78
|
+
pass
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def parse_key(key: str) -> bytes:
|
|
82
|
+
"""接受 32/64 位 hex 字符串,返回 PBKDF2 口令字节。"""
|
|
83
|
+
key = key.strip()
|
|
84
|
+
if key.startswith("x'") and key.endswith("'"):
|
|
85
|
+
key = key[2:-1]
|
|
86
|
+
try:
|
|
87
|
+
raw = bytes.fromhex(key)
|
|
88
|
+
except ValueError as exc:
|
|
89
|
+
raise DecryptError(f"密钥不是合法 hex:{key[:8]}...") from exc
|
|
90
|
+
if len(raw) not in (16, 32):
|
|
91
|
+
raise DecryptError(f"密钥长度应为 16 或 32 字节,实际 {len(raw)}")
|
|
92
|
+
return raw
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _split_cache(profile: CipherProfile, passphrase: bytes, salt: bytes):
|
|
96
|
+
return profile.derive(passphrase, salt)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def probe(passphrase: bytes, page: bytes, profile: CipherProfile) -> bool:
|
|
100
|
+
"""用首页密文验证密钥是否正确:校验 HMAC,或解密后出现 SQLite 头部字段。"""
|
|
101
|
+
if len(page) < profile.page_size:
|
|
102
|
+
return False
|
|
103
|
+
salt = page[:16]
|
|
104
|
+
try:
|
|
105
|
+
enc_key, mac_key = _split_cache(profile, passphrase, salt)
|
|
106
|
+
iv_start = profile.page_size - profile.reserve
|
|
107
|
+
iv = page[iv_start:iv_start + 16]
|
|
108
|
+
body = page[16:iv_start]
|
|
109
|
+
if profile.hmac_digest:
|
|
110
|
+
expect = _hmac(profile, mac_key, body, iv, 1)
|
|
111
|
+
got = page[iv_start + 16:iv_start + 16 + profile.hmac_size]
|
|
112
|
+
if expect == got:
|
|
113
|
+
return True
|
|
114
|
+
plain = AES.new(enc_key, AES.MODE_CBC, iv).decrypt(body)
|
|
115
|
+
except Exception:
|
|
116
|
+
return False
|
|
117
|
+
# 首页明文从 SQLite 头第 16 字节开始(前 16 字节被 salt 占据)
|
|
118
|
+
declared = int.from_bytes(plain[0:2], "big")
|
|
119
|
+
reserved = plain[4]
|
|
120
|
+
return (declared in {512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}
|
|
121
|
+
and reserved == profile.reserve)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def detect_profile(passphrase: bytes, db_path: Path,
|
|
125
|
+
profiles: tuple[CipherProfile, ...] = PROFILES) -> CipherProfile | None:
|
|
126
|
+
with open(db_path, "rb") as fh:
|
|
127
|
+
head = fh.read(4096)
|
|
128
|
+
for profile in profiles:
|
|
129
|
+
if probe(passphrase, head, profile):
|
|
130
|
+
return profile
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _hmac(profile: CipherProfile, mac_key: bytes, ciphertext: bytes,
|
|
135
|
+
iv: bytes, page_no: int) -> bytes:
|
|
136
|
+
"""SQLCipher 单页 MAC = HMAC(mac_key, 密文 || IV || 页码小端 4 字节)。"""
|
|
137
|
+
if not profile.hmac_digest:
|
|
138
|
+
return b""
|
|
139
|
+
import hmac as _hmac_mod
|
|
140
|
+
|
|
141
|
+
msg = ciphertext + iv + page_no.to_bytes(4, "little")
|
|
142
|
+
return _hmac_mod.new(mac_key, msg, profile.hmac_digest).digest()[:profile.hmac_size]
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def decrypt_database(src: Path, dst: Path, passphrase: bytes,
|
|
146
|
+
profile: CipherProfile, verify: bool = False) -> dict:
|
|
147
|
+
"""逐页解密为明文 SQLite;返回坏页等统计。"""
|
|
148
|
+
src_size = src.stat().st_size
|
|
149
|
+
page_size = profile.page_size
|
|
150
|
+
salt = b""
|
|
151
|
+
enc_key = mac_key = b""
|
|
152
|
+
bad_pages: list[int] = []
|
|
153
|
+
written = 0
|
|
154
|
+
|
|
155
|
+
dst.parent.mkdir(parents=True, exist_ok=True)
|
|
156
|
+
with open(src, "rb") as fin, open(dst, "wb") as fout:
|
|
157
|
+
first = fin.read(page_size)
|
|
158
|
+
if len(first) < page_size:
|
|
159
|
+
raise DecryptError(f"{src.name} 过小,不是有效数据库")
|
|
160
|
+
salt = first[:16]
|
|
161
|
+
enc_key, mac_key = profile.derive(passphrase, salt)
|
|
162
|
+
|
|
163
|
+
page_no = 0
|
|
164
|
+
page = first
|
|
165
|
+
while page:
|
|
166
|
+
size = len(page)
|
|
167
|
+
if size < profile.reserve + 32:
|
|
168
|
+
bad_pages.append(page_no)
|
|
169
|
+
break
|
|
170
|
+
start = 16 if page_no == 0 else 0
|
|
171
|
+
body = page[start:size - profile.reserve]
|
|
172
|
+
iv = page[size - profile.reserve:size - profile.reserve + 16]
|
|
173
|
+
try:
|
|
174
|
+
plain = AES.new(enc_key, AES.MODE_CBC, iv).decrypt(body)
|
|
175
|
+
if verify and profile.hmac_digest:
|
|
176
|
+
expect = _hmac(profile, mac_key, body, iv, page_no + 1)
|
|
177
|
+
off = size - profile.reserve + 16
|
|
178
|
+
got = page[off:off + profile.hmac_size]
|
|
179
|
+
if expect != got:
|
|
180
|
+
raise DecryptError("hmac mismatch")
|
|
181
|
+
plain = _restore_page(plain, page_no, profile, size)
|
|
182
|
+
fout.write(plain)
|
|
183
|
+
written += len(plain)
|
|
184
|
+
except Exception:
|
|
185
|
+
bad_pages.append(page_no)
|
|
186
|
+
fout.write(b"\x00" * page_size)
|
|
187
|
+
written += page_size
|
|
188
|
+
page_no += 1
|
|
189
|
+
page = fin.read(page_size)
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
"db": src.name,
|
|
193
|
+
"pages": page_no,
|
|
194
|
+
"bytes": written,
|
|
195
|
+
"bad_pages": bad_pages,
|
|
196
|
+
"src_bytes": src_size,
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _restore_page(plain: bytes, page_no: int, profile: CipherProfile, size: int) -> bytes:
|
|
201
|
+
"""把解密出的片段还原成 SQLite 期望的整页长度。
|
|
202
|
+
|
|
203
|
+
首页明文只从文件头第 16 字节开始(前 16 字节是 salt),需要补回 magic;
|
|
204
|
+
每页末尾的 reserve 区域 SQLite 不解释,补零即可保持页长一致。
|
|
205
|
+
"""
|
|
206
|
+
if page_no == 0:
|
|
207
|
+
return SQLITE_MAGIC + plain + b"\x00" * profile.reserve
|
|
208
|
+
return plain + b"\x00" * profile.reserve
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def unpad_or_none(data: bytes) -> bytes | None:
|
|
212
|
+
try:
|
|
213
|
+
return unpad(data, 16)
|
|
214
|
+
except ValueError:
|
|
215
|
+
return None
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import shutil
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .. import SCHEMA_VERSION
|
|
9
|
+
from ..media.resolver import MediaResolver
|
|
10
|
+
from ..model.entities import Conversation
|
|
11
|
+
from ..model.normalizer import normalize
|
|
12
|
+
from ..schema.wechat3 import Wechat3Schema
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Exporter:
|
|
16
|
+
def __init__(self, account, schema: Wechat3Schema, export_root: Path,
|
|
17
|
+
resolver: MediaResolver, state_path: Path,
|
|
18
|
+
incremental: bool = False):
|
|
19
|
+
self.account = account
|
|
20
|
+
self.schema = schema
|
|
21
|
+
self.export_root = export_root
|
|
22
|
+
self.resolver = resolver
|
|
23
|
+
self.state_path = state_path
|
|
24
|
+
self.incremental = incremental
|
|
25
|
+
self.state = self._load_state()
|
|
26
|
+
self.contacts = self.schema.contacts()
|
|
27
|
+
self.stats = {
|
|
28
|
+
"conversations": 0,
|
|
29
|
+
"messages": 0,
|
|
30
|
+
"attachments_ok": 0,
|
|
31
|
+
"attachments_unresolved": 0,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def _load_state(self) -> dict:
|
|
35
|
+
if self.state_path.exists():
|
|
36
|
+
try:
|
|
37
|
+
return json.loads(self.state_path.read_text("utf-8"))
|
|
38
|
+
except Exception:
|
|
39
|
+
return {}
|
|
40
|
+
return {"conversations": {}}
|
|
41
|
+
|
|
42
|
+
def _save_state(self) -> None:
|
|
43
|
+
self.state_path.parent.mkdir(parents=True, exist_ok=True)
|
|
44
|
+
self.state_path.write_text(
|
|
45
|
+
json.dumps(self.state, ensure_ascii=False, indent=2), "utf-8"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def run(self, conversations: list[Conversation], progress=None) -> dict:
|
|
49
|
+
conv_root = self.export_root / "conversations"
|
|
50
|
+
if not self.incremental and conv_root.exists():
|
|
51
|
+
shutil.rmtree(conv_root)
|
|
52
|
+
conv_root.mkdir(parents=True, exist_ok=True)
|
|
53
|
+
|
|
54
|
+
emitted: list[Conversation] = []
|
|
55
|
+
total = len(conversations)
|
|
56
|
+
for index, conv in enumerate(conversations, 1):
|
|
57
|
+
count = self._export_conversation(conv)
|
|
58
|
+
conv.message_count = count
|
|
59
|
+
emitted.append(conv)
|
|
60
|
+
self.stats["conversations"] += 1
|
|
61
|
+
if progress:
|
|
62
|
+
progress(index, total, conv, count)
|
|
63
|
+
|
|
64
|
+
self._write_index(emitted)
|
|
65
|
+
self._save_state()
|
|
66
|
+
return self._write_manifest(emitted)
|
|
67
|
+
|
|
68
|
+
def _export_conversation(self, conv: Conversation) -> int:
|
|
69
|
+
conv_dir = self.export_root / "conversations" / conv.id
|
|
70
|
+
conv_dir.mkdir(parents=True, exist_ok=True)
|
|
71
|
+
media_dir = conv_dir / "media"
|
|
72
|
+
messages_path = conv_dir / "messages.jsonl"
|
|
73
|
+
|
|
74
|
+
prev = self.state.get("conversations", {}).get(conv.id, {})
|
|
75
|
+
since = prev.get("last_time") if self.incremental else None
|
|
76
|
+
seq = prev.get("count", 0) if self.incremental and messages_path.exists() else 0
|
|
77
|
+
mode = "a" if self.incremental and messages_path.exists() else "w"
|
|
78
|
+
|
|
79
|
+
contacts = self.contacts
|
|
80
|
+
count = 0
|
|
81
|
+
first_time = None
|
|
82
|
+
last_time = None
|
|
83
|
+
with open(messages_path, mode, encoding="utf-8") as fh:
|
|
84
|
+
for raw in self.schema.iter_messages(conv.username, since):
|
|
85
|
+
seq += 1
|
|
86
|
+
if first_time is None:
|
|
87
|
+
first_time = raw.create_time
|
|
88
|
+
last_time = raw.create_time
|
|
89
|
+
message = normalize(raw, conv, contacts, self.account.wxid, seq)
|
|
90
|
+
self.resolver.resolve(message, raw, media_dir)
|
|
91
|
+
fh.write(json.dumps(message.to_json(), ensure_ascii=False) + "\n")
|
|
92
|
+
count += 1
|
|
93
|
+
|
|
94
|
+
total = seq if self.incremental else count
|
|
95
|
+
self.stats["messages"] += count
|
|
96
|
+
self.state.setdefault("conversations", {})[conv.id] = {
|
|
97
|
+
"username": conv.username,
|
|
98
|
+
"count": total,
|
|
99
|
+
"last_time": last_time or prev.get("last_time"),
|
|
100
|
+
"name": conv.name,
|
|
101
|
+
}
|
|
102
|
+
meta = conv.to_json()
|
|
103
|
+
meta["message_count"] = total
|
|
104
|
+
meta["first_time"] = first_time or conv.first_time
|
|
105
|
+
meta["last_time"] = last_time or conv.last_time
|
|
106
|
+
meta["members"] = (
|
|
107
|
+
[self.schema.contact_name(u) for u in contacts[conv.username].members]
|
|
108
|
+
if conv.username in contacts and contacts[conv.username].is_group
|
|
109
|
+
else []
|
|
110
|
+
)
|
|
111
|
+
(conv_dir / "conversation.json").write_text(
|
|
112
|
+
json.dumps(meta, ensure_ascii=False, indent=2), "utf-8"
|
|
113
|
+
)
|
|
114
|
+
return total
|
|
115
|
+
|
|
116
|
+
def _write_index(self, conversations: list[Conversation]) -> None:
|
|
117
|
+
items = [c.to_json() for c in conversations]
|
|
118
|
+
items.sort(key=lambda c: c["last_time"] or 0, reverse=True)
|
|
119
|
+
(self.export_root / "index.json").write_text(
|
|
120
|
+
json.dumps(
|
|
121
|
+
{"schema": SCHEMA_VERSION, "conversations": items},
|
|
122
|
+
ensure_ascii=False,
|
|
123
|
+
indent=2,
|
|
124
|
+
),
|
|
125
|
+
"utf-8",
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
def _write_manifest(self, conversations: list[Conversation]) -> dict:
|
|
129
|
+
manifest = {
|
|
130
|
+
"schema": SCHEMA_VERSION,
|
|
131
|
+
"generated_at": datetime.now().astimezone().isoformat(timespec="seconds"),
|
|
132
|
+
"account": {
|
|
133
|
+
"wxid": self.account.wxid,
|
|
134
|
+
"client_version": self.account.client_version,
|
|
135
|
+
"data_root": str(self.account.root),
|
|
136
|
+
},
|
|
137
|
+
"stats": {
|
|
138
|
+
**self.stats,
|
|
139
|
+
"attachments_ok": self.resolver.stats["saved"],
|
|
140
|
+
"attachments_unresolved": self.resolver.stats["unresolved"],
|
|
141
|
+
},
|
|
142
|
+
"conversations": [
|
|
143
|
+
{
|
|
144
|
+
"id": c.id,
|
|
145
|
+
"name": c.name,
|
|
146
|
+
"username": c.username,
|
|
147
|
+
"count": c.message_count,
|
|
148
|
+
}
|
|
149
|
+
for c in conversations
|
|
150
|
+
],
|
|
151
|
+
}
|
|
152
|
+
(self.export_root / "manifest.json").write_text(
|
|
153
|
+
json.dumps(manifest, ensure_ascii=False, indent=2), "utf-8"
|
|
154
|
+
)
|
|
155
|
+
return manifest
|
wxexport/key/__init__.py
ADDED