source: trunk/python/pyregfi/__init__.py @ 257

Last change on this file since 257 was 257, checked in by tim, 13 years ago

documentation fixes/additions

File size: 35.3 KB
RevLine 
[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]93import sys
[219]94import time
[204]95import ctypes
96import ctypes.util
[228]97import threading
98from 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
105class 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
138class 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]147def _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]158def _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]170def _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
186def 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]197def 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]220def 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
233def 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]239class _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]288class Key():
[206]289    pass
290
[221]291
292class Value():
[206]293    pass
294
[221]295
[253]296
297## Represents a registry SK record which contains a security descriptor
298#
[206]299class 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]336class _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]446class 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]467class 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]481class 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]592class 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]696SubkeyList._constructor = Key
697ValueList._constructor = Value
[208]698
699
700
[210]701## Represents a single registry hive (file)
702class 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.
752            if not is_win32 and hasattr(fh, 'fileno'):
753                fn = fh.fileno()
[205]754        except:
755            pass
[204]756
[226]757        if fn != None:
758            self.file = regfi.regfi_alloc(fn, REGFI_ENCODING_UTF8)
759            if not self.file:
760                # XXX: switch to non-generic exception
761                raise Exception("Could not open registry file.  Current log:\n"
[232]762                                + getLogMessages())
[226]763        else:
764            fh.seek(0)
765            self.raw_file = structures.REGFI_RAW_FILE()
766            self.raw_file.fh = fh
767            self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek)
768            self.raw_file.read = read_cb_type(self.raw_file.cb_read)
769            self.file = regfi.regfi_alloc_cb(pointer(self.raw_file), REGFI_ENCODING_UTF8)
770            if not self.file:
771                # XXX: switch to non-generic exception
772                raise Exception("Could not open registry file.  Current log:\n"
[232]773                                + getLogMessages())
[226]774
775
[204]776    def __getattr__(self, name):
[218]777        if name == "root":
[224]778            # XXX: This creates reference loops.  Need to cache better inside regfi
779            #if self._root == None:
780            #    self._root = Key(self, regfi.regfi_get_rootkey(self.file))
781            #return self._root
782            return Key(self, regfi.regfi_get_rootkey(self.file))
[218]783
[221]784        elif name == "modified":
[256]785            return regfi.regfi_nt2unix_time(self._base.contents.mtime)
[221]786
[204]787        return getattr(self.file.contents, name)
[221]788
[205]789   
[210]790    def __del__(self):
[255]791        if self.file:
792            regfi.regfi_free(self.file)
[204]793
[205]794    def __iter__(self):
795        return HiveIterator(self)
[204]796
[215]797
[210]798    ## Creates a @ref HiveIterator initialized at the specified path in
[221]799    #  the hive.
[210]800    #
[221]801    # @param path A list of Key names which represent an absolute path within
802    #             the Hive
803    #
804    # @return A @ref HiveIterator which is positioned at the specified path.
805    #
806    # @exception Exception If the path could not be found/traversed
[206]807    def subtree(self, path):
808        hi = HiveIterator(self)
809        hi.descend(path)
810        return hi
[205]811
[206]812
[210]813## A special purpose iterator for registry hives
814#
815# Iterating over an object of this type causes all keys in a specific
816# hive subtree to be returned in a depth-first manner. These iterators
817# are typically created using the @ref Hive.subtree() function on a @ref Hive
818# object.
819#
820# HiveIterators can also be used to manually traverse up and down a
821# registry hive as they retain information about the current position in
822# the hive, along with which iteration state for subkeys and values for
823# every parent key.  See the @ref up and @ref down methods for more
824# information.
[205]825class HiveIterator():
[220]826    _hive = None
827    _iter = None
828    _iteration_root = None
[228]829    _lock = None
[205]830
831    def __init__(self, hive):
[228]832        self._iter = regfi.regfi_iterator_new(hive.file)
[226]833        if not self._iter:
[205]834            raise Exception("Could not create iterator.  Current log:\n"
[232]835                            + getLogMessages())
[214]836        self._hive = hive
[228]837        self._lock = threading.RLock()
838   
[205]839    def __getattr__(self, name):
[228]840        self._lock.acquire()
841        ret_val = getattr(self._iter.contents, name)
842        self._lock.release()
843        return ret_val
[205]844
[228]845    def __del__(self):
846        self._lock.acquire()
[220]847        regfi.regfi_iterator_free(self._iter)
[228]848        self._lock.release()
[205]849
850    def __iter__(self):
[228]851        self._lock.acquire()
[220]852        self._iteration_root = None
[228]853        self._lock.release()
[205]854        return self
855
856    def __next__(self):
[228]857        self._lock.acquire()
[220]858        if self._iteration_root == None:
[228]859            self._iteration_root = self.current_key().offset
[220]860        elif not regfi.regfi_iterator_down(self._iter):
861            up_ret = regfi.regfi_iterator_up(self._iter)
[206]862            while (up_ret and
[220]863                   not regfi.regfi_iterator_next_subkey(self._iter)):
[228]864                if self._iteration_root == self.current_key().offset:
[220]865                    self._iteration_root = None
[228]866                    self._lock.release()
[206]867                    raise StopIteration('')
[220]868                up_ret = regfi.regfi_iterator_up(self._iter)
[205]869
870            if not up_ret:
[221]871                self._iteration_root = None
[228]872                self._lock.release()
[205]873                raise StopIteration('')
874           
[210]875            # XXX: Use non-generic exception
[220]876            if not regfi.regfi_iterator_down(self._iter):
[228]877                self._lock.release()
[205]878                raise Exception('Error traversing iterator downward.'+
[232]879                                ' Current log:\n'+ getLogMessages())
[205]880
[220]881        regfi.regfi_iterator_first_subkey(self._iter)
[228]882        ret_val = self.current_key()
883        self._lock.release()
[205]884
[228]885        return ret_val
886
887
[212]888    # For Python 2.x
[214]889    next = __next__
[212]890
[221]891    # XXX: Should add sanity checks on some of these traversal functions
892    #      to throw exceptions if a traversal/retrieval *should* have worked
893    #      but failed for some reason.
894
895    ## Descends the iterator to a subkey
896    #
897    # Descends the iterator one level to the current subkey, or a subkey
898    # specified by name.
899    #
900    # @param subkey_name If specified, locates specified subkey by name
901    #                    (via find_subkey()) and descends to it.
902    #
903    # @return True if successful, False otherwise
[220]904    def down(self, subkey_name=None):
[228]905        ret_val = None
[220]906        if subkey_name == None:
[228]907            self._lock.acquire()
908            ret_val = regfi.regfi_iterator_down(self._iter)
[220]909        else:
910            if name != None:
911                name = name.encode('utf-8')
[228]912            self._lock.acquire()
913            ret_val = (regfi.regfi_iterator_find_subkey(self._iter, name) 
914                       and regfi.regfi_iterator_down(self._iter))
915       
916        self._lock.release()
917        return ret_val
[206]918
[221]919
920    ## Causes the iterator to ascend to the current Key's parent
921    #
922    # @return True if successful, False otherwise
923    #
924    # @note The state of current subkeys and values at this level in the tree
925    #       is lost as a side effect.  That is, if you go up() and then back
926    #       down() again, current_subkey() and current_value() will return
927    #       default selections.
[206]928    def up(self):
[228]929        self._lock.acquire()
930        ret_val = regfi.regfi_iterator_up(self._iter)
931        self._lock.release()
932        return ret_val
[206]933
[221]934
935    ## Selects first subkey of current key
936    #
937    # @return A Key instance for the first subkey. 
938    #         None on error or if the current key has no subkeys.
[220]939    def first_subkey(self):
[228]940        ret_val = None
941        self._lock.acquire()
[220]942        if regfi.regfi_iterator_first_subkey(self._iter):
[228]943            ret_val = self.current_subkey()
944        self._lock.release()
945        return ret_val
[220]946
[221]947
948    ## Selects first value of current Key
949    #
950    # @return A Value instance for the first value. 
951    #         None on error or if the current key has no values.
[220]952    def first_value(self):
[228]953        ret_val = None
954        self._lock.acquire()
[220]955        if regfi.regfi_iterator_first_value(self._iter):
[228]956            ret_val = self.current_value()
957        self._lock.release()
958        return ret_val
[220]959
[221]960
961    ## Selects the next subkey in the current Key's list
962    #
963    # @return A Key instance for the next subkey.
964    #         None if there are no remaining subkeys or an error occurred.
[220]965    def next_subkey(self):
[228]966        ret_val = None
967        self._lock.acquire()
[220]968        if regfi.regfi_iterator_next_subkey(self._iter):
[228]969            ret_val = self.current_subkey()
970        self._lock.release()
971        return ret_val
[220]972
[221]973
974    ## Selects the next value in the current Key's list
975   
976    # @return A Value instance for the next value.
977    #         None if there are no remaining values or an error occurred.
[220]978    def next_value(self):
[228]979        ret_val = None
980        self._lock.acquire()
[220]981        if regfi.regfi_iterator_next_value(self._iter):
[228]982            ret_val = self.current_value()
983        self._lock.release()
984        return ret_val
[220]985
[221]986
987    ## Selects the first subkey which has the specified name
988    #
989    # @return A Key instance for the selected key.
990    #         None if it could not be located or an error occurred.
[220]991    def find_subkey(self, name):
992        if name != None:
993            name = name.encode('utf-8')
[228]994        ret_val = None
995        self._lock.acquire()
[220]996        if regfi.regfi_iterator_find_subkey(self._iter, name):
[228]997            ret_val = self.current_subkey()
998        self._lock.release()
999        return ret_val
[220]1000
[221]1001
1002    ## Selects the first value which has the specified name
1003    #
1004    # @return A Value instance for the selected value.
1005    #         None if it could not be located or an error occurred.
[220]1006    def find_value(self, name):
1007        if name != None:
1008            name = name.encode('utf-8')
[228]1009        ret_val = None
1010        self._lock.acquire()
[220]1011        if regfi.regfi_iterator_find_value(self._iter, name):
[228]1012            ret_val = self.current_value()
1013        self._lock.release()
1014        return ret_val
[220]1015
[221]1016    ## Retrieves the currently selected subkey
1017    #
1018    # @return A Key instance of the current subkey
[220]1019    def current_subkey(self):
[228]1020        self._lock.acquire()
1021        ret_val = Key(self._hive, regfi.regfi_iterator_cur_subkey(self._iter))
1022        self._lock.release()
1023        return ret_val
[220]1024
[221]1025    ## Retrieves the currently selected value
1026    #
1027    # @return A Value instance of the current value
[220]1028    def current_value(self):
[228]1029        self._lock.acquire()
1030        ret_val = Value(self._hive, regfi.regfi_iterator_cur_value(self._iter))
1031        self._lock.release()
1032        return ret_val
[220]1033
[221]1034    ## Retrieves the current key
1035    #
1036    # @return A Key instance of the current position of the iterator
[220]1037    def current_key(self):
[228]1038        self._lock.acquire()
1039        ret_val = Key(self._hive, regfi.regfi_iterator_cur_key(self._iter))
1040        self._lock.release()
1041        return ret_val
[220]1042
[221]1043    ## Traverse downward multiple levels
1044    #
1045    # This is more efficient than calling down() multiple times
1046    #
1047    # @param path A list of Key names which represent the path to descend
1048    #
1049    # @exception Exception If path could not be located
[206]1050    def descend(self, path):
[215]1051        cpath = _strlist2charss(path)
[206]1052
[228]1053        self._lock.acquire()
[252]1054        result = regfi.regfi_iterator_descend(self._iter, cpath)
[228]1055        self._lock.release()
1056        if not result:
1057            # XXX: Use non-generic exception
[232]1058            raise Exception('Could not locate path.\n'+getLogMessages())
[221]1059
[252]1060    ## Obtains a list of the current key's ancestry
1061    #
1062    # @return A list of all parent keys starting with the root Key and ending
1063    #         with the current Key
1064    def ancestry(self):
1065        self._lock.acquire()
1066        result = regfi.regfi_iterator_ancestry(self._iter)
1067        self._lock.release()
[221]1068
[252]1069        ret_val = []
1070        i = 0
1071        k = result[i]
1072        while k:
1073            k = cast(regfi.regfi_reference_record(self._hive.file, k), POINTER(REGFI_NK))
1074            ret_val.append(Key(self._hive, k))
1075            i += 1
1076            k = result[i]
1077
1078        regfi.regfi_free_record(self._hive.file, result)
1079        return ret_val
1080
1081    ## Obtains the current path of the iterator
1082    #
1083    # @return A list of key names starting with the root up to and
1084    #         including the current key
1085    #
1086    def current_path(self):
1087        ancestry = self.ancestry()
1088        return [str(a.name) for a in ancestry]
1089
1090
[221]1091# Freeing symbols defined for the sake of documentation
1092del Value.name,Value.name_raw,Value.offset,Value.data_size,Value.type,Value.flags
1093del Key.name,Key.name_raw,Key.offset,Key.modified,Key.flags
1094del Hive.root,Hive.modified,Hive.sequence1,Hive.sequence2,Hive.major_version,Hive.minor_version
[253]1095del Security.ref_count,Security.offset,Security.descriptor
Note: See TracBrowser for help on using the repository browser.