[213] | 1 | #!/usr/bin/env python3 |
---|
[197] | 2 | |
---|
| 3 | import sys |
---|
[213] | 4 | import gc |
---|
[225] | 5 | import io |
---|
[215] | 6 | import time |
---|
[228] | 7 | import threading |
---|
[197] | 8 | import pyregfi |
---|
| 9 | |
---|
[225] | 10 | |
---|
[197] | 11 | |
---|
[232] | 12 | pyregfi.setLogMask((pyregfi.LOG_TYPES.INFO, pyregfi.LOG_TYPES.WARN, pyregfi.LOG_TYPES.ERROR)) |
---|
[197] | 13 | |
---|
[213] | 14 | # Uses the HiveIterator to walk all keys |
---|
| 15 | # Gathers various (meaningless) statistics to exercise simple attribute access |
---|
| 16 | # and to hopefully smoke out any bugs that can be identified by changing stats |
---|
[225] | 17 | def iterTallyNames(hive, fh): |
---|
[213] | 18 | key_count = 0 |
---|
| 19 | key_lens = 0 |
---|
| 20 | key_rawlens = 0 |
---|
| 21 | value_count = 0 |
---|
| 22 | value_lens = 0 |
---|
| 23 | value_rawlens = 0 |
---|
[197] | 24 | |
---|
[213] | 25 | for k in hive: |
---|
| 26 | key_count += 1 |
---|
| 27 | if k.name != None: |
---|
| 28 | key_lens += len(k.name) |
---|
| 29 | if k.name_raw != None: |
---|
| 30 | key_rawlens += len(k.name_raw) |
---|
[199] | 31 | |
---|
[213] | 32 | for v in k.values: |
---|
| 33 | value_count += 1 |
---|
| 34 | if v.name != None: |
---|
| 35 | value_lens += len(v.name) |
---|
| 36 | if v.name_raw != None: |
---|
| 37 | value_rawlens += len(v.name_raw) |
---|
[200] | 38 | |
---|
[214] | 39 | print(" Counts: keys=%d, values=%d" % (key_count, value_count)) |
---|
| 40 | print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens)) |
---|
| 41 | print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens)) |
---|
[200] | 42 | |
---|
[209] | 43 | |
---|
[252] | 44 | # walks up parents to obtain path, rather than using downward links like iterator |
---|
[220] | 45 | def getCurrentPath(key): |
---|
| 46 | if key == None: |
---|
| 47 | return '' |
---|
| 48 | path = [] |
---|
| 49 | p = key |
---|
| 50 | while p != None: |
---|
| 51 | path.append(p.name) |
---|
| 52 | p = p.get_parent() |
---|
| 53 | path.reverse() |
---|
| 54 | del path[0] |
---|
| 55 | |
---|
| 56 | return path |
---|
| 57 | |
---|
[215] | 58 | # For each key in the hive, this traverses the parent links up to the root, |
---|
| 59 | # recording the path as it goes, and then uses the subtree/descend method |
---|
| 60 | # to find the same key again, verifying it is the same. This test is currently |
---|
| 61 | # very slow because no key caching is used. |
---|
[225] | 62 | def iterParentWalk(hive, fh): |
---|
[215] | 63 | i = 1 |
---|
| 64 | for k in hive: |
---|
| 65 | path = getCurrentPath(k) |
---|
| 66 | try: |
---|
[262] | 67 | print(repr(path)) |
---|
[215] | 68 | hive_iter = hive.subtree(path) |
---|
| 69 | if hive_iter.current_key() != k: |
---|
| 70 | print("WARNING: k != current_key for path '%s'." % path) |
---|
| 71 | else: |
---|
| 72 | i += 1 |
---|
| 73 | except Exception as e: |
---|
[252] | 74 | print("WARNING: Could not descend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) |
---|
[215] | 75 | print(" Successfully tested paths on %d keys." % i) |
---|
[209] | 76 | |
---|
[215] | 77 | |
---|
[216] | 78 | # Uses the HiveIterator to walk all keys |
---|
| 79 | # Gathers various (meaningless) statistics about data/data_raw attributes |
---|
[225] | 80 | def iterTallyData(hive, fh): |
---|
[216] | 81 | data_stat = 0.0 |
---|
| 82 | dataraw_stat = 0.0 |
---|
| 83 | |
---|
| 84 | for k in hive: |
---|
| 85 | for v in k.values: |
---|
[219] | 86 | d = v.fetch_data() |
---|
[216] | 87 | if d == None: |
---|
| 88 | data_stat += 0.1 |
---|
| 89 | elif hasattr(d, "__len__"): |
---|
| 90 | data_stat += len(d) |
---|
| 91 | else: |
---|
| 92 | data_stat += d/2.0**64 |
---|
| 93 | |
---|
[219] | 94 | d = v.fetch_raw_data() |
---|
[216] | 95 | if d == None: |
---|
| 96 | dataraw_stat += 0.1 |
---|
| 97 | else: |
---|
| 98 | dataraw_stat += len(d) |
---|
| 99 | |
---|
| 100 | print(" Data stat: %f" % data_stat) |
---|
| 101 | print(" Raw data stat: %f" % dataraw_stat) |
---|
| 102 | |
---|
| 103 | |
---|
[217] | 104 | recurseKey_stat = 0.0 |
---|
| 105 | recurseValue_stat = 0.0 |
---|
| 106 | def checkValues(key): |
---|
| 107 | global recurseKey_stat |
---|
| 108 | global recurseValue_stat |
---|
[255] | 109 | recurseKey_stat += (key.mtime - key.max_bytes_subkeyname) * key.flags |
---|
[217] | 110 | for v in key.values: |
---|
| 111 | recurseValue_stat += (v.data_off - v.data_size) / (1.0 + v.flags) + v.data_in_offset |
---|
[255] | 112 | try: |
---|
| 113 | value = key.values[v.name] |
---|
| 114 | if v != value: |
---|
| 115 | print("WARNING: iterator value '%s' does not match dictionary value '%s'." |
---|
| 116 | % (v.name, value.name)) |
---|
| 117 | except Exception as e: |
---|
| 118 | print("WARNING: iterator value name '%s' is not retrievable via value list\n" |
---|
| 119 | % (v.name,)) |
---|
| 120 | |
---|
[216] | 121 | |
---|
[217] | 122 | def recurseTree(cur, operation): |
---|
| 123 | for k in cur.subkeys: |
---|
| 124 | key = cur.subkeys[k.name] |
---|
| 125 | if k != key: |
---|
| 126 | print("WARNING: iterator subkey '%s' does not match dictionary subkey '%s'." |
---|
| 127 | % (k.name, key.name)) |
---|
| 128 | del key |
---|
| 129 | operation(k) |
---|
| 130 | recurseTree(k, operation) |
---|
| 131 | |
---|
| 132 | # Retrieves all keys by recursion, rather than the iterator, and validates |
---|
| 133 | # list dictionary access. Also builds nonsensical statistics as an excuse |
---|
| 134 | # to access various base structure attributes. |
---|
[225] | 135 | def recurseKeyTally(hive, fh): |
---|
[218] | 136 | checkValues(hive.root) |
---|
| 137 | recurseTree(hive.root, checkValues) |
---|
[217] | 138 | print(" Key stat: %f" % recurseKey_stat) |
---|
| 139 | print(" Value stat: %f" % recurseValue_stat) |
---|
| 140 | |
---|
| 141 | |
---|
[219] | 142 | # Iterates hive gathering stats about security and classname records |
---|
[225] | 143 | def iterFetchRelated(hive, fh): |
---|
[219] | 144 | security_stat = 0.0 |
---|
| 145 | classname_stat = 0.0 |
---|
| 146 | modified_stat = 0.0 |
---|
| 147 | |
---|
| 148 | for k in hive: |
---|
| 149 | cn = k.fetch_classname() |
---|
| 150 | if cn == None: |
---|
| 151 | classname_stat += 0.000001 |
---|
| 152 | elif type(cn) == bytearray: |
---|
| 153 | classname_stat += len(cn)/2**32 |
---|
| 154 | else: |
---|
| 155 | classname_stat += len(cn) |
---|
| 156 | |
---|
| 157 | modified_stat += k.modified |
---|
| 158 | |
---|
| 159 | print(" Security stat: %f" % security_stat) |
---|
| 160 | print(" Classname stat: %f" % classname_stat) |
---|
| 161 | print(" Modified stat: %f" % modified_stat) |
---|
| 162 | |
---|
[220] | 163 | |
---|
| 164 | |
---|
[225] | 165 | def iterIterWalk(hive, fh): |
---|
[220] | 166 | sk_stat = 0.0 |
---|
| 167 | v_stat = 0.0 |
---|
| 168 | iter = pyregfi.HiveIterator(hive) |
---|
| 169 | for k in iter: |
---|
[252] | 170 | path = iter.current_path() |
---|
[220] | 171 | try: |
---|
[252] | 172 | hive_iter = hive.subtree(path[1:]) |
---|
[220] | 173 | sk = hive_iter.first_subkey() |
---|
| 174 | while sk != None: |
---|
| 175 | ssk = hive_iter.find_subkey(sk.name) |
---|
[252] | 176 | if ssk != None: |
---|
[262] | 177 | if ssk.name != None: |
---|
| 178 | sk_stat += len(ssk.name) |
---|
[252] | 179 | else: |
---|
| 180 | print("WARNING: ssk was None") |
---|
[220] | 181 | sk = hive_iter.next_subkey() |
---|
| 182 | |
---|
| 183 | v = hive_iter.first_value() |
---|
| 184 | while v != None: |
---|
| 185 | vv = hive_iter.find_value(v.name) |
---|
[252] | 186 | if vv != None: |
---|
[262] | 187 | if vv.name != None: |
---|
| 188 | v_stat += len(vv.name) |
---|
[252] | 189 | else: |
---|
| 190 | print("WARNING: vv was None") |
---|
[220] | 191 | v = hive_iter.next_value() |
---|
| 192 | |
---|
| 193 | except Exception as e: |
---|
[252] | 194 | print("WARNING: Could not descend to path '%s'.\nError:\n %s\n%s" % (path[1:],e.args,e)) |
---|
[220] | 195 | print(" Subkey stat: %f" % sk_stat) |
---|
| 196 | print(" Value stat: %f" % v_stat) |
---|
| 197 | |
---|
| 198 | |
---|
[225] | 199 | def iterCallbackIO(hive, fh): |
---|
| 200 | fh.seek(0) |
---|
| 201 | new_fh = io.BytesIO(fh.read()) |
---|
| 202 | new_hive = pyregfi.Hive(new_fh) |
---|
| 203 | for k in new_hive: |
---|
| 204 | pass |
---|
[220] | 205 | |
---|
[228] | 206 | |
---|
| 207 | def threadIterMain(iter): |
---|
| 208 | x = 0 |
---|
| 209 | try: |
---|
| 210 | for k in iter: |
---|
| 211 | #x += len(k.name) + len(k.subkeys) |
---|
| 212 | pass |
---|
| 213 | except Exception as e: |
---|
| 214 | print("%s dying young: %s" % (threading.current_thread().name, repr(e))) |
---|
| 215 | # Exceptions are thrown on iteration because python state gets out of |
---|
| 216 | # whack. That's fine, because we're really just interested in finding |
---|
| 217 | # segfaults. People should not use iterators without locks, but it |
---|
| 218 | # should at least not segfault on them. |
---|
| 219 | pass |
---|
| 220 | print("%s finished" % threading.current_thread().name) |
---|
| 221 | |
---|
| 222 | def iterMultithread(hive, fh): |
---|
| 223 | num_threads = 10 |
---|
| 224 | iter = pyregfi.HiveIterator(hive) |
---|
| 225 | threads = [] |
---|
| 226 | for t in range(0,num_threads): |
---|
| 227 | threads.append(threading.Thread(target=threadIterMain, args=(iter,))) |
---|
| 228 | for t in threads: |
---|
| 229 | t.start() |
---|
| 230 | for t in threads: |
---|
| 231 | t.join() |
---|
[253] | 232 | |
---|
| 233 | |
---|
| 234 | def loopSecurity(hive, fh): |
---|
[262] | 235 | cur = hive.root.fetch_security() |
---|
| 236 | while True: |
---|
| 237 | stat += len(cur.descriptor.owner) |
---|
| 238 | stat += len(cur.descriptor.group) |
---|
| 239 | if cur.descriptor.sacl: |
---|
| 240 | stat += len(cur.descriptor.sacl) |
---|
| 241 | if cur.descriptor.dacl: |
---|
| 242 | stat += len(cur.descriptor.dacl) |
---|
| 243 | |
---|
| 244 | nxt = cur.next_security() |
---|
| 245 | if cur == nxt: |
---|
| 246 | break |
---|
[253] | 247 | |
---|
[228] | 248 | |
---|
[253] | 249 | def iterSecurity(hive, fh): |
---|
| 250 | stat = 0 |
---|
| 251 | for k in hive: |
---|
| 252 | security = k.fetch_security() |
---|
| 253 | stat += security.ref_count |
---|
| 254 | stat += len(security.descriptor.owner) |
---|
| 255 | stat += len(security.descriptor.group) |
---|
| 256 | if security.descriptor.sacl: |
---|
| 257 | for ace in security.descriptor.sacl: |
---|
| 258 | stat += ace.flags |
---|
| 259 | if ace.object: |
---|
| 260 | stat += ace.object.int |
---|
| 261 | if ace.inherited_object: |
---|
| 262 | stat += ace.inherited_object.int |
---|
[228] | 263 | |
---|
[253] | 264 | if security.descriptor.dacl: |
---|
| 265 | for ace in security.descriptor.dacl: |
---|
| 266 | stat += ace.flags |
---|
| 267 | if ace.object: |
---|
| 268 | stat += ace.object.int |
---|
| 269 | if ace.inherited_object: |
---|
| 270 | stat += ace.inherited_object.int |
---|
| 271 | print(" Security stat: %d" % stat) |
---|
| 272 | |
---|
[255] | 273 | |
---|
[227] | 274 | tests = { |
---|
| 275 | "iterTallyNames":iterTallyNames, |
---|
| 276 | "iterParentWalk":iterParentWalk, |
---|
| 277 | "iterTallyData":iterTallyData, |
---|
| 278 | "recurseKeyTally":recurseKeyTally, |
---|
| 279 | "iterFetchRelated":iterFetchRelated, |
---|
| 280 | "iterIterWalk":iterIterWalk, |
---|
| 281 | "iterCallbackIO":iterCallbackIO, |
---|
[228] | 282 | "iterMultithread":iterMultithread, |
---|
[253] | 283 | "loopSecurity":loopSecurity, |
---|
| 284 | "iterSecurity":iterSecurity, |
---|
[227] | 285 | } |
---|
[209] | 286 | |
---|
[227] | 287 | def usage(): |
---|
[262] | 288 | sys.stderr.write("USAGE: pyregfi-smoketest.py { test1[,test2[,...]] | * } hive1 [hive2 ...]\n") |
---|
[227] | 289 | sys.stderr.write("\tAvailable tests:\n") |
---|
| 290 | for t in tests.keys(): |
---|
| 291 | sys.stderr.write("\t\t%s\n" % t) |
---|
[215] | 292 | |
---|
| 293 | |
---|
[227] | 294 | if len(sys.argv) < 3: |
---|
| 295 | usage() |
---|
| 296 | sys.exit(1) |
---|
[220] | 297 | |
---|
[262] | 298 | if sys.argv[1] == '*': |
---|
| 299 | selected_tests = tests.keys() |
---|
| 300 | else: |
---|
| 301 | selected_tests = sys.argv[1].split(',') |
---|
| 302 | for st in selected_tests: |
---|
| 303 | if st not in tests: |
---|
| 304 | usage() |
---|
| 305 | sys.stderr.write("ERROR: %s not a valid test type\n\n" % st) |
---|
| 306 | sys.exit(1) |
---|
[220] | 307 | |
---|
[213] | 308 | files = [] |
---|
[227] | 309 | for f in sys.argv[2:]: |
---|
[224] | 310 | files.append((f, open(f,"rb"))) |
---|
[213] | 311 | |
---|
| 312 | |
---|
[215] | 313 | start_time = time.time() |
---|
[213] | 314 | for hname,fh in files: |
---|
[255] | 315 | #hive = pyregfi.Hive(fh) |
---|
| 316 | hive = pyregfi.openHive(hname) |
---|
[227] | 317 | for tname in selected_tests: |
---|
| 318 | t = tests[tname] |
---|
[215] | 319 | teststart = time.time() |
---|
[213] | 320 | tstr = "'%s' on '%s'" % (tname,hname) |
---|
| 321 | print("##BEGIN %s:" % tstr) |
---|
[225] | 322 | t(hive, fh) |
---|
[215] | 323 | print("##END %s; runtime=%f; messages:" % (tstr, time.time() - teststart)) |
---|
[232] | 324 | print(pyregfi.getLogMessages()) |
---|
[213] | 325 | print |
---|
[215] | 326 | sys.stdout.flush() |
---|
[224] | 327 | fh.close() |
---|
[213] | 328 | |
---|
[215] | 329 | hive = None |
---|
[213] | 330 | files = None |
---|
| 331 | tests = None |
---|
| 332 | gc.collect() |
---|
[215] | 333 | print("### Tests Completed, runtime: %f ###" % (time.time() - start_time)) |
---|
[214] | 334 | #print(gc.garbage) |
---|