นำไปใช้กับ
Windows 10 Home and Pro, version 21H2 Windows 10 Enterprise and Education, version 21H2 Windows 10 IoT Enterprise, version 21H2 Windows 10 Home and Pro, version 22H2 Windows 10 Enterprise Multi-Session, version 22H2 Windows 10 Enterprise and Education, version 22H2 Windows 10 IoT Enterprise, version 22H2 Windows 11 Home and Pro, version 21H2 Windows 11 Enterprise Multi-Session, version 21H2 Windows 11 Enterprise and Education, version 21H2 Windows 11 IoT Enterprise, version 21H2 Azure Local, version 22H2 Windows Server 2022

บทนำ

Microsoft ได้พัฒนาสคริปต์ PowerShell ตัวอย่างที่สามารถช่วยคุณอัปเดต Windows Recovery Environment (WinRE) บนอุปกรณ์ที่ปรับใช้โดยอัตโนมัติเพื่อแก้ไขช่องโหว่ด้านความปลอดภัยใน CVE-2024-20666

ตัวอย่างสคริปต์ PowerShell

ตัวอย่างสคริปต์ PowerShell ได้รับการพัฒนาโดยทีมผลิตภัณฑ์ Microsoft เพื่อช่วยอัปเดตรูปภาพ WinRE โดยอัตโนมัติ เรียกใช้สคริปต์ด้วยข้อมูลประจําตัวของผู้ดูแลระบบใน PowerShell บนอุปกรณ์ที่ได้รับผลกระทบ

หมายเหตุ สคริปต์นี้มีไว้สําหรับ Windows รุ่นที่รองรับทั้งหมด

#################################################################################

#

# Copyright (c) Microsoft Corporation.

# Licensed under the MIT License.

#

# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

# SOFTWARE.

#

#################################################################################

Param (
    [Parameter(Mandatory=$true, HelpMessage="Path to target package")][string]$PackagePath,
    [Parameter(Mandatory=$false, HelpMessage="Directory to dism get-packages logs")][string]$LogDir
)

Set-StrictMode -Version Latest;

# Function to get WinRE path
function GetWinREPath {
    $WinRELocation = (reagentc /info | Select-String "Windows RE location")
    if ($WinRELocation) {
        return $WinRELocation.ToString().Split(':')[-1].Trim()
    } else {
        Write-Host "Failed to find WinRE path" -ForegroundColor Red
        exit 1
    }
}

# Function to get WinRE version
function GetWinREVersion {
    $filePath = "C:\mnt\Windows\System32\winpeshl.exe"
    $WinREVersion = (Get-Item $filePath).VersionInfo.FileVersionRaw.Revision
    return [int]$WinREVersion
}

# Function to get package version
function GetPackageVersion {
    $PackageInfo = dism /Online /Get-PackageInfo /PackagePath:"$PackagePath" | Select-String "Version :"
    if ($PackageInfo) {
        $VersionString = ($PackageInfo -split ':')[-1].Trim()
        if ($VersionString -match "\d+\.\d+\.\d+\.(\d+)") {
            return [int]$matches[1]  # Extract the last part (build number)
        } else {
            Write-Host "Failed to parse package version" -ForegroundColor Red
            exit 1
        }
    } else {
        Write-Host "Failed to retrieve package version" -ForegroundColor Red
        exit 1
    }
}

# Function to ensure log directory access
function EnsureLogDirAccess {
    param([string]$LogDir)
    if (Test-Path $LogDir) {
        try {
            $TestFile = "$LogDir\test_write_access.txt"
            Set-Content -Path $TestFile -Value "Test" -ErrorAction Stop
            Remove-Item -Path $TestFile -Force -ErrorAction Stop
            Write-Host "Log directory access verified." -ForegroundColor Green
        } catch {
            Write-Host "Insufficient permissions for log directory: $LogDir. Attempting to gain access..." -ForegroundColor Yellow
            try {
                $acl = Get-Acl $LogDir
                $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Everyone", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
                $acl.SetAccessRule($rule)
                Set-Acl -Path $LogDir -AclObject $acl
                Write-Host "Successfully gained access to log directory: $LogDir" -ForegroundColor Green
            } catch {
                Write-Host "Failed to gain access to log directory: $LogDir" -ForegroundColor Red
                exit 1
            }
        }
    } else {
        Write-Host "Log directory does not exist: $LogDir. Creating directory..." -ForegroundColor Yellow
        try {
            New-Item -ItemType Directory -Path $LogDir -Force | Out-Null
            Write-Host "Log directory created: $LogDir" -ForegroundColor Green
        } catch {
            Write-Host "Failed to create log directory: $LogDir" -ForegroundColor Red
            exit 1
        }
    }
}

# If file is other than .cab then exit
if ($PackagePath -notmatch "\.cab$") {
    Write-Host "Invalid package format. Only .cab files are supported." -ForegroundColor Red
    exit 1
}

# Main Execution
$WinREPath = GetWinREPath
$PackageVersion = GetPackageVersion

Write-Host "WinRE Path: $WinREPath" -ForegroundColor Cyan
Write-Host "Package Version: $PackageVersion" -ForegroundColor Cyan

if ($LogDir) {
    EnsureLogDirAccess -LogDir $LogDir
}

# Make dir C:\mnt if not exists
$TempDir = "C:\mnt"

# Get the read write permission for this directory
if (-not (Test-Path $TempDir)) {
    New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
}

# Mount WinRE image
dism /Mount-Image /ImageFile:"$WinREPath\winre.wim" /Index:1 /MountDir:"$TempDir"

$WinREVersion = GetWinREVersion
Write-Host "WinRE Version: $WinREVersion" -ForegroundColor Cyan

if ($PackageVersion -gt $WinREVersion)
{
    Write-Host "Applying patch..." -ForegroundColor Yellow
    if ($LogDir) {
        dism /Image:"$TempDir" /Add-Package /PackagePath:"$PackagePath" /LogPath:"$LogDir\PatchWinRE.log"
    } else {
        dism /Image:"$TempDir" /Add-Package /PackagePath:"$PackagePath"
    }
    Write-Host "Committing changes..." -ForegroundColor Yellow
    dism /Unmount-Image /MountDir:"$TempDir" /Commit
    Write-Host "Patch applied and committed successfully!" -ForegroundColor Green
}
else{
    Write-Host "Already have a greater or equal version, no update needed." -ForegroundColor Green
    dism /Unmount-Image /MountDir:"$TempDir" /Discard
}

# Disable WinRE and re-enable it to let new WinRE be trusted by BitLocker
Write-Host "Disable WinRE"
reagentc /disable
Write-Host "Re-enable WinRE"
reagentc /enable
reagentc /info

# Cleanup
Remove-Item -Path $TempDir -Force -Recurse

## Usage
# .\Patch_WinRE_Generic.ps1 -PackagePath <path_to_cab_file>
# .\Patch_WinRE_Generic.ps1 -PackagePath <path_to_cab_file> -LogDir <path_to_custom_log_folder>

ข้อมูลเพิ่มเติม

เมื่ออุปกรณ์เริ่มต้นระบบใน Windows เวอร์ชันที่ใช้งานที่ติดตั้งบนอุปกรณ์ สคริปต์จะดําเนินการตามขั้นตอนต่อไปนี้:

  1. ติดตั้งอิมเมจ WinRE ที่มีอยู่ (WINRE) WIM)

  2. อัปเดตอิมเมจ WinRE ด้วยแพคเกจการอัปเดตแบบไดนามิก (การอัปเดตความเข้ากันได้) ของ Safe OS ที่ระบุที่พร้อมใช้งานจาก Windows Update Catalog เราขอแนะนําให้คุณใช้การอัปเดตแบบไดนามิกของระบบปฏิบัติการที่ปลอดภัยรุ่นล่าสุดที่พร้อมใช้งานสําหรับ Windows เวอร์ชันที่ติดตั้งบนอุปกรณ์

  3. ยกเลิกการเมาต์อิมเมจ WinRE

  4. หากมีตัวป้องกัน BitLocker TPM ให้กําหนดค่า WinRE สําหรับบริการ BitLocker ใหม่

    สําคัญ ขั้นตอนนี้ไม่ปรากฏในสคริปต์ของบริษัทอื่นส่วนใหญ่สําหรับการนําการปรับปรุงไปใช้กับอิมเมจ WinRE

การใช้งาน

พารามิเตอร์ต่อไปนี้สามารถส่งผ่านไปยังสคริปต์ได้:

พารามิเตอร์

คำอธิบาย

LogDir

>เพิ่มเติมของ< ระบุพื้นที่ร่างที่ใช้ในการแก้ไข WinRE หากไม่ได้ระบุสคริปต์จะใช้โฟลเดอร์ชั่วคราวเริ่มต้นสําหรับอุปกรณ์

packagePath

>ที่จําเป็น< ระบุพาธและชื่อของแพคเกจการอัปเดต Safe OS Dynamic เฉพาะเวอร์ชันระบบปฏิบัติการและสถาปัตยกรรมของตัวประมวลผลเพื่อใช้ในการอัปเดตอิมเมจ WinREโน้ต ซึ่งอาจเป็นเส้นทางภายในเครื่องหรือเส้นทาง UNC ระยะไกล แต่ ต้องดาวน์โหลดการอัปเดตแบบไดนามิกของระบบปฏิบัติการที่ปลอดภัย และพร้อมใช้งานสําหรับสคริปต์

ตัวอย่าง: 

.\Patch_WinRE_Generic.ps1 -packagePath "\\server\share\windows10.0-kb5021043-x64_efa19d2d431c5e782a59daaf2d.cab

การอ้างอิง

วิธีการเขียนและเรียกใช้สคริปต์ใน ISE Windows PowerShell 

ต้องการความช่วยเหลือเพิ่มเติมหรือไม่

ต้องการตัวเลือกเพิ่มเติมหรือไม่

สํารวจสิทธิประโยชน์ของการสมัครใช้งาน เรียกดูหลักสูตรการฝึกอบรม เรียนรู้วิธีการรักษาความปลอดภัยอุปกรณ์ของคุณ และอื่นๆ