繁体   English   中英

如何阅读Windows注册表文件以检查值? [蟒蛇]

[英]How do I read Windows Registry file to check for values? [Python]

我正在尝试对Windows注册表文件(.reg文件,脱机)执行审核检查,我希望可以利用Python对reg文件进行检查。

例如(伪代码):

#Configure registry policy processing: Do not apply during periodic background processing 

testloc = "C:\\Users\\test.reg"
datafile = open(testloc, "r")
read = datafile.read()

find(Software\Policies\Microsoft\Windows\Group 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的实时系统上进行了检查。 另一个问题是.reg文件大(〜200MB)。

如何使用Python执行这种检查?

我不知道是否有一个库可以专门读取.reg文件,但是从外观上看,它只是一个INI文件,顶部带有附加版本信息。

这是如何使用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_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Group Policy\{35378EAC-683F-11D2-A89A-00C04FBBCFA2}"]
value = key['"NoBackgroundPolicy"']

print(value)

但是,以这种方式进行操作可能会有一些缺点,例如,如何格式化您获得的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM