Don't request all access for OpenSCManager

This commit is contained in:
PhyxionNL
2023-03-23 10:46:55 +01:00
parent 8892fa48ee
commit 77f2e4b399
3 changed files with 30 additions and 34 deletions
@@ -25,17 +25,15 @@ internal class KernelDriver
_driverId = driverId;
}
public bool IsOpen
{
get { return _device != null; }
}
public bool IsOpen => _device != null;
public bool Install(string path, out string errorMessage)
{
IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_ALL_ACCESS);
IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_CREATE_SERVICE);
if (manager == IntPtr.Zero)
{
errorMessage = "OpenSCManager returned zero.";
int errorCode = Marshal.GetLastWin32Error();
errorMessage = $"OpenSCManager returned the error code: {errorCode:X8}.";
return false;
}
@@ -55,24 +53,24 @@ internal class KernelDriver
if (service == IntPtr.Zero)
{
int error = Marshal.GetHRForLastWin32Error();
if (error == Kernel32.ERROR_SERVICE_EXISTS)
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == Kernel32.ERROR_SERVICE_EXISTS)
{
errorMessage = "Service already exists";
return false;
}
errorMessage = "CreateService returned the error: " + Marshal.GetExceptionForHR(error).Message;
errorMessage = $"CreateService returned the error code: {errorCode:X8}.";
AdvApi32.CloseServiceHandle(manager);
return false;
}
if (!AdvApi32.StartService(service, 0, null))
{
int error = Marshal.GetHRForLastWin32Error();
if (error != Kernel32.ERROR_SERVICE_ALREADY_RUNNING)
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != Kernel32.ERROR_SERVICE_ALREADY_RUNNING)
{
errorMessage = "StartService returned the error: " + Marshal.GetExceptionForHR(error).Message;
errorMessage = $"StartService returned the error code: {errorCode:X8}.";
AdvApi32.CloseServiceHandle(service);
AdvApi32.CloseServiceHandle(manager);
return false;
@@ -101,23 +99,17 @@ internal class KernelDriver
public bool Open()
{
IntPtr fileHandle = Kernel32.CreateFile(@"\\.\" + _driverId, 0xC0000000, FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
_device = new SafeFileHandle(fileHandle, true);
if (_device.IsInvalid)
{
_device.Close();
_device.Dispose();
_device = null;
}
Close();
return _device != null;
}
public bool DeviceIOControl(Kernel32.IOControlCode ioControlCode, object inBuffer)
{
if (_device == null)
return false;
return Kernel32.DeviceIoControl(_device, ioControlCode, inBuffer, inBuffer == null ? 0 : (uint)Marshal.SizeOf(inBuffer), null, 0, out uint _, IntPtr.Zero);
return _device != null && Kernel32.DeviceIoControl(_device, ioControlCode, inBuffer, inBuffer == null ? 0 : (uint)Marshal.SizeOf(inBuffer), null, 0, out uint _, IntPtr.Zero);
}
public bool DeviceIOControl<T>(Kernel32.IOControlCode ioControlCode, object inBuffer, ref T outBuffer)
@@ -170,7 +162,7 @@ internal class KernelDriver
public bool Delete()
{
IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_ALL_ACCESS);
IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_CONNECT);
if (manager == IntPtr.Zero)
return false;
@@ -189,4 +181,4 @@ internal class KernelDriver
return true;
}
}
}
+13 -9
View File
@@ -72,7 +72,7 @@ internal static class Ring0
}
else
{
_report.Append("Status: Installing driver \"").Append(_filePath).Append("\" failed").AppendLine(File.Exists(_filePath) ? " and file exists" : string.Empty);
_report.Append($"Status: Installing driver \"{_filePath}\" failed").AppendLine(File.Exists(_filePath) ? " and file exists" : string.Empty);
_report.Append("First Exception: ").AppendLine(installError);
_report.Append("Second Exception: ").AppendLine(secondError);
}
@@ -205,7 +205,9 @@ internal static class Ring0
_filePath = null;
}
catch
{ }
{
// Ignored.
}
}
private static string GetServiceName()
@@ -251,7 +253,7 @@ internal static class Ring0
private static string GetFilePath()
{
string filePath;
string filePath = null;
try
{
@@ -259,7 +261,7 @@ internal static class Ring0
if (!string.IsNullOrEmpty(processModule?.FileName))
{
filePath = Path.ChangeExtension(processModule.FileName, ".sys");
if (TryCreate(filePath))
if (CanCreate(filePath))
return filePath;
}
}
@@ -268,12 +270,14 @@ internal static class Ring0
// Continue with the other options.
}
string previousFilePath = filePath;
filePath = GetPathFromAssembly(Assembly.GetExecutingAssembly());
if (!string.IsNullOrEmpty(filePath) && TryCreate(filePath))
if (previousFilePath != filePath && !string.IsNullOrEmpty(filePath) && CanCreate(filePath))
return filePath;
previousFilePath = filePath;
filePath = GetPathFromAssembly(typeof(Ring0).Assembly);
if (!string.IsNullOrEmpty(filePath) && TryCreate(filePath))
if (previousFilePath != filePath && !string.IsNullOrEmpty(filePath) && CanCreate(filePath))
return filePath;
try
@@ -282,7 +286,7 @@ internal static class Ring0
if (!string.IsNullOrEmpty(filePath))
{
filePath = Path.ChangeExtension(filePath, ".sys");
if (TryCreate(filePath))
if (CanCreate(filePath))
return filePath;
}
}
@@ -306,11 +310,11 @@ internal static class Ring0
}
}
static bool TryCreate(string path)
static bool CanCreate(string path)
{
try
{
using (File.Create(path))
using (File.Create(path, 1, FileOptions.DeleteOnClose))
return true;
}
catch
+2 -2
View File
@@ -14,7 +14,7 @@ internal class AdvApi32
{
private const string DllName = "advapi32.dll";
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DllImport(DllName, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, SC_MANAGER_ACCESS_MASK dwDesiredAccess);
@@ -147,4 +147,4 @@ internal class AdvApi32
public uint dwCheckPoint;
public uint dwWaitHint;
}
}
}