[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,85 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Implementazione Windows-only della protezione machine-binding tramite DPAPI.
|
||||
/// Utilizza <see cref="ProtectedData"/> con scope <see cref="DataProtectionScope.LocalMachine"/>:
|
||||
/// il dato cifrato è legato fisicamente alla macchina e non può essere decifrato
|
||||
/// su un'altra macchina, anche con gli stessi account o credenziali.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal sealed class DpapiMachineGuard : IMachineGuard
|
||||
{
|
||||
private readonly MachineGuardOptions _options;
|
||||
private readonly ILogger<DpapiMachineGuard> _logger;
|
||||
|
||||
public DpapiMachineGuard(IOptions<MachineGuardOptions> options, ILogger<DpapiMachineGuard> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Verify()
|
||||
{
|
||||
var secretPath = ResolveSecretFilePath();
|
||||
|
||||
if (!File.Exists(secretPath))
|
||||
{
|
||||
_logger.LogError(
|
||||
"MachineGuard: file secret non trovato in '{Path}'. " +
|
||||
"Eseguire MachineGuardSetup.exe su questa macchina per inizializzare l'autorizzazione.",
|
||||
secretPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var encryptedBytes = File.ReadAllBytes(secretPath);
|
||||
var decryptedBytes = ProtectedData.Unprotect(encryptedBytes, null, DataProtectionScope.LocalMachine);
|
||||
var decryptedToken = Encoding.UTF8.GetString(decryptedBytes);
|
||||
|
||||
if (!string.Equals(decryptedToken, MachineGuardToken.ExpectedToken, StringComparison.Ordinal))
|
||||
{
|
||||
_logger.LogError(
|
||||
"MachineGuard: il token decifrato non corrisponde al token atteso. " +
|
||||
"Questa macchina non è autorizzata a eseguire questa applicazione.");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("MachineGuard: autorizzazione macchina verificata con successo.");
|
||||
return true;
|
||||
}
|
||||
catch (CryptographicException ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"MachineGuard: decifrazione fallita. " +
|
||||
"Il file secret potrebbe provenire da un'altra macchina o essere corrotto. " +
|
||||
"Percorso: '{Path}'", secretPath);
|
||||
return false;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"MachineGuard: errore imprevisto durante la verifica. Percorso: '{Path}'", secretPath);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private string ResolveSecretFilePath()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_options.SecretFilePath))
|
||||
return _options.SecretFilePath;
|
||||
|
||||
var appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
|
||||
if (string.IsNullOrEmpty(appData))
|
||||
appData = @"C:\ProgramData";
|
||||
|
||||
return Path.Combine(appData, "DataCoupler", "machine.guard");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Interfaccia per il meccanismo di protezione machine-binding tramite DPAPI.
|
||||
/// </summary>
|
||||
public interface IMachineGuard
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifica che l'applicazione sia in esecuzione su una macchina autorizzata.
|
||||
/// Restituisce <c>true</c> se l'autorizzazione è confermata, <c>false</c> altrimenti.
|
||||
/// </summary>
|
||||
bool Verify();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
|
||||
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,75 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Metodi di estensione per la registrazione di MachineGuard nel container DI.
|
||||
/// </summary>
|
||||
public static class MachineGuardExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registra il servizio <see cref="IMachineGuard"/> nel container DI.
|
||||
/// <para>
|
||||
/// Se <c>MachineGuard:Enabled</c> è <c>false</c> in appsettings, viene registrato
|
||||
/// un guard no-op che approva sempre la verifica (utile per sviluppo/CI).
|
||||
/// Su piattaforme non-Windows, DPAPI non è disponibile e il guard viene disabilitato automaticamente.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static IServiceCollection AddMachineGuard(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
services.Configure<MachineGuardOptions>(
|
||||
configuration.GetSection(MachineGuardOptions.SectionName));
|
||||
|
||||
services.AddSingleton<IMachineGuard>(sp =>
|
||||
{
|
||||
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
|
||||
var logger = loggerFactory.CreateLogger("MachineGuard.Startup");
|
||||
|
||||
#if DEBUG
|
||||
// In build Debug la protezione è sempre disabilitata — nessuna configurazione richiesta.
|
||||
logger.LogInformation(
|
||||
"MachineGuard: build DEBUG — protezione machine-binding disabilitata automaticamente.");
|
||||
return new NullMachineGuard();
|
||||
#else
|
||||
// In build Release la protezione è sempre attiva.
|
||||
// Può essere disabilitata esplicitamente via MachineGuard:Enabled = false
|
||||
// (utile per ambienti Linux/Docker o casi eccezionali).
|
||||
var options = sp.GetRequiredService<IOptions<MachineGuardOptions>>();
|
||||
|
||||
if (!options.Value.Enabled)
|
||||
{
|
||||
logger.LogWarning(
|
||||
"MachineGuard: protezione machine-binding DISABILITATA via configurazione. " +
|
||||
"Impostare MachineGuard:Enabled = true in produzione.");
|
||||
return new NullMachineGuard();
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
logger.LogWarning(
|
||||
"MachineGuard: DPAPI non è disponibile su piattaforme non-Windows. " +
|
||||
"La protezione machine-binding è bypassata automaticamente.");
|
||||
return new NullMachineGuard();
|
||||
}
|
||||
|
||||
return CreateWindowsGuard(sp, options);
|
||||
#endif
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
|
||||
private static IMachineGuard CreateWindowsGuard(
|
||||
IServiceProvider sp,
|
||||
IOptions<MachineGuardOptions> options)
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DpapiMachineGuard>>();
|
||||
return new DpapiMachineGuard(options, logger);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Opzioni di configurazione per MachineGuard.
|
||||
/// Configurabili tramite appsettings.json nella sezione "MachineGuard".
|
||||
/// </summary>
|
||||
public sealed class MachineGuardOptions
|
||||
{
|
||||
/// <summary>Nome della sezione in appsettings.json.</summary>
|
||||
public const string SectionName = "MachineGuard";
|
||||
|
||||
/// <summary>
|
||||
/// Imposta a <c>false</c> per disabilitare completamente la protezione machine-binding.
|
||||
/// Utile in ambienti di sviluppo o CI. Default: <c>true</c>.
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Percorso del file secret cifrato.
|
||||
/// Se vuoto, viene usato il percorso predefinito:
|
||||
/// Windows: %ProgramData%\DataCoupler\machine.guard
|
||||
/// Linux: /etc/datacoupler/machine.guard
|
||||
/// </summary>
|
||||
public string? SecretFilePath { get; set; }
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Contiene il token di machine-binding cablato nel codice.
|
||||
/// Modificare questo valore per ogni distribuzione autorizzata,
|
||||
/// quindi eseguire MachineGuardSetup per scrivere il secret su ogni macchina target.
|
||||
/// </summary>
|
||||
internal static class MachineGuardToken
|
||||
{
|
||||
/// <summary>
|
||||
/// Token atteso. Deve corrispondere esattamente al valore firmato durante il setup.
|
||||
/// </summary>
|
||||
internal const string ExpectedToken = "DC-F47AC10B-58CC-4372-A567-0E02B2C3D479";
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace MachineGuard;
|
||||
|
||||
/// <summary>
|
||||
/// Implementazione no-op di <see cref="IMachineGuard"/>.
|
||||
/// Usata quando la protezione è disabilitata via configurazione
|
||||
/// o quando il sistema operativo non supporta DPAPI (es. Linux, macOS).
|
||||
/// Restituisce sempre <c>true</c> senza eseguire alcuna verifica.
|
||||
/// </summary>
|
||||
internal sealed class NullMachineGuard : IMachineGuard
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool Verify() => true;
|
||||
}
|
||||
Reference in New Issue
Block a user