C# 从C操作注册表配置单元文件#

C# 从C操作注册表配置单元文件#,c#,registry,hive,C#,Registry,Hive,1.) 如何从C#为注册表加载、编辑和保存二进制配置单元文件 我找到了这个Win32 api。 这家伙分享了将二进制配置单元文件的内容转储为文本的代码。 (二) 除了操纵配置单元文件外,我还搜索一种方法,以便在运行时使用C将配置单元文件加载到注册表中# (类似于在regedit中的文件many上的加载配置单元和卸载配置单元命令) /谢谢您看过Microsoft.Win32中的注册表和注册表项类了吗 听起来您可能需要创建自己的表示来读取配置单元文件,然后排队或立即进行相应的注册表更改。同样,

1.) 如何从C#为注册表加载、编辑和保存二进制配置单元文件

我找到了这个Win32 api。

这家伙分享了将二进制配置单元文件的内容转储为文本的代码。

(二) 除了操纵配置单元文件外,我还搜索一种方法,以便在运行时使用C将配置单元文件加载到注册表中# (类似于在regedit中的文件many上的加载配置单元和卸载配置单元命令)


/谢谢

您看过Microsoft.Win32中的注册表和注册表项类了吗


听起来您可能需要创建自己的表示来读取配置单元文件,然后排队或立即进行相应的注册表更改。同样,您需要将自己的转换器写回磁盘。

下面的文章解释了如何在不使用WinAPI(advapi32.dll)的情况下分析注册表文件。在这种特殊情况下,这家伙正在使用Mono:

使用(FileStream fs=File.OpenRead(path)){
变量数据=新字节[已检查((int)fs.Length)];
int i=0;
int-read;
使用(var ms=newmemoryStream(选中((int)fs.Length))){
而((read=fs.read(data,0,data.Length))>0){
ms.写入(数据,0,读取);
i+=读取;
}
字节[]配置单元=ms.ToArray();
char[]cList=新字符[fs.Length];
i=0;
foreach(配置单元中的字节b)
cList[i++]=(char)b;
字符串d=新字符串(cList);
int all=0;
foreach(在lf.Matches(d)中匹配mx){//您可以在这里更改所需的正则表达式。
字节[]bb=新字节[mx.Value.Length];
char[]cb=新字符[mx.Value.Length];
对于(int k=0;k
请参见:


它通过GTK接口在C#中读取脱机蜂箱。虽然还没有写支持。

这已经9年了,但我认为这可以帮助其他人。我编写了这个类,它允许您执行以下操作:

Hive.AcquirePrivileges() // Acquires the privileges necessary for loading the hive
Hive myregistryhive = Hive.LoadFromFile("hivepathhere") // Loads the hive
// use myregistryhive.RootKey (a RegistryKey), read and/or write to it and its sub keys
myregistryhive.SaveAndUnload() // Unloads the hive
Hive.ReturnPrivileges() // De-elevate back to normal privileges.
该类的代码:

class Hive
{
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegLoadKey(IntPtr hKey, string lpSubKey, string lpFile);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegSaveKey(IntPtr hKey, string lpFile, uint securityAttrPtr = 0);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegUnLoadKey(IntPtr hKey, string lpSubKey);

    [DllImport("ntdll.dll", SetLastError = true)]
    static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref UInt64 lpLuid);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(IntPtr lpSystemName, string lpName, ref UInt64 lpLuid);

    private RegistryKey parentKey;
    private string name;
    private string originalPath;
    public RegistryKey RootKey;

    private Hive() { }

    public static Hive LoadFromFile(string Path)
    {
        Hive result = new Hive();

        result.parentKey = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Default);
        result.name = Guid.NewGuid().ToString();
        result.originalPath = Path;
        IntPtr parentHandle = result.parentKey.Handle.DangerousGetHandle();
        RegLoadKey(parentHandle, result.name, Path);
        //Console.WriteLine(Marshal.GetLastWin32Error());
        result.RootKey = result.parentKey.OpenSubKey(result.name, true);
        return result;
    }
    public static void AcquirePrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
    }
    public static void ReturnPrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
    }
    public void SaveAndUnload()
    {
        RootKey.Close();
        RegUnLoadKey(parentKey.Handle.DangerousGetHandle(), name);
        parentKey.Close();
    }
}

编辑:请注意,这需要管理员权限。

。。。如果没有书面支持,这似乎不是问题的答案,因为它明确表示“编辑并保存”。我没有说这是答案。但是我正在积极地做这部分的工作。我实际上写了上面的代码,那是我的博客。那个代码真的很差,我不推荐使用它。我确实写了一个,一个使用它的工具在Metasploit框架(tools/reg.rb)中。最终,我也会用C#实现这个库,但不是很快。。这不是很快的时间。。。你有吗??)@User1577946 win32 api有一个.NET包装器:
class Hive
{
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegLoadKey(IntPtr hKey, string lpSubKey, string lpFile);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegSaveKey(IntPtr hKey, string lpFile, uint securityAttrPtr = 0);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RegUnLoadKey(IntPtr hKey, string lpSubKey);

    [DllImport("ntdll.dll", SetLastError = true)]
    static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref UInt64 lpLuid);

    [DllImport("advapi32.dll")]
    static extern bool LookupPrivilegeValue(IntPtr lpSystemName, string lpName, ref UInt64 lpLuid);

    private RegistryKey parentKey;
    private string name;
    private string originalPath;
    public RegistryKey RootKey;

    private Hive() { }

    public static Hive LoadFromFile(string Path)
    {
        Hive result = new Hive();

        result.parentKey = RegistryKey.OpenBaseKey(RegistryHive.Users, RegistryView.Default);
        result.name = Guid.NewGuid().ToString();
        result.originalPath = Path;
        IntPtr parentHandle = result.parentKey.Handle.DangerousGetHandle();
        RegLoadKey(parentHandle, result.name, Path);
        //Console.WriteLine(Marshal.GetLastWin32Error());
        result.RootKey = result.parentKey.OpenSubKey(result.name, true);
        return result;
    }
    public static void AcquirePrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, true, false, out throwaway);
    }
    public static void ReturnPrivileges()
    {
        ulong luid = 0;
        bool throwaway;
        LookupPrivilegeValue(IntPtr.Zero, "SeRestorePrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
        LookupPrivilegeValue(IntPtr.Zero, "SeBackupPrivilege", ref luid);
        RtlAdjustPrivilege((int)luid, false, false, out throwaway);
    }
    public void SaveAndUnload()
    {
        RootKey.Close();
        RegUnLoadKey(parentKey.Handle.DangerousGetHandle(), name);
        parentKey.Close();
    }
}