forked from ntdiff/ntdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (174 loc) · 6.44 KB
/
main.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
import os
import re
import subprocess
import shutil
import json
from collections import namedtuple
from win32api import GetFileVersionInfo, LOWORD, HIWORD
script_dir = os.path.dirname(os.path.realpath(__file__))
BinPdbPair = namedtuple('BinPdbPair', [ 'bin_path', 'pdb_path' ])
def get_pe_version(path):
info = GetFileVersionInfo(path, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return '{}.{}.{}.{}'.format(HIWORD(ms), LOWORD(ms), HIWORD(ls), LOWORD(ls))
def main():
#
# Fetch all PDB files.
#
print('Downloading PDB files...')
os.makedirs(os.path.join(script_dir, 'PDB'), exist_ok=True)
out = subprocess.getoutput(
r'tools\symchk.exe /r "{}" /s '
r'SRV*{}*http://msdl.microsoft.com/download/symbols '
r'/v '
r'2>&1 >NUL'.format(os.path.join(script_dir, 'Bin'),
os.path.join(script_dir, 'PDBCache'))
)
#
# Create list of [bin_path, pdb_path] pairs.
#
bin_path = None
pdb_path = None
pair_list = []
for line in out.splitlines():
if 'ImageName:' in line:
assert bin_path is None
bin_path = re.findall('ImageName: (.*)', line)[0]
if 'PDB:' in line:
assert bin_path is not None
assert pdb_path is None
pdb_path = re.findall('"([^"]*)"', line)[0]
assert pdb_path is not None
pair_list.append(BinPdbPair(bin_path, pdb_path))
bin_path = None
pdb_path = None
for pair in pair_list:
print('"{}"'.format(pair.bin_path))
print(' -> {}'.format(pair.pdb_path))
#
# Mirror directory structure of "Bin" folder to the "PDB" folder.
#
descriptor = {
'version': [],
'filename': [],
'type': [],
}
version_set = set()
filename_set = set()
type_set = set()
descriptor['type'].append({
'key': 'ALL',
'value': 'ALL',
'text': 'ALL'
})
descriptor['type'].append({
'key': 'ALL_SORTED',
'value': 'ALL_SORTED',
'text': 'ALL_SORTED'
})
descriptor['type'].append({
'key': 'ALL_FUNCTIONS',
'value': 'ALL_FUNCTIONS',
'text': 'ALL_FUNCTIONS'
})
for pair in pair_list:
print()
print('{}'.format(pair.bin_path))
print('----------------------------------------------------------------------------------')
bin_path_basename = os.path.basename(pair.bin_path)
if bin_path_basename not in filename_set:
filename_set.add(bin_path_basename)
descriptor['filename'].append({
'key': bin_path_basename,
'value': bin_path_basename,
'text': bin_path_basename
})
pdb_path = os.path.join(
'PDB',
os.path.relpath(
os.path.dirname(pair.bin_path),
'Bin'
),
bin_path_basename + '.pdb'
)
version = get_pe_version(pair.bin_path)
_, system, arch, _ = pdb_path.split('\\', 3)
version = '{}-{} ({})'.format(system, arch, version)
if version not in version_set:
version_set.add(version)
descriptor['version'].append({
'key': '{}/{}/System32'.format(system, arch),
'value': '{}/{}/System32'.format(system, arch),
'text': version
})
print(' Copying "{}"'.format(pair.pdb_path))
print(' -> "{}"'.format(pdb_path))
os.makedirs(os.path.dirname(pdb_path), exist_ok=True)
shutil.copy(pair.pdb_path, pdb_path)
dest_path = re.sub('\\\\system32\\\\drivers\\\\', '\\\\System32\\\\', pdb_path, flags=re.I)
dest_dir = os.path.join(
script_dir,
'Output',
os.path.relpath(
os.path.dirname(dest_path),
'PDB'
),
os.path.splitext(os.path.basename(pdb_path))[0]
)
os.makedirs(dest_dir, exist_ok=True)
#
# Dump all.
#
print(' Creating "ALL.h"')
if not os.path.isfile(os.path.join(dest_dir, 'ALL.h')):
subprocess.check_output(
r'tools\pdbex.exe * "{}" -o "{}" -k- -z- -f'.format(pdb_path, os.path.join(dest_dir, 'ALL.h'))
)
else:
print(' ... already exists, skipping')
print(' Creating "ALL_SORTED.h"')
if not os.path.isfile(os.path.join(dest_dir, 'ALL_SORTED.h')):
subprocess.check_output(
r'tools\pdbex.exe * "{}" -o "{}" -k- -z- -p- -f -y'.format(pdb_path, os.path.join(dest_dir, 'ALL_SORTED.h'))
)
else:
print(' ... already exists, skipping')
print(' Creating "ALL_FUNCTIONS.h"')
if not os.path.isfile(os.path.join(dest_dir, 'ALL_FUNCTIONS.h')):
subprocess.check_output(
r'tools\pdbex.exe * "{}" -o "{}" -k- -z- -n- -l- -f'.format(pdb_path, os.path.join(dest_dir, 'ALL_FUNCTIONS.h'))
)
else:
print(' ... already exists, skipping')
dest_path = os.path.join(dest_dir, 'Standalone')
print(' Creating "Standalone\\*.h"')
if not os.path.isdir(dest_path):
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
subprocess.check_output(
r'tools\pdbex.exe % "{}" -o "{}" -k- -z- -p-'.format(pdb_path, dest_path)
)
else:
print(' ... already exists, skipping')
print(' Creating descriptor...')
type_list = subprocess.check_output(
r'tools\pdbex.exe * "{}" -k- -z- -l-'.format(pdb_path)
).strip().splitlines()
print(' Got {} types'.format(len(type_list)))
for type in type_list:
#
# Parse string in format: 'kind _TYPENAME;'
#
kind, type_name = type.decode('ascii').split(' ', 1)
type_name = type_name.rstrip(';')
if type_name not in type_set:
type_set.add(type_name)
descriptor['type'].append({
'key': 'Standalone/' + type_name,
'value': 'Standalone/' + type_name,
'text': type_name
})
with open(os.path.join(script_dir, 'Output', 'descriptor.json'), 'w+') as f:
json.dump(descriptor, f, sort_keys=True, indent=4)
if __name__ == '__main__':
main()