[Feature] Aggiunta protezione machine-binding tramite MachineGuard
- Nuovo progetto MachineGuard: libreria che verifica se la macchina corrente è autorizzata all'esecuzione tramite DPAPI (Data Protection API di Windows) - Nuovo progetto MachineGuardSetup: tool di configurazione da eseguire come Amministratore per registrare la macchina autorizzata - Data_Coupler.sln: aggiunti entrambi i nuovi progetti alla soluzione - Data_Coupler.csproj: aggiunto riferimento al progetto MachineGuard - Program.cs: integrazione MachineGuard all'avvio dell'applicazione; se la macchina non è autorizzata l'app viene arrestata immediatamente con log critico e scrittura nel Windows Event Log
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Helper pubblico per la scrittura e la verifica del file secret di MachineGuard.
|
||||
/// Usato da MachineGuardSetup e da eventuali script di deployment.
|
||||
/// </summary>
|
||||
public static class MachineGuardSetupHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Cifra il token interno con DPAPI (LocalMachine scope) e lo scrive nel percorso specificato.
|
||||
/// Crea la directory di destinazione se non esiste.
|
||||
/// </summary>
|
||||
/// <param name="secretFilePath">
|
||||
/// Percorso completo del file in cui salvare il secret cifrato.
|
||||
/// Usare <see cref="GetDefaultSecretFilePath"/> per il percorso di default.
|
||||
/// </param>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static void WriteSecret(string secretFilePath)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(secretFilePath);
|
||||
|
||||
var tokenBytes = Encoding.UTF8.GetBytes(MachineGuardToken.ExpectedToken);
|
||||
var encryptedBytes = ProtectedData.Protect(tokenBytes, null, DataProtectionScope.LocalMachine);
|
||||
|
||||
var directory = Path.GetDirectoryName(secretFilePath);
|
||||
if (!string.IsNullOrEmpty(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
File.WriteAllBytes(secretFilePath, encryptedBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifica che il file secret nel percorso specificato decifrabile con successo prima del deployment.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static bool VerifySecret(string secretFilePath)
|
||||
{
|
||||
if (!File.Exists(secretFilePath))
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
var encryptedBytes = File.ReadAllBytes(secretFilePath);
|
||||
var decryptedBytes = ProtectedData.Unprotect(encryptedBytes, null, DataProtectionScope.LocalMachine);
|
||||
var decryptedToken = Encoding.UTF8.GetString(decryptedBytes);
|
||||
return string.Equals(decryptedToken, MachineGuardToken.ExpectedToken, StringComparison.Ordinal);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restituisce il percorso predefinito del file secret in base al sistema operativo corrente.
|
||||
/// </summary>
|
||||
public static string GetDefaultSecretFilePath()
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
var appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
|
||||
if (string.IsNullOrEmpty(appData))
|
||||
appData = @"C:\ProgramData";
|
||||
return Path.Combine(appData, "DataCoupler", "machine.guard");
|
||||
}
|
||||
|
||||
return "/etc/datacoupler/machine.guard";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user