07 - Hardening Logging SOC

Algunas fuentes críticas no llegan a Splunk aunque el UF las escuche, porque el SO no las está generando. Hay que activarlas en registros / audit policy de Windows antes de reiniciar el UF.

1. PowerShell: Script Block Logging (EID 4104) + Module Logging (EID 4103)

Imprescindible para detectar scripts ofuscados/decimalización y AMSI-bypass.

Vía registry (aplica con reinicio del host o nuevo proceso PowerShell)

# ScriptBlock Logging (4104): captura el código completo del script interpretado
$psP = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell'
New-Item -Path "$psP\ScriptBlockLogging" -Force | Out-Null
Set-ItemProperty -Path "$psP\ScriptBlockLogging" -Name 'EnableScriptBlockLogging' -Value 1 -Type DWord
 
# Module Logging (4103): captura pipelines y comandos de módulos listados
New-Item -Path "$psP\ModuleLogging" -Force | Out-Null
Set-ItemProperty -Path "$psP\ModuleLogging" -Name 'EnableModuleLogging' -Value 1 -Type DWord
Set-ItemProperty -Path "$psP\ModuleLogging" -Name 'ModuleNames' -Value '*' -Type MultiString

Esto produce el log Microsoft-Windows-PowerShell/Operational, con EIDs:

  • 4104 Script Block (长长的 script code)
  • 4103 Module
  • 400 / 800 Engine lifecycle
  • 500 Warning (por ejemplo script bloqueado)

Vía GPO (entornos domain)

Computer Configuration → Policies → Administrative Templates → Windows Components → Windows PowerShell → Turn on PowerShell Script Block Logging = Enabled.

2. Process Creation con command line (4688 con CommandLine)

Por defecto Windows no incluye la línea de comandos. Activarlo:

$apc = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\AuditProcessCreation'
New-Item -Path $apc -Force | Out-Null
Set-ItemProperty -Path $apc -Name 'IncludeCommandLine' -Value 1 -Type DWord
 
# Y activar la subcategoría Process Creation en auditpol
auditpol /set /subcategory:"Process Creation" /success:enable /failure:enable

Tras esto, EID 4688 (win_security) incluirá el campo CommandLine (en XML renderizado se ve como Event_Data\CommandLine).

Requisito: en Windows 11 Education está disponible sin group policy extra.

3. Privilege Use y Logon (auditpol mínimo SOC)

Recomendado:

auditpol /set /subcategory:"Logon" /success:enable /failure:enable
auditpol /set /subcategory:"Logoff" /success:enable
auditpol /set /subcategory:"Special Logon" /success:enable
auditpol /set /subcategory:"Sensitive Privilege Use" /success:enable /failure:enable
auditpol /set /subcategory:"Process Creation" /success:enable /failure:enable
auditpol /set /subcategory:"Other Object Access Events" /success:enable

Para ver qué está activo:

auditpol /get /category:* | Select-String -Pattern 'Logon|Process Creation|Privilege Use'

4. Despliegue Sysmon (opcional, recomendado)

En este PC Sysmon ya estaba instalado durante el proyecto. Si falta en otro PC:

  1. Descargar Sysmon desde https://learn.microsoft.com/en-us/sysinternals/downloads/sysmon.
  2. Usar config validada por la comunidad (recomendable):
    • SwiftOnSecurity: https://github.com/SwiftOnSecurity/sysmon-config
    • Olaf Hartong (sysmon-modular): https://github.com/olafhartong/sysmon-modular
  3. Instalar:
sysmon.exe -accepteula -i sysmonconfig.xml
  1. Actualizar config:
sysmon.exe -c sysmonconfig.xml
  1. El UF recoge WinEventLog://Microsoft-Windows-Sysmon/Operational automáticamente.

Cómo revisar config activa de Sysmon

Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\SYSPREP\Settings' -ErrorAction SilentlyContinue
sysmon -s           # imprime config activa (requires admin)

En este PC: archivo de config visible no está en C:\Windows\sysmonconfig.xml (ruta esperada). Para instalar una config más actualizada, descargar SwiftOnSecurity y actualizar:

sysmon -accepteula -c swiftonsecurity-config.xml

5. Windows Defender (ya generado)

El log Microsoft-Windows-Windows Defender/Operational se genera por defecto si Defender está habilitado. Para confirmar:

Get-MpComputerStatus | Select-Object AntivirusEnabled,AMServiceEnabled,BehaviorMonitorEnabled

6. WMI Event Subscription (persistencia típica)

El log Microsoft-Windows-WMI-Activity/Operational ya se recoge con win_wmi. Para más detalle instalar Sysmon rule 19/20/21 (WmiEventFilter / WmiEventConsumer / WmiEventConsumerToFilter). En config SwiftOnSecurity o Olaf Hartong viene cubierto.

7. Verificación general post-hardening

# Revisa que las políticas están activas
Get-ItemProperty 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ScriptBlockLogging'
Get-ItemProperty 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging'
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\AuditProcessCreation'
auditpol /get /subcategory:* | Select-String -Pattern 'Process Creation|Logon|Privilege Use'

Evento de prueba para ver 4104 llegando a Splunk:

powershell -Command "Write-Output test-scriptblock-soc"

Búsqueda Splunk:

index=win_powershell EventCode=4104 ScriptBlockText="*test-scriptblock-soc*"

Volver a: 00 - MOC Monitorización PC Local