-
Notifications
You must be signed in to change notification settings - Fork 20
/
filters.py
201 lines (149 loc) · 5.85 KB
/
filters.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
# -*- coding: utf-8 -*-
"""The Windows Registry key and value filters."""
import abc
class BaseWindowsRegistryKeyFilter(object):
"""Windows Registry key filter interface."""
# Note that redundant-returns-doc is broken for pylint 1.7.x
# pylint: disable=redundant-returns-doc
@property
def key_paths(self):
"""List of key paths defined by the filter."""
return []
@abc.abstractmethod
def Match(self, registry_key):
"""Determines if a Windows Registry key matches the filter.
Args:
registry_key (dfwinreg.WinRegistryKey): a Windows Registry key.
Returns:
bool: True if a match, False otherwise.
"""
class WindowsRegistryKeyPathFilter(BaseWindowsRegistryKeyFilter):
"""Windows Registry key path filter."""
_CONTROL_SET_PREFIX = (
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet')
# This list must be ordered with most specific matches first.
_WOW64_PREFIXES = [
'HKEY_CURRENT_USER\\Software\\Classes',
'HKEY_CURRENT_USER\\Software',
'HKEY_LOCAL_MACHINE\\Software\\Classes',
'HKEY_LOCAL_MACHINE\\Software']
def __init__(self, key_path):
"""Initializes a Windows Registry key filter.
Args:
key_path (str): key path.
"""
super(WindowsRegistryKeyPathFilter, self).__init__()
key_path.rstrip('\\')
self._key_path = key_path
key_path = key_path.upper()
self._key_path_upper = key_path
self._wow64_key_path = None
self._wow64_key_path_upper = None
if key_path.startswith(self._CONTROL_SET_PREFIX.upper()):
self._key_path_prefix, _, self._key_path_suffix = key_path.partition(
'CurrentControlSet'.upper())
else:
self._key_path_prefix = None
self._key_path_suffix = None
# Handle WoW64 Windows Registry key redirection.
# Also see:
# https://msdn.microsoft.com/en-us/library/windows/desktop/
# ms724072%28v=vs.85%29.aspx
# https://msdn.microsoft.com/en-us/library/windows/desktop/
# aa384253(v=vs.85).aspx
wow64_prefix = None
for key_path_prefix in self._WOW64_PREFIXES:
if key_path.startswith(key_path_prefix.upper()):
wow64_prefix = key_path_prefix
break
if wow64_prefix:
key_path_suffix = self._key_path[len(wow64_prefix):]
if key_path_suffix.startswith('\\'):
key_path_suffix = key_path_suffix[1:]
self._wow64_key_path = '\\'.join([
wow64_prefix, 'Wow6432Node', key_path_suffix])
self._wow64_key_path_upper = self._wow64_key_path.upper()
@property
def key_paths(self):
"""Retrieves the key paths defined by the filter.
Returns:
list[str]: key paths defined by the filter.
"""
if self._wow64_key_path:
return [self._key_path, self._wow64_key_path]
return [self._key_path]
def Match(self, registry_key):
"""Determines if a Windows Registry key matches the filter.
Args:
registry_key (dfwinreg.WinRegistryKey): a Windows Registry key.
Returns:
bool: True if a match, False otherwise.
"""
key_path = registry_key.path.upper()
if self._key_path_prefix and self._key_path_suffix:
if (key_path.startswith(self._key_path_prefix) and
key_path.endswith(self._key_path_suffix)):
key_path_segment = key_path[
len(self._key_path_prefix):-len(self._key_path_suffix)]
if key_path_segment.startswith('ControlSet'.upper()):
try:
control_set = int(key_path_segment[10:], 10)
except ValueError:
control_set = None
# TODO: check if control_set is in bounds.
return control_set is not None
return key_path in (self._key_path_upper, self._wow64_key_path_upper)
class WindowsRegistryKeyPathPrefixFilter(BaseWindowsRegistryKeyFilter):
"""Windows Registry key path prefix filter."""
def __init__(self, key_path_prefix):
"""Initializes a Windows Registry key filter.
Args:
key_path_prefix (str): key path prefix.
"""
super(WindowsRegistryKeyPathPrefixFilter, self).__init__()
self._key_path_prefix = key_path_prefix
def Match(self, registry_key):
"""Determines if a Windows Registry key matches the filter.
Args:
registry_key (dfwinreg.WinRegistryKey): a Windows Registry key.
Returns:
bool: True if a match, False otherwise.
"""
return registry_key.path.startsswith(self._key_path_prefix)
class WindowsRegistryKeyPathSuffixFilter(BaseWindowsRegistryKeyFilter):
"""Windows Registry key path suffix filter."""
def __init__(self, key_path_suffix):
"""Initializes a Windows Registry key filter.
Args:
key_path_suffix (str): key path suffix.
"""
super(WindowsRegistryKeyPathSuffixFilter, self).__init__()
self._key_path_suffix = key_path_suffix
def Match(self, registry_key):
"""Determines if a Windows Registry key matches the filter.
Args:
registry_key (dfwinreg.WinRegistryKey): a Windows Registry key.
Returns:
bool: True if a match, False otherwise.
"""
return registry_key.path.endswith(self._key_path_suffix)
class WindowsRegistryKeyWithValuesFilter(BaseWindowsRegistryKeyFilter):
"""Windows Registry key with values filter."""
_EMPTY_SET = frozenset()
def __init__(self, value_names):
"""Initializes a Windows Registry key filter.
Args:
value_names (list[str]): value names that should be present in the key.
"""
super(WindowsRegistryKeyWithValuesFilter, self).__init__()
self._value_names = frozenset(value_names)
def Match(self, registry_key):
"""Determines if a Windows Registry key matches the filter.
Args:
registry_key (dfwinreg.WinRegistryKey): a Windows Registry key.
Returns:
bool: True if a match, False otherwise.
"""
value_names = frozenset([
registry_value.name for registry_value in registry_key.GetValues()])
return self._value_names.issubset(value_names)