ค้นหาเว็บไซต์

วิธีสร้างรหัสผ่านแบบสุ่มที่ปลอดภัยใน PowerShell


มีสคริปต์และ cmdlets มากมายสำหรับสร้างรหัสผ่านใน PowerShell น่าเสียดายที่ส่วนใหญ่ไม่ปลอดภัย และคุณไม่ควรใช้มัน ลองดูที่พวกเขาและดูว่าอันไหนที่คุณควรใช้ ในบทความนี้ คุณจะได้เรียนรู้วิธีสร้างรหัสผ่านแบบสุ่มที่ปลอดภัยใน PowerShell

ก่อนที่คุณจะเริ่ม

มีหลายวิธีในการสร้างรหัสผ่านแบบสุ่มใน PowerShell อย่างไรก็ตาม ส่วนใหญ่ไม่ทราบว่าสคริปต์หรือคำสั่งส่วนใหญ่ไม่ได้สร้างรหัสผ่านที่ปลอดภัย และคุณไม่ควรใช้รหัสผ่านเหล่านั้น!

มาดู cmdlets และคลาสตัวสร้างรหัสผ่านแบบสุ่มที่ได้รับความนิยมมากที่สุด:

  1. Get-Random - Get-Random cmdlet ใช้งานได้บน Windows PowerShell 5.1 เท่านั้น และที่สำคัญกว่านั้นคือไม่ได้รับประกันการสุ่มที่ปลอดภัยด้วยการเข้ารหัส กำจัดมัน!
  2. Get-SecureRandom - Get-SecureRandom cmdlet ใช้งานได้กับ PowerShell 7.x เท่านั้น เป็น cmdlet ที่ยอดเยี่ยม แม้ว่าจะเข้ากันไม่ได้กับผู้ใช้ที่ต้องการเรียกใช้บน Windows PowerShell 5.1
  3. RNGCryptoServiceProvider - คลาสนามธรรม RNGCryptoServiceProvider ล้าสมัย ดังนั้นคุณไม่ควรใช้มันทุกที่ กำจัดมัน!
  4. RandomNumberGenerator - คลาสนามธรรม RandomNumberGenerator มีความปลอดภัย ข้อดีคือใช้งานได้กับทั้ง Windows PowerShell 5.1 และ PowerShell 7.x

ดังนั้น ฉันขอแนะนำให้ใช้ RandomNumberGenerator เพื่อสร้างรหัสผ่านแบบสุ่มที่ปลอดภัยใน PowerShell

สร้างสคริปต์ Powershell รหัสผ่านที่ปลอดภัยแบบสุ่ม

ดาวน์โหลดสคริปต์ Get-RandomPassword.ps1 PowerShell และวางไว้ในโฟลเดอร์ C:\scripts

ตรวจสอบให้แน่ใจว่าไฟล์ไม่ได้ถูกบล็อกเพื่อป้องกันข้อผิดพลาดเมื่อเรียกใช้สคริปต์ อ่านเพิ่มเติมในบทความไม่มีข้อผิดพลาดในการเซ็นชื่อแบบดิจิทัลเมื่อเรียกใช้สคริปต์ PowerShell

อีกทางเลือกหนึ่งคือการคัดลอกและวางโค้ดด้านล่างลงใน Notepad ตั้งชื่อให้ว่า Get-RandomPassword.ps1 และวางไว้ในโฟลเดอร์ C:\scripts

<#
    .SYNOPSIS
    Get-RandomPassword.ps1

    .DESCRIPTION
   This script generates passwords based on user-defined criteria for length, character
   sets, and quantity. It ensures each password contains at least one character from
   each specified set.

   .LINK
    www.alitajran.com/generate-secure-random-passwords-powershell/

    .NOTES
    Written by: ALI TAJRAN
    Website:    alitajran.com
    X:          x.com/alitajran
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 10/12/2024 - Initial version
#>

param (
    # The length of each password which should be created.
    [Parameter(Mandatory = $true)]
    [ValidateRange(8, 255)]
    [Int32]$Length,

    # The number of passwords to generate.
    [Parameter(Mandatory = $false)]
    [Int32]$Count = 1,

    # The character sets the password may contain.
    # A password will contain at least one of each of the characters.
    [String[]]$CharacterSet = @(
        'abcdefghijklmnopqrstuvwxyz',
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        '0123456789',
        '!$%&^.#;'
    )
)

# Generate a cryptographically secure seed
$bytes = [Byte[]]::new(4)
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$seed = [System.BitConverter]::ToInt32($bytes, 0)
$rnd = [Random]::new($seed)

# Combine all character sets for random selection
$allCharacterSets = [String]::Concat($CharacterSet)

try {
    for ($i = 0; $i -lt $Count; $i++) {
        $password = [Char[]]::new($Length)
        $index = 0

        # Ensure at least one character from each set
        foreach ($set in $CharacterSet) {
            $password[$index++] = $set[$rnd.Next($set.Length)]
        }

        # Fill remaining characters randomly from all sets
        for ($j = $index; $j -lt $Length; $j++) {
            $password[$index++] = $allCharacterSets[$rnd.Next($allCharacterSets.Length)]
        }

        # Fisher-Yates shuffle for randomness
        for ($j = $Length - 1; $j -gt 0; $j--) {
            $m = $rnd.Next($j + 1)
            $t = $password[$j]
            $password[$j] = $password[$m]
            $password[$m] = $t
        }

        # Output each password
            Write-Output ([String]::new($password))
    }
}
catch {
    Write-Host "Error: $_" -ForegroundColor Red
}

ตอนนี้ทุกอย่างเข้าที่แล้ว มาดูตัวอย่าง PowerShell สำหรับการสร้างรหัสผ่านที่ปลอดภัยกันดีกว่า

สร้างตัวอย่างสคริปต์ PowerShell รหัสผ่านแบบสุ่มที่ปลอดภัย

สร้างรหัสผ่านแบบสุ่มที่ปลอดภัยโดยมีความยาวอักขระ 20 ตัว

C:\scripts\.\Get-RandomPassword.ps1 -Length "20"

สร้างรหัสผ่านแบบสุ่มที่ปลอดภัย 100 รหัสโดยมีความยาวอักขระ 20 ตัว

C:\scripts\.\Get-RandomPassword.ps1 -Length "20" -Count "100"

สร้างรหัสผ่านแบบสุ่มที่ปลอดภัยโดยต้องมีอักขระอย่างน้อย 1 ตัวต่อชุด

C:\scripts\.\Get-RandomPassword.ps1 -Length 20 -CharacterSet "ABCDEF", "1234567890", "!@#$%^&*()"

หากคุณมีสคริปต์ PowerShell ที่คุณต้องการสร้างรหัสผ่านแบบสุ่ม ให้ใช้สคริปต์นี้เป็นฟังก์ชัน นี่คือสองบทความที่ทำอย่างนั้น

  • บังคับให้เปลี่ยนรหัสผ่านสำหรับผู้ใช้ทั้งหมดใน Microsoft 365
  • สร้างผู้ใช้ AD จำนวนมากด้วยรหัสผ่านแบบสุ่ม

บทสรุป

คุณได้เรียนรู้วิธีสร้างรหัสผ่านแบบสุ่มที่ปลอดภัยใน PowerShell ใช้ Get-SecureRandom cmdlet ใน PowerShell หากคุณแน่ใจว่าต้องการใช้เฉพาะ PowerShell 7 เท่านั้น หากคุณต้องการสร้างรหัสผ่านใน PowerShell เวอร์ชันทั้งหมด วิธีที่ดีที่สุดคือใช้ RandomNumberGenerator คลาสนามธรรม นั่นคือสิ่งที่ฉันชอบใช้และแนะนำ

คุณสนุกกับบทความนี้หรือไม่? คุณอาจต้องการวิธีรักษาความปลอดภัยรหัสผ่าน Active Directory จากการละเมิด อย่าลืมติดตามเราและแบ่งปันบทความนี้