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.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;
/// <summary>
/// Gets or sets the current value if BCDE_LIBRARY_TYPE_ALLOW_PRERELEASE_SIGNATURES from the default boot entry.
/// </summary>
@ -31,7 +31,7 @@ namespace DerpingDrivers.Util
{
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)
var connectionOptions =
new ConnectionOptions
{
// 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 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)