1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | from pyregfi.structures import * |
---|
5 | |
---|
6 | import ctypes |
---|
7 | import ctypes.util |
---|
8 | from ctypes import c_char,c_char_p,c_int,c_uint16,c_bool,POINTER |
---|
9 | |
---|
10 | regfi = ctypes.CDLL(ctypes.util.find_library('regfi'), use_errno=True) |
---|
11 | |
---|
12 | |
---|
13 | regfi.regfi_alloc.argtypes = [c_int] |
---|
14 | regfi.regfi_alloc.restype = POINTER(REGFI_FILE) |
---|
15 | |
---|
16 | regfi.regfi_alloc_cb.argtypes = [POINTER(REGFI_RAW_FILE)] |
---|
17 | regfi.regfi_alloc_cb.restype = POINTER(REGFI_FILE) |
---|
18 | |
---|
19 | regfi.regfi_free.argtypes = [POINTER(REGFI_FILE)] |
---|
20 | regfi.regfi_free.restype = None |
---|
21 | |
---|
22 | regfi.regfi_log_get_str.argtypes = [] |
---|
23 | regfi.regfi_log_get_str.restype = c_char_p |
---|
24 | |
---|
25 | regfi.regfi_log_set_mask.argtypes = [c_uint16] |
---|
26 | regfi.regfi_log_set_mask.restype = c_bool |
---|
27 | |
---|
28 | regfi.regfi_free_record.argtypes = [c_void_p] |
---|
29 | regfi.regfi_free_record.restype = None |
---|
30 | |
---|
31 | regfi.regfi_iterator_new.argtypes = [POINTER(REGFI_FILE), REGFI_ENCODING] |
---|
32 | regfi.regfi_iterator_new.restype = POINTER(REGFI_ITERATOR) |
---|
33 | |
---|
34 | regfi.regfi_iterator_free.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
35 | regfi.regfi_iterator_free.restype = None |
---|
36 | |
---|
37 | regfi.regfi_iterator_down.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
38 | regfi.regfi_iterator_down.restype = c_bool |
---|
39 | |
---|
40 | regfi.regfi_iterator_up.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
41 | regfi.regfi_iterator_up.restype = c_bool |
---|
42 | |
---|
43 | regfi.regfi_iterator_to_root.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
44 | regfi.regfi_iterator_to_root.restype = c_bool |
---|
45 | |
---|
46 | regfi.regfi_iterator_walk_path.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
47 | regfi.regfi_iterator_walk_path.restype = c_bool |
---|
48 | |
---|
49 | regfi.regfi_iterator_cur_key.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
50 | regfi.regfi_iterator_cur_key.restype = POINTER(REGFI_NK) |
---|
51 | |
---|
52 | regfi.regfi_iterator_cur_sk.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
53 | regfi.regfi_iterator_cur_sk.restype = POINTER(REGFI_SK) |
---|
54 | |
---|
55 | regfi.regfi_iterator_first_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
56 | regfi.regfi_iterator_first_subkey.restype = c_bool |
---|
57 | |
---|
58 | regfi.regfi_iterator_cur_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
59 | regfi.regfi_iterator_cur_subkey.restype = POINTER(REGFI_NK) |
---|
60 | |
---|
61 | regfi.regfi_iterator_next_subkey.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
62 | regfi.regfi_iterator_next_subkey.restype = c_bool |
---|
63 | |
---|
64 | regfi.regfi_iterator_find_subkey.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] |
---|
65 | regfi.regfi_iterator_find_subkey.restype = c_bool |
---|
66 | |
---|
67 | regfi.regfi_iterator_first_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
68 | regfi.regfi_iterator_first_value.restype = c_bool |
---|
69 | |
---|
70 | regfi.regfi_iterator_cur_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
71 | regfi.regfi_iterator_cur_value.restype = POINTER(REGFI_VK) |
---|
72 | |
---|
73 | regfi.regfi_iterator_next_value.argtypes = [POINTER(REGFI_ITERATOR)] |
---|
74 | regfi.regfi_iterator_next_value.restype = c_bool |
---|
75 | |
---|
76 | regfi.regfi_iterator_find_value.argtypes = [POINTER(REGFI_ITERATOR), c_char_p] |
---|
77 | regfi.regfi_iterator_find_value.restype = c_bool |
---|
78 | |
---|
79 | # XXX: possibly move REGFI_ENCODING to file object and eliminate need for ITERATOR here. |
---|
80 | regfi.regfi_iterator_fetch_classname.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_NK)] |
---|
81 | regfi.regfi_iterator_fetch_classname.restype = POINTER(REGFI_CLASSNAME) |
---|
82 | |
---|
83 | regfi.regfi_iterator_fetch_data.argtypes = [POINTER(REGFI_ITERATOR), POINTER(REGFI_VK)] |
---|
84 | regfi.regfi_iterator_fetch_data.restype = POINTER(REGFI_DATA) |
---|
85 | |
---|
86 | |
---|
87 | regfi.regfi_init.argtypes = [] |
---|
88 | regfi.regfi_init.restype = None |
---|
89 | regfi.regfi_init() |
---|
90 | |
---|
91 | |
---|
92 | def GetLogMessages(): |
---|
93 | return regfi.regfi_log_get_str() |
---|
94 | |
---|
95 | |
---|
96 | class Hive(): |
---|
97 | file = None |
---|
98 | raw_file = None |
---|
99 | |
---|
100 | def __init__(self, fh): |
---|
101 | # The fileno method may not exist, or it may thrown an exception |
---|
102 | # when called if the file isn't backed with a descriptor. |
---|
103 | try: |
---|
104 | if hasattr(fh, 'fileno'): |
---|
105 | self.file = regfi.regfi_alloc(fh.fileno()) |
---|
106 | return |
---|
107 | except: |
---|
108 | pass |
---|
109 | |
---|
110 | self.raw_file = structures.REGFI_RAW_FILE() |
---|
111 | self.raw_file.fh = fh |
---|
112 | self.raw_file.seek = seek_cb_type(self.raw_file.cb_seek) |
---|
113 | self.raw_file.read = read_cb_type(self.raw_file.cb_read) |
---|
114 | self.file = regfi.regfi_alloc_cb(self.raw_file) |
---|
115 | |
---|
116 | def __getattr__(self, name): |
---|
117 | return getattr(self.file.contents, name) |
---|
118 | |
---|
119 | def __del__(self): |
---|
120 | regfi.regfi_free(self.file) |
---|
121 | if self.raw_file != None: |
---|
122 | regfi.regfi_free(self.file) |
---|
123 | |
---|
124 | |
---|
125 | def __iter__(self): |
---|
126 | return HiveIterator(self) |
---|
127 | |
---|
128 | |
---|
129 | class HiveIterator(): |
---|
130 | hive = None |
---|
131 | iter = None |
---|
132 | root_traversed = False |
---|
133 | |
---|
134 | def __init__(self, hive): |
---|
135 | # REGFI_ENCODING_UTF8==1 |
---|
136 | self.iter = regfi.regfi_iterator_new(hive.file, 1) |
---|
137 | if self.iter == None: |
---|
138 | raise Exception("Could not create iterator. Current log:\n" |
---|
139 | + GetLogMessages()) |
---|
140 | self.hive = hive |
---|
141 | |
---|
142 | def __getattr__(self, name): |
---|
143 | return getattr(self.file.contents, name) |
---|
144 | |
---|
145 | def __del__(self): |
---|
146 | regfi.regfi_iterator_free(self.iter) |
---|
147 | |
---|
148 | def __iter__(self): |
---|
149 | return self |
---|
150 | |
---|
151 | def __next__(self): |
---|
152 | if self.root_traversed: |
---|
153 | self.root_traversed = True |
---|
154 | |
---|
155 | elif not regfi.regfi_iterator_down(self.iter): |
---|
156 | up_ret = regfi.regfi_iterator_up(self.iter) |
---|
157 | while up_ret and not regfi.regfi_iterator_next_subkey(self.iter): |
---|
158 | up_ret = regfi.regfi_iterator_up(self.iter) |
---|
159 | |
---|
160 | if not up_ret: |
---|
161 | raise StopIteration('') |
---|
162 | |
---|
163 | if not regfi.regfi_iterator_down(self.iter): |
---|
164 | raise Exception('Error traversing iterator downward.'+ |
---|
165 | ' Current log:\n'+ GetLogMessages()) |
---|
166 | |
---|
167 | regfi.regfi_iterator_first_subkey(self.iter) |
---|
168 | print(regfi.regfi_iterator_cur_key(self.iter).contents.keyname) |
---|
169 | return regfi.regfi_iterator_cur_key(self.iter) |
---|
170 | |
---|