[213] | 1 | #!/usr/bin/env python3 |
---|
[197] | 2 | |
---|
| 3 | import sys |
---|
[213] | 4 | import gc |
---|
[225] | 5 | import io |
---|
[215] | 6 | import time |
---|
[197] | 7 | import pyregfi |
---|
| 8 | |
---|
[225] | 9 | |
---|
[213] | 10 | def usage(): |
---|
| 11 | sys.stderr.write("USAGE: pyregfi-smoketest.py hive1 [hive2 ...]\n") |
---|
[197] | 12 | |
---|
| 13 | |
---|
[215] | 14 | |
---|
| 15 | |
---|
[213] | 16 | # Uses the HiveIterator to walk all keys |
---|
| 17 | # Gathers various (meaningless) statistics to exercise simple attribute access |
---|
| 18 | # and to hopefully smoke out any bugs that can be identified by changing stats |
---|
[225] | 19 | def iterTallyNames(hive, fh): |
---|
[213] | 20 | key_count = 0 |
---|
| 21 | key_lens = 0 |
---|
| 22 | key_rawlens = 0 |
---|
| 23 | value_count = 0 |
---|
| 24 | value_lens = 0 |
---|
| 25 | value_rawlens = 0 |
---|
[197] | 26 | |
---|
[213] | 27 | for k in hive: |
---|
| 28 | key_count += 1 |
---|
| 29 | if k.name != None: |
---|
| 30 | key_lens += len(k.name) |
---|
| 31 | if k.name_raw != None: |
---|
| 32 | key_rawlens += len(k.name_raw) |
---|
[199] | 33 | |
---|
[213] | 34 | for v in k.values: |
---|
| 35 | value_count += 1 |
---|
| 36 | if v.name != None: |
---|
| 37 | value_lens += len(v.name) |
---|
| 38 | if v.name_raw != None: |
---|
| 39 | value_rawlens += len(v.name_raw) |
---|
[200] | 40 | |
---|
[214] | 41 | print(" Counts: keys=%d, values=%d" % (key_count, value_count)) |
---|
| 42 | print(" Total name length: keys=%d, values=%d" % (key_lens, value_lens)) |
---|
| 43 | print(" Total raw name lengths: keys=%d, values=%d" % (key_rawlens, value_rawlens)) |
---|
[200] | 44 | |
---|
[209] | 45 | |
---|
[220] | 46 | # helper function |
---|
| 47 | def getCurrentPath(key): |
---|
| 48 | if key == None: |
---|
| 49 | return '' |
---|
| 50 | path = [] |
---|
| 51 | p = key |
---|
| 52 | while p != None: |
---|
| 53 | path.append(p.name) |
---|
| 54 | p = p.get_parent() |
---|
| 55 | path.reverse() |
---|
| 56 | del path[0] |
---|
| 57 | |
---|
| 58 | return path |
---|
| 59 | |
---|
[215] | 60 | # For each key in the hive, this traverses the parent links up to the root, |
---|
| 61 | # recording the path as it goes, and then uses the subtree/descend method |
---|
| 62 | # to find the same key again, verifying it is the same. This test is currently |
---|
| 63 | # very slow because no key caching is used. |
---|
[225] | 64 | def iterParentWalk(hive, fh): |
---|
[215] | 65 | i = 1 |
---|
| 66 | for k in hive: |
---|
| 67 | path = getCurrentPath(k) |
---|
| 68 | try: |
---|
| 69 | hive_iter = hive.subtree(path) |
---|
| 70 | if hive_iter.current_key() != k: |
---|
| 71 | print("WARNING: k != current_key for path '%s'." % path) |
---|
| 72 | else: |
---|
| 73 | i += 1 |
---|
| 74 | except Exception as e: |
---|
| 75 | print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) |
---|
| 76 | print(" Successfully tested paths on %d keys." % i) |
---|
[209] | 77 | |
---|
[215] | 78 | |
---|
[216] | 79 | # Uses the HiveIterator to walk all keys |
---|
| 80 | # Gathers various (meaningless) statistics about data/data_raw attributes |
---|
[225] | 81 | def iterTallyData(hive, fh): |
---|
[216] | 82 | data_stat = 0.0 |
---|
| 83 | dataraw_stat = 0.0 |
---|
| 84 | |
---|
| 85 | for k in hive: |
---|
| 86 | for v in k.values: |
---|
[219] | 87 | d = v.fetch_data() |
---|
[216] | 88 | if d == None: |
---|
| 89 | data_stat += 0.1 |
---|
| 90 | elif hasattr(d, "__len__"): |
---|
| 91 | data_stat += len(d) |
---|
| 92 | else: |
---|
| 93 | data_stat += d/2.0**64 |
---|
| 94 | |
---|
[219] | 95 | d = v.fetch_raw_data() |
---|
[216] | 96 | if d == None: |
---|
| 97 | dataraw_stat += 0.1 |
---|
| 98 | else: |
---|
| 99 | dataraw_stat += len(d) |
---|
| 100 | |
---|
| 101 | print(" Data stat: %f" % data_stat) |
---|
| 102 | print(" Raw data stat: %f" % dataraw_stat) |
---|
| 103 | |
---|
| 104 | |
---|
[217] | 105 | recurseKey_stat = 0.0 |
---|
| 106 | recurseValue_stat = 0.0 |
---|
| 107 | def checkValues(key): |
---|
| 108 | global recurseKey_stat |
---|
| 109 | global recurseValue_stat |
---|
| 110 | recurseKey_stat += (key.mtime.low^key.mtime.high - key.max_bytes_subkeyname) * key.flags |
---|
| 111 | for v in key.values: |
---|
| 112 | recurseValue_stat += (v.data_off - v.data_size) / (1.0 + v.flags) + v.data_in_offset |
---|
| 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)) |
---|
[216] | 117 | |
---|
[217] | 118 | def recurseTree(cur, operation): |
---|
| 119 | for k in cur.subkeys: |
---|
| 120 | key = cur.subkeys[k.name] |
---|
| 121 | if k != key: |
---|
| 122 | print("WARNING: iterator subkey '%s' does not match dictionary subkey '%s'." |
---|
| 123 | % (k.name, key.name)) |
---|
| 124 | del key |
---|
| 125 | operation(k) |
---|
| 126 | recurseTree(k, operation) |
---|
| 127 | |
---|
| 128 | # Retrieves all keys by recursion, rather than the iterator, and validates |
---|
| 129 | # list dictionary access. Also builds nonsensical statistics as an excuse |
---|
| 130 | # to access various base structure attributes. |
---|
[225] | 131 | def recurseKeyTally(hive, fh): |
---|
[218] | 132 | checkValues(hive.root) |
---|
| 133 | recurseTree(hive.root, checkValues) |
---|
[217] | 134 | print(" Key stat: %f" % recurseKey_stat) |
---|
| 135 | print(" Value stat: %f" % recurseValue_stat) |
---|
| 136 | |
---|
| 137 | |
---|
[219] | 138 | # Iterates hive gathering stats about security and classname records |
---|
[225] | 139 | def iterFetchRelated(hive, fh): |
---|
[219] | 140 | security_stat = 0.0 |
---|
| 141 | classname_stat = 0.0 |
---|
| 142 | modified_stat = 0.0 |
---|
| 143 | |
---|
| 144 | for k in hive: |
---|
| 145 | cn = k.fetch_classname() |
---|
| 146 | if cn == None: |
---|
| 147 | classname_stat += 0.000001 |
---|
| 148 | elif type(cn) == bytearray: |
---|
| 149 | classname_stat += len(cn)/2**32 |
---|
| 150 | else: |
---|
| 151 | classname_stat += len(cn) |
---|
| 152 | |
---|
| 153 | modified_stat += k.modified |
---|
| 154 | |
---|
| 155 | print(" Security stat: %f" % security_stat) |
---|
| 156 | print(" Classname stat: %f" % classname_stat) |
---|
| 157 | print(" Modified stat: %f" % modified_stat) |
---|
| 158 | |
---|
[220] | 159 | |
---|
| 160 | |
---|
[225] | 161 | def iterIterWalk(hive, fh): |
---|
[220] | 162 | sk_stat = 0.0 |
---|
| 163 | v_stat = 0.0 |
---|
| 164 | iter = pyregfi.HiveIterator(hive) |
---|
| 165 | for k in iter: |
---|
| 166 | path = getCurrentPath(k) |
---|
| 167 | try: |
---|
| 168 | hive_iter = hive.subtree(path) |
---|
| 169 | sk = hive_iter.first_subkey() |
---|
| 170 | while sk != None: |
---|
| 171 | ssk = hive_iter.find_subkey(sk.name) |
---|
| 172 | sk_stat += len(ssk.name) |
---|
| 173 | sk = hive_iter.next_subkey() |
---|
| 174 | |
---|
| 175 | v = hive_iter.first_value() |
---|
| 176 | while v != None: |
---|
| 177 | vv = hive_iter.find_value(v.name) |
---|
| 178 | v_stat += len(vv.name) |
---|
| 179 | v = hive_iter.next_value() |
---|
| 180 | |
---|
| 181 | except Exception as e: |
---|
| 182 | print("WARNING: Could not decend to path '%s'.\nError:\n %s\n%s" % (path,e.args,e)) |
---|
| 183 | print(" Subkey stat: %f" % sk_stat) |
---|
| 184 | print(" Value stat: %f" % v_stat) |
---|
| 185 | |
---|
| 186 | |
---|
[225] | 187 | def iterCallbackIO(hive, fh): |
---|
| 188 | fh.seek(0) |
---|
| 189 | new_fh = io.BytesIO(fh.read()) |
---|
| 190 | new_hive = pyregfi.Hive(new_fh) |
---|
| 191 | for k in new_hive: |
---|
| 192 | pass |
---|
[220] | 193 | |
---|
[225] | 194 | |
---|
[213] | 195 | if len(sys.argv) < 2: |
---|
| 196 | usage() |
---|
| 197 | sys.exit(1) |
---|
[209] | 198 | |
---|
[215] | 199 | |
---|
[220] | 200 | tests = [("iterTallyNames",iterTallyNames), |
---|
| 201 | ("iterParentWalk",iterParentWalk), |
---|
| 202 | ("iterTallyData",iterTallyData), |
---|
| 203 | ("recurseKeyTally",recurseKeyTally), |
---|
| 204 | ("iterFetchRelated",iterFetchRelated), |
---|
| 205 | ("iterIterWalk",iterIterWalk),] |
---|
[215] | 206 | |
---|
[225] | 207 | tests = [("iterCallbackIO",iterCallbackIO),] |
---|
[220] | 208 | |
---|
| 209 | |
---|
[213] | 210 | files = [] |
---|
| 211 | for f in sys.argv[1:]: |
---|
[224] | 212 | files.append((f, open(f,"rb"))) |
---|
[213] | 213 | |
---|
| 214 | |
---|
[215] | 215 | start_time = time.time() |
---|
[213] | 216 | for hname,fh in files: |
---|
| 217 | hive = pyregfi.Hive(fh) |
---|
| 218 | for tname,t in tests: |
---|
[215] | 219 | teststart = time.time() |
---|
[213] | 220 | tstr = "'%s' on '%s'" % (tname,hname) |
---|
| 221 | print("##BEGIN %s:" % tstr) |
---|
[225] | 222 | t(hive, fh) |
---|
[215] | 223 | print("##END %s; runtime=%f; messages:" % (tstr, time.time() - teststart)) |
---|
[213] | 224 | print(pyregfi.GetLogMessages()) |
---|
| 225 | print |
---|
[215] | 226 | sys.stdout.flush() |
---|
[224] | 227 | fh.close() |
---|
[213] | 228 | |
---|
[215] | 229 | hive = None |
---|
[213] | 230 | files = None |
---|
| 231 | tests = None |
---|
| 232 | gc.collect() |
---|
[215] | 233 | print("### Tests Completed, runtime: %f ###" % (time.time() - start_time)) |
---|
[214] | 234 | #print(gc.garbage) |
---|