使用Python打开注册表配置单元文件

问题描述 投票:0回答:1

我需要使用 Python 3 打开注册表配置单元文件。这应该可以在 Windows 系统上运行,也可以使用从其他系统复制的配置单元文件。

不幸的是,我无法使用 Python 打开甚至查看这些文件:

#!/usr/bin/env python3

import os.path
import os

hive_dir = os.path.join(os.path.expandvars(r"%SystemRoot%"), "System32", "Config")
HIVES = ["System", "San", "Security", "Software", "Ntuser.dat"]

def main():
    print("Hive directory {} exists: {}".format(hive_dir, os.path.exists(hive_dir)))
    print("Content of {}: {}".format(hive_dir, os.listdir(hive_dir)))
    for hive in HIVES:
        hive_path = os.path.join(hive_dir, hive)
        print("{} exists: {}".format(hive_path, os.path.exists(hive_path)))

if __name__ == '__main__':
    main()

脚本首先检查 hive 文件所在的目录以及文件是否确实存在。输出是:

Hive directory C:\WINDOWS\System32\Config exists: True
Content of C:\WINDOWS\System32\Config: ['Journal', 'RegBack', 'systemprofile', 'TxR']
C:\WINDOWS\System32\Config\System exists: False
C:\WINDOWS\System32\Config\San exists: False
C:\WINDOWS\System32\Config\Security exists: False
C:\WINDOWS\System32\Config\Software exists: False
C:\WINDOWS\System32\Config\Ntuser.dat exists: False

根据 Microsoft MSDN 文档,文件应该在那里,并且在 Windows 资源管理器中打开目录确实会显示文件:

file listing

使用 PowerShell,我还可以验证文件是否就位:

PS C:\Users\test> dir "$env:SystemRoot\System32\Config"

    Verzeichnis: C:\WINDOWS\System32\Config

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       05.12.2016     13:38                bbimigrate
d-----       16.07.2016     13:47                Journal
d-----       14.03.2017     10:19                RegBack
d-----       05.12.2016     13:19                systemprofile
d-----       16.03.2017     10:14                TxR
-a----       16.03.2017     10:16        1048576 BBI
-a----       05.12.2016     13:15          28672 BCD-Template
-a----       20.03.2017     09:32       91488256 COMPONENTS
-a----       16.03.2017     10:16        1572864 DEFAULT
-a----       16.03.2017     12:16        5259264 DRIVERS
-a----       05.12.2016     14:02          32768 ELAM
-a----       20.03.2017     09:22            120 netlogon.ftl
-a----       05.12.2016     13:12          73728 SAM
-a----       16.03.2017     10:16          73728 SECURITY
-a----       16.03.2017     10:16      103022592 SOFTWARE
-a----       16.03.2017     10:16       19136512 SYSTEM
-a----       05.12.2016     12:39           8192 userdiff
-a----       16.07.2016     13:45           4096 VSMIDK

PS C:\Users\test> Test-Path "$env:SystemRoot\System32\Config\SECURITY"
True

我运行的是 64 位 Windows 10 Enterprise 和 Python 3.5。我在我的生产系统以及虚拟机上验证了该行为。以管理员身份运行 Python 并没有改变任何东西。

这里出了什么问题?

python windows hive registry windows-10
1个回答
2
投票

由于 WOW64 文件系统重定向,您正在运行 32 位 Python 并查看

SysWOW64\config
。在 64 位 Windows 上运行的 32 位进程可以通过
"%SystemRoot%\SysNative"
访问本机系统目录。该目录是虚拟的,在本机进程中不存在,因此首先检查它是否存在。

另外,“San”是一个拼写错误;应该是“SAM”。并且系统配置目录中不应该有“NTUSER.DAT”。该文件仅存在于用户配置文件目录中。

© www.soinside.com 2019 - 2024. All rights reserved.