191 lines
6.8 KiB
C#
191 lines
6.8 KiB
C#
/*
|
|
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
|
* Copyright (C) 2025 Prime Build
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, version 3 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
namespace ThreadPilot.Services
|
|
{
|
|
using System;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
/// <summary>
|
|
/// Service for security validation and auditing of elevated operations.
|
|
/// </summary>
|
|
public class SecurityService : ISecurityService
|
|
{
|
|
private readonly ILogger<SecurityService> logger;
|
|
private readonly IEnhancedLoggingService enhancedLogger;
|
|
|
|
// List of operations that are allowed to be performed with elevated privileges
|
|
private static readonly string[] AllowedElevatedOperations = new[]
|
|
{
|
|
"SetProcessAffinity",
|
|
"SetProcessPriority",
|
|
"ChangePowerPlan",
|
|
"ImportPowerPlan",
|
|
"CreatePowerPlan",
|
|
"DeletePowerPlan",
|
|
"ModifyPowerPlanSettings",
|
|
"ApplicationRestart",
|
|
"CreateScheduledTask",
|
|
"DeleteScheduledTask",
|
|
"ModifyRegistryAutostart",
|
|
};
|
|
|
|
// List of critical system processes that should not be modified
|
|
private static readonly string[] ProtectedProcesses = new[]
|
|
{
|
|
"System",
|
|
"csrss",
|
|
"winlogon",
|
|
"services",
|
|
"lsass",
|
|
"svchost",
|
|
"dwm",
|
|
"explorer",
|
|
"wininit",
|
|
"smss",
|
|
};
|
|
|
|
public SecurityService(ILogger<SecurityService> logger, IEnhancedLoggingService enhancedLogger)
|
|
{
|
|
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
this.enhancedLogger = enhancedLogger ?? throw new ArgumentNullException(nameof(enhancedLogger));
|
|
}
|
|
|
|
public bool ValidateElevatedOperation(string operation)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(operation))
|
|
{
|
|
this.logger.LogWarning("Attempted to validate null or empty operation");
|
|
return false;
|
|
}
|
|
|
|
var isAllowed = AllowedElevatedOperations.Contains(operation, StringComparer.OrdinalIgnoreCase);
|
|
|
|
if (!isAllowed)
|
|
{
|
|
this.logger.LogWarning("Attempted to perform unauthorized elevated operation: {Operation}", operation);
|
|
}
|
|
else
|
|
{
|
|
this.logger.LogDebug("Validated elevated operation: {Operation}", operation);
|
|
}
|
|
|
|
return isAllowed;
|
|
}
|
|
|
|
public async Task AuditElevatedAction(string action, string target, bool success)
|
|
{
|
|
var logLevel = success ? LogLevel.Information : LogLevel.Warning;
|
|
var message = $"Elevated action performed: {action} on {target} - Success: {success}";
|
|
|
|
this.logger.Log(logLevel, message);
|
|
|
|
// Use enhanced logging for structured audit trail
|
|
await this.enhancedLogger.LogSystemEventAsync(
|
|
success ? "ElevatedActionSuccess" : "ElevatedActionFailure",
|
|
$"Security audit: {action} on {target} - Success: {success}",
|
|
logLevel);
|
|
}
|
|
|
|
public bool ValidateProcessOperation(string processName, string operation)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(processName) || string.IsNullOrWhiteSpace(operation))
|
|
{
|
|
this.logger.LogWarning("Attempted to validate process operation with null or empty parameters");
|
|
return false;
|
|
}
|
|
|
|
// Check if the process is in the protected list
|
|
var isProtected = ProtectedProcesses.Contains(processName, StringComparer.OrdinalIgnoreCase);
|
|
|
|
if (isProtected)
|
|
{
|
|
this.logger.LogWarning(
|
|
"Attempted to perform operation '{Operation}' on protected process '{ProcessName}'",
|
|
operation, processName);
|
|
return false;
|
|
}
|
|
|
|
// Validate the operation itself
|
|
var isValidOperation = operation switch
|
|
{
|
|
"SetProcessAffinity" => true,
|
|
"SetProcessPriority" => true,
|
|
"TerminateProcess" => false, // We don't allow process termination
|
|
_ => false,
|
|
};
|
|
|
|
if (!isValidOperation)
|
|
{
|
|
this.logger.LogWarning(
|
|
"Attempted to perform invalid process operation '{Operation}' on '{ProcessName}'",
|
|
operation, processName);
|
|
}
|
|
|
|
return isValidOperation;
|
|
}
|
|
|
|
public bool ValidatePowerPlanOperation(string powerPlanId, string operation)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(powerPlanId) || string.IsNullOrWhiteSpace(operation))
|
|
{
|
|
this.logger.LogWarning("Attempted to validate power plan operation with null or empty parameters");
|
|
return false;
|
|
}
|
|
|
|
// Validate the operation
|
|
var isValidOperation = operation switch
|
|
{
|
|
"ChangePowerPlan" => true,
|
|
"ImportPowerPlan" => true,
|
|
"CreatePowerPlan" => true,
|
|
"DeletePowerPlan" => !IsSystemPowerPlan(powerPlanId), // Don't allow deletion of system power plans
|
|
"ModifyPowerPlanSettings" => true,
|
|
_ => false,
|
|
};
|
|
|
|
if (!isValidOperation)
|
|
{
|
|
this.logger.LogWarning(
|
|
"Attempted to perform invalid power plan operation '{Operation}' on '{PowerPlanId}'",
|
|
operation, powerPlanId);
|
|
}
|
|
|
|
return isValidOperation;
|
|
}
|
|
|
|
public string[] GetAllowedElevatedOperations()
|
|
{
|
|
return AllowedElevatedOperations.ToArray();
|
|
}
|
|
|
|
private static bool IsSystemPowerPlan(string powerPlanId)
|
|
{
|
|
// Common system power plan GUIDs
|
|
var systemPowerPlans = new[]
|
|
{
|
|
"381b4222-f694-41f0-9685-ff5bb260df2e", // Balanced
|
|
"8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c", // High performance
|
|
"a1841308-3541-4fab-bc81-f71556f20b4a", // Power saver
|
|
};
|
|
|
|
return systemPowerPlans.Contains(powerPlanId, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
}
|
|
}
|
|
|