iT邦幫忙

0

只要偵測到 Windows 有登入失敗事件(Event ID 4625),系統就會自動寄出警告信

  • 分享至 

  • xImage
  •  

在ISO27001的附錄控制項要求,系統日誌的保存、異常監控及審查一直是一個重要議題:

  • 8.15 存錄:紀錄活動、異常、錯誤及其他相關事件之日誌,應產生、儲存、保護及分析之。
  • 8.16 監視活動:應監視網路、系統及應用之異常行為,並採取適切措施,以評估潛在資訊安全事故

以下以Windows O.S.為例,在不引進第三方工具的方法上,如何簡單地自動化實施自動化監控機制:只要偵測到 Windows 有登入失敗事件(Event ID 4625),系統就會自動寄出警告信。


功能概念說明

  • Windows 安全性事件記錄(Security Event Log)中,事件 ID 4625 代表「使用者登入失敗」。

流程設計

  1. 建立 PowerShell 腳本:負責寄信。

  2. 在「事件檢視器」中建立觸發器:只要出現 4625,就自動執行此腳本。


第一步:建立 PowerShell 寄信腳本

  1. 打開記事本(Notepad),貼上以下內容:
# 檔名:C:\Scripts\SendFailedLoginAlert.ps1

$From = "syslog@company.com"
$To = "admin@company.com"
$Subject = "Windows Log Alert: Failed login detected on $(hostname)"
$SMTPServer = "smtp.company.com"

# 取得最新登入失敗事件的摘要內容
$Event = Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625} -MaxEvents 1
$Body = "A failed login attempt was detected.`n`n" +
        "Computer: $(hostname)`n" +
        "Time: $($Event.TimeCreated)`n" +
        "Account: $($Event.Properties[5].Value)`n" +
        "IP Address: $($Event.Properties[18].Value)`n"

# 寄送通知郵件
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer
  1. 儲存檔案到:
C:\Scripts\SendFailedLoginAlert.ps1

如果沒有這個資料夾,請先建立。


第二步:允許 PowerShell 腳本執行

  1. 以系統管理員身份開啟 PowerShell。
  2. 執行:
Set-ExecutionPolicy RemoteSigned
  1. 出現提示時輸入 Y

第三步:建立事件觸發的自動動作

  1. 按下 Win + R → 輸入
taskschd.msc

開啟「工作排程器 (Task Scheduler)」。

  1. 在右側選「建立工作 (Create Task...)」(不是基本工作)。

  2. 一般頁籤 (General):

    • 名稱:Failed Login Email Alert
    • 勾選「以最高權限執行 (Run with highest privileges)」
    • 選擇「使用者或群組」:選「SYSTEM」或管理者帳號
  3. 觸發條件 (Triggers):

    • 點「新增 (New...)」
    • 在「開始任務」選:
在事件發生時 (On an event)
  • 設定如下:
    • 記錄檔(Log): Security
    • 來源(Source): Microsoft Windows security auditing.
    • 事件 ID(Event ID): 4625
  • 按確定。
  1. 動作 (Actions):
    • 點「新增 (New...)」
    • 動作:啟動程式 (Start a program)
    • 程式或指令碼輸入:
powershell.exe
  • 新增引數 (Add arguments):
-ExecutionPolicy Bypass -File "C:\Scripts\SendFailedLoginAlert.ps1"
  • 按確定。
  1. 按「確定」完成。

第四步:測試是否成功

  1. 嘗試使用錯誤的帳號或密碼登入這台 Windows。
  2. 幾秒後檢查你的收件信箱,應該會收到主旨為:
Windows Log Alert: Failed login detected on COMPUTERNAME

的郵件。


如果你的 SMTP 伺服器要登入認證

可改成這個版本(支援 TLS 與登入):

$Credential = New-Object System.Management.Automation.PSCredential("syslog@company.com", (ConvertTo-SecureString "yourpassword" -AsPlainText -Force))

Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body `
  -SmtpServer "smtp.company.com" -UseSsl -Port 587 -Credential $Credential
  • 這裡的 "yourpassword" 可改成安全保存的方式(例如使用 Get-Credential 存成安全檔)。

第五步(可選):防止重複大量通知

若你擔心有惡意攻擊造成短時間大量登入失敗事件,可在腳本中加入時間檢查機制:

# 僅在過去5分鐘內未寄過信才寄送
$LogFile = "C:\Scripts\SendFailedLoginAlert.log"
$Now = Get-Date

if (Test-Path $LogFile) {
    $LastRun = Get-Content $LogFile | Get-Date
    if (($Now - $LastRun).TotalMinutes -lt 5) { exit }  # 5分鐘內不再寄信
}

$Now.ToString() | Out-File $LogFile

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言