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

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

fixed name/flags bug
improved error reporting on bad key/value names

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