简体   繁体   English

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

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

I am trying to perform auditing checks on Windows Registry file (.reg file, offline) and I am hoping that I can utilize Python to perform a check on the reg file. 我正在尝试对Windows注册表文件(.reg文件,脱机)执行审核检查,我希望可以利用Python对reg文件进行检查。

For example (pseudo code): 例如(伪代码):

#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")

I have tried looking at _winreg but it seems like it does a check on a live system using Windows API. 我尝试查看_winreg,但似乎它在使用Windows API的实时系统上进行了检查。 Another issue is the large .reg file size(~200MB). 另一个问题是.reg文件大(〜200MB)。

How can I perform such a check using Python? 如何使用Python执行这种检查?

I don't know if there's a lib that can read .reg files specifically, but from what it looks like it's just an INI file with an extra version information at the top. 我不知道是否有一个库可以专门读取.reg文件,但是从外观上看,它只是一个INI文件,顶部带有附加版本信息。

Here's an example of how you could use the configparser module for that. 这是如何使用configparser模块的示例。 Some notes: 一些注意事项:

  • .reg files are encoded in utf16 .reg文件编码为utf16
  • Before giving the file to the ConfigParser , a readline will skip the version info (something like Windows Registry Editor Version 5.00 ). 给文件到之前ConfigParser ,一个readline会跳过版本信息(类似于Windows Registry Editor Version 5.00 )。 Otherwise it will result in a MissingSectionHeaderError . 否则将导致MissingSectionHeaderError
  • The names of values include quotation marks, which means you need to add them explicitly when looking up the value in the key. 值的名称包括引号,这意味着在键中查找值时需要显式添加它们。

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)

There may be drawbacks of doing it this way though, for example in how the values you obtain are formatted. 但是,以这种方式进行操作可能会有一些缺点,例如,如何格式化您获得的值。

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

相关问题 在不引入回车符的情况下,如何在Windows上使用Python(3)读取/写入文件? - How do I read / write a file in Python (3) on Windows without introducing carriage returns? 如何使用python读取Windows历史记录(文件夹)的内容? - How do I read the contents of Windows History (folder) using python? 如何将 arguments 从命令行传递到 python 脚本以读取和更新 json 文件中存在的某些键的值 - How do I pass arguments from command line to the python script to read and update values of certain keys present in a json file 如何检查要完成的文件下载Python3? - How do I check for a file to be finished downloading Python3? 如何使用python检查文本文件中是否存在数据? - How do I check a data exist or not in text file using python? 如何从 Python 中的文件检查用户输入? - How do I check user input from a file in Python? 如何读取python 3.3.3中的文本文件并将其存储在变量中? - How do I read in a text file in python 3.3.3 and store it in a variable? 我如何只读取 .text 文件的特定行? Python - How do i read only a specific line of a .text file? Python 如何在 Python 中只读取文件的一部分? - How do I read only a part of a file in Python? 如何在 python 中读取具有固定开始和结束的文件 - How do i read file in python with fixed start and end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM