I'm able to successfully retrieve the 5 sub-keys from my windows 7 machine registry hive "HKEY_LOCAL_MACHINE" with the code below.
from _winreg import *
try:
i = 0
while True:
subkey = EnumKey(HKEY_LOCAL_MACHINE, i)
print subkey
i += 1
except WindowsError:
pass
My question is, how do I then enumerate the keys under those? I want to end up listing all the keys in the SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged folder but I can't figure out how to step my way down there.
In response to the first comment, I ran this code on my machine and while it didn't error out, it didn't produce results.
from _winreg import *
aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Signatures\Unmanaged")
for i in range(1024):
try:
keyname = EnumKey(aKey, i)
asubkey = OpenKey(aKey, keyname)
val = QueryValueEx(asubkey, "Description")
print val
except WindowsError:
break
A regedit or reg query shows 6 values in that folder but I can't get a python script to show me those six.
Built-in module winreg
has QueryInfoKey
function which allows you to query number of subkeys
https://docs.python.org/3/library/winreg.html#winreg.QueryInfoKey
The result is a tuple of 3 items:
Index Meaning 0 An integer giving the number of sub keys this key has. 1 An integer giving the number of values this key has. 2 An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.
So you can just iterate for exact number of times without having to catch errors:
import winreg
with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as key:
subkeys_number, values_number, weird_last_modified = winreg.QueryInfoKey(key)
print(f"{subkeys_number=}")
for i in range(subkeys_number):
subkey_name = winreg.EnumKey(key, i)
print(subkey_name)
Then you can just use winreg.OpenKey(key, subkey_name)
to step deeper.