<# --------------------------------------------------------------------------- Module......: Security.psm1 Projekt.....: Veeam Enterprise Installer Version.....: 2.0.0 Beschreibung Sicherheitsfunktionen für Installation und Betrieb. Funktionen: • Administratorprüfung • Benutzerprüfung • Gruppenprüfung • Berechtigungen • Credentials • Zertifikate ---------------------------------------------------------------------------#> Set-StrictMode -Version Latest #region Variablen $Script:SecurityVersion = "2.0.0" #endregion ############################################################ # Get-CurrentIdentity ############################################################ function Get-CurrentIdentity { <# .SYNOPSIS Liefert aktuellen Benutzer. #> [System.Security.Principal.WindowsIdentity]::GetCurrent() } ############################################################ # Get-CurrentUserName ############################################################ function Get-CurrentUserName { $Identity = Get-CurrentIdentity return $Identity.Name } ############################################################ # Test-IsAdministrator ############################################################ function Test-IsAdministrator { <# .SYNOPSIS Prüft Administratorrechte. #> $Identity = Get-CurrentIdentity $Principal = New-Object ` System.Security.Principal.WindowsPrincipal( $Identity ) return ( $Principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator ) ) } ############################################################ # Test-UserExists ############################################################ function Test-UserExists { <# .SYNOPSIS Prüft lokales Konto. #> param( [Parameter(Mandatory)] [string]$UserName ) try { Get-LocalUser ` -Name $UserName ` -ErrorAction Stop | Out-Null return $true } catch { return $false } } ############################################################ # Get-LocalGroupMembers ############################################################ function Get-LocalGroupMembers { param( [Parameter(Mandatory)] [string]$GroupName ) try { Get-LocalGroupMember ` -Group $GroupName } catch { return $null } } #Beginn PART 2 ############################################################ # Test-LocalGroupExists ############################################################ function Test-LocalGroupExists { <# .SYNOPSIS Prüft lokale Gruppe. #> param( [Parameter(Mandatory)] [string]$GroupName ) try { Get-LocalGroup ` -Name $GroupName ` -ErrorAction Stop | Out-Null return $true } catch { return $false } } ############################################################ # Add-UserToLocalGroupSafe ############################################################ function Add-UserToLocalGroupSafe { <# .SYNOPSIS Fügt Benutzer zu Gruppe hinzu. #> param( [Parameter(Mandatory)] [string]$User, [Parameter(Mandatory)] [string]$Group ) if(!(Test-LocalGroupExists $Group)) { throw ` "Lokale Gruppe nicht gefunden: $Group" } $Existing = Get-LocalGroupMembers ` -GroupName $Group $ExistingNames = @($Existing | ForEach-Object { $_.Name }) if($ExistingNames -notcontains $User) { Add-LocalGroupMember ` -Group $Group ` -Member $User } } ############################################################ # Remove-UserFromLocalGroupSafe ############################################################ function Remove-UserFromLocalGroupSafe { param( [Parameter(Mandatory)] [string]$User, [Parameter(Mandatory)] [string]$Group ) try { Remove-LocalGroupMember ` -Group $Group ` -Member $User ` -ErrorAction Stop } catch { # Benutzer nicht vorhanden } } ############################################################ # Get-NTFSPermission ############################################################ function Get-NTFSPermission { <# .SYNOPSIS Liest NTFS Berechtigungen. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { throw ` "Pfad nicht gefunden: $Path" } Get-Acl ` -Path $Path } ############################################################ # Set-NTFSPermission ############################################################ function Set-NTFSPermission { <# .SYNOPSIS Setzt NTFS Berechtigung. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account, [ValidateSet( "Read", "Modify", "FullControl" )] [string]$Permission = "Modify" ) if(!(Test-Path $Path)) { throw ` "Pfad nicht gefunden: $Path" } $ACL = Get-Acl ` -Path $Path $Rule = New-Object ` System.Security.AccessControl.FileSystemAccessRule( $Account, $Permission, "ContainerInherit,ObjectInherit", "None", "Allow" ) $ACL.AddAccessRule($Rule) Set-Acl ` -Path $Path ` -AclObject $ACL } #Beginn PART 3 ############################################################ # New-SecureCredential ############################################################ function New-SecureCredential { <# .SYNOPSIS Erstellt ein Credential Objekt. #> param( [Parameter(Mandatory)] [string]$UserName, [Parameter(Mandatory)] [SecureString]$Password ) New-Object ` System.Management.Automation.PSCredential( $UserName, $Password ) } ############################################################ # ConvertTo-SecurePassword ############################################################ function ConvertTo-SecurePassword { <# .SYNOPSIS Wandelt Klartext in SecureString um. #> param( [Parameter(Mandatory)] [string]$Password ) ConvertTo-SecureString ` -String $Password ` -AsPlainText ` -Force } ############################################################ # Export-SecureCredential ############################################################ function Export-SecureCredential { <# .SYNOPSIS Speichert Credential verschlüsselt. #> param( [Parameter(Mandatory)] [PSCredential]$Credential, [Parameter(Mandatory)] [string]$Path ) $ParentPath = Split-Path ` -Path $Path ` -Parent if( ![string]::IsNullOrWhiteSpace($ParentPath) -and !(Test-Path $ParentPath) ) { New-Item ` -Path $ParentPath ` -ItemType Directory ` -Force | Out-Null } $Credential | Export-Clixml ` -Path $Path return $Path } ############################################################ # Import-SecureCredential ############################################################ function Import-SecureCredential { <# .SYNOPSIS Lädt Credential. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { throw ` "Credential Datei nicht gefunden: $Path" } $Credential = Import-Clixml ` -Path $Path if($Credential -isnot [System.Management.Automation.PSCredential]) { throw "Die Datei enthält kein gültiges PowerShell-Credential: $Path" } return $Credential } ############################################################ # Test-DomainAccount ############################################################ function Test-DomainAccount { <# .SYNOPSIS Prüft Domänenkonto. #> param( [Parameter(Mandatory)] [string]$Account ) try { $User = New-Object ` System.Security.Principal.NTAccount( $Account ) $SID = $User.Translate( [System.Security.Principal.SecurityIdentifier] ) return $true } catch { return $false } } ############################################################ # Test-ServiceAccountFormat ############################################################ function Test-ServiceAccountFormat { <# .SYNOPSIS Prüft Kontoformat. #> param( [Parameter(Mandatory)] [string]$Account ) return ( $Account -match "\\" ) } #Beginn PART 4 ############################################################ # Get-Certificate ############################################################ function Get-Certificate { <# .SYNOPSIS Sucht Zertifikate. #> param( [string]$Thumbprint, [string]$Store = "Cert:\LocalMachine\My" ) if($Thumbprint) { return Get-ChildItem ` -Path $Store | Where-Object { $_.Thumbprint ` -eq $Thumbprint } } Get-ChildItem ` -Path $Store } ############################################################ # Find-CertificateBySubject ############################################################ function Find-CertificateBySubject { <# .SYNOPSIS Sucht Zertifikat nach Name. #> param( [Parameter(Mandatory)] [string]$Subject, [string]$Store = "Cert:\LocalMachine\My" ) Get-ChildItem ` -Path $Store | Where-Object { $_.Subject ` -like "*$Subject*" } } ############################################################ # Test-CertificateValid ############################################################ function Test-CertificateValid { <# .SYNOPSIS Prüft Zertifikat. #> param( [Parameter(Mandatory)] [System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate ) $Now = Get-Date return ( $Certificate.NotBefore -lt $Now -and $Certificate.NotAfter -gt $Now ) } ############################################################ # Get-CertificateInformation ############################################################ function Get-CertificateInformation { param( [Parameter(Mandatory)] [System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate ) [PSCustomObject]@{ Subject = $Certificate.Subject Issuer = $Certificate.Issuer Thumbprint = $Certificate.Thumbprint NotBefore = $Certificate.NotBefore NotAfter = $Certificate.NotAfter DaysRemaining = ( $Certificate.NotAfter - (Get-Date) ).Days } } ############################################################ # Import-CertificateSafe ############################################################ function Import-CertificateSafe { <# .SYNOPSIS Importiert Zertifikat. #> param( [Parameter(Mandatory)] [string]$Path, [string]$Store = "Cert:\LocalMachine\My" ) if(!(Test-Path -Path $Path -PathType Leaf)) { throw ` "Zertifikat nicht gefunden: $Path" } Import-Certificate ` -FilePath $Path ` -CertStoreLocation $Store } ############################################################ # Export-CertificateSafe ############################################################ function Export-CertificateSafe { <# .SYNOPSIS Exportiert Zertifikat. #> param( [Parameter(Mandatory)] [System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate, [Parameter(Mandatory)] [string]$Path ) $ParentPath = Split-Path ` -Path $Path ` -Parent if( ![string]::IsNullOrWhiteSpace($ParentPath) -and !(Test-Path $ParentPath) ) { New-Item ` -Path $ParentPath ` -ItemType Directory ` -Force | Out-Null } Export-Certificate ` -Cert $Certificate ` -FilePath $Path ` -Force } #Beginn PART 5 ############################################################ # Test-DirectoryPermission ############################################################ function Test-DirectoryPermission { <# .SYNOPSIS Prüft Verzeichnisrechte. #> param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$Account ) if(!(Test-Path $Path)) { return $false } $ACL = Get-Acl ` -Path $Path foreach($Entry in $ACL.Access) { if( $Entry.IdentityReference ` -eq $Account ) { return $true } } return $false } ############################################################ # Test-AccountPermission ############################################################ function Test-AccountPermission { <# .SYNOPSIS Prüft mehrere Konten. #> param( [string[]]$Accounts ) $Result = @() foreach($Account in $Accounts) { $Result += [PSCustomObject]@{ Account = $Account Exists = Test-DomainAccount ` -Account $Account } } return $Result } ############################################################ # Get-SecuritySummary ############################################################ function Get-SecuritySummary { <# .SYNOPSIS Erstellt Sicherheitsübersicht. #> [PSCustomObject]@{ User = Get-CurrentUserName Administrator = Test-IsAdministrator Computer = $env:COMPUTERNAME Time = Get-Date Module = "Security.psm1" } } ############################################################ # Protect-PasswordFile ############################################################ function Protect-PasswordFile { <# .SYNOPSIS Schützt Passwortdatei über NTFS. #> param( [Parameter(Mandatory)] [string]$Path ) if(!(Test-Path $Path)) { return } $ACL = Get-Acl ` $Path $CurrentUser = Get-CurrentUserName $Rule = New-Object ` System.Security.AccessControl.FileSystemAccessRule( $CurrentUser, "FullControl", "Allow" ) $ACL.SetAccessRule($Rule) $ACL.SetAccessRuleProtection( $true, $false ) Set-Acl ` -Path $Path ` -AclObject $ACL } ############################################################ # Test-SecurityBaseline ############################################################ function Test-SecurityBaseline { <# .SYNOPSIS Grundlegende Sicherheitsprüfung. #> [PSCustomObject]@{ Administrator = Test-IsAdministrator Computer = $env:COMPUTERNAME User = Get-CurrentUserName Time = Get-Date } } #Beginn PART 6 ############################################################ # Get-SecurityInformation ############################################################ function Get-SecurityInformation { <# .SYNOPSIS Liefert Modulinformationen. #> [PSCustomObject]@{ Module = "Security.psm1" Version = $Script:SecurityVersion Loaded = $true Computer = $env:COMPUTERNAME User = Get-CurrentUserName } } ############################################################ # Test-SecurityModule ############################################################ function Test-SecurityModule { <# .SYNOPSIS Prüft ob Modul funktioniert. #> [PSCustomObject]@{ Module = "Security" Status = "OK" Administrator = Test-IsAdministrator Timestamp = Get-Date } } ############################################################ # Export Module Functions ############################################################ Export-ModuleMember ` -Function ` Get-CurrentIdentity, Get-CurrentUserName, Test-IsAdministrator, Test-UserExists, Get-LocalGroupMembers, Test-LocalGroupExists, Add-UserToLocalGroupSafe, Remove-UserFromLocalGroupSafe, Get-NTFSPermission, Set-NTFSPermission, New-SecureCredential, ConvertTo-SecurePassword, Export-SecureCredential, Import-SecureCredential, Test-DomainAccount, Test-ServiceAccountFormat, Get-Certificate, Find-CertificateBySubject, Test-CertificateValid, Get-CertificateInformation, Import-CertificateSafe, Export-CertificateSafe, Test-DirectoryPermission, Test-AccountPermission, Get-SecuritySummary, Protect-PasswordFile, Test-SecurityBaseline, Get-SecurityInformation, Test-SecurityModule ############################################################ # Ende Security.psm1 ############################################################