Fix auditpol for RU subcategory and safer stderr handling
This commit is contained in:
+68
-9
@@ -178,18 +178,77 @@ Write-Log "Скрипт запущен с правами администрат
|
||||
|
||||
function Enable-SecurityAudit {
|
||||
Write-Log "Проверка настроек аудита..."
|
||||
$auditPolicy = auditpol /get /subcategory:"Logon" 2>$null
|
||||
if ($auditPolicy -like "*Success*Failure*") {
|
||||
Write-Log "Аудит входа уже настроен"
|
||||
|
||||
# Важно: auditpol пишет часть сообщений в stderr. Если перенаправлять stderr в $null,
|
||||
# PowerShell может превратить это в завершающую ошибку (и скрипт упадёт из-за $ErrorActionPreference=Stop).
|
||||
function Invoke-AuditPol {
|
||||
param([Parameter(Mandatory = $true)][string]$Arguments)
|
||||
$cmd = "auditpol $Arguments 2>&1"
|
||||
$text = cmd.exe /c $cmd
|
||||
$code = $LASTEXITCODE
|
||||
return [pscustomobject]@{
|
||||
ExitCode = $code
|
||||
Text = ($text | Out-String).Trim()
|
||||
}
|
||||
}
|
||||
|
||||
function Test-LogonAuditEnabled {
|
||||
# На русской Windows подкатегория называется "Вход в систему", а не "Logon".
|
||||
# В выводе auditpol обычно встречаются слова "успех" и "отказ" (или Success/Failure).
|
||||
$candidates = @(
|
||||
'/get /subcategory:"Вход в систему"',
|
||||
'/get /subcategory:"Logon"'
|
||||
)
|
||||
|
||||
foreach ($args in $candidates) {
|
||||
$r = Invoke-AuditPol -Arguments $args
|
||||
if ($r.ExitCode -ne 0) {
|
||||
Write-Log ("Не удалось прочитать auditpol (код {0}) для: {1}. Вывод:`n{2}" -f $r.ExitCode, $args.Trim(), $r.Text)
|
||||
continue
|
||||
}
|
||||
|
||||
$t = $r.Text
|
||||
if (($t -match '(?i)Success') -and ($t -match '(?i)Failure')) { return $true }
|
||||
if (($t -match '(?i)успех') -and ($t -match '(?i)отказ')) { return $true }
|
||||
}
|
||||
|
||||
return $false
|
||||
}
|
||||
|
||||
if (Test-LogonAuditEnabled) {
|
||||
Write-Log "Аудит входа уже настроен (Success+Failure)"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Log "Аудит входа не настроен полностью. Пытаюсь включить..."
|
||||
|
||||
# Порядок попыток: от самого "каноничного" к более совместимому.
|
||||
$attempts = @(
|
||||
# RU Windows (как в вашем выводе auditpol /list /subcategory:*)
|
||||
'/set /subcategory:"Вход в систему" /success:enable /failure:enable',
|
||||
'/set /category:"Вход/выход" /success:enable /failure:enable',
|
||||
# EN Windows
|
||||
'/set /subcategory:"Logon" /success:enable /failure:enable',
|
||||
'/set /category:"Logon/Logoff" /success:enable /failure:enable',
|
||||
# GUID категории (как было у вас изначально)
|
||||
'/set /category:"{69979849-797A-11D9-BED3-505054503030}" /success:enable /failure:enable'
|
||||
)
|
||||
|
||||
foreach ($a in $attempts) {
|
||||
$r = Invoke-AuditPol -Arguments $a
|
||||
if ($r.ExitCode -eq 0) {
|
||||
Write-Log ("auditpol OK: {0}" -f $a.Trim())
|
||||
} else {
|
||||
Write-Log "Включение аудита входа..."
|
||||
try {
|
||||
auditpol /set /category:"{69979849-797A-11D9-BED3-505054503030}" /success:enable /failure:enable
|
||||
Write-Log "Аудит входа успешно включен"
|
||||
} catch {
|
||||
Write-Log "Ошибка включения аудита: $($_.Exception.Message)"
|
||||
Write-Log ("auditpol FAIL (код {0}): {1}`n{2}" -f $r.ExitCode, $a.Trim(), $r.Text)
|
||||
}
|
||||
|
||||
if (Test-LogonAuditEnabled) {
|
||||
Write-Log "Аудит входа успешно включен (Success+Failure)"
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Write-Log "ВНИМАНИЕ: не удалось автоматически включить аудит входа через auditpol. Скрипт продолжит работу, но часть событий может отсутствовать. Проверьте политику аудита вручную (локальная/доменная GPO)."
|
||||
}
|
||||
|
||||
function Send-Heartbeat {
|
||||
|
||||
Reference in New Issue
Block a user