Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: Eleкtro en 3 Marzo 2024, 21:54 pm



Título: [APORTE] [PowerShell] Windows Event Logs Cleaner
Publicado por: Eleкtro en 3 Marzo 2024, 21:54 pm
El siguiente script desarrollado en el lenguaje Powershell sirve como "atajo" para eliminar todas las entradas de los registros de eventos de Windows (que se pueden analizar mediante la herramienta ubicada en: "C:\Windows\System32\eventvwr.exe").

Nota: esto es algo que también se puede hacer con CCleaner.

(http://i.imgur.com/eo7Ephzl.png) (https://i.imgur.com/eo7Ephz.png)

(http://i.imgur.com/efTpqOVl.png) (https://i.imgur.com/efTpqOV.png)



Código
  1. <#
  2. ===========================================================================================
  3. |                                                                                         |
  4. |                                    Functions                                            |
  5. |                                                                                         |
  6. ===========================================================================================
  7. #>
  8.  
  9. function Show-WelcomeScreen {
  10.    Clear-Host
  11.    Write-Output ""
  12.    Write-Output " $($host.ui.RawUI.WindowTitle)"
  13.    Write-Output " +=================================================+"
  14.    Write-Output " |                                                 |"
  15.    Write-Output " | This script will clear all the entries from the |"
  16.    Write-Output " | Windows event logs on the current computer, and |"
  17.    Write-Output " | display a table with the deletion results.      |"
  18.    Write-Output " |                                                 |"
  19.    Write-Output " +=================================================+"
  20.    Write-Output ""
  21. }
  22.  
  23. function Confirm-Continue {
  24.    Write-Host " Press 'Y' key to continue or 'N' key to exit."
  25.    Write-Host ""
  26.    Write-Host " -Continue? (Y/N)"
  27.    do {
  28.        $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  29.        $char = $key.Character.ToString().ToUpper()
  30.        if ($char -ne "Y" -and $char -ne "N") {
  31.            [console]::beep(1500, 500)
  32.        }
  33.    } while ($char -ne "Y" -and $char -ne "N")
  34.    if ($char -eq "N") {Exit(1)} else {Clear-Host}
  35. }
  36.  
  37. # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/clear-eventlog?view=powershell-5.1#example-4-clear-all-logs-on-the-specified-computers-then-display-the-event-log-list
  38. function Clear-EventLogs ($computerName=".") {
  39.    try {
  40.        $logsBefore = Get-EventLog -ComputerName $computerName -List | Select-Object Log, @{Name="Before";Expression={$_.Entries.Count}}
  41.        Get-EventLog -ComputerName $computername -List | ForEach-Object {$_.Log} | ForEach-Object {
  42.            Write-Host "Deleting $_ event logs..."
  43.            Clear-EventLog -ComputerName $computername -LogName $_
  44.        }
  45.        $logsAfter = Get-EventLog -ComputerName $computerName -List | Select-Object Log, @{Name="After";Expression={$_.Entries.Count}}
  46.        $logsDiff = $logsBefore | ForEach-Object {
  47.            $log = $_.Log
  48.            $Before = $_.Before
  49.            $After = ($logsAfter | Where-Object {$_.Log -eq $log}).After
  50.            [PSCustomObject]@{
  51.                Log = $log
  52.                Before = $Before
  53.                After = $After
  54.            }
  55.        }
  56.        $logsDiff|Format-Table
  57.    } catch {
  58.        Write-Host "Something went wrong when calling '$($MyInvocation.MyCommand.Name)' method:"
  59.        Write-Host ""
  60.        Write-Warning ($_.Exception.InnerException.Message)
  61.        Write-Host ""
  62.        Write-Error -Message ($_.Exception | Format-List * -Force | Out-String)
  63.        Write-Host ""
  64.        Write-Host "Press any key to exit..."
  65.        $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
  66.        Exit(1)
  67.    }
  68. }
  69.  
  70. function Show-GoodbyeScreen {
  71.    Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
  72.    Write-Host ""
  73.    Write-Host "Press any key to exit..."
  74.    $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
  75. }
  76.  
  77. <#
  78. ===========================================================================================
  79. |                                                                                         |
  80. |                                         Main                                            |
  81. |                                                                                         |
  82. ===========================================================================================
  83. #>
  84.  
  85. [System.Console]::Title = "Windows Event Logs Cleaner - by Elektro"
  86. [CultureInfo]::CurrentUICulture = "en-US"
  87.  
  88. try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
  89.  
  90. Show-WelcomeScreen
  91. Confirm-Continue
  92. Clear-EventLogs -ComputerName "."
  93. Show-GoodbyeScreen
  94. Exit(0)