[204] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
[210] | 3 | ## @package pyregfi |
---|
| 4 | # Python interface to the regfi library. |
---|
| 5 | # |
---|
| 6 | |
---|
[221] | 7 | ## @mainpage API Documentation |
---|
| 8 | # |
---|
| 9 | # The pyregfi module provides a Python interface to the @ref regfi Windows |
---|
| 10 | # registry library. |
---|
| 11 | # |
---|
| 12 | # The library operates on registry hives, each of which is contained within a |
---|
[257] | 13 | # single file. The quickest way to get started, is to use the @ref openHive() |
---|
| 14 | # function to obtain a Hive object. For example: |
---|
[221] | 15 | # @code |
---|
| 16 | # >>> import pyregfi |
---|
[257] | 17 | # >>> myHive = pyregfi.openHive('/mnt/win/c/WINDOWS/system32/config/system') |
---|
[221] | 18 | # @endcode |
---|
| 19 | # |
---|
| 20 | # Using this Hive object, one can begin investigating what top-level keys |
---|
| 21 | # exist by starting with the root Key attribute: |
---|
| 22 | # @code |
---|
| 23 | # >>> for key in myHive.root.subkeys: |
---|
| 24 | # ... print(key.name) |
---|
| 25 | # ControlSet001 |
---|
| 26 | # ControlSet003 |
---|
| 27 | # LastKnownGoodRecovery |
---|
| 28 | # MountedDevices |
---|
| 29 | # Select |
---|
| 30 | # Setup |
---|
| 31 | # WPA |
---|
| 32 | # @endcode |
---|
| 33 | # |
---|
| 34 | # From there, accessing subkeys and values by name is a simple matter of: |
---|
| 35 | # @code |
---|
| 36 | # >>> myKey = myHive.root.subkeys['Select'] |
---|
| 37 | # >>> myValue = myKey.values['Current'] |
---|
| 38 | # @endcode |
---|
| 39 | # |
---|
| 40 | # The data associated with a Value can be obtained through the fetch_data() |
---|
| 41 | # method: |
---|
| 42 | # @code |
---|
| 43 | # >>> print(myValue.fetch_data()) |
---|
| 44 | # 1 |
---|
| 45 | # @endcode |
---|
| 46 | # |
---|
| 47 | # While useful for simple exercises, using the subkeys object for deeply nested |
---|
| 48 | # paths is not efficient and doesn't make for particularly attractive code. |
---|
| 49 | # Instead, a special-purpose HiveIterator class is provided for simplicity of |
---|
| 50 | # use and fast access to specific known paths: |
---|
| 51 | # @code |
---|
| 52 | # >>> myIter = pyregfi.HiveIterator(myHive) |
---|
| 53 | # >>> myIter.descend(['ControlSet001','Control','NetworkProvider','HwOrder']) |
---|
| 54 | # >>> myKey = myIter.current_key() |
---|
| 55 | # >>> print(myKey.values['ProviderOrder'].fetch_data()) |
---|
| 56 | # RDPNP,LanmanWorkstation,WebClient |
---|
| 57 | # @endcode |
---|
| 58 | # |
---|
| 59 | # The first two lines above can be simplified in some "syntactic sugar" provided |
---|
| 60 | # by the Hive.subtree() method. Also, as one might expect, the HiveIterator |
---|
| 61 | # also acts as an iterator, producing keys in a depth-first order. |
---|
| 62 | # For instance, to traverse all keys under the ControlSet003\\Services key, |
---|
| 63 | # printing their names as we go, we could do: |
---|
| 64 | # @code |
---|
| 65 | # >>> for key in Hive.subtree(['ControlSet003','Services']): |
---|
| 66 | # >>> print(key.name) |
---|
| 67 | # Services |
---|
| 68 | # Abiosdsk |
---|
| 69 | # abp480n5 |
---|
| 70 | # Parameters |
---|
| 71 | # PnpInterface |
---|
| 72 | # ACPI |
---|
| 73 | # [...] |
---|
| 74 | # @endcode |
---|
| 75 | # |
---|
| 76 | # Note that "Services" was printed first, since the subtree is traversed as a |
---|
| 77 | # "preordering depth-first" search starting with the HiveIterator's current_key(). |
---|
| 78 | # As one might expect, traversals of subtrees stops when all elements in a |
---|
| 79 | # specific subtree (and none outside of it) have been traversed. |
---|
| 80 | # |
---|
| 81 | # For more information, peruse the various attributes and methods available on |
---|
| 82 | # the Hive, HiveIterator, Key, Value, and Security classes. |
---|
| 83 | # |
---|
| 84 | # @note @ref regfi is a read-only library by design and there |
---|
| 85 | # are no plans to implement write support. |
---|
| 86 | # |
---|
| 87 | # @note At present, pyregfi has been tested with Python versions 2.6 and 3.1 |
---|
| 88 | # |
---|
| 89 | # @note Developers strive to make pyregfi thread-safe. |
---|
| 90 | # |
---|
| 91 | # @note Key and Value names are case-sensitive in regfi and pyregfi |
---|
| 92 | # |
---|
[204] | 93 | import sys |
---|
[219] | 94 | import time |
---|
[204] | 95 | import ctypes |
---|
| 96 | import ctypes.util |
---|
[228] | 97 | import threading |
---|
| 98 | from pyregfi.structures import * |
---|
[204] | 99 | |
---|
[228] | 100 | |
---|
[221] | 101 | ## An enumeration of registry Value data types |
---|
[210] | 102 | # |
---|
[221] | 103 | # @note This is a static class, there is no need to instantiate it. |
---|
| 104 | # Just access its attributes directly as DATA_TYPES.SZ, etc |
---|
| 105 | class DATA_TYPES(object): |
---|
| 106 | ## None / Unknown |
---|
| 107 | NONE = 0 |
---|
| 108 | ## String |
---|
| 109 | SZ = 1 |
---|
| 110 | ## String with %...% expansions |
---|
| 111 | EXPAND_SZ = 2 |
---|
| 112 | ## Binary buffer |
---|
| 113 | BINARY = 3 |
---|
| 114 | ## 32 bit integer (little endian) |
---|
| 115 | DWORD = 4 # DWORD, little endian |
---|
| 116 | ## 32 bit integer (little endian) |
---|
| 117 | DWORD_LE = 4 |
---|
| 118 | ## 32 bit integer (big endian) |
---|
| 119 | DWORD_BE = 5 # DWORD, big endian |
---|
| 120 | ## Symbolic link |
---|
| 121 | LINK = 6 |
---|
| 122 | ## List of strings |
---|
| 123 | MULTI_SZ = 7 |
---|
| 124 | ## Unknown structure |
---|
| 125 | RESOURCE_LIST = 8 |
---|
| 126 | ## Unknown structure |
---|
| 127 | FULL_RESOURCE_DESCRIPTOR = 9 |
---|
| 128 | ## Unknown structure |
---|
| 129 | RESOURCE_REQUIREMENTS_LIST = 10 |
---|
| 130 | ## 64 bit integer |
---|
| 131 | QWORD = 11 # 64-bit little endian |
---|
[205] | 132 | |
---|
| 133 | |
---|
[227] | 134 | ## An enumeration of log message types |
---|
| 135 | # |
---|
| 136 | # @note This is a static class, there is no need to instantiate it. |
---|
| 137 | # Just access its attributes directly as LOG_TYPES.INFO, etc |
---|
| 138 | class LOG_TYPES(object): |
---|
| 139 | ## Informational messages, useful in debugging |
---|
| 140 | INFO = 0x01 |
---|
| 141 | ## Non-critical problems in structure parsing or intepretation |
---|
| 142 | WARN = 0x04 |
---|
| 143 | ## Major failures |
---|
| 144 | ERROR = 0x10 |
---|
| 145 | |
---|
| 146 | |
---|
[208] | 147 | def _buffer2bytearray(char_pointer, length): |
---|
| 148 | if length == 0 or char_pointer == None: |
---|
| 149 | return None |
---|
| 150 | |
---|
| 151 | ret_val = bytearray(length) |
---|
| 152 | for i in range(0,length): |
---|
| 153 | ret_val[i] = char_pointer[i][0] |
---|
| 154 | |
---|
| 155 | return ret_val |
---|
| 156 | |
---|
| 157 | |
---|
[215] | 158 | def _strlist2charss(str_list): |
---|
| 159 | ret_val = [] |
---|
| 160 | for s in str_list: |
---|
| 161 | ret_val.append(s.encode('utf-8', 'replace')) |
---|
| 162 | |
---|
[220] | 163 | ret_val = (ctypes.c_char_p*(len(str_list)+1))(*ret_val) |
---|
[215] | 164 | # Terminate the char** with a NULL pointer |
---|
| 165 | ret_val[-1] = 0 |
---|
| 166 | |
---|
| 167 | return ret_val |
---|
| 168 | |
---|
| 169 | |
---|
[209] | 170 | def _charss2strlist(chars_pointer): |
---|
| 171 | ret_val = [] |
---|
| 172 | i = 0 |
---|
| 173 | s = chars_pointer[i] |
---|
[252] | 174 | while s: |
---|
[213] | 175 | ret_val.append(s.decode('utf-8', 'replace')) |
---|
[209] | 176 | i += 1 |
---|
| 177 | s = chars_pointer[i] |
---|
[208] | 178 | |
---|
[209] | 179 | return ret_val |
---|
[208] | 180 | |
---|
[210] | 181 | |
---|
[252] | 182 | |
---|
[233] | 183 | ## Returns the (py)regfi library version |
---|
| 184 | # |
---|
| 185 | # @return A string indicating the version |
---|
| 186 | def getVersion(): |
---|
| 187 | return regfi.regfi_version() |
---|
| 188 | |
---|
| 189 | |
---|
[221] | 190 | ## Retrieves messages produced by regfi during parsing and interpretation |
---|
| 191 | # |
---|
| 192 | # The regfi C library may generate log messages stored in a special thread-safe |
---|
| 193 | # global data structure. These messages should be retrieved periodically or |
---|
| 194 | # after each major operation by callers to determine if any errors or warnings |
---|
| 195 | # should be reported to the user. Failure to retrieve these could result in |
---|
| 196 | # excessive memory consumption. |
---|
[232] | 197 | def getLogMessages(): |
---|
[221] | 198 | msgs = regfi.regfi_log_get_str() |
---|
[226] | 199 | if not msgs: |
---|
[221] | 200 | return '' |
---|
| 201 | return msgs.decode('utf-8') |
---|
| 202 | |
---|
| 203 | |
---|
[227] | 204 | ## Sets the types of log messages to record |
---|
| 205 | # |
---|
| 206 | # @param log_types A sequence of message types that regfi should generate. |
---|
| 207 | # Message types can be found in the LOG_TYPES enumeration. |
---|
| 208 | # |
---|
| 209 | # @return True on success, False on failure. Failures are rare, but could |
---|
| 210 | # indicate that global logging is not operating as expected. |
---|
| 211 | # |
---|
| 212 | # Example: |
---|
| 213 | # @code |
---|
[232] | 214 | # setLogMask((LOG_TYPES.ERROR, LOG_TYPES.WARN, LOG_TYPES.INFO)) |
---|
[227] | 215 | # @endcode |
---|
| 216 | # |
---|
| 217 | # The message mask is a global (all hives, iterators), thread-specific value. |
---|
| 218 | # For more information, see @ref regfi_log_set_mask. |
---|
| 219 | # |
---|
[232] | 220 | def setLogMask(log_types): |
---|
[227] | 221 | mask = 0 |
---|
| 222 | for m in log_types: |
---|
| 223 | mask |= m |
---|
| 224 | return regfi.regfi_log_set_mask(mask) |
---|
| 225 | |
---|
| 226 | |
---|
[232] | 227 | ## Opens a file as a registry hive |
---|
| 228 | # |
---|
| 229 | # @param path The file path of a hive, as one would provide to the |
---|
| 230 | # open() built-in |
---|
| 231 | # |
---|
| 232 | # @return A new Hive instance |
---|
| 233 | def openHive(path): |
---|
| 234 | fh = open(path, 'rb') |
---|
| 235 | return Hive(fh) |
---|
| 236 | |
---|
| 237 | |
---|
[221] | 238 | ## Abstract class for most objects returned by the library |
---|
[212] | 239 | class _StructureWrapper(object): |
---|
[214] | 240 | _hive = None |
---|
| 241 | _base = None |
---|
[206] | 242 | |
---|
[207] | 243 | def __init__(self, hive, base): |
---|
[215] | 244 | if not hive: |
---|
| 245 | raise Exception("Could not create _StructureWrapper," |
---|
| 246 | + " hive is NULL. Current log:\n" |
---|
[232] | 247 | + getLogMessages()) |
---|
[215] | 248 | if not base: |
---|
| 249 | raise Exception("Could not create _StructureWrapper," |
---|
| 250 | + " base is NULL. Current log:\n" |
---|
[232] | 251 | + getLogMessages()) |
---|
[214] | 252 | self._hive = hive |
---|
| 253 | self._base = base |
---|
[206] | 254 | |
---|
[224] | 255 | |
---|
[221] | 256 | # Memory management for most regfi structures is taken care of here |
---|
[206] | 257 | def __del__(self): |
---|
[255] | 258 | if self._base: |
---|
| 259 | regfi.regfi_free_record(self._hive.file, self._base) |
---|
[206] | 260 | |
---|
[224] | 261 | |
---|
[221] | 262 | # Any attribute requests not explicitly defined in subclasses gets passed |
---|
| 263 | # to the equivalent REGFI_* structure defined in structures.py |
---|
[206] | 264 | def __getattr__(self, name): |
---|
[214] | 265 | return getattr(self._base.contents, name) |
---|
[224] | 266 | |
---|
[221] | 267 | |
---|
| 268 | ## Test for equality |
---|
| 269 | # |
---|
| 270 | # Records returned by pyregfi may be compared with one another. For example: |
---|
| 271 | # @code |
---|
| 272 | # >>> key2 = key1.subkeys['child'] |
---|
| 273 | # >>> key1 == key2 |
---|
| 274 | # False |
---|
| 275 | # >>> key1 != key2 |
---|
| 276 | # True |
---|
| 277 | # >>> key1 == key2.get_parent() |
---|
| 278 | # True |
---|
| 279 | # @endcode |
---|
[206] | 280 | def __eq__(self, other): |
---|
| 281 | return (type(self) == type(other)) and (self.offset == other.offset) |
---|
| 282 | |
---|
[224] | 283 | |
---|
[206] | 284 | def __ne__(self, other): |
---|
| 285 | return (not self.__eq__(other)) |
---|
| 286 | |
---|
[208] | 287 | |
---|
[221] | 288 | class Key(): |
---|
[206] | 289 | pass |
---|
| 290 | |
---|
[221] | 291 | |
---|
| 292 | class Value(): |
---|
[206] | 293 | pass |
---|
| 294 | |
---|
[221] | 295 | |
---|
[253] | 296 | |
---|
| 297 | ## Represents a registry SK record which contains a security descriptor |
---|
| 298 | # |
---|
[206] | 299 | class Security(_StructureWrapper): |
---|
[257] | 300 | ## Number of registry Keys referencing this SK record |
---|
[253] | 301 | ref_count = 1 |
---|
[206] | 302 | |
---|
[253] | 303 | ## The absolute file offset of the SK record's cell in the Hive file |
---|
| 304 | offset = 0xCAFEBABE |
---|
| 305 | |
---|
[257] | 306 | ## The @ref winsec.SecurityDescriptor for this SK record |
---|
[253] | 307 | descriptor = object() |
---|
| 308 | |
---|
| 309 | def __init__(self, hive, base): |
---|
| 310 | super(Security, self).__init__(hive, base) |
---|
| 311 | # XXX: add checks for NULL pointers |
---|
| 312 | self.descriptor = winsec.SecurityDescriptor(base.contents.sec_desc.contents) |
---|
| 313 | |
---|
[257] | 314 | ## Loads the "next" Security record in the hive |
---|
[253] | 315 | # |
---|
| 316 | # @note |
---|
| 317 | # SK records are included in a circular, doubly-linked list. |
---|
| 318 | # To iterate over all SK records, be sure to check for the repetition of |
---|
| 319 | # the SK record you started with to determine when all have been traversed. |
---|
| 320 | def next_security(self): |
---|
| 321 | return Security(self._hive, |
---|
| 322 | regfi.regfi_next_sk(self._hive.file, self._base)) |
---|
| 323 | |
---|
| 324 | ## Loads the "previous" Security record in the hive |
---|
| 325 | # |
---|
| 326 | # @note |
---|
| 327 | # SK records are included in a circular, doubly-linked list. |
---|
| 328 | # To iterate over all SK records, be sure to check for the repetition of |
---|
| 329 | # the SK record you started with to determine when all have been traversed. |
---|
| 330 | def prev_security(self): |
---|
| 331 | return Security(self._hive, |
---|
| 332 | regfi.regfi_prev_sk(self._hive.file, self._base)) |
---|
| 333 | |
---|
| 334 | |
---|
[221] | 335 | ## Abstract class for ValueList and SubkeyList |
---|
[212] | 336 | class _GenericList(object): |
---|
[257] | 337 | # XXX: consider implementing keys(), values(), items() and other dictionary methods |
---|
[214] | 338 | _hive = None |
---|
[224] | 339 | _key_base = None |
---|
[214] | 340 | _length = None |
---|
| 341 | _current = None |
---|
[207] | 342 | |
---|
[221] | 343 | # implementation-specific functions for SubkeyList and ValueList |
---|
[214] | 344 | _fetch_num = None |
---|
| 345 | _find_element = None |
---|
| 346 | _get_element = None |
---|
| 347 | _constructor = None |
---|
[208] | 348 | |
---|
[207] | 349 | def __init__(self, key): |
---|
[224] | 350 | if not key: |
---|
| 351 | raise Exception("Could not create _GenericList; key is NULL." |
---|
[232] | 352 | + "Current log:\n" + getLogMessages()) |
---|
[252] | 353 | |
---|
| 354 | base = regfi.regfi_reference_record(key._hive.file, key._base) |
---|
| 355 | if not base: |
---|
[224] | 356 | raise Exception("Could not create _GenericList; memory error." |
---|
[232] | 357 | + "Current log:\n" + getLogMessages()) |
---|
[252] | 358 | self._key_base = cast(base, type(key._base)) |
---|
[224] | 359 | self._length = self._fetch_num(self._key_base) |
---|
[214] | 360 | self._hive = key._hive |
---|
| 361 | |
---|
[207] | 362 | |
---|
[224] | 363 | def __del__(self): |
---|
[228] | 364 | regfi.regfi_free_record(self._hive.file, self._key_base) |
---|
[224] | 365 | |
---|
[228] | 366 | |
---|
[221] | 367 | ## Length of list |
---|
[207] | 368 | def __len__(self): |
---|
[214] | 369 | return self._length |
---|
[207] | 370 | |
---|
[221] | 371 | |
---|
| 372 | ## Retrieves a list element by name |
---|
| 373 | # |
---|
[257] | 374 | # @param name The name of the subkey or value desired. |
---|
| 375 | # This is case-sensitive. |
---|
| 376 | # |
---|
| 377 | # @note The registry format does inherently prevent multiple |
---|
| 378 | # subkeys or values from having the same name. |
---|
| 379 | # This interface simply returns the first match. |
---|
| 380 | # Lookups using this method could also fail due to incorrectly |
---|
| 381 | # encoded strings. |
---|
| 382 | # To identify any duplicates, use the iterator interface to |
---|
| 383 | # check every list element. |
---|
| 384 | # |
---|
[221] | 385 | # @return the first element whose name matches, or None if the element |
---|
| 386 | # could not be found |
---|
[207] | 387 | def __getitem__(self, name): |
---|
[257] | 388 | # XXX: Consider interpreting integer names as offsets in the underlying list |
---|
[220] | 389 | index = ctypes.c_uint32() |
---|
[208] | 390 | if isinstance(name, str): |
---|
| 391 | name = name.encode('utf-8') |
---|
| 392 | |
---|
[209] | 393 | if name != None: |
---|
| 394 | name = create_string_buffer(bytes(name)) |
---|
| 395 | |
---|
[224] | 396 | if self._find_element(self._hive.file, self._key_base, |
---|
[220] | 397 | name, byref(index)): |
---|
| 398 | return self._constructor(self._hive, |
---|
[214] | 399 | self._get_element(self._hive.file, |
---|
[224] | 400 | self._key_base, |
---|
[214] | 401 | index)) |
---|
[207] | 402 | raise KeyError('') |
---|
| 403 | |
---|
[257] | 404 | |
---|
| 405 | ## Fetches the requested element by name, or the default value if the lookup |
---|
| 406 | # fails. |
---|
| 407 | # |
---|
[209] | 408 | def get(self, name, default): |
---|
| 409 | try: |
---|
| 410 | return self[name] |
---|
| 411 | except KeyError: |
---|
| 412 | return default |
---|
| 413 | |
---|
[207] | 414 | def __iter__(self): |
---|
[214] | 415 | self._current = 0 |
---|
[207] | 416 | return self |
---|
| 417 | |
---|
| 418 | def __next__(self): |
---|
[214] | 419 | if self._current >= self._length: |
---|
[207] | 420 | raise StopIteration('') |
---|
| 421 | |
---|
[224] | 422 | elem = self._get_element(self._hive.file, self._key_base, |
---|
[220] | 423 | ctypes.c_uint32(self._current)) |
---|
[214] | 424 | self._current += 1 |
---|
| 425 | return self._constructor(self._hive, elem) |
---|
[207] | 426 | |
---|
[212] | 427 | # For Python 2.x |
---|
[214] | 428 | next = __next__ |
---|
[207] | 429 | |
---|
[212] | 430 | |
---|
[221] | 431 | ## The list of subkeys associated with a Key |
---|
| 432 | # |
---|
| 433 | # This attribute is both iterable: |
---|
| 434 | # @code |
---|
| 435 | # for k in myKey.subkeys: |
---|
| 436 | # ... |
---|
| 437 | # @endcode |
---|
| 438 | # and accessible as a dictionary: |
---|
| 439 | # @code |
---|
| 440 | # mySubkey = myKey.subkeys["keyName"] |
---|
| 441 | # @endcode |
---|
| 442 | # |
---|
[257] | 443 | # You may also request the len() of a subkeys list. |
---|
| 444 | # However keys(), values(), items() and similar methods are not currently |
---|
| 445 | # implemented. |
---|
[221] | 446 | class SubkeyList(_GenericList): |
---|
[214] | 447 | _fetch_num = regfi.regfi_fetch_num_subkeys |
---|
| 448 | _find_element = regfi.regfi_find_subkey |
---|
| 449 | _get_element = regfi.regfi_get_subkey |
---|
[208] | 450 | |
---|
| 451 | |
---|
[221] | 452 | ## The list of values associated with a Key |
---|
| 453 | # |
---|
| 454 | # This attribute is both iterable: |
---|
| 455 | # @code |
---|
| 456 | # for v in myKey.values: |
---|
| 457 | # ... |
---|
| 458 | # @endcode |
---|
| 459 | # and accessible as a dictionary: |
---|
| 460 | # @code |
---|
| 461 | # myValue = myKey.values["valueName"] |
---|
| 462 | # @endcode |
---|
| 463 | # |
---|
[257] | 464 | # You may also request the len() of a values list. |
---|
| 465 | # However keys(), values(), items() and similar methods are not currently |
---|
| 466 | # implemented. |
---|
[221] | 467 | class ValueList(_GenericList): |
---|
[214] | 468 | _fetch_num = regfi.regfi_fetch_num_values |
---|
| 469 | _find_element = regfi.regfi_find_value |
---|
| 470 | _get_element = regfi.regfi_get_value |
---|
[208] | 471 | |
---|
| 472 | |
---|
[215] | 473 | ## Registry key |
---|
[221] | 474 | # These represent registry keys (@ref REGFI_NK records) and provide |
---|
| 475 | # access to their subkeys, values, and other metadata. |
---|
| 476 | # |
---|
[257] | 477 | # @note Key instances may provide access to more attributes than are |
---|
[221] | 478 | # documented here. However, undocumented attributes may change over time |
---|
| 479 | # and are not officially supported. If you need access to an attribute |
---|
[257] | 480 | # not shown here, see @ref pyregfi.structures. |
---|
[207] | 481 | class Key(_StructureWrapper): |
---|
[221] | 482 | ## A @ref ValueList object representing the list of Values |
---|
| 483 | # stored on this Key |
---|
[207] | 484 | values = None |
---|
[221] | 485 | |
---|
| 486 | ## A @ref SubkeyList object representing the list of subkeys |
---|
| 487 | # stored on this Key |
---|
[208] | 488 | subkeys = None |
---|
[207] | 489 | |
---|
[221] | 490 | ## The raw Key name as an uninterpreted bytearray |
---|
| 491 | name_raw = (b"...") |
---|
| 492 | |
---|
| 493 | ## The name of the Key as a (unicode) string |
---|
| 494 | name = "..." |
---|
| 495 | |
---|
| 496 | ## The absolute file offset of the Key record's cell in the Hive file |
---|
| 497 | offset = 0xCAFEBABE |
---|
| 498 | |
---|
| 499 | ## This Key's last modified time represented as the number of seconds |
---|
| 500 | # since the UNIX epoch in UTC; similar to what time.time() returns |
---|
| 501 | modified = 1300000000.123456 |
---|
| 502 | |
---|
| 503 | ## The NK record's flags field |
---|
| 504 | flags = 0x10110001 |
---|
| 505 | |
---|
[207] | 506 | def __init__(self, hive, base): |
---|
| 507 | super(Key, self).__init__(hive, base) |
---|
[221] | 508 | self.values = ValueList(self) |
---|
| 509 | self.subkeys = SubkeyList(self) |
---|
[207] | 510 | |
---|
[208] | 511 | def __getattr__(self, name): |
---|
| 512 | if name == "name": |
---|
[219] | 513 | ret_val = super(Key, self).__getattr__(name) |
---|
| 514 | |
---|
[252] | 515 | if not ret_val: |
---|
[209] | 516 | ret_val = self.name_raw |
---|
| 517 | else: |
---|
[213] | 518 | ret_val = ret_val.decode('utf-8', 'replace') |
---|
[209] | 519 | |
---|
[208] | 520 | elif name == "name_raw": |
---|
[219] | 521 | ret_val = super(Key, self).__getattr__(name) |
---|
[208] | 522 | length = super(Key, self).__getattr__('name_length') |
---|
| 523 | ret_val = _buffer2bytearray(ret_val, length) |
---|
| 524 | |
---|
[219] | 525 | elif name == "modified": |
---|
[256] | 526 | ret_val = regfi.regfi_nt2unix_time(self._base.contents.mtime) |
---|
[219] | 527 | |
---|
| 528 | else: |
---|
| 529 | ret_val = super(Key, self).__getattr__(name) |
---|
| 530 | |
---|
[208] | 531 | return ret_val |
---|
| 532 | |
---|
[221] | 533 | |
---|
| 534 | ## Retrieves the Security properties for this key |
---|
[207] | 535 | def fetch_security(self): |
---|
[214] | 536 | return Security(self._hive, |
---|
[215] | 537 | regfi.regfi_fetch_sk(self._hive.file, self._base)) |
---|
[207] | 538 | |
---|
[221] | 539 | |
---|
| 540 | ## Retrieves the class name for this key |
---|
| 541 | # |
---|
| 542 | # Class names are typically stored as UTF-16LE strings, so these are decoded |
---|
| 543 | # into proper python (unicode) strings. However, if this fails, a bytearray |
---|
| 544 | # is instead returned containing the raw buffer stored for the class name. |
---|
| 545 | # |
---|
| 546 | # @return The class name as a string or bytearray. None if a class name |
---|
| 547 | # doesn't exist or an unrecoverable error occurred during retrieval. |
---|
[219] | 548 | def fetch_classname(self): |
---|
| 549 | ret_val = None |
---|
| 550 | cn_p = regfi.regfi_fetch_classname(self._hive.file, self._base) |
---|
| 551 | if cn_p: |
---|
| 552 | cn_struct = cn_p.contents |
---|
| 553 | if cn_struct.interpreted: |
---|
| 554 | ret_val = cn_struct.interpreted.decode('utf-8', 'replace') |
---|
| 555 | else: |
---|
| 556 | ret_val = _buffer2bytearray(cn_struct.raw, |
---|
| 557 | cn_struct.size) |
---|
[228] | 558 | regfi.regfi_free_record(self._hive.file, cn_p) |
---|
[219] | 559 | |
---|
| 560 | return ret_val |
---|
| 561 | |
---|
[221] | 562 | |
---|
| 563 | ## Retrieves this key's parent key |
---|
| 564 | # |
---|
| 565 | # @return The parent's Key instance or None if current key is root |
---|
| 566 | # (or an error occured) |
---|
[215] | 567 | def get_parent(self): |
---|
[218] | 568 | if self.is_root(): |
---|
| 569 | return None |
---|
[215] | 570 | parent_base = regfi.regfi_get_parentkey(self._hive.file, self._base) |
---|
| 571 | if parent_base: |
---|
| 572 | return Key(self._hive, parent_base) |
---|
| 573 | return None |
---|
| 574 | |
---|
[257] | 575 | |
---|
| 576 | ## Checks to see if this Key is the root of its Hive |
---|
| 577 | # |
---|
| 578 | # @return True if it is, False otherwise |
---|
[215] | 579 | def is_root(self): |
---|
[218] | 580 | return (self._hive.root == self) |
---|
[215] | 581 | |
---|
| 582 | |
---|
[210] | 583 | ## Registry value (metadata) |
---|
| 584 | # |
---|
| 585 | # These represent registry values (@ref REGFI_VK records) and provide |
---|
| 586 | # access to their associated data. |
---|
[221] | 587 | # |
---|
[257] | 588 | # @note Value instances may provide access to more attributes than are |
---|
[221] | 589 | # documented here. However, undocumented attributes may change over time |
---|
| 590 | # and are not officially supported. If you need access to an attribute |
---|
[257] | 591 | # not shown here, see @ref pyregfi.structures. |
---|
[208] | 592 | class Value(_StructureWrapper): |
---|
[221] | 593 | ## The raw Value name as an uninterpreted bytearray |
---|
| 594 | name_raw = (b"...") |
---|
| 595 | |
---|
| 596 | ## The name of the Value as a (unicode) string |
---|
| 597 | name = "..." |
---|
| 598 | |
---|
| 599 | ## The absolute file offset of the Value record's cell in the Hive file |
---|
| 600 | offset = 0xCAFEBABE |
---|
| 601 | |
---|
| 602 | ## The length of data advertised in the VK record |
---|
| 603 | data_size = 0xCAFEBABE |
---|
| 604 | |
---|
| 605 | ## An integer which represents the data type for this Value's data |
---|
| 606 | # Typically this value is one of 12 types defined in @ref DATA_TYPES, |
---|
| 607 | # but in some cases (the SAM hive) it may be used for other purposes |
---|
| 608 | type = DATA_TYPES.NONE |
---|
| 609 | |
---|
| 610 | ## The VK record's flags field |
---|
| 611 | flags = 0x10110001 |
---|
| 612 | |
---|
| 613 | ## Retrieves the Value's data according to advertised type |
---|
| 614 | # |
---|
| 615 | # Data is loaded from its cell(s) and then interpreted based on the data |
---|
| 616 | # type recorded in the Value. It is not uncommon for data to be stored with |
---|
| 617 | # the wrong type or even with invalid types. If you have difficulty |
---|
| 618 | # obtaining desired data here, use @ref fetch_raw_data(). |
---|
| 619 | # |
---|
| 620 | # @return The interpreted representation of the data as one of several |
---|
| 621 | # possible Python types, as listed below. None if any failure |
---|
| 622 | # occurred during extraction or conversion. |
---|
| 623 | # |
---|
| 624 | # @retval string for SZ, EXPAND_SZ, and LINK |
---|
| 625 | # @retval int for DWORD, DWORD_BE, and QWORD |
---|
| 626 | # @retval list(string) for MULTI_SZ |
---|
| 627 | # @retval bytearray for NONE, BINARY, RESOURCE_LIST, |
---|
| 628 | # FULL_RESOURCE_DESCRIPTOR, and RESOURCE_REQUIREMENTS_LIST |
---|
| 629 | # |
---|
[219] | 630 | def fetch_data(self): |
---|
[209] | 631 | ret_val = None |
---|
[219] | 632 | data_p = regfi.regfi_fetch_data(self._hive.file, self._base) |
---|
| 633 | if not data_p: |
---|
| 634 | return None |
---|
| 635 | data_struct = data_p.contents |
---|
[208] | 636 | |
---|
[219] | 637 | if data_struct.interpreted_size == 0: |
---|
| 638 | ret_val = None |
---|
[221] | 639 | elif data_struct.type in (DATA_TYPES.SZ, DATA_TYPES.EXPAND_SZ, DATA_TYPES.LINK): |
---|
[219] | 640 | # Unicode strings |
---|
| 641 | ret_val = data_struct.interpreted.string.decode('utf-8', 'replace') |
---|
[221] | 642 | elif data_struct.type in (DATA_TYPES.DWORD, DATA_TYPES.DWORD_BE): |
---|
[219] | 643 | # 32 bit integers |
---|
| 644 | ret_val = data_struct.interpreted.dword |
---|
[221] | 645 | elif data_struct.type == DATA_TYPES.QWORD: |
---|
[219] | 646 | # 64 bit integers |
---|
| 647 | ret_val = data_struct.interpreted.qword |
---|
[221] | 648 | elif data_struct.type == DATA_TYPES.MULTI_SZ: |
---|
[219] | 649 | ret_val = _charss2strlist(data_struct.interpreted.multiple_string) |
---|
[221] | 650 | elif data_struct.type in (DATA_TYPES.NONE, DATA_TYPES.RESOURCE_LIST, |
---|
| 651 | DATA_TYPES.FULL_RESOURCE_DESCRIPTOR, |
---|
| 652 | DATA_TYPES.RESOURCE_REQUIREMENTS_LIST, |
---|
| 653 | DATA_TYPES.BINARY): |
---|
[219] | 654 | ret_val = _buffer2bytearray(data_struct.interpreted.none, |
---|
| 655 | data_struct.interpreted_size) |
---|
[209] | 656 | |
---|
[228] | 657 | regfi.regfi_free_record(self._hive.file, data_p) |
---|
[219] | 658 | return ret_val |
---|
[221] | 659 | |
---|
| 660 | |
---|
| 661 | ## Retrieves raw representation of Value's data |
---|
| 662 | # |
---|
| 663 | # @return A bytearray containing the data |
---|
| 664 | # |
---|
[219] | 665 | def fetch_raw_data(self): |
---|
| 666 | ret_val = None |
---|
| 667 | # XXX: should we load the data without interpretation instead? |
---|
| 668 | data_p = regfi.regfi_fetch_data(self._hive.file, self._base) |
---|
| 669 | if not data_p: |
---|
| 670 | return None |
---|
[209] | 671 | |
---|
[219] | 672 | data_struct = data_p.contents |
---|
| 673 | ret_val = _buffer2bytearray(data_struct.raw, |
---|
| 674 | data_struct.size) |
---|
[228] | 675 | regfi.regfi_free_record(self._hive.file, data_p) |
---|
[208] | 676 | return ret_val |
---|
| 677 | |
---|
[221] | 678 | |
---|
[219] | 679 | def __getattr__(self, name): |
---|
| 680 | ret_val = super(Value, self).__getattr__(name) |
---|
| 681 | if name == "name": |
---|
[252] | 682 | if not ret_val: |
---|
[219] | 683 | ret_val = self.name_raw |
---|
| 684 | else: |
---|
| 685 | ret_val = ret_val.decode('utf-8', 'replace') |
---|
[208] | 686 | |
---|
[219] | 687 | elif name == "name_raw": |
---|
| 688 | length = super(Value, self).__getattr__('name_length') |
---|
| 689 | ret_val = _buffer2bytearray(ret_val, length) |
---|
| 690 | |
---|
| 691 | return ret_val |
---|
| 692 | |
---|
| 693 | |
---|
[208] | 694 | # Avoids chicken/egg class definitions. |
---|
| 695 | # Also makes for convenient code reuse in these lists' parent classes. |
---|
[221] | 696 | SubkeyList._constructor = Key |
---|
| 697 | ValueList._constructor = Value |
---|
[208] | 698 | |
---|
| 699 | |
---|
| 700 | |
---|
[210] | 701 | ## Represents a single registry hive (file) |
---|
| 702 | class Hive(): |
---|
[204] | 703 | file = None |
---|
| 704 | raw_file = None |
---|
[255] | 705 | _fh = None |
---|
| 706 | #_root = None |
---|
[218] | 707 | |
---|
[255] | 708 | |
---|
[221] | 709 | ## The root Key of this Hive |
---|
| 710 | root = None |
---|
| 711 | |
---|
| 712 | ## This Hives's last modified time represented as the number of seconds |
---|
| 713 | # since the UNIX epoch in UTC; similar to what time.time() returns |
---|
| 714 | modified = 1300000000.123456 |
---|
| 715 | |
---|
| 716 | ## First sequence number |
---|
| 717 | sequence1 = 12345678 |
---|
| 718 | |
---|
| 719 | ## Second sequence number |
---|
| 720 | sequence2 = 12345678 |
---|
| 721 | |
---|
| 722 | ## Major version |
---|
| 723 | major_version = 1 |
---|
| 724 | |
---|
| 725 | ## Minor version |
---|
| 726 | minor_version = 5 |
---|
| 727 | |
---|
| 728 | ## Constructor |
---|
| 729 | # |
---|
[232] | 730 | # Initialize a new Hive based on a Python file object. To open a file by |
---|
| 731 | # path, see @ref openHive. |
---|
| 732 | # |
---|
[221] | 733 | # @param fh A Python file object. The constructor first looks for a valid |
---|
| 734 | # fileno attribute on this object and uses it if possible. |
---|
| 735 | # Otherwise, the seek and read methods are used for file |
---|
| 736 | # access. |
---|
| 737 | # |
---|
[232] | 738 | # @note Supplied file must be seekable. Do not perform any operation on |
---|
| 739 | # the provided file object while a Hive is using it. Do not |
---|
| 740 | # construct multiple Hive instances from the same file object. |
---|
| 741 | # If a file must be accessed by separate code and pyregfi |
---|
| 742 | # simultaneously, use a separate file descriptor. Hives are |
---|
| 743 | # thread-safe, so multiple threads may use a single Hive object. |
---|
[204] | 744 | def __init__(self, fh): |
---|
[226] | 745 | # The fileno method may not exist, or it may throw an exception |
---|
| 746 | # when called if the file isn't backed with a descriptor. |
---|
[255] | 747 | self._fh = fh |
---|
[226] | 748 | fn = None |
---|
[205] | 749 | try: |
---|
[226] | 750 | # XXX: Native calls to Windows filenos don't seem to work. |
---|
| 751 | # Need to investigate why. |
---|
[259] | 752 | #if not is_win32 and hasattr(fh, 'fileno'): |
---|
| 753 | if hasattr(fh, 'fileno'): |
---|
[226] | 754 | fn = fh.fileno() |
---|
[205] | 755 | except: |
---|
| 756 | pass |
---|
[204] | 757 | |
---|
[226] | 758 | if fn != None: |
---|
| 759 | self.file = regfi.regfi_alloc(fn, REGFI_ENCODING_UTF8) |
---|
| 760 | if not self.file: |
---|
| 761 | # XXX: switch to non-generic exception |
---|
| 762 | raise Exception("Could not open registry file. Current log:\n" |
---|
[232] | 763 | + getLogMessages()) |
---|
[226] | 764 | else: |
---|
| 765 | fh.seek(0) |
---|
| 766 | self.raw_file = structures.REGFI_RAW_FILE() |
---|
| 767 | self.raw_file.fh = fh |
---|
| 768 | self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek) |
---|
| 769 | self.raw_file.read = read_cb_type(self.raw_file.cb_read) |
---|
| 770 | self.file = regfi.regfi_alloc_cb(pointer(self.raw_file), REGFI_ENCODING_UTF8) |
---|
| 771 | if not self.file: |
---|
| 772 | # XXX: switch to non-generic exception |
---|
| 773 | raise Exception("Could not open registry file. Current log:\n" |
---|
[232] | 774 | + getLogMessages()) |
---|
[226] | 775 | |
---|
| 776 | |
---|
[204] | 777 | def __getattr__(self, name): |
---|
[218] | 778 | if name == "root": |
---|
[224] | 779 | # XXX: This creates reference loops. Need to cache better inside regfi |
---|
| 780 | #if self._root == None: |
---|
| 781 | # self._root = Key(self, regfi.regfi_get_rootkey(self.file)) |
---|
| 782 | #return self._root |
---|
| 783 | return Key(self, regfi.regfi_get_rootkey(self.file)) |
---|
[218] | 784 | |
---|
[221] | 785 | elif name == "modified": |
---|
[256] | 786 | return regfi.regfi_nt2unix_time(self._base.contents.mtime) |
---|
[221] | 787 | |
---|
[204] | 788 | return getattr(self.file.contents, name) |
---|
[221] | 789 | |
---|
[205] | 790 | |
---|
[210] | 791 | def __del__(self): |
---|
[255] | 792 | if self.file: |
---|
| 793 | regfi.regfi_free(self.file) |
---|
[204] | 794 | |
---|
[205] | 795 | def __iter__(self): |
---|
| 796 | return HiveIterator(self) |
---|
[204] | 797 | |
---|
[215] | 798 | |
---|
[210] | 799 | ## Creates a @ref HiveIterator initialized at the specified path in |
---|
[221] | 800 | # the hive. |
---|
[210] | 801 | # |
---|
[221] | 802 | # @param path A list of Key names which represent an absolute path within |
---|
| 803 | # the Hive |
---|
| 804 | # |
---|
| 805 | # @return A @ref HiveIterator which is positioned at the specified path. |
---|
| 806 | # |
---|
| 807 | # @exception Exception If the path could not be found/traversed |
---|
[206] | 808 | def subtree(self, path): |
---|
| 809 | hi = HiveIterator(self) |
---|
| 810 | hi.descend(path) |
---|
| 811 | return hi |
---|
[205] | 812 | |
---|
[206] | 813 | |
---|
[210] | 814 | ## A special purpose iterator for registry hives |
---|
| 815 | # |
---|
| 816 | # Iterating over an object of this type causes all keys in a specific |
---|
| 817 | # hive subtree to be returned in a depth-first manner. These iterators |
---|
| 818 | # are typically created using the @ref Hive.subtree() function on a @ref Hive |
---|
| 819 | # object. |
---|
| 820 | # |
---|
| 821 | # HiveIterators can also be used to manually traverse up and down a |
---|
| 822 | # registry hive as they retain information about the current position in |
---|
| 823 | # the hive, along with which iteration state for subkeys and values for |
---|
| 824 | # every parent key. See the @ref up and @ref down methods for more |
---|
| 825 | # information. |
---|
[205] | 826 | class HiveIterator(): |
---|
[220] | 827 | _hive = None |
---|
| 828 | _iter = None |
---|
| 829 | _iteration_root = None |
---|
[228] | 830 | _lock = None |
---|
[205] | 831 | |
---|
| 832 | def __init__(self, hive): |
---|
[228] | 833 | self._iter = regfi.regfi_iterator_new(hive.file) |
---|
[226] | 834 | if not self._iter: |
---|
[205] | 835 | raise Exception("Could not create iterator. Current log:\n" |
---|
[232] | 836 | + getLogMessages()) |
---|
[214] | 837 | self._hive = hive |
---|
[228] | 838 | self._lock = threading.RLock() |
---|
| 839 | |
---|
[205] | 840 | def __getattr__(self, name): |
---|
[228] | 841 | self._lock.acquire() |
---|
| 842 | ret_val = getattr(self._iter.contents, name) |
---|
| 843 | self._lock.release() |
---|
| 844 | return ret_val |
---|
[205] | 845 | |
---|
[228] | 846 | def __del__(self): |
---|
| 847 | self._lock.acquire() |
---|
[220] | 848 | regfi.regfi_iterator_free(self._iter) |
---|
[228] | 849 | self._lock.release() |
---|
[205] | 850 | |
---|
| 851 | def __iter__(self): |
---|
[228] | 852 | self._lock.acquire() |
---|
[220] | 853 | self._iteration_root = None |
---|
[228] | 854 | self._lock.release() |
---|
[205] | 855 | return self |
---|
| 856 | |
---|
| 857 | def __next__(self): |
---|
[228] | 858 | self._lock.acquire() |
---|
[220] | 859 | if self._iteration_root == None: |
---|
[228] | 860 | self._iteration_root = self.current_key().offset |
---|
[220] | 861 | elif not regfi.regfi_iterator_down(self._iter): |
---|
| 862 | up_ret = regfi.regfi_iterator_up(self._iter) |
---|
[206] | 863 | while (up_ret and |
---|
[220] | 864 | not regfi.regfi_iterator_next_subkey(self._iter)): |
---|
[228] | 865 | if self._iteration_root == self.current_key().offset: |
---|
[220] | 866 | self._iteration_root = None |
---|
[228] | 867 | self._lock.release() |
---|
[206] | 868 | raise StopIteration('') |
---|
[220] | 869 | up_ret = regfi.regfi_iterator_up(self._iter) |
---|
[205] | 870 | |
---|
| 871 | if not up_ret: |
---|
[221] | 872 | self._iteration_root = None |
---|
[228] | 873 | self._lock.release() |
---|
[205] | 874 | raise StopIteration('') |
---|
| 875 | |
---|
[210] | 876 | # XXX: Use non-generic exception |
---|
[220] | 877 | if not regfi.regfi_iterator_down(self._iter): |
---|
[228] | 878 | self._lock.release() |
---|
[205] | 879 | raise Exception('Error traversing iterator downward.'+ |
---|
[232] | 880 | ' Current log:\n'+ getLogMessages()) |
---|
[205] | 881 | |
---|
[220] | 882 | regfi.regfi_iterator_first_subkey(self._iter) |
---|
[228] | 883 | ret_val = self.current_key() |
---|
| 884 | self._lock.release() |
---|
[205] | 885 | |
---|
[228] | 886 | return ret_val |
---|
| 887 | |
---|
| 888 | |
---|
[212] | 889 | # For Python 2.x |
---|
[214] | 890 | next = __next__ |
---|
[212] | 891 | |
---|
[221] | 892 | # XXX: Should add sanity checks on some of these traversal functions |
---|
| 893 | # to throw exceptions if a traversal/retrieval *should* have worked |
---|
| 894 | # but failed for some reason. |
---|
| 895 | |
---|
| 896 | ## Descends the iterator to a subkey |
---|
| 897 | # |
---|
| 898 | # Descends the iterator one level to the current subkey, or a subkey |
---|
| 899 | # specified by name. |
---|
| 900 | # |
---|
| 901 | # @param subkey_name If specified, locates specified subkey by name |
---|
| 902 | # (via find_subkey()) and descends to it. |
---|
| 903 | # |
---|
| 904 | # @return True if successful, False otherwise |
---|
[220] | 905 | def down(self, subkey_name=None): |
---|
[228] | 906 | ret_val = None |
---|
[220] | 907 | if subkey_name == None: |
---|
[228] | 908 | self._lock.acquire() |
---|
| 909 | ret_val = regfi.regfi_iterator_down(self._iter) |
---|
[220] | 910 | else: |
---|
| 911 | if name != None: |
---|
| 912 | name = name.encode('utf-8') |
---|
[228] | 913 | self._lock.acquire() |
---|
| 914 | ret_val = (regfi.regfi_iterator_find_subkey(self._iter, name) |
---|
| 915 | and regfi.regfi_iterator_down(self._iter)) |
---|
| 916 | |
---|
| 917 | self._lock.release() |
---|
| 918 | return ret_val |
---|
[206] | 919 | |
---|
[221] | 920 | |
---|
| 921 | ## Causes the iterator to ascend to the current Key's parent |
---|
| 922 | # |
---|
| 923 | # @return True if successful, False otherwise |
---|
| 924 | # |
---|
| 925 | # @note The state of current subkeys and values at this level in the tree |
---|
| 926 | # is lost as a side effect. That is, if you go up() and then back |
---|
| 927 | # down() again, current_subkey() and current_value() will return |
---|
| 928 | # default selections. |
---|
[206] | 929 | def up(self): |
---|
[228] | 930 | self._lock.acquire() |
---|
| 931 | ret_val = regfi.regfi_iterator_up(self._iter) |
---|
| 932 | self._lock.release() |
---|
| 933 | return ret_val |
---|
[206] | 934 | |
---|
[221] | 935 | |
---|
| 936 | ## Selects first subkey of current key |
---|
| 937 | # |
---|
| 938 | # @return A Key instance for the first subkey. |
---|
| 939 | # None on error or if the current key has no subkeys. |
---|
[220] | 940 | def first_subkey(self): |
---|
[228] | 941 | ret_val = None |
---|
| 942 | self._lock.acquire() |
---|
[220] | 943 | if regfi.regfi_iterator_first_subkey(self._iter): |
---|
[228] | 944 | ret_val = self.current_subkey() |
---|
| 945 | self._lock.release() |
---|
| 946 | return ret_val |
---|
[220] | 947 | |
---|
[221] | 948 | |
---|
| 949 | ## Selects first value of current Key |
---|
| 950 | # |
---|
| 951 | # @return A Value instance for the first value. |
---|
| 952 | # None on error or if the current key has no values. |
---|
[220] | 953 | def first_value(self): |
---|
[228] | 954 | ret_val = None |
---|
| 955 | self._lock.acquire() |
---|
[220] | 956 | if regfi.regfi_iterator_first_value(self._iter): |
---|
[228] | 957 | ret_val = self.current_value() |
---|
| 958 | self._lock.release() |
---|
| 959 | return ret_val |
---|
[220] | 960 | |
---|
[221] | 961 | |
---|
| 962 | ## Selects the next subkey in the current Key's list |
---|
| 963 | # |
---|
| 964 | # @return A Key instance for the next subkey. |
---|
| 965 | # None if there are no remaining subkeys or an error occurred. |
---|
[220] | 966 | def next_subkey(self): |
---|
[228] | 967 | ret_val = None |
---|
| 968 | self._lock.acquire() |
---|
[220] | 969 | if regfi.regfi_iterator_next_subkey(self._iter): |
---|
[228] | 970 | ret_val = self.current_subkey() |
---|
| 971 | self._lock.release() |
---|
| 972 | return ret_val |
---|
[220] | 973 | |
---|
[221] | 974 | |
---|
| 975 | ## Selects the next value in the current Key's list |
---|
| 976 | # |
---|
| 977 | # @return A Value instance for the next value. |
---|
| 978 | # None if there are no remaining values or an error occurred. |
---|
[220] | 979 | def next_value(self): |
---|
[228] | 980 | ret_val = None |
---|
| 981 | self._lock.acquire() |
---|
[220] | 982 | if regfi.regfi_iterator_next_value(self._iter): |
---|
[228] | 983 | ret_val = self.current_value() |
---|
| 984 | self._lock.release() |
---|
| 985 | return ret_val |
---|
[220] | 986 | |
---|
[221] | 987 | |
---|
| 988 | ## Selects the first subkey which has the specified name |
---|
| 989 | # |
---|
| 990 | # @return A Key instance for the selected key. |
---|
| 991 | # None if it could not be located or an error occurred. |
---|
[220] | 992 | def find_subkey(self, name): |
---|
| 993 | if name != None: |
---|
| 994 | name = name.encode('utf-8') |
---|
[228] | 995 | ret_val = None |
---|
| 996 | self._lock.acquire() |
---|
[220] | 997 | if regfi.regfi_iterator_find_subkey(self._iter, name): |
---|
[228] | 998 | ret_val = self.current_subkey() |
---|
| 999 | self._lock.release() |
---|
| 1000 | return ret_val |
---|
[220] | 1001 | |
---|
[221] | 1002 | |
---|
| 1003 | ## Selects the first value which has the specified name |
---|
| 1004 | # |
---|
| 1005 | # @return A Value instance for the selected value. |
---|
| 1006 | # None if it could not be located or an error occurred. |
---|
[220] | 1007 | def find_value(self, name): |
---|
| 1008 | if name != None: |
---|
| 1009 | name = name.encode('utf-8') |
---|
[228] | 1010 | ret_val = None |
---|
| 1011 | self._lock.acquire() |
---|
[220] | 1012 | if regfi.regfi_iterator_find_value(self._iter, name): |
---|
[228] | 1013 | ret_val = self.current_value() |
---|
| 1014 | self._lock.release() |
---|
| 1015 | return ret_val |
---|
[220] | 1016 | |
---|
[221] | 1017 | ## Retrieves the currently selected subkey |
---|
| 1018 | # |
---|
| 1019 | # @return A Key instance of the current subkey |
---|
[220] | 1020 | def current_subkey(self): |
---|
[228] | 1021 | self._lock.acquire() |
---|
| 1022 | ret_val = Key(self._hive, regfi.regfi_iterator_cur_subkey(self._iter)) |
---|
| 1023 | self._lock.release() |
---|
| 1024 | return ret_val |
---|
[220] | 1025 | |
---|
[221] | 1026 | ## Retrieves the currently selected value |
---|
| 1027 | # |
---|
| 1028 | # @return A Value instance of the current value |
---|
[220] | 1029 | def current_value(self): |
---|
[228] | 1030 | self._lock.acquire() |
---|
| 1031 | ret_val = Value(self._hive, regfi.regfi_iterator_cur_value(self._iter)) |
---|
| 1032 | self._lock.release() |
---|
| 1033 | return ret_val |
---|
[220] | 1034 | |
---|
[221] | 1035 | ## Retrieves the current key |
---|
| 1036 | # |
---|
| 1037 | # @return A Key instance of the current position of the iterator |
---|
[220] | 1038 | def current_key(self): |
---|
[228] | 1039 | self._lock.acquire() |
---|
| 1040 | ret_val = Key(self._hive, regfi.regfi_iterator_cur_key(self._iter)) |
---|
| 1041 | self._lock.release() |
---|
| 1042 | return ret_val |
---|
[220] | 1043 | |
---|
[221] | 1044 | ## Traverse downward multiple levels |
---|
| 1045 | # |
---|
| 1046 | # This is more efficient than calling down() multiple times |
---|
| 1047 | # |
---|
| 1048 | # @param path A list of Key names which represent the path to descend |
---|
| 1049 | # |
---|
| 1050 | # @exception Exception If path could not be located |
---|
[206] | 1051 | def descend(self, path): |
---|
[215] | 1052 | cpath = _strlist2charss(path) |
---|
[206] | 1053 | |
---|
[228] | 1054 | self._lock.acquire() |
---|
[252] | 1055 | result = regfi.regfi_iterator_descend(self._iter, cpath) |
---|
[228] | 1056 | self._lock.release() |
---|
| 1057 | if not result: |
---|
| 1058 | # XXX: Use non-generic exception |
---|
[232] | 1059 | raise Exception('Could not locate path.\n'+getLogMessages()) |
---|
[221] | 1060 | |
---|
[252] | 1061 | ## Obtains a list of the current key's ancestry |
---|
| 1062 | # |
---|
| 1063 | # @return A list of all parent keys starting with the root Key and ending |
---|
| 1064 | # with the current Key |
---|
| 1065 | def ancestry(self): |
---|
| 1066 | self._lock.acquire() |
---|
| 1067 | result = regfi.regfi_iterator_ancestry(self._iter) |
---|
| 1068 | self._lock.release() |
---|
[221] | 1069 | |
---|
[252] | 1070 | ret_val = [] |
---|
| 1071 | i = 0 |
---|
| 1072 | k = result[i] |
---|
| 1073 | while k: |
---|
| 1074 | k = cast(regfi.regfi_reference_record(self._hive.file, k), POINTER(REGFI_NK)) |
---|
| 1075 | ret_val.append(Key(self._hive, k)) |
---|
| 1076 | i += 1 |
---|
| 1077 | k = result[i] |
---|
| 1078 | |
---|
| 1079 | regfi.regfi_free_record(self._hive.file, result) |
---|
| 1080 | return ret_val |
---|
| 1081 | |
---|
| 1082 | ## Obtains the current path of the iterator |
---|
| 1083 | # |
---|
| 1084 | # @return A list of key names starting with the root up to and |
---|
| 1085 | # including the current key |
---|
| 1086 | # |
---|
| 1087 | def current_path(self): |
---|
| 1088 | ancestry = self.ancestry() |
---|
| 1089 | return [str(a.name) for a in ancestry] |
---|
| 1090 | |
---|
| 1091 | |
---|
[221] | 1092 | # Freeing symbols defined for the sake of documentation |
---|
| 1093 | del Value.name,Value.name_raw,Value.offset,Value.data_size,Value.type,Value.flags |
---|
| 1094 | del Key.name,Key.name_raw,Key.offset,Key.modified,Key.flags |
---|
| 1095 | del Hive.root,Hive.modified,Hive.sequence1,Hive.sequence2,Hive.major_version,Hive.minor_version |
---|
[253] | 1096 | del Security.ref_count,Security.offset,Security.descriptor |
---|