簡體   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