-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
win_reg.py
452 lines (373 loc) · 14.8 KB
/
win_reg.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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import logging
import os
import sys
from pathlib import Path
from pathlib import PureWindowsPath
import attr
try:
from regipy.exceptions import NoRegistrySubkeysException
from regipy.exceptions import RegistryKeyNotFoundException
from regipy.registry import RegistryHive
except ImportError:
pass
from packagedcode import models
# TODO: Find "boilerplate" files, what are the things that we do not care about, e.g. thumbs.db
# TODO: check for chocolatey
# TODO: Windows appstore
TRACE = False
def logger_debug(*args):
pass
logger = logging.getLogger(__name__)
if TRACE:
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
def get_registry_name_key_entry(registry_hive, registry_path):
"""
Return the "name" key entry for ```registry_path`` from the ```registry_hive``
RegistryHive. Return None if the ``registry_path`` is not found.
"""
try:
return registry_hive.get_key(registry_path)
except (RegistryKeyNotFoundException, NoRegistrySubkeysException):
return
def get_registry_tree(registry_location, registry_path):
"""
Return a list of dictionaries of Window registry entries from the hive at
``registry_location`` found under a given ``registry_path``.
"""
registry_hive = RegistryHive(registry_location)
name_key_entry = get_registry_name_key_entry(
registry_hive=registry_hive, registry_path=registry_path
)
if not name_key_entry:
return []
return [
attr.asdict(entry) for entry in registry_hive.recurse_subkeys(name_key_entry, as_json=True)
]
def get_installed_dotnet_versions_from_hive(
location,
datasource_id,
package_type,
registry_path='\\Microsoft\\NET Framework Setup\\NDP',
package_only=False,
):
"""
Yield PackageData for the installed versions of .NET framework from the
registry hive at ``location``.
The logic to retrieve installed .NET version has been outlined here:
https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
"""
registry_tree = get_registry_tree(registry_location=location, registry_path=registry_path)
yield from get_installed_dotnet_versions_from_regtree(
registry_tree=registry_tree,
datasource_id=datasource_id,
package_type=package_type,
package_only=package_only,
)
def get_installed_dotnet_versions_from_regtree(
registry_tree,
datasource_id,
package_type,
package_only=False,
):
"""
Yield PackageData for the installed versions of .NET framework from a
Windows ``registry_tree``.
"""
if not registry_tree:
return
for entry in registry_tree:
# The .NET version can be found in the path whose last segment ends with
# `Full`
if not entry.get('path', '').endswith('\\Full'):
continue
file_references = []
version = None
for values in entry.get('values', []):
key = values.get('name')
value = values.get('value')
if key == 'Version':
version = value
if key == 'InstallPath':
file_references.append(models.FileReference(path=value))
package_data = dict(
datasource_id=datasource_id,
type=package_type,
name='microsoft-dot-net-framework',
version=version,
file_references=file_references,
)
yield models.PackageData.from_data(package_data, package_only)
def get_installed_windows_programs_from_hive(
location,
datasource_id,
package_type,
registry_path='\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
package_only=False,
):
"""
Yield installed Windows PackageData from a Windows registry file at
``location``.
This is done by looking at the entries of the uninstallable programs list.
If ``registry_path`` is provided, then we will load Registry entries
starting from ``registry_path``
"""
registry_tree = get_registry_tree(registry_location=location, registry_path=registry_path)
yield from get_installed_windows_programs_from_regtree(
registry_tree=registry_tree,
datasource_id=datasource_id,
package_type=package_type,
package_only=package_only,
)
def get_installed_windows_programs_from_regtree(
registry_tree,
datasource_id,
package_type,
package_only=False,
):
"""
Yield installed Windows PackageData from a Windows ``registry_tree``.
"""
if not registry_tree:
return
field_by_regkey = {
'DisplayName': 'name',
'DisplayVersion': 'version',
'URLInfoAbout': 'homepage_url',
'Publisher': 'publisher',
'DisplayIcon': 'display_icon',
'UninstallString': 'uninstall_string',
'InstallLocation': 'install_location',
}
for entry in registry_tree:
package_info = {}
for entry_value in entry.get('values', []):
key = entry_value.get('name')
value = entry_value.get('value')
pkg_field = field_by_regkey.get(key)
if pkg_field:
package_info[pkg_field] = value
name = package_info.get('name')
version = package_info.get('version')
homepage_url = package_info.get('homepage_url')
publisher = package_info.get('publisher')
parties = []
if publisher:
parties.append(
models.Party(
type=models.party_org,
role='publisher',
name=publisher,
)
)
file_references = []
install_location = package_info.get('install_location')
if install_location:
file_references.append(models.FileReference(path=install_location))
display_icon = package_info.get('display_icon')
if display_icon:
file_references.append(models.FileReference(path=display_icon))
uninstall_string = package_info.get('uninstall_string')
if uninstall_string:
file_references.append(models.FileReference(path=uninstall_string))
package_data = dict(
datasource_id=datasource_id,
type=package_type,
name=name,
version=version,
parties=parties,
homepage_url=homepage_url,
file_references=file_references,
)
yield models.PackageData.from_data(package_data, package_only)
def get_packages_from_registry_from_hive(
location,
datasource_id,
package_type,
package_only=False,
):
"""
Yield PackageData for Installed Windows Programs from the Windows registry
hive at ``location``
"""
yield from get_installed_windows_programs_from_hive(
location=location,
datasource_id=datasource_id,
package_type=package_type,
registry_path='\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
package_only=package_only,
)
yield from get_installed_windows_programs_from_hive(
location=location,
datasource_id=datasource_id,
package_type=package_type,
registry_path='\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall',
package_only=package_only,
)
yield from get_installed_dotnet_versions_from_hive(
location=location,
datasource_id=datasource_id,
package_type=package_type,
registry_path='\\Microsoft\\NET Framework Setup\\NDP',
package_only=package_only,
)
def get_installed_packages(root_dir, is_container=True, package_only=False):
"""
Yield PackageData for Installed Windows Programs for every detected
installed program from Windows registry hive files found in well known
locations under a ``root_dir`` root filesystem directory.
"""
# These paths are relative to a Windows docker image layer root directory
if is_container:
hives_software_delta_loc = os.path.join(root_dir, 'Hives/Software_Delta')
files_software_loc = os.path.join(root_dir, 'Files/Windows/System32/config/SOFTWARE')
utilityvm_software_loc = os.path.join(
root_dir, 'UtilityVM/Files/Windows/System32/config/SOFTWARE'
)
root_prefixes_by_software_reg_locations = {
hives_software_delta_loc: 'Files',
files_software_loc: 'Files',
utilityvm_software_loc: 'UtilityVM/Files',
}
else:
# TODO: Add support for virtual machines
raise Exception('Unsuported file system type')
for software_reg_loc, root_prefix in root_prefixes_by_software_reg_locations.items():
if not os.path.exists(software_reg_loc):
continue
for package in get_packages_from_registry_from_hive(software_reg_loc, package_only):
package.populate_installed_files(root_dir, root_prefix=root_prefix)
yield package
def remove_drive_letter(path):
"""
Given a Windows path string, remove the leading drive letter and return the
path string as a POSIX-styled path.
"""
# Remove leading drive letter ("C:\\")
path = PureWindowsPath(path)
path_no_drive_letter = path.relative_to(*path.parts[:1])
# POSIX-ize the path
posixed_path = path_no_drive_letter.as_posix()
return posixed_path
def create_absolute_installed_file_path(root_dir, file_path):
"""
Return an absolute path to `file_path` given the root directory path at
`root_dir`
"""
file_path = remove_drive_letter(file_path)
# Append the install location to the path string `root_dir`
return str(Path(root_dir).joinpath(file_path))
def create_relative_file_path(file_path, root_dir, root_prefix=''):
"""
Return a subpath of `file_path` that is relative to `root_dir`
>>> file_path = '/home/test/example/foo.txt'
>>> root_dir = '/home/test/'
>>> create_relative_file_path(file_path, root_dir)
'example/foo.txt'
If there is a `root_prefix`, then it is prepended to the resulting
relative file path.
>>> file_path = '/home/test/example/foo.txt'
>>> root_dir = '/home/test/'
>>> create_relative_file_path(file_path, root_dir, 'prefix')
'prefix/example/foo.txt'
"""
relative_file_path = str(Path(file_path).relative_to(root_dir))
if root_prefix:
return os.path.join(root_prefix, relative_file_path)
return relative_file_path
class BaseRegInstalledProgramHandler(models.DatafileHandler):
default_package_type = 'windows-program'
documentation_url = 'https://en.wikipedia.org/wiki/Windows_Registry'
# The rootfs location (of a Docker image layer) can be in a
# subdirectory of the layer tree. This is a path to the root of the windows filesystem relative to the
# datafile (e.g. the registry hive file)
root_path_relative_to_datafile_path = None
@classmethod
def parse(cls, location, package_only=False):
yield from get_packages_from_registry_from_hive(
location=location,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
package_only=package_only,
)
@classmethod
def get_root_resource(cls, resource, codebase):
"""
Return the root Resource given a datafile ``resource`` in ``codebase``.
"""
segments = cls.root_path_relative_to_datafile_path.split('/')
has_root = True
for segment in segments:
if segment == '..':
resource = resource.parent(codebase)
if not resource:
has_root = False
break
else:
ress = [r for r in resource.children(codebase) if r.name == segment]
if not len(ress) == 1:
has_root = False
break
resource = ress[0]
if not resource:
has_root = False
break
if has_root:
return resource
@classmethod
def assign_package_to_resources(cls, package, resource, codebase, package_adder):
package_uid = package.package_uid
if package_uid:
package_adder(package_uid, resource, codebase)
refs = package.file_references
if not refs:
return
root = cls.get_root_resource(resource, codebase)
if not root:
# FIXME: this should not happen
return
root_path = Path(root.path)
refs_by_path = {}
for ref in refs:
# a file ref may be a Windows path with a drive
ref_path = remove_drive_letter(ref.path)
# a file ref extends from the root of the Windows filesystem
refs_by_path[str(root_path / ref_path)] = ref
for res in root.walk(codebase):
ref = refs_by_path.get(res.path)
if not ref:
continue
if package_uid:
# path is found and processed: remove it, so we can check if we
# found all of them
del refs_by_path[res.path]
package_adder(package_uid, res, codebase)
# if we have left over file references, add these to extra data
if refs_by_path:
missing = sorted(refs_by_path.values(), key=lambda r: r.path)
package.extra_data['missing_file_references'] = missing
class InstalledProgramFromDockerSoftwareDeltaHandler(BaseRegInstalledProgramHandler):
datasource_id = 'win_reg_installed_programs_docker_software_delta'
path_patterns = ('*/Hives/Software_Delta',)
description = 'Windows Registry Installed Program - Docker Software Delta'
root_path_relative_to_datafile_path = '../../Files'
class InstalledProgramFromDockerFilesSoftwareHandler(BaseRegInstalledProgramHandler):
datasource_id = 'win_reg_installed_programs_docker_file_software'
path_patterns = ('*/Files/Windows/System32/config/SOFTWARE',)
description = 'Windows Registry Installed Program - Docker SOFTWARE'
root_path_relative_to_datafile_path = '../../../..'
class InstalledProgramFromDockerUtilityvmSoftwareHandler(BaseRegInstalledProgramHandler):
datasource_id = 'win_reg_installed_programs_docker_utility_software'
path_patterns = ('*/UtilityVM/Files/Windows/System32/config/SOFTWARE',)
description = 'Windows Registry Installed Program - Docker UtilityVM SOFTWARE'
root_path_relative_to_datafile_path = '../../../..'