Powershell securite

De wiki.nexiat.fr
Aller à la navigation Aller à la recherche
Fiche express
Domaine Sécurité et remoting PowerShell
Fournisseur WSMan:\
Cmdlets ConvertTo-SecureString, Get-Credential
Voir aussi WSMan · Get-credential

Authentification distante

Par défaut PowerShell utilise Kerberos pour l'authentification distante des machines. Kerberos requiert un Active Directory. Pour s'en affranchir (hors domaine), on renseigne les trusted hosts du client WinRM :

Set-Item wsman:\localhost\client\trustedhosts * -Force
Set-Item wsman:\localhost\client\trustedhosts 10.0.0.* -Force -Concatenate
dir wsman:\localhost\client

Désactiver un listener :

Remove-Item wsman:\localhost\listener\* -Recurse -WhatIf

# Désactiver uniquement les listeners sur le port 5985
dir WSMan:\localhost\Listener -Recurse |
    ForEach-Object { $_.PSPath } |
    Where-Object { (Get-Item "$_\Port").Value -eq 5985 } |
    Remove-Item -WhatIf

Crypter une chaîne (chiffre de César)

Exemple pédagogique de chiffrement par décalage :

Param([string]$texte, [int]$cle)

$message_origine = $texte
$alphabet_MAJ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$alphabet_MIN = 'abcdefghijklmnopqrstuvwxyz'
$message_modif = ''

for ($i = 0; $i -lt $message_origine.Length; $i++) {
    $trouve = 0
    for ($j = 0; $j -lt $alphabet_MAJ.Length; $j++) {
        $tmp = $cle
        while (($j + $tmp) -ge $alphabet_MAJ.Length) { $tmp -= 26 }

        if ($message_origine[$i] -ceq $alphabet_MAJ[$j]) {
            $message_modif += $alphabet_MAJ[$j + $tmp]; $trouve = 1
        }
        elseif ($message_origine[$i] -ceq $alphabet_MIN[$j]) {
            $message_modif += $alphabet_MIN[$j + $tmp]; $trouve = 1
        }
    }
    if (!$trouve) { $message_modif += $message_origine[$i] }
}
Write-Host "`nMessage origine : $message_origine"
Write-Host "`n`nMessage codé : $message_modif `n"
.\cesar.ps1 -texte "powershell c'est facile" -cle 14

SecureString

$secure_string_pwd = ConvertTo-SecureString "Texte confidentiel à protéger" -AsPlainText -Force
ConvertFrom-SecureString $secure_string_pwd > c:\chaine_c1.txt
Get-Content c:\chaine_c1.txt

Utiliser sa propre clé :

$cle = (6,10,19,85,4,7,89,5,13,20,56,58,47,92,15,35,96,48,41,65,41,44)
ConvertFrom-SecureString -SecureString $secure_string_pwd -Key $cle > c:\temp\chaine_c2.txt
Get-Content c:\temp\chaine_c2.txt

Déchiffrer :

$chaine_chiffree  = Get-Content c:\temp\chaine_c2.txt
$chaine_originale = ConvertTo-SecureString -Key $cle -String $chaine_chiffree
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($chaine_originale)
[System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)

Credential

Voir Get-credential pour obtenir un objet PSCredential.

Voir aussi