1
0

Replaced call to BCDEdit.exe with proper WMI calls

This commit is contained in:
Benjamin Höglinger-Stelzer 2018-11-12 02:15:47 +01:00
parent 8ad0aeede6
commit 2089d62dfd

View File

@ -1,14 +1,14 @@
using System; using System;
using System.Diagnostics;
using System.Management; using System.Management;
using DerpingDrivers.Exceptions; using DerpingDrivers.Exceptions;
using ExceptionReporting; using ExceptionReporting;
using Microsoft.Win32;
namespace DerpingDrivers.Util namespace DerpingDrivers.Util
{ {
public static class BcdHelper public static class BcdHelper
{ {
private const uint BcdLibraryBoolean_AllowPrereleaseSignatures = 0x16000049;
/// <summary> /// <summary>
/// Gets or sets the current value if BCDE_LIBRARY_TYPE_ALLOW_PRERELEASE_SIGNATURES from the default boot entry. /// Gets or sets the current value if BCDE_LIBRARY_TYPE_ALLOW_PRERELEASE_SIGNATURES from the default boot entry.
/// </summary> /// </summary>
@ -31,7 +31,7 @@ namespace DerpingDrivers.Util
{ {
var inParams = bootMgrObj.GetMethodParameters("GetElement"); var inParams = bootMgrObj.GetMethodParameters("GetElement");
inParams["Type"] = (uint) 0x16000049; inParams["Type"] = BcdLibraryBoolean_AllowPrereleaseSignatures;
var outParams = bootMgrObj.InvokeMethod("GetElement", inParams, null); var outParams = bootMgrObj.InvokeMethod("GetElement", inParams, null);
var outObj = (ManagementBaseObject) outParams?.Properties["Element"].Value; var outObj = (ManagementBaseObject) outParams?.Properties["Element"].Value;
@ -45,47 +45,31 @@ namespace DerpingDrivers.Util
{ {
try try
{ {
if (value) var connectionOptions =
new ConnectionOptions
{ {
// NOTE: haven't found an API for this (yet), so system() we go! Impersonation = ImpersonationLevel.Impersonate,
var cmd = new Process EnablePrivileges = true
{
StartInfo =
{
FileName = @"C:\WINDOWS\system32\bcdedit.exe",
Arguments = "-set TESTSIGNING ON",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
}; };
cmd.Start(); var managementScope = new ManagementScope(@"root\WMI", connectionOptions);
cmd.WaitForExit();
if (cmd.ExitCode != 0) using (var bootMgrObj = new ManagementObject(managementScope,
throw new BcdAlterAllowPrereleaseSignaturesFailedException("Couldn't enable TESTSIGNING"); new ManagementPath(
} "root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\""),
else null))
{ {
// NOTE: haven't found an API for this (yet), so system() we go! var inParams = bootMgrObj.GetMethodParameters("SetBooleanElement");
var cmd = new Process
{
StartInfo =
{
FileName = @"C:\WINDOWS\system32\bcdedit.exe",
Arguments = "-set TESTSIGNING OFF",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false
}
};
cmd.Start(); inParams["Type"] = BcdLibraryBoolean_AllowPrereleaseSignatures;
cmd.WaitForExit(); 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) if (!returnValue)
throw new BcdAlterAllowPrereleaseSignaturesFailedException("Couldn't disable TESTSIGNING"); throw new BcdAlterAllowPrereleaseSignaturesFailedException(
"Couldn't change TESTSIGNING state");
} }
} }
catch (Exception ex) catch (Exception ex)