This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpk_archiver
executable file
·115 lines (100 loc) · 3.69 KB
/
mpk_archiver
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
#!/usr/bin/python3
import os
import struct
import sys
import types
import open_ext
Entry = types.SimpleNamespace
def make_u32_key(u8_key):
return struct.unpack('I', bytes([u8_key] * 4))[0]
def unpack_mpk(src_path, dst_dir):
os.makedirs(dst_dir, exist_ok=True)
with open_ext.open_ext(src_path, 'rb') as src_fh:
#read file table
table_offset = src_fh.read_u32_le()
entry_count = src_fh.read_u32_le()
src_fh.seek(table_offset)
entries = []
for i in range(entry_count):
entry = Entry(
name = src_fh.read(32),
offset = src_fh.read_u32_le(),
size = src_fh.read_u32_le())
entries.append(entry)
#unobfuscate
key_u8 = entries[0].name[31]
key_u32 = make_u32_key(key_u8)
for entry in entries:
name = u''
for i in range(32):
name += chr(entry.name[i] ^ key_u8)
if name[0] == '\\':
name = name[1:]
name = name[0:name.index('\0')]
entry.name = name
entry.offset ^= key_u32
entry.size ^= key_u32
#extract
for entry in entries:
src_fh.seek(entry.offset)
data = src_fh.read(entry.size)
dst_path = os.path.join(dst_dir, entry.name)
with open(dst_path, 'wb') as dst_fh:
dst_fh.write(data)
print('Unpacked %s' % dst_path)
def pack_mpk(src_dir, dst_path, key_u8=0x58):
#prepare initial entries from file system
entries = []
for root, dirs, files in os.walk(src_dir):
for file in sorted(files):
path = os.path.join(root, file)
entry = Entry(
name = '\\' + os.path.relpath(path, src_dir),
size = os.path.getsize(path),
offset = 0,
original_path = path)
entries.append(entry)
#calculate offsets
current_offset = 8
for entry in entries:
entry.offset = current_offset
current_offset += entry.size
table_offset = current_offset
#obfuscate
key_u32 = make_u32_key(key_u8)
for entry in entries:
name_bytes = (entry.name.encode('ascii') + b"\x00" * 32)[0:32]
entry.name = struct.pack('32B', *[i ^ key_u8 for i in name_bytes])
entry.offset ^= key_u32
entry.size ^= key_u32
#dump entries to mpk file
with open_ext.open_ext(dst_path, 'wb') as dst_fh:
dst_fh.write_u32_le(table_offset)
dst_fh.write_u32_le(len(entries))
for entry in entries:
with open(entry.original_path, 'rb') as src_fh:
assert(dst_fh.tell() == entry.offset ^ key_u32)
dst_fh.write(src_fh.read())
print('Packed %s' % entry.original_path)
assert(dst_fh.tell() == table_offset)
for entry in entries:
dst_fh.write(entry.name)
dst_fh.write_u32_le(entry.offset)
dst_fh.write_u32_le(entry.size)
def parse_args():
import argparse
parser = argparse.ArgumentParser(description='Pack or unpack MPK archives')
parser.add_argument('input', metavar='INPUT')
parser.add_argument('output', metavar='OUTPUT')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-p', '--pack', action='store_true', help='pack INPUT directory to OUTPUT MPK file')
group.add_argument('-u', '--unpack', action='store_true', help='unpack INPUT MPK file to OUTPUT directory')
return parser.parse_args()
def main():
args = parse_args()
if args.unpack:
unpack_mpk(args.input, args.output)
else:
pack_mpk(args.input, args.output)
if __name__ == '__main__':
main()