forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pkg.py
executable file
·821 lines (703 loc) · 21.8 KB
/
pkg.py
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
#!/usr/bin/env python2
from __future__ import with_statement
import struct, sys
class StructType(tuple):
def __getitem__(self, value):
return [self] * value
def __call__(self, value, endian='<'):
if isinstance(value, str):
return struct.unpack(endian + tuple.__getitem__(self, 0), value[:tuple.__getitem__(self, 1)])[0]
else:
return struct.pack(endian + tuple.__getitem__(self, 0), value)
class Struct(object):
__slots__ = ('__attrs__', '__baked__', '__defs__', '__endian__', '__next__', '__sizes__', '__values__')
int8 = StructType(('b', 1))
uint8 = StructType(('B', 1))
int16 = StructType(('h', 2))
uint16 = StructType(('H', 2))
int32 = StructType(('l', 4))
uint32 = StructType(('L', 4))
int64 = StructType(('q', 8))
uint64 = StructType(('Q', 8))
float = StructType(('f', 4))
def string(cls, len, offset=0, encoding=None, stripNulls=False, value=''):
return StructType(('string', (len, offset, encoding, stripNulls, value)))
string = classmethod(string)
LE = '<'
BE = '>'
__endian__ = '<'
def __init__(self, func=None, unpack=None, **kwargs):
self.__defs__ = []
self.__sizes__ = []
self.__attrs__ = []
self.__values__ = {}
self.__next__ = True
self.__baked__ = False
if func == None:
self.__format__()
else:
sys.settrace(self.__trace__)
func()
for name in func.func_code.co_varnames:
value = self.__frame__.f_locals[name]
self.__setattr__(name, value)
self.__baked__ = True
if unpack != None:
if isinstance(unpack, tuple):
self.unpack(*unpack)
else:
self.unpack(unpack)
if len(kwargs):
for name in kwargs:
self.__values__[name] = kwargs[name]
def __trace__(self, frame, event, arg):
self.__frame__ = frame
sys.settrace(None)
def __setattr__(self, name, value):
if name in self.__slots__:
return object.__setattr__(self, name, value)
if self.__baked__ == False:
if not isinstance(value, list):
value = [value]
attrname = name
else:
attrname = '*' + name
self.__values__[name] = None
for sub in value:
if isinstance(sub, Struct):
sub = sub.__class__
try:
if issubclass(sub, Struct):
sub = ('struct', sub)
except TypeError:
pass
type_, size = tuple(sub)
if type_ == 'string':
self.__defs__.append(Struct.string)
self.__sizes__.append(size)
self.__attrs__.append(attrname)
self.__next__ = True
if attrname[0] != '*':
self.__values__[name] = size[3]
elif self.__values__[name] == None:
self.__values__[name] = [size[3] for val in value]
elif type_ == 'struct':
self.__defs__.append(Struct)
self.__sizes__.append(size)
self.__attrs__.append(attrname)
self.__next__ = True
if attrname[0] != '*':
self.__values__[name] = size()
elif self.__values__[name] == None:
self.__values__[name] = [size() for val in value]
else:
if self.__next__:
self.__defs__.append('')
self.__sizes__.append(0)
self.__attrs__.append([])
self.__next__ = False
self.__defs__[-1] += type_
self.__sizes__[-1] += size
self.__attrs__[-1].append(attrname)
if attrname[0] != '*':
self.__values__[name] = 0
elif self.__values__[name] == None:
self.__values__[name] = [0 for val in value]
else:
try:
self.__values__[name] = value
except KeyError:
raise AttributeError(name)
def __getattr__(self, name):
if self.__baked__ == False:
return name
else:
try:
return self.__values__[name]
except KeyError:
raise AttributeError(name)
def __len__(self):
ret = 0
arraypos, arrayname = None, None
for i in range(len(self.__defs__)):
sdef, size, attrs = self.__defs__[i], self.__sizes__[i], self.__attrs__[i]
if sdef == Struct.string:
size, offset, encoding, stripNulls, value = size
if isinstance(size, str):
size = self.__values__[size] + offset
elif sdef == Struct:
if attrs[0] == '*':
if arrayname != attrs:
arrayname = attrs
arraypos = 0
size = len(self.__values__[attrs[1:]][arraypos])
size = len(self.__values__[attrs])
ret += size
return ret
def unpack(self, data, pos=0):
for name in self.__values__:
if not isinstance(self.__values__[name], Struct):
self.__values__[name] = None
elif self.__values__[name].__class__ == list and len(self.__values__[name]) != 0:
if not isinstance(self.__values__[name][0], Struct):
self.__values__[name] = None
arraypos, arrayname = None, None
for i in range(len(self.__defs__)):
sdef, size, attrs = self.__defs__[i], self.__sizes__[i], self.__attrs__[i]
if sdef == Struct.string:
size, offset, encoding, stripNulls, value = size
if isinstance(size, str):
size = self.__values__[size] + offset
temp = data[pos:pos+size]
if len(temp) != size:
raise StructException('Expected %i byte string, got %i' % (size, len(temp)))
if encoding != None:
temp = temp.decode(encoding)
if stripNulls:
temp = temp.rstrip('\0')
if attrs[0] == '*':
name = attrs[1:]
if self.__values__[name] == None:
self.__values__[name] = []
self.__values__[name].append(temp)
else:
self.__values__[attrs] = temp
pos += size
elif sdef == Struct:
if attrs[0] == '*':
if arrayname != attrs:
arrayname = attrs
arraypos = 0
name = attrs[1:]
self.__values__[attrs][arraypos].unpack(data, pos)
pos += len(self.__values__[attrs][arraypos])
arraypos += 1
else:
self.__values__[attrs].unpack(data, pos)
pos += len(self.__values__[attrs])
else:
values = struct.unpack(self.__endian__+sdef, data[pos:pos+size])
pos += size
j = 0
for name in attrs:
if name[0] == '*':
name = name[1:]
if self.__values__[name] == None:
self.__values__[name] = []
self.__values__[name].append(values[j])
else:
self.__values__[name] = values[j]
j += 1
return self
def pack(self):
arraypos, arrayname = None, None
ret = ''
for i in range(len(self.__defs__)):
sdef, size, attrs = self.__defs__[i], self.__sizes__[i], self.__attrs__[i]
if sdef == Struct.string:
size, offset, encoding, stripNulls, value = size
if isinstance(size, str):
size = self.__values__[size]+offset
if attrs[0] == '*':
if arrayname != attrs:
arraypos = 0
arrayname = attrs
temp = self.__values__[attrs[1:]][arraypos]
arraypos += 1
else:
temp = self.__values__[attrs]
if encoding != None:
temp = temp.encode(encoding)
temp = temp[:size]
ret += temp + ('\0' * (size - len(temp)))
elif sdef == Struct:
if attrs[0] == '*':
if arrayname != attrs:
arraypos = 0
arrayname = attrs
ret += self.__values__[attrs[1:]][arraypos].pack()
arraypos += 1
else:
ret += self.__values__[attrs].pack()
else:
values = []
for name in attrs:
if name[0] == '*':
if arrayname != name:
arraypos = 0
arrayname = name
values.append(self.__values__[name[1:]][arraypos])
arraypos += 1
else:
values.append(self.__values__[name])
ret += struct.pack(self.__endian__+sdef, *values)
return ret
def __getitem__(self, value):
return [('struct', self.__class__)] * value
class SelfHeader(Struct):
__endian__ = Struct.BE
def __format__(self):
self.magic = Struct.uint32
self.headerVer = Struct.uint32
self.flags = Struct.uint16
self.type = Struct.uint16
self.meta = Struct.uint32
self.headerSize = Struct.uint64
self.encryptedSize = Struct.uint64
self.unknown = Struct.uint64
self.AppInfo = Struct.uint64
self.elf = Struct.uint64
self.phdr = Struct.uint64
self.shdr = Struct.uint64
self.phdrOffsets = Struct.uint64
self.sceversion = Struct.uint64
self.digest = Struct.uint64
self.digestSize = Struct.uint64
class AppInfo(Struct):
__endian__ = Struct.BE
def __format__(self):
self.authid = Struct.uint64
self.unknown = Struct.uint32
self.appType = Struct.uint32
self.appVersion = Struct.uint64
import struct
import sys
import hashlib
import os
import getopt
import ConfigParser
import io
import glob
TYPE_NPDRMSELF = 0x1
TYPE_RAW = 0x3
TYPE_DIRECTORY = 0x4
TYPE_OVERWRITE_ALLOWED = 0x80000000
class EbootMeta(Struct):
__endian__ = Struct.BE
def __format__(self):
self.magic = Struct.uint32
self.unk1 = Struct.uint32
self.drmType = Struct.uint32
self.unk2 = Struct.uint32
self.contentID = Struct.uint8[0x30]
self.fileSHA1 = Struct.uint8[0x10]
self.notSHA1 = Struct.uint8[0x10]
self.notXORKLSHA1 = Struct.uint8[0x10]
self.nulls = Struct.uint8[0x10]
class MetaHeader(Struct):
__endian__ = Struct.BE
def __format__(self):
self.unk1 = Struct.uint32
self.unk2 = Struct.uint32
self.drmType = Struct.uint32
self.unk4 = Struct.uint32
self.unk21 = Struct.uint32
self.unk22 = Struct.uint32
self.unk23 = Struct.uint32
self.unk24 = Struct.uint32
self.unk31 = Struct.uint32
self.unk32 = Struct.uint32
self.unk33 = Struct.uint32
self.secondaryVersion = Struct.uint16
self.unk34 = Struct.uint16
self.dataSize = Struct.uint32
self.unk42 = Struct.uint32
self.unk43 = Struct.uint32
self.packagedBy = Struct.uint16
self.packageVersion = Struct.uint16
class DigestBlock(Struct):
__endian__ = Struct.BE
def __format__(self):
self.type = Struct.uint32
self.size = Struct.uint32
self.isNext = Struct.uint64
class FileHeader(Struct):
__endian__ = Struct.BE
def __format__(self):
self.fileNameOff = Struct.uint32
self.fileNameLength = Struct.uint32
self.fileOff = Struct.uint64
self.fileSize = Struct.uint64
self.flags = Struct.uint32
self.padding = Struct.uint32
def __str__(self):
out = ""
out += "[X] File Name: %s [" % self.fileName
if self.flags & 0xFF == TYPE_NPDRMSELF:
out += "NPDRM Self]"
elif self.flags & 0xFF == TYPE_DIRECTORY:
out += "Directory]"
elif self.flags & 0xFF == TYPE_RAW:
out += "Raw Data]"
else:
out += "Unknown]"
if (self.flags & TYPE_OVERWRITE_ALLOWED ) != 0:
out += " Overwrite allowed.\n"
else:
out += " Overwrite NOT allowed.\n"
out += "\n"
out += "[X] File Name offset: %08x\n" % self.fileNameOff
out += "[X] File Name Length: %08x\n" % self.fileNameLength
out += "[X] Offset To File Data: %016x\n" % self.fileOff
out += "[X] File Size: %016x\n" % self.fileSize
out += "[X] Flags: %08x\n" % self.flags
out += "[X] Padding: %08x\n\n" % self.padding
assert self.padding == 0, "I guess I was wrong, this is not padding."
return out
def __repr__(self):
return self.fileName + ("<FileHeader> Size: 0x%016x" % self.fileSize)
def __init__(self):
Struct.__init__(self)
self.fileName = ""
class Header(Struct):
__endian__ = Struct.BE
def __format__(self):
self.magic = Struct.uint32
self.type = Struct.uint32
self.pkgInfoOff = Struct.uint32
self.unk1 = Struct.uint32
self.headSize = Struct.uint32
self.itemCount = Struct.uint32
self.packageSize = Struct.uint64
self.dataOff = Struct.uint64
self.dataSize = Struct.uint64
self.contentID = Struct.uint8[0x30]
self.QADigest = Struct.uint8[0x10]
self.KLicensee = Struct.uint8[0x10]
def __str__(self):
context = keyToContext(self.QADigest)
setContextNum(context, 0xFFFFFFFFFFFFFFFF)
licensee = crypt(context, listToString(self.KLicensee), 0x10)
out = ""
out += "[X] Magic: %08x\n" % self.magic
out += "[X] Type: %08x\n" % self.type
out += "[X] Offset to package info: %08x\n" % self.pkgInfoOff
out += "[ ] unk1: %08x\n" % self.unk1
out += "[X] Head Size: %08x\n" % self.headSize
out += "[X] Item Count: %08x\n" % self.itemCount
out += "[X] Package Size: %016x\n" % self.packageSize
out += "[X] Data Offset: %016x\n" % self.dataOff
out += "[X] Data Size: %016x\n" % self.dataSize
out += "[X] ContentID: '%s'\n" % (nullterm(self.contentID))
out += "[X] QA_Digest: %s\n" % (nullterm(self.QADigest, True))
out += "[X] K Licensee: %s\n" % licensee.encode('hex')
return out
def listToString(inlist):
if isinstance(inlist, list):
return ''.join(["%c" % el for el in inlist])
else:
return ""
def nullterm(str_plus, printhex=False):
if isinstance(str_plus, list):
if printhex:
str_plus = ''.join(["%X" % el for el in str_plus])
else:
str_plus = listToString(str_plus)
z = str_plus.find('\0')
if z != -1:
return str_plus[:z]
else:
return str_plus
def keyToContext(key):
if isinstance(key, list):
key = listToString(key)
key = key[0:16]
largekey = []
for i in range(0, 8):
largekey.append(ord(key[i]))
for i in range(0, 8):
largekey.append(ord(key[i]))
for i in range(0, 8):
largekey.append(ord(key[i+8]))
for i in range(0, 8):
largekey.append(ord(key[i+8]))
for i in range(0, 0x20):
largekey.append(0)
return largekey
#Thanks to anonymous for the help with the RE of this part,
# the x86 mess of ands and ors made my head go BOOM headshot.
def manipulate(key):
if not isinstance(key, list):
return
tmp = listToString(key[0x38:])
tmpnum = struct.unpack('>Q', tmp)[0]
tmpnum += 1
tmpnum = tmpnum & 0xFFFFFFFFFFFFFFFF
setContextNum(key, tmpnum)
def setContextNum(key, tmpnum):
tmpchrs = struct.pack('>Q', tmpnum)
key[0x38] = ord(tmpchrs[0])
key[0x39] = ord(tmpchrs[1])
key[0x3a] = ord(tmpchrs[2])
key[0x3b] = ord(tmpchrs[3])
key[0x3c] = ord(tmpchrs[4])
key[0x3d] = ord(tmpchrs[5])
key[0x3e] = ord(tmpchrs[6])
key[0x3f] = ord(tmpchrs[7])
try:
import pkgcrypt
except:
print ""
print "---------------------"
print "RETROARCH BUILD ERROR"
print "---------------------"
print "Couldn't make PKG file. Go into the ps3py directory, and type the following:"
print ""
print "python2 setup.py build"
print ""
print "This should create a pkgcrypt.so file in the build/ directory. Move that file"
print "over to the root of the ps3py directory and try running this script again."
def crypt(key, inbuf, length):
if not isinstance(key, list):
return ""
return pkgcrypt.pkgcrypt(listToString(key), inbuf, length);
# Original python (slow) implementation
ret = ""
offset = 0
while length > 0:
bytes_to_dump = length
if length > 0x10:
bytes_to_dump = 0x10
outhash = SHA1(listToString(key)[0:0x40])
for i in range(0, bytes_to_dump):
ret += chr(ord(outhash[i]) ^ ord(inbuf[offset]))
offset += 1
manipulate(key)
length -= bytes_to_dump
return ret
def SHA1(data):
m = hashlib.sha1()
m.update(data)
return m.digest()
pkgcrypt.register_sha1_callback(SHA1)
def getFiles(files, folder, original):
oldfolder = folder
foundFiles = glob.glob( os.path.join(folder, '*') )
sortedList = []
for filepath in foundFiles:
if not os.path.isdir(filepath):
sortedList.append(filepath)
for filepath in foundFiles:
if os.path.isdir(filepath):
sortedList.append(filepath)
for filepath in sortedList:
newpath = filepath.replace("\\", "/")
newpath = newpath[len(original):]
if os.path.isdir(filepath):
folder = FileHeader()
folder.fileName = newpath
folder.fileNameOff = 0
folder.fileNameLength = len(folder.fileName)
folder.fileOff = 0
folder.fileSize = 0
folder.flags = TYPE_OVERWRITE_ALLOWED | TYPE_DIRECTORY
folder.padding = 0
files.append(folder)
getFiles(files, filepath, original)
else:
file = FileHeader()
file.fileName = newpath
file.fileNameOff = 0
file.fileNameLength = len(file.fileName)
file.fileOff = 0
file.fileSize = os.path.getsize(filepath)
file.flags = TYPE_OVERWRITE_ALLOWED | TYPE_RAW
if newpath == "USRDIR/EBOOT.BIN":
file.fileSize = ((file.fileSize - 0x30 + 63) & ~63) + 0x30
file.flags = TYPE_OVERWRITE_ALLOWED | TYPE_NPDRMSELF
file.padding = 0
files.append(file)
def pack(folder, contentid, outname=None):
qadigest = hashlib.sha1()
header = Header()
header.magic = 0x7F504B47
header.type = 0x01
header.pkgInfoOff = 0xC0
header.unk1 = 0x05
header.headSize = 0x80
header.itemCount = 0
header.packageSize = 0
header.dataOff = 0x140
header.dataSize = 0
for i in range(0, 0x30):
header.contentID[i] = 0
for i in range(0,0x10):
header.QADigest[i] = 0
header.KLicensee[i] = 0
metaBlock = MetaHeader()
metaBlock.unk1 = 1 #doesnt change output of --extract
metaBlock.unk2 = 4 #doesnt change output of --extract
metaBlock.drmType = 3 #1 = Network, 2 = Local, 3 = Free, anything else = unknown
metaBlock.unk4 = 2
metaBlock.unk21 = 4
metaBlock.unk22 = 5 #5 == gameexec, 4 == gamedata
metaBlock.unk23 = 3
metaBlock.unk24 = 4
metaBlock.unk31 = 0xE #packageType 0x10 == patch, 0x8 == Demo&Key, 0x0 == Demo&Key (AND UserFiles = NotOverWrite), 0xE == normal, use 0xE for gamexec, and 8 for gamedata
metaBlock.unk32 = 4 #when this is 5 secondary version gets used??
metaBlock.unk33 = 8 #doesnt change output of --extract
metaBlock.secondaryVersion = 0
metaBlock.unk34 = 0
metaBlock.dataSize = 0
metaBlock.unk42 = 5
metaBlock.unk43 = 4
metaBlock.packagedBy = 0x1061
metaBlock.packageVersion = 0
files = []
getFiles(files, folder, folder)
header.itemCount = len(files)
dataToEncrypt = ""
fileDescLength = 0
fileOff = 0x20 * len(files)
for file in files:
alignedSize = (file.fileNameLength + 0x0F) & ~0x0F
file.fileNameOff = fileOff
fileOff += alignedSize
for file in files:
file.fileOff = fileOff
fileOff += (file.fileSize + 0x0F) & ~0x0F
dataToEncrypt += file.pack()
for file in files:
alignedSize = (file.fileNameLength + 0x0F) & ~0x0F
dataToEncrypt += file.fileName
dataToEncrypt += "\0" * (alignedSize-file.fileNameLength)
fileDescLength = len(dataToEncrypt)
for file in files:
if not file.flags & 0xFF == TYPE_DIRECTORY:
path = os.path.join(folder, file.fileName)
fp = open(path, 'rb')
fileData = fp.read()
qadigest.update(fileData)
fileSHA1 = SHA1(fileData)
fp.close()
if fileData[0:9] == "SCE\0\0\0\0\x02\x80":
fselfheader = SelfHeader()
fselfheader.unpack(fileData[0:len(fselfheader)])
appheader = AppInfo()
appheader.unpack(fileData[fselfheader.AppInfo:fselfheader.AppInfo+len(appheader)])
found = False
digestOff = fselfheader.digest
while not found:
digest = DigestBlock()
digest.unpack(fileData[digestOff:digestOff+len(digest)])
if digest.type == 3:
found = True
else:
digestOff += digest.size
if digest.isNext != 1:
break
digestOff += len(digest)
if appheader.appType == 8 and found:
dataToEncrypt += fileData[0:digestOff]
meta = EbootMeta()
meta.magic = 0x4E504400
meta.unk1 = 1
meta.drmType = metaBlock.drmType
meta.unk2 = 1
for i in range(0,min(len(contentid), 0x30)):
meta.contentID[i] = ord(contentid[i])
for i in range(0,0x10):
meta.fileSHA1[i] = ord(fileSHA1[i])
meta.notSHA1[i] = (~meta.fileSHA1[i]) & 0xFF
if i == 0xF:
meta.notXORKLSHA1[i] = (1 ^ meta.notSHA1[i] ^ 0xAA) & 0xFF
else:
meta.notXORKLSHA1[i] = (0 ^ meta.notSHA1[i] ^ 0xAA) & 0xFF
meta.nulls[i] = 0
dataToEncrypt += meta.pack()
dataToEncrypt += fileData[digestOff + 0x80:]
else:
dataToEncrypt += fileData
else:
dataToEncrypt += fileData
dataToEncrypt += '\0' * (((file.fileSize + 0x0F) & ~0x0F) - len(fileData))
header.dataSize = len(dataToEncrypt)
metaBlock.dataSize = header.dataSize
header.packageSize = header.dataSize + 0x1A0
head = header.pack()
qadigest.update(head)
qadigest.update(dataToEncrypt[0:fileDescLength])
QA_Digest = qadigest.digest()
for i in range(0, 0x10):
header.QADigest[i] = ord(QA_Digest[i])
for i in range(0, min(len(contentid), 0x30)):
header.contentID[i] = ord(contentid[i])
context = keyToContext(header.QADigest)
setContextNum(context, 0xFFFFFFFFFFFFFFFF)
licensee = crypt(context, listToString(header.KLicensee), 0x10)
for i in range(0, min(len(contentid), 0x10)):
header.KLicensee[i] = ord(licensee[i])
if outname != None:
outFile = open(outname, 'wb')
else:
outFile = open(contentid + ".pkg", 'wb')
outFile.write(header.pack())
headerSHA = SHA1(header.pack())[3:19]
outFile.write(headerSHA)
metaData = metaBlock.pack()
metaBlockSHA = SHA1(metaData)[3:19]
metaBlockSHAPad = '\0' * 0x30
context = keyToContext([ord(c) for c in metaBlockSHA])
metaBlockSHAPadEnc = crypt(context, metaBlockSHAPad, 0x30)
context = keyToContext([ord(c) for c in headerSHA])
metaBlockSHAPadEnc2 = crypt(context, metaBlockSHAPadEnc, 0x30)
outFile.write(metaBlockSHAPadEnc2)
outFile.write(metaData)
outFile.write(metaBlockSHA)
outFile.write(metaBlockSHAPadEnc)
context = keyToContext(header.QADigest)
encData = crypt(context, dataToEncrypt, header.dataSize)
outFile.write(encData)
outFile.write('\0' * 0x60)
outFile.close()
print header
def usage():
print """usage: [based on revision 1061]
python pkg.py target-directory [out-file]
python pkg.py [options] npdrm-package
-l | --list list packaged files.
-x | --extract extract package.
python pkg.py [options]
--version print revision.
--help print this message."""
def version():
print """pkg.py 0.5"""
def main():
extract = False
list = False
contentid = None
try:
opts, args = getopt.getopt(sys.argv[1:], "hx:dvl:c:", ["help", "extract=", "version", "list=", "contentid="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(2)
elif opt in ("-v", "--version"):
version()
sys.exit(2)
elif opt in ("-x", "--extract"):
fileToExtract = arg
extract = True
elif opt in ("-l", "--list"):
fileToList = arg
list = True
elif opt in ("-c", "--contentid"):
contentid = arg
else:
usage()
sys.exit(2)
if extract:
unpack(fileToExtract)
else:
if len(args) == 1 and contentid != None:
pack(args[0], contentid)
elif len(args) == 2 and contentid != None:
pack(args[0], contentid, args[1])
else:
usage()
sys.exit(2)
if __name__ == "__main__":
main()