Repository navigation
Expand file tree
/
Copy pathpak_engine.py
More file actions
928 lines (808 loc) · 35.6 KB
/
Copy pathpak_engine.py
File metadata and controls
928 lines (808 loc) · 35.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
"""
PAK ULTRA MASTER PRO - CORE ENGINE
High-Performance Unreal Engine & Tencent .PAK Format Handler
Supports: Unpack, In-Place Repack, Full Rebuild Repack, Custom Path Injection, Game-Native SM4 Protection
Optimized for Multi-threading, Vectorized Crypto, and Future Game Versions.
"""
import os
import sys
import math
import struct
import shutil
import zlib
import copy
import colorsys
import random
import tempfile
import concurrent.futures
from dataclasses import dataclass
from functools import lru_cache
from pathlib import PurePath, Path
from typing import List, Dict, Tuple, Optional, Any, Set
import itertools as it
# Third-party libraries
import gmalg
from Crypto.Cipher import AES
from Crypto.Cipher.AES import MODE_CBC
from Crypto.Hash import SHA1
from Crypto.Util.Padding import unpad
from zstandard import ZstdDecompressor, ZstdCompressionDict, DICT_TYPE_AUTO, ZstdCompressor
# ==================== CRYPTO CONSTANTS & KEYS ====================
ZUC_KEY = bytes.fromhex('01010101010101010101010101010101')
ZUC_IV = bytes.fromhex('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
RSA_MOD_1 = bytes.fromhex(
'CBE8B9F2504050EF9831B719E9A6249A6D238505ADE909BDE78C180DED6072A0C3347B8AF4780E1F212D952D82D4BF7F233C1ECA'
'499E1F9D9A85B4FAD759F54BABC1666C5DE411EA9E4B2374425DD6C6F54333BBC8F2610FE6063E4D0D6C21A671A8F7C3740555E5'
'DC06D4E1691C456DB4116C0C012BF7B206E8311AAAEC689952BF804EF638F09D5822B4117B114208F14DEB459E80CB770E5B0D79'
'78E21F5E6CED4999D3583108221A7AB28B960277ADB5690A332784019D9C195BE4EA9EA0A09459010F236465DE0D59C3EF7324E9'
'54E1118D93EE19F299760C2CDB963CE87973EA5ECC9BBE81C27D4C7C8572AC07E9BCEAC9BD72AB7A56A3C0AD736ABCE4'
)
RSA_MOD_2 = bytes.fromhex(
'7F58E8A39A4DA4E87357DDD650EAA16D3B5CE95B213D1030A662566444796A78A84AE9AC3DBFFDE7F41094896696835DAF13B89E'
'6EC2B84963B1B1BAF7151DA245C3FBFAE2A6AE18B2684D03F9229DE2C91440F2A3A3BCDE1E5680C16722A88039C73560D5D43F4B'
'6562C2EEA5B1D926D86B51108A2643C70FB74D6442CE3A08339B8FD8F660AE88129B7AB8C46F2FA58124485CCCB1E987B05A6DA6'
'5A01858ED3F89905449AE42BB07290FCB9994BF22E26610BCABB9804783A3B9587917F3D97316EDDA15C5E13F79066407B55A93B'
'291B68A4AC42A98D6E35FED84B14A792D154E62028DDAD20FC301951E5924BE9AD62FB719DD94CC30CAB871BEC4377A8'
)
SIMPLE1_DECRYPT_KEY = 121
SIMPLE2_DECRYPT_KEY = bytes.fromhex('E55B4ED1')
SIMPLE2_BLOCK_SIZE = 16
SM4_SECRET_4 = 'eb691efea914241317a8'
SM4_SECRET_2 = 'Q0hVTKey$as*1ZFlQCiA'
SM4_SECRET_NEW = [
'xG2qW5lP7lV2iN5fN5pG',
'xT1cJ6dL5wC0kK1rB4dK',
'qC4jS5bZ6fL5xE6nD4zA',
'gD4jQ2aL3bS3lC3xT0iW',
'xU1yQ8wE9zY3gZ3bT5aE',
'gW1fR0jK6wQ4oN0oK1kZ',
'uQ3cO2dX7xY4xU7gH7iS',
'aJ4pV7iZ7pU4wP2aC2cZ',
'cX6jT3cM2oT3vK0kJ1qN',
'iT2vS0cS6yT6cZ1sE1lO',
'hM1pH9iY8wM9hT4lN5uJ',
'kG6bC8jK0fL0dE4sH4mL',
'dB6lB3vE0eZ8wM8rI0aC',
'tP7sP7nI9rA2vQ4cV5yQ',
'aT0cL1yN4pT3sZ7eM2vY',
'uV6fU8fC9zN3mP5dH8mN',
'jU5bH7IQ0fM9hK2kl0oF',
'WJyb2tlci13cy0xMzE0M',
'iQ0eM0mJ7uT0kV6kL5zY',
'wD2rP3lP9xF4mE1eC5jS',
'9ydE1vbml0b3JMb2cmbG',
'fO3kW1fE6eD0pU1kY7xK',
]
# Encryption Methods
EM_NONE = 0
EM_SIMPLE1 = 1
EM_SM4_2 = 2
EM_SM4_4 = 4
EM_SIMPLE2 = 16
EM_UNKNOWN_17 = 17
EM_SM4_NEW_BASE = 31
EM_SM4_NEW_MASK = ~EM_SM4_NEW_BASE
# Compression Methods
CM_NONE = 0
CM_ZLIB = 1
CM_ZSTD = 6
CM_ZSTD_DICT = 8
CM_MASK = 15
# ==================== HIGH-PERFORMANCE SM4 ====================
class SM4Engine:
"""Optimized SM4 Cipher with pre-computed lookup tables."""
_S_BOX = bytes([
52, 102, 37, 116, 137, 120, 228, 169, 90, 65, 188, 122, 214, 22, 33, 35,
77, 97, 218, 148, 155, 223, 19, 60, 105, 58, 49, 10, 95, 215, 153, 149,
241, 174, 114, 61, 7, 96, 36, 182, 152, 238, 196, 162, 45, 136, 221, 141,
4, 234, 187, 17, 202, 62, 93, 161, 246, 63, 176, 151, 128, 71, 43, 166,
230, 247, 217, 177, 89, 192, 124, 190, 84, 40, 183, 126, 79, 248, 67, 110,
160, 80, 14, 245, 144, 184, 251, 163, 123, 98, 25, 70, 3, 42, 185, 143,
159, 119, 180, 91, 131, 135, 8, 235, 226, 30, 66, 240, 15, 232, 113, 106,
117, 173, 85, 31, 181, 171, 51, 250, 127, 21, 189, 133, 216, 6, 104, 179,
82, 48, 72, 11, 0, 237, 239, 178, 87, 142, 231, 108, 213, 229, 46, 83,
130, 5, 249, 129, 244, 86, 191, 140, 75, 227, 219, 74, 145, 76, 44, 211,
64, 41, 78, 32, 20, 54, 121, 9, 111, 209, 55, 224, 57, 12, 138, 146,
56, 18, 53, 109, 225, 253, 147, 154, 23, 212, 201, 156, 107, 132, 38, 157,
175, 118, 193, 158, 208, 150, 197, 203, 233, 115, 73, 210, 205, 100, 195, 199,
1, 125, 243, 172, 252, 222, 164, 68, 50, 27, 194, 186, 28, 2, 198, 39,
69, 139, 242, 24, 167, 16, 81, 29, 200, 207, 99, 255, 47, 13, 88, 206,
101, 165, 220, 26, 59, 134, 254, 34, 92, 168, 94, 103, 170, 236, 112, 204
])
_FK = [1184304796, 1270900830, 1493524870, 3164752158]
_CK = [
964907, 973793155, 2654690407, 2916866751, 2071233739, 1226140771, 3348805095, 2045549823,
388349611, 800627875, 612403927, 3721562911, 1195432523, 3150178931, 612053223, 2445162591,
67183755, 1174197155, 1393249511, 3331183455, 3822152747, 1332317203, 1804781383, 1990130463,
1282653851, 3376591251, 2910902311, 925872959, 332098219, 735840931, 396665415, 3588844719
]
_T_TABLES = None
@staticmethod
def _rol32(x, n):
return ((x << n) & 0xFFFFFFFF) | (x >> (32 - n))
@staticmethod
def _t1(x):
b0 = SM4Engine._S_BOX[x >> 24 & 255]
b1 = SM4Engine._S_BOX[x >> 16 & 255]
b2 = SM4Engine._S_BOX[x >> 8 & 255]
b3 = SM4Engine._S_BOX[x & 255]
v = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3
return v ^ SM4Engine._rol32(v, 13) ^ SM4Engine._rol32(v, 23)
@classmethod
def _init_tables(cls):
if cls._T_TABLES is not None:
return
def L(y):
return y ^ cls._rol32(y, 2) ^ cls._rol32(y, 10) ^ cls._rol32(y, 18) ^ cls._rol32(y, 24)
t0, t1, t2, t3 = [0] * 256, [0] * 256, [0] * 256, [0] * 256
for b in range(256):
s = cls._S_BOX[b]
t0[b] = L(s << 24)
t1[b] = L(s << 16)
t2[b] = L(s << 8)
t3[b] = L(s)
cls._T_TABLES = (t0, t1, t2, t3)
def __init__(self, key: bytes):
if len(key) != 16:
raise ValueError('SM4 key must be 16 bytes')
SM4Engine._init_tables()
self._rkey = [0] * 32
k0 = int.from_bytes(key[0:4], 'big') ^ self._FK[0]
k1 = int.from_bytes(key[4:8], 'big') ^ self._FK[1]
k2 = int.from_bytes(key[8:12], 'big') ^ self._FK[2]
k3 = int.from_bytes(key[12:16], 'big') ^ self._FK[3]
for i in range(0, 32, 4):
k0 ^= self._t1(k1 ^ k2 ^ k3 ^ self._CK[i])
self._rkey[i] = k0
k1 ^= self._t1(k2 ^ k3 ^ k0 ^ self._CK[i + 1])
self._rkey[i + 1] = k1
k2 ^= self._t1(k3 ^ k0 ^ k1 ^ self._CK[i + 2])
self._rkey[i + 2] = k2
k3 ^= self._t1(k0 ^ k1 ^ k2 ^ self._CK[i + 3])
self._rkey[i + 3] = k3
def _crypt_bulk(self, data: bytes, rkeys: List[int]) -> bytes:
t0, t1, t2, t3 = self._T_TABLES
num_blocks = len(data) // 16
words = list(struct.unpack(f'>{num_blocks * 4}I', data))
for b in range(num_blocks):
base = b * 4
x0, x1, x2, x3 = words[base], words[base + 1], words[base + 2], words[base + 3]
for rk in rkeys:
y = x1 ^ x2 ^ x3 ^ rk
x0 ^= t0[y >> 24 & 255] ^ t1[y >> 16 & 255] ^ t2[y >> 8 & 255] ^ t3[y & 255]
x0, x1, x2, x3 = x1, x2, x3, x0
words[base], words[base + 1], words[base + 2], words[base + 3] = x3, x2, x1, x0
return struct.pack(f'>{num_blocks * 4}I', *words)
def encrypt_bulk(self, data: bytes) -> bytes:
if len(data) % 16 != 0:
pad_len = 16 - (len(data) % 16)
data += b'\x00' * pad_len
return self._crypt_bulk(data, self._rkey)
def decrypt_bulk(self, data: bytes) -> bytes:
if len(data) % 16 != 0:
raise ValueError("Data length must be multiple of 16")
return self._crypt_bulk(data, self._rkey[::-1])
# ==================== UTILITIES & CRYPTO ADAPTERS ====================
class Misc:
@staticmethod
def pad_to_n(data: bytes, n: int) -> bytes:
if n <= 0: return data
padding = n - (len(data) % n)
return data if padding == n else data + b'\x00' * padding
@staticmethod
def align_up(x: int, n: int) -> int:
return (x + n - 1) // n * n
class Reader:
"""Fast binary buffer reader."""
__slots__ = ('_buffer', '_cursor')
def __init__(self, buffer, cursor=0):
self._buffer = buffer
self._cursor = cursor
def u1(self, move=True) -> int:
v = self._buffer[self._cursor]
if move: self._cursor += 1
return v
def u4(self, move=True) -> int:
v = struct.unpack_from('<I', self._buffer, self._cursor)[0]
if move: self._cursor += 4
return v
def u8(self, move=True) -> int:
v = struct.unpack_from('<Q', self._buffer, self._cursor)[0]
if move: self._cursor += 8
return v
def i4(self, move=True) -> int:
v = struct.unpack_from('<i', self._buffer, self._cursor)[0]
if move: self._cursor += 4
return v
def s(self, n: int, move=True) -> bytes:
v = bytes(self._buffer[self._cursor:self._cursor + n])
if move: self._cursor += n
return v
def string(self, move=True) -> str:
length = self.i4(move=move)
if length == 0:
return ""
v = bytes(self._buffer[self._cursor:self._cursor + length]).rstrip(b'\x00').decode('utf-8', errors='ignore')
if move: self._cursor += length
return v
class PakCrypto:
class _LCG:
def __init__(self, seed: int):
self.state = seed
def next(self) -> int:
MASK_32 = 4294967295
MSB_1 = 2147483648
def wrap(x: int) -> int:
x &= MASK_32
return x if not (x & MSB_1) else (x + MSB_1 & MASK_32) - MSB_1
x1 = wrap(1103515245 * self.state)
self.state = wrap(x1 + 12345)
x2 = wrap(x1 + 77880) if self.state < 0 else self.state
return (x2 >> 16 & MASK_32) % 32767
@staticmethod
def zuc_keystream() -> List[int]:
zuc = gmalg.ZUC(ZUC_KEY, ZUC_IV)
return [struct.unpack('>I', zuc.generate())[0] for _ in range(16)]
@staticmethod
def _xorxor(buffer, x) -> bytes:
lx = len(x)
return bytes(b ^ x[i % lx] for i, b in enumerate(buffer))
@staticmethod
def _hashhash(buffer, n: int) -> bytes:
result = bytearray()
needed = math.ceil(n / SHA1.digest_size)
for _ in range(needed):
result += SHA1.new(buffer).digest()
return bytes(result[:n].ljust(n, b'\x00'))
@staticmethod
def _meowmeow(buffer) -> bytes:
if len(buffer) < 43:
return bytes()
x1 = buffer[1:1 + SHA1.digest_size]
x2 = buffer[SHA1.digest_size + 1:]
x1 = PakCrypto._xorxor(x1, PakCrypto._hashhash(x2, len(x1)))
x2 = PakCrypto._xorxor(x2, PakCrypto._hashhash(x1, len(x2)))
part1, m = x2[:SHA1.digest_size], x2[SHA1.digest_size:]
if part1 != SHA1.new(b'\x00' * SHA1.digest_size).digest():
return bytes()
skip = 1 + next((i for i, b in enumerate(m) if b != 0), 0)
return m[skip:]
@staticmethod
def rsa_extract(signature: bytes, modulus: bytes) -> bytes:
c = int.from_bytes(signature, 'little')
n = int.from_bytes(modulus, 'little')
m = pow(c, 65537, n).to_bytes(256, 'little').rstrip(b'\x00')
return PakCrypto._meowmeow(Misc.pad_to_n(m, 4))
@staticmethod
def _decrypt_simple1(ciphertext: bytes) -> bytes:
return bytes(x ^ SIMPLE1_DECRYPT_KEY for x in ciphertext)
@staticmethod
def _decrypt_simple2(ciphertext: bytes) -> bytes:
if len(ciphertext) % SIMPLE2_BLOCK_SIZE != 0:
return ciphertext
initial_key, = struct.unpack('<I', SIMPLE2_DECRYPT_KEY)
rolling = initial_key
out = []
for x, in struct.iter_unpack('<I', ciphertext):
rolling ^= x
out.append(rolling)
return struct.pack(f'<{len(out)}I', *out)
@staticmethod
@lru_cache(maxsize=8192)
def derive_sm4_key(file_path: PurePath, encryption_method: int) -> bytes:
part1 = file_path.stem.lower()
if encryption_method == EM_SM4_2:
secret = SM4_SECRET_2
elif encryption_method == EM_SM4_4:
secret = SM4_SECRET_4
elif encryption_method == EM_UNKNOWN_17:
idx = (encryption_method - EM_SM4_NEW_BASE) % len(SM4_SECRET_NEW)
secret = SM4_SECRET_NEW[idx]
else:
idx = (encryption_method - EM_SM4_NEW_BASE) % len(SM4_SECRET_NEW)
secret = f'{SM4_SECRET_NEW[idx]}{encryption_method}'
return SHA1.new(f'{part1}{secret}'.encode('utf-8')).digest()[:16]
@staticmethod
@lru_cache(maxsize=2048)
def get_sm4_cipher(key: bytes) -> SM4Engine:
return SM4Engine(key)
@staticmethod
def decrypt_sm4(ciphertext: bytes, file_path: PurePath, encryption_method: int) -> bytes:
if len(ciphertext) % 16 != 0:
return ciphertext
key = PakCrypto.derive_sm4_key(file_path, encryption_method)
cipher = PakCrypto.get_sm4_cipher(key)
return cipher.decrypt_bulk(ciphertext)
@staticmethod
def encrypt_sm4(plaintext: bytes, file_path: PurePath, encryption_method: int) -> bytes:
key = PakCrypto.derive_sm4_key(file_path, encryption_method)
cipher = PakCrypto.get_sm4_cipher(key)
return cipher.encrypt_bulk(plaintext)
@staticmethod
def decrypt_index(ciphertext: bytes, pak_info) -> bytes:
if pak_info.version > 7:
key = PakCrypto.rsa_extract(pak_info.packed_key, RSA_MOD_1)
iv = PakCrypto.rsa_extract(pak_info.packed_iv, RSA_MOD_1)
if len(key) == 32 and len(iv) == 32:
aes = AES.new(key, MODE_CBC, iv[:16])
return unpad(aes.decrypt(ciphertext), AES.block_size)
return PakCrypto._decrypt_simple1(ciphertext)
@staticmethod
def is_sm4(em: int) -> bool:
return em == EM_SM4_2 or em == EM_SM4_4 or em == EM_UNKNOWN_17 or (em & EM_SM4_NEW_MASK) != 0
@staticmethod
def align_encrypted_size(n: int, em: int) -> int:
if em == EM_SIMPLE2:
return Misc.align_up(n, SIMPLE2_BLOCK_SIZE)
elif PakCrypto.is_sm4(em):
return Misc.align_up(n, 16)
return n
@staticmethod
def decrypt_block(ciphertext: bytes, file_path: PurePath, em: int) -> bytes:
if em == EM_SIMPLE1:
return PakCrypto._decrypt_simple1(ciphertext)
elif em == EM_SIMPLE2:
return PakCrypto._decrypt_simple2(ciphertext)
elif PakCrypto.is_sm4(em):
return PakCrypto.decrypt_sm4(ciphertext, file_path, em)
return ciphertext
@staticmethod
def encrypt_block(plaintext: bytes, file_path: PurePath, em: int) -> bytes:
if em == EM_SIMPLE1:
return bytes(x ^ SIMPLE1_DECRYPT_KEY for x in plaintext)
elif em == EM_SIMPLE2:
pad = -len(plaintext) % SIMPLE2_BLOCK_SIZE
if pad: plaintext += b'\x00' * pad
key, = struct.unpack('<I', SIMPLE2_DECRYPT_KEY)
rolling = key
out = []
for x, in struct.iter_unpack('<I', plaintext):
c = rolling ^ x
out.append(c)
rolling ^= c
return struct.pack(f'<{len(out)}I', *out)
elif PakCrypto.is_sm4(em):
return PakCrypto.encrypt_sm4(plaintext, file_path, em)
return plaintext
@staticmethod
@lru_cache(maxsize=128)
def generate_block_indices(n: int, em: int) -> List[int]:
if not PakCrypto.is_sm4(em) or n <= 1:
return list(range(n))
permutation = []
lcg = PakCrypto._LCG(n)
while len(permutation) != n:
x = lcg.next() % n
if x not in permutation:
permutation.append(x)
inverse = [0] * len(permutation)
for i, x in enumerate(permutation):
inverse[x] = i
return inverse
# ==================== DATA STRUCTURES ====================
class PakCompressedBlock:
__slots__ = ('start', 'end')
def __init__(self, start: int = 0, end: int = 0):
self.start = start
self.end = end
@dataclass
class TencentPakEntry:
content_hash: bytes
offset: int
uncompressed_size: int
compression_method: int
size: int
unk1: int
unk2: bytes
compressed_blocks: List[PakCompressedBlock]
compression_block_size: int
encrypted: bool
encryption_method: int
index_new_sep: int
@classmethod
def read_from(cls, reader: Reader, version: int):
c_hash = reader.s(20)
if version <= 1:
reader.u8()
offset = reader.u8()
uncompressed_size = reader.u8()
compression_method = reader.u4() & CM_MASK
size = reader.u8()
unk1 = reader.u1() if version >= 5 else 0
unk2 = reader.s(20) if version >= 5 else bytes()
blocks = []
if compression_method != 0 and version >= 3:
num_blocks = reader.u4()
for _ in range(num_blocks):
blocks.append(PakCompressedBlock(reader.u8(), reader.u8()))
compression_block_size = reader.u4() if version >= 4 else 0
encrypted = (reader.u1() == 1) if version >= 4 else False
encryption_method = reader.u4() if version >= 12 else 0
index_new_sep = reader.u4() if version >= 12 else 0
return cls(
content_hash=c_hash,
offset=offset,
uncompressed_size=uncompressed_size,
compression_method=compression_method,
size=size,
unk1=unk1,
unk2=unk2,
compressed_blocks=blocks,
compression_block_size=compression_block_size,
encrypted=encrypted,
encryption_method=encryption_method,
index_new_sep=index_new_sep
)
class TencentPakInfo:
def __init__(self, buffer, keystream: List[int]):
ks = keystream
reader = Reader(buffer[-TencentPakInfo.calc_mem_size(12):])
# Parse basic PakInfo fields from the tail
raw_tail = buffer[-45:]
r_tail = Reader(raw_tail)
self.index_encrypted = ((r_tail.u1() ^ ks[3]) & 0xFF) == 1
self.magic = r_tail.u4() ^ ks[2]
self.version = r_tail.u4()
self.index_hash = bytes(a ^ b for a, b in zip(r_tail.s(20), struct.pack('<5I', *ks[4:9]))) if self.version >= 6 else bytes()
self.index_size = r_tail.u8() ^ (ks[10] << 32 | ks[11])
self.index_offset = r_tail.u8() ^ (ks[0] << 32 | ks[1])
# Parse Tencent extended fields
mem_sz = TencentPakInfo.calc_mem_size(self.version)
r_ext = Reader(buffer[-mem_sz:])
self.unk1 = bytes(a ^ b for a, b in zip(r_ext.s(32), struct.pack('<8I', *ks[7:15]))) if self.version >= 7 else bytes()
self.packed_key = r_ext.s(256) if self.version >= 8 else bytes()
self.packed_iv = r_ext.s(256) if self.version >= 8 else bytes()
self.packed_index_hash = r_ext.s(256) if self.version >= 8 else bytes()
self.stem_hash = (r_ext.u4() ^ ks[8]) if self.version >= 9 else 0
self.unk2 = (r_ext.u4() ^ ks[9]) if self.version >= 9 else 0
self.content_org_hash = r_ext.s(20) if self.version >= 12 else bytes()
@staticmethod
def calc_mem_size(version: int) -> int:
sz = 45
if version >= 7: sz += 32
if version >= 8: sz += 768
if version >= 9: sz += 8
if version >= 12: sz += 20
return sz
# ==================== MAIN PAK FILE CLASS ====================
class TencentPakFile:
"""Complete Tencent Unreal Pak Reader, Injector, and Protector."""
def __init__(self, file_path: Path):
self.file_path = Path(file_path)
with open(self.file_path, 'rb') as f:
self.file_content = memoryview(f.read())
self.keystream = PakCrypto.zuc_keystream()
self.pak_info = TencentPakInfo(self.file_content, self.keystream)
self.mount_point = PurePath()
self.mount_point_raw = ""
self.files: List[TencentPakEntry] = []
self.index: Dict[PurePath, Dict[str, TencentPakEntry]] = {}
self.all_dirs: Dict[str, Dict[str, TencentPakEntry]] = {}
self.zstd_dict = None
self.zstd_dict_entry = None
self.is_zstd_with_dict = 'zsdic' in self.file_path.name.lower()
self._load_index()
def _load_index(self):
idx_data = bytes(self.file_content[self.pak_info.index_offset:self.pak_info.index_offset + self.pak_info.index_size])
if self.pak_info.index_encrypted:
idx_data = PakCrypto.decrypt_index(idx_data, self.pak_info)
r = Reader(idx_data)
self.mount_point_raw = r.string()
mp = PurePath()
for p in PurePath(self.mount_point_raw).parts:
if p != '..': mp /= p
self.mount_point = mp
num_files = r.u4()
self.files = [TencentPakEntry.read_from(r, self.pak_info.version) for _ in range(num_files)]
num_dirs = r.u8()
for _ in range(num_dirs):
dir_str = r.string()
count = r.u8()
dir_files = {}
for _ in range(count):
fn = r.string()
idx = ~r.i4()
if 0 <= idx < len(self.files):
entry = self.files[idx]
dir_files[fn] = entry
self.all_dirs[dir_str] = dir_files
dp = PurePath(dir_str)
if self.is_zstd_with_dict and dp.name == 'zstddic' and dir_files:
self.zstd_dict_entry = next(iter(dir_files.values()))
self._load_zstd_dict(self.zstd_dict_entry)
else:
self.index[dp] = dir_files
def _load_zstd_dict(self, entry: TencentPakEntry):
raw = bytes(self.file_content[entry.offset:entry.offset + entry.size])
r = Reader(raw)
dict_size = r.u8()
_ = r.u4()
dict_data = r.s(dict_size)
self.zstd_dict = ZstdCompressionDict(dict_data, DICT_TYPE_AUTO)
def extract_entry(self, entry: TencentPakEntry, relative_path: str) -> bytes:
path = PurePath(relative_path)
em = entry.encryption_method
cm = entry.compression_method
if cm == CM_NONE:
sz = PakCrypto.align_encrypted_size(entry.size, em) if entry.encrypted else entry.size
raw = bytes(self.file_content[entry.offset:entry.offset + sz])
if entry.encrypted:
raw = PakCrypto.decrypt_block(raw, path, em)
return raw[:entry.uncompressed_size]
out = bytearray()
indices = PakCrypto.generate_block_indices(len(entry.compressed_blocks), em) if entry.encrypted else list(range(len(entry.compressed_blocks)))
zd = self.zstd_dict if cm == CM_ZSTD_DICT else None
for idx in indices:
blk = entry.compressed_blocks[idx]
sz = PakCrypto.align_encrypted_size(blk.end - blk.start, em) if entry.encrypted else (blk.end - blk.start)
blk_data = bytes(self.file_content[blk.start:blk.start + sz])
if entry.encrypted:
blk_data = PakCrypto.decrypt_block(blk_data, path, em)
if cm == CM_ZLIB:
out += zlib.decompress(blk_data)
elif cm in (CM_ZSTD, CM_ZSTD_DICT):
d = ZstdDecompressor(dict_data=zd)
out += d.decompress(blk_data)
else:
out += blk_data
return bytes(out[:entry.uncompressed_size]) if entry.uncompressed_size else bytes(out)
def unpack_all(self, out_dir: Path, progress_callback=None, max_workers: int = 8) -> Tuple[int, int]:
"""High-speed multi-threaded unpack."""
out_base = out_dir / self.mount_point
out_base.mkdir(parents=True, exist_ok=True)
all_items = []
for dir_path, files in self.index.items():
for file_name, entry in files.items():
full_rel = str(PurePath(dir_path) / file_name).replace('\\', '/')
target_file = out_base / dir_path / file_name
all_items.append((target_file, entry, full_rel))
success_count = 0
fail_count = 0
def _worker(item):
t_path, e, f_rel = item
try:
t_path.parent.mkdir(parents=True, exist_ok=True)
data = self.extract_entry(e, f_rel)
with open(t_path, 'wb') as f:
f.write(data)
return True
except Exception:
# Raw dump fallback on un-decryptable or corrupted blocks
try:
with open(t_path, 'wb') as f:
if e.compressed_blocks:
for b in e.compressed_blocks:
f.write(bytes(self.file_content[b.start:b.end]))
else:
f.write(bytes(self.file_content[e.offset:e.offset + e.size]))
except Exception:
pass
return False
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(_worker, itm) for itm in all_items]
for fut in concurrent.futures.as_completed(futures):
if fut.result():
success_count += 1
else:
fail_count += 1
if progress_callback:
progress_callback(1)
return success_count, fail_count
# ==================== SERIALIZATION & REBUILD ENGINE ====================
def pw_string(s: str) -> bytes:
if not s: return struct.pack('<i', 0)
b = s.encode('utf-8') + b'\x00'
return struct.pack('<i', len(b)) + b
def pw_entry(e: TencentPakEntry, v: int) -> bytes:
w = bytearray(e.content_hash)
w += struct.pack('<QQIQ', e.offset, e.uncompressed_size, e.compression_method, e.size)
if v >= 5:
w += bytes([e.unk1]) + e.unk2
if e.compression_method != CM_NONE and v >= 3:
w += struct.pack('<I', len(e.compressed_blocks))
for b in e.compressed_blocks:
w += struct.pack('<QQ', b.start, b.end)
if v >= 4:
w += struct.pack('<I', e.compression_block_size)
w += bytes([1 if e.encrypted else 0])
if v >= 12:
w += struct.pack('<II', e.encryption_method, e.index_new_sep)
return bytes(w)
def compress_chunk(chunk: bytes, cm: int, zstd_dict=None, fast: bool = False) -> bytes:
if cm == CM_ZLIB:
return zlib.compress(chunk, 1 if fast else 9)
if cm in (CM_ZSTD, CM_ZSTD_DICT):
zd = zstd_dict if cm == CM_ZSTD_DICT else None
levels = [3, 1] if fast else [22, 19, 16, 13, 9, 3]
for lvl in levels:
try:
return ZstdCompressor(level=lvl, dict_data=zd, threads=1).compress(chunk)
except Exception:
continue
return chunk
def normalize_match_path(p_str: str, mp_str: str) -> str:
s = p_str.replace('\\', '/').strip('/')
mp = mp_str.replace('\\', '/').strip('/')
if mp and (s.lower() == mp.lower() or s.lower().startswith(mp.lower() + '/')):
s = s[len(mp):].lstrip('/')
return s.lower()
def build_reconstructed_pak(
pak: TencentPakFile,
modified_files: Dict[str, bytes],
output_pak_path: Path,
target_custom_dir: Optional[str] = None,
force_encrypt: bool = False,
forced_sm4_type: int = 47,
progress_callback = None
) -> Tuple[int, int]:
"""
Universal High-Precision PAK Rebuilder.
Rebuilds entire payload and index with proper RSA/SHA1 signatures.
"""
version = pak.pak_info.version
mp_str = pak.mount_point_raw
all_dirs = {dp: dict(files) for dp, files in pak.all_dirs.items()}
# Clone entries 1:1
new_files: List[TencentPakEntry] = []
old_to_new = {}
for i, e in enumerate(pak.files):
ne = copy.copy(e)
ne.compressed_blocks = [PakCompressedBlock(b.start, b.end) for b in e.compressed_blocks]
new_files.append(ne)
old_to_new[id(e)] = ne
# Standardize modified paths map
clean_mods = {k.replace('\\', '/').strip('/'): v for k, v in modified_files.items()}
out_buf = bytearray()
processed_count = 0
added_count = 0
# 1. Process existing files & directories
for dp_str, dir_files in list(all_dirs.items()):
for name, old_entry in list(dir_files.items()):
full_path = str(PurePath(dp_str) / name).replace('\\', '/')
ne = old_to_new.get(id(old_entry))
if ne is None:
ne = copy.copy(old_entry)
new_files.append(ne)
old_to_new[id(old_entry)] = ne
# Check if this file is modified (using mount-point agnostic matching)
matched_key = None
norm_full = normalize_match_path(full_path, mp_str)
for mk in list(clean_mods.keys()):
norm_mk = normalize_match_path(mk, mp_str)
if norm_mk == norm_full or mk.rsplit('/', 1)[-1].lower() == name.lower():
matched_key = mk
break
if matched_key is not None:
new_data = clean_mods.pop(matched_key)
pak_rel = PurePath(full_path)
ne.content_hash = SHA1.new(new_data).digest()
ne.uncompressed_size = len(new_data)
if force_encrypt:
ne.encrypted = True
ne.encryption_method = forced_sm4_type
ne.unk2 = SHA1.new((mp_str + full_path).lower().encode('utf-8')).digest()
# Encode content
_encode_entry_payload(out_buf, ne, new_data, pak_rel, pak.zstd_dict)
processed_count += 1
else:
# Copy original payload
_copy_entry_payload(out_buf, pak.file_content, ne, old_entry)
if progress_callback: progress_callback(1)
# 2. Add brand-new files (if any left in clean_mods)
for mk, new_data in clean_mods.items():
fn = mk.rsplit('/', 1)[-1]
target_dir = target_custom_dir if target_custom_dir else (mk.rsplit('/', 1)[0] + '/' if '/' in mk else '')
if target_dir and not target_dir.endswith('/'): target_dir += '/'
full_path = target_dir + fn
# Create new entry template
template = pak.files[0] if pak.files else None
ne = copy.copy(template) if template else TencentPakEntry(
content_hash=b'', offset=0, uncompressed_size=0, compression_method=CM_ZSTD,
size=0, unk1=0, unk2=b'', compressed_blocks=[], compression_block_size=65536,
encrypted=force_encrypt, encryption_method=forced_sm4_type if force_encrypt else 0,
index_new_sep=0
)
ne.content_hash = SHA1.new(new_data).digest()
ne.uncompressed_size = len(new_data)
ne.unk2 = SHA1.new((mp_str + full_path).lower().encode('utf-8')).digest()
if force_encrypt:
ne.encrypted = True
ne.encryption_method = forced_sm4_type
_encode_entry_payload(out_buf, ne, new_data, PurePath(full_path), pak.zstd_dict)
new_files.append(ne)
all_dirs.setdefault(target_dir, {})[fn] = ne
added_count += 1
if progress_callback: progress_callback(1)
# 3. Rebuild Index table
eidx = {id(f): i for i, f in enumerate(new_files)}
idx_buf = bytearray(pw_string(mp_str))
idx_buf += struct.pack('<I', len(new_files))
for ne in new_files:
idx_buf += pw_entry(ne, version)
idx_buf += struct.pack('<Q', len(all_dirs))
for dp_str, dir_files in all_dirs.items():
idx_buf += pw_string(dp_str)
idx_buf += struct.pack('<Q', len(dir_files))
for name, e_ref in dir_files.items():
idx_buf += pw_string(name)
ne_ref = old_to_new.get(id(e_ref), e_ref)
found = eidx.get(id(ne_ref))
if found is None:
found = eidx.get(id(e_ref))
idx_buf += struct.pack('<i', ~found if found is not None else -1)
index_plain = bytes(idx_buf)
new_sha1 = SHA1.new(index_plain).digest()
if pak.pak_info.index_encrypted:
key = PakCrypto.rsa_extract(pak.pak_info.packed_key, RSA_MOD_1)
iv = PakCrypto.rsa_extract(pak.pak_info.packed_iv, RSA_MOD_1)
aes = AES.new(key, MODE_CBC, iv[:16])
pad = (-len(index_plain)) % AES.block_size or AES.block_size
index_bytes = aes.encrypt(index_plain + bytes([pad] * pad))
else:
index_bytes = index_plain
new_idx_offset = len(out_buf)
new_idx_size = len(index_bytes)
out_buf += index_bytes
# 4. Rebuild Footer
footer_sz = TencentPakInfo.calc_mem_size(version)
new_footer = bytearray(pak.file_content[-footer_sz:])
ks = pak.keystream
h_key = struct.pack('<5I', *ks[4:9])
new_footer[-36:-16] = bytes(a ^ b for a, b in zip(new_sha1, h_key))
new_footer[-16:-8] = (new_idx_size ^ (ks[10] << 32 | ks[11])).to_bytes(8, 'little')
new_footer[-8:] = (new_idx_offset ^ (ks[0] << 32 | ks[1])).to_bytes(8, 'little')
# Auto-sign Stem Hash for any current and future version (4.7, 4.8, 5.0+)
if version >= 9:
stem_crc = zlib.crc32(output_pak_path.stem.encode('utf-32le'))
stem_pos = -45 - (20 if version >= 12 else 0) - 8
new_footer[stem_pos:stem_pos + 4] = struct.pack('<I', stem_crc ^ ks[8])
out_buf += new_footer
# Write output
output_pak_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_pak_path, 'wb') as f:
f.write(out_buf)
return processed_count, added_count
def _encode_entry_payload(out_buf: bytearray, ne: TencentPakEntry, data: bytes, pak_rel: PurePath, zstd_dict):
cm = ne.compression_method
em = ne.encryption_method
if cm == CM_NONE:
cipher = PakCrypto.encrypt_block(data, pak_rel, em) if ne.encrypted else data
ne.offset = len(out_buf)
ne.size = len(cipher)
ne.uncompressed_size = len(data)
out_buf += cipher
return
cs = ne.compression_block_size if ne.compression_block_size > 0 else 65536
chunks = [data[i:i + cs] for i in range(0, len(data), cs)] if data else [b'']
n = len(chunks)
inv = PakCrypto.generate_block_indices(n, em) if ne.encrypted else list(range(n))
file_order = [0] * n
for j in range(n): file_order[inv[j]] = j
new_blks = []
for k in range(n):
chunk = chunks[file_order[k]]
comp = compress_chunk(chunk, cm, zstd_dict)
cipher = PakCrypto.encrypt_block(comp, pak_rel, em) if ne.encrypted else comp
blk = PakCompressedBlock(len(out_buf), len(out_buf) + len(cipher))
out_buf += cipher
new_blks.append(blk)
ne.compressed_blocks = new_blks
ne.offset = new_blks[0].start if new_blks else len(out_buf)
ne.size = sum(b.end - b.start for b in new_blks)
ne.uncompressed_size = len(data)
def _copy_entry_payload(out_buf: bytearray, src_fc: memoryview, ne: TencentPakEntry, old_e: TencentPakEntry):
em = old_e.encryption_method
if old_e.compression_method == CM_NONE:
sz = PakCrypto.align_encrypted_size(old_e.size, em) if old_e.encrypted else old_e.size
ne.offset = len(out_buf)
out_buf += bytes(src_fc[old_e.offset:old_e.offset + sz])
elif old_e.compressed_blocks:
new_blks = []
for ob in old_e.compressed_blocks:
unc = ob.end - ob.start
enc = PakCrypto.align_encrypted_size(unc, em) if old_e.encrypted else unc
nb = PakCompressedBlock(len(out_buf), len(out_buf) + unc)
out_buf += bytes(src_fc[ob.start:ob.start + enc])
new_blks.append(nb)
ne.compressed_blocks = new_blks
ne.offset = new_blks[0].start if new_blks else len(out_buf)