From 2089d62dfdbce287b124026f5ee5c753dfb258ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 12 Nov 2018 02:15:47 +0100 Subject: [PATCH] Replaced call to BCDEdit.exe with proper WMI calls --- DerpingDrivers/Util/BcdHelper.cs | 60 ++++++++++++-------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/DerpingDrivers/Util/BcdHelper.cs b/DerpingDrivers/Util/BcdHelper.cs index fa60a3b..edb22c6 100644 --- a/DerpingDrivers/Util/BcdHelper.cs +++ b/DerpingDrivers/Util/BcdHelper.cs @@ -1,14 +1,14 @@ using System; -using System.Diagnostics; using System.Management; using DerpingDrivers.Exceptions; using ExceptionReporting; -using Microsoft.Win32; namespace DerpingDrivers.Util { public static class BcdHelper { + private const uint BcdLibraryBoolean_AllowPrereleaseSignatures = 0x16000049; + /// /// Gets or sets the current value if BCDE_LIBRARY_TYPE_ALLOW_PRERELEASE_SIGNATURES from the default boot entry. /// @@ -30,8 +30,8 @@ namespace DerpingDrivers.Util "root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""), null)) { var inParams = bootMgrObj.GetMethodParameters("GetElement"); - - inParams["Type"] = (uint) 0x16000049; + + inParams["Type"] = BcdLibraryBoolean_AllowPrereleaseSignatures; var outParams = bootMgrObj.InvokeMethod("GetElement", inParams, null); var outObj = (ManagementBaseObject) outParams?.Properties["Element"].Value; @@ -45,47 +45,31 @@ namespace DerpingDrivers.Util { try { - if (value) - { - // NOTE: haven't found an API for this (yet), so system() we go! - var cmd = new Process + var connectionOptions = + new ConnectionOptions { - StartInfo = - { - FileName = @"C:\WINDOWS\system32\bcdedit.exe", - Arguments = "-set TESTSIGNING ON", - WindowStyle = ProcessWindowStyle.Hidden, - CreateNoWindow = true, - UseShellExecute = false - } + Impersonation = ImpersonationLevel.Impersonate, + EnablePrivileges = true }; - cmd.Start(); - cmd.WaitForExit(); + var managementScope = new ManagementScope(@"root\WMI", connectionOptions); - if (cmd.ExitCode != 0) - throw new BcdAlterAllowPrereleaseSignaturesFailedException("Couldn't enable TESTSIGNING"); - } - else + using (var bootMgrObj = new ManagementObject(managementScope, + new ManagementPath( + "root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""), + null)) { - // NOTE: haven't found an API for this (yet), so system() we go! - var cmd = new Process - { - StartInfo = - { - FileName = @"C:\WINDOWS\system32\bcdedit.exe", - Arguments = "-set TESTSIGNING OFF", - WindowStyle = ProcessWindowStyle.Hidden, - CreateNoWindow = true, - UseShellExecute = false - } - }; + var inParams = bootMgrObj.GetMethodParameters("SetBooleanElement"); - cmd.Start(); - cmd.WaitForExit(); + inParams["Type"] = BcdLibraryBoolean_AllowPrereleaseSignatures; + inParams["Boolean"] = value; + var outParams = bootMgrObj.InvokeMethod("SetBooleanElement", inParams, null); + var ret = outParams?.Properties["ReturnValue"].Value; + var returnValue = ret != null && (bool) ret; - if (cmd.ExitCode != 0) - throw new BcdAlterAllowPrereleaseSignaturesFailedException("Couldn't disable TESTSIGNING"); + if (!returnValue) + throw new BcdAlterAllowPrereleaseSignaturesFailedException( + "Couldn't change TESTSIGNING state"); } } catch (Exception ex)