-
Notifications
You must be signed in to change notification settings - Fork 1
/
psfextract.py
129 lines (103 loc) · 4.02 KB
/
psfextract.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
#!/usr/bin/env python3
#
# Microsoft PSTREAM File Extractor
# Copyright (C) 2021 algirdas666
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import struct
import ctypes
import os
import sys
import logging
import xml.etree.ElementTree as ET
def unpack(file):
logging.debug(f'Unpacking {file}')
result = ctypes.windll.msdelta.ApplyDeltaW(
ctypes.c_longlong(0),
None,
ctypes.c_wchar_p(file),
ctypes.c_wchar_p(file)
)
if result:
return True
logging.error(f'Failed to unpack {file}')
return False
def extract(f, off, leng, dest):
logging.debug(f'Extracting data from PSTREAM to {dest}')
os.makedirs(os.path.dirname(dest), exist_ok=True)
buff = 1024*1024
f.seek(off)
read = 0
with open(dest, 'wb') as d:
while(read < leng):
if (read+buff) >= leng:
buff = leng-read
d.write(f.read(buff))
read += buff
if __name__ == '__main__':
if sys.platform != 'win32':
print('PSTREAM files can be only extracted on Windows')
exit(1)
if len(sys.argv) != 3:
print('Usage:')
print('psfextract.py <psf_file> <destination>')
exit(1)
logging.basicConfig(level=logging.INFO)
psf_file = sys.argv[1]
dest_dir = sys.argv[2]
dir = os.path.abspath(dest_dir).replace('\\', '/') + '/'
if os.path.exists(dir) and len(os.listdir(dir)) != 0:
logging.critical(f'{dest_dir} is not empty!')
exit(1)
elif not os.path.exists(dir):
os.mkdir(dir)
with open(psf_file, 'rb') as f:
if f.read(7) != b'PSTREAM':
logging.critical('Specified file is not PSTREAM!')
exit(1)
f.seek(40)
offset, length = struct.unpack('ll', f.read(8))
logging.debug(f'Manifest offset: {offset}')
logging.debug(f'Manifest packed length: {length}')
extract(f, offset, length, dir + 'manifest.cix.xml')
if not unpack(dir + 'manifest.cix.xml'):
logging.critical('Failed to unpack the manifest!')
exit(1)
logging.debug('Parsing manifest XML')
tree = ET.parse(dir + 'manifest.cix.xml')
root = tree.getroot()
logging.debug('Getting files')
files = root.findall('./{urn:ContainerIndex}Files/{urn:ContainerIndex}File')
filecount = len(files)
extracted = 0
for file in files:
fil_name = file.get('name')
name = dir + fil_name.replace('\\', '/')
logging.debug(fil_name)
logging.debug(f'Getting delta for {name}')
delta = file.find('./{urn:ContainerIndex}Delta/{urn:ContainerIndex}Source')
type = delta.get('type')
offset = int(delta.get('offset'))
length = int(delta.get('length'))
logging.debug(f'Delta type: {type}')
logging.debug(f'Delta offset: {offset}')
logging.debug(f'Delta length: {length}')
extract(f, offset, length, name)
if type == 'PA30' and not unpack(name):
logging.critical('Failed to unpack {name}!')
exit(1)
extracted += 1
if extracted % 100 == 0 or extracted == filecount:
percent = extracted / filecount
logging.info(f'{extracted}/{filecount} files extracted ({percent:.2%})')