我正在尝试在Windows注册表文件(.REG文件,离线)上执行审核检查,我希望我可以利用Python对Reg File进行检查。
例如(伪代码):
#Configure registry policy processing: Do not apply during periodic background processing
testloc = "C:\Users\test.reg"
datafile = open(testloc, "r")
read = datafile.read()
find(SoftwarePoliciesMicrosoftWindowsGroup Policy{35378EAC-683F-11D2-A89A-00C04FBBCFA2})
check(NoBackgroundPolicy) #check if dword value is correct
if(dword == correct):
print("correct")
else:
print("wrong")
我尝试查看_winreg,但似乎它使用Windows API对实时系统进行检查。另一个问题是大文件大小(〜200MB)。
如何使用Python执行此类检查?
我不知道是否有一个可以读取.reg文件的lib,但是从看起来它只是一个ini文件,顶部有额外的版本信息。p>这是您如何使用configparser
模块的一个示例。一些笔记:
- .REG文件在UTF16中编码
- 将文件提供给
ConfigParser
之前,readline
将跳过版本信息(类似于Windows Registry Editor Version 5.00
)。否则将导致MissingSectionHeaderError
。 - 值的名称包括引号,这意味着您在查找密钥中的值时需要明确添加它们。
import configparser
testloc = "C:\Users\test.reg"
regdata = configparser.ConfigParser()
with open(testloc, "r", encoding="utf-16") as f:
f.readline() # skip version info in first line
regdata.read_file(f)
key = regdata[r"HKEY_LOCAL_MACHINESOFTWAREPoliciesMicrosoftWindowsGroup Policy{35378EAC-683F-11D2-A89A-00C04FBBCFA2}"]
value = key['"NoBackgroundPolicy"']
print(value)
不过,这样做可能存在缺点