|
| 1 | +#Author: NerbalOne |
| 2 | +#This PowerShell script will first create the Sysmon folder if it does not exist. It will then identify which OS architecture the endpoint is running and download the appropriate Sysmon version along with the Sysmon config and Sysmon Update script. It will then install Sysmon with the config and create a Scheduled Task to run hourly to update the Sysmon config. |
| 3 | +#You may have issues while running this script on Windows Server 2012 R2 servers as it seems this server version only works with the Sysmon.exe and not the Sysmon64.exe with the newer Sysmon versions. |
| 4 | + |
| 5 | +# Define Sysmon URLs |
| 6 | +$sysmon32URL = "https://live.sysinternals.com/sysmon.exe" |
| 7 | +$sysmon64URL = "https://live.sysinternals.com/sysmon64.exe" |
| 8 | +$sysmonConfigURL = "https://raw.githubusercontent.com/ion-storm/sysmon-config/master/sysmonconfig-export.xml" |
| 9 | +$sysmonUpdateConfig = "https://raw.githubusercontent.com/ion-storm/sysmon-config/master/SysmonUpdateConfig.ps1" |
| 10 | + |
| 11 | +# Define Local Path for Sysmon File and Sysmon Config |
| 12 | +$sysmon32Path = "C:\Programdata\Sysmon\sysmon.exe" |
| 13 | +$sysmon64Path = "C:\Programdata\Sysmon\sysmon64.exe" |
| 14 | +$sysmonConfigPath = "C:\Programdata\Sysmon\sysmonconfig-export.xml" |
| 15 | +$sysmonUpdatePath = "C:\Programdata\Sysmon\SysmonUpdateConfig.ps1" |
| 16 | +$sysmonFolderPath = "C:\ProgramData\Sysmon\" |
| 17 | + |
| 18 | +# Create Sysmon Folder if it Doesn't Exist |
| 19 | +if (-not (Test-Path $sysmonFolderPath)) { |
| 20 | + # Create the Folder |
| 21 | + try { |
| 22 | + New-Item -ItemType Directory -Path $sysmonFolderPath -Force |
| 23 | + Write-Host "Folder created successfully at $folderPath" |
| 24 | + } |
| 25 | + catch { |
| 26 | + Write-Host "Error creating the folder: $_" |
| 27 | + } |
| 28 | +} |
| 29 | +else { |
| 30 | + Write-Host "The folder already exists at $folderPath" |
| 31 | +} |
| 32 | + |
| 33 | +# Check OS Architecture |
| 34 | +$OSArchitecture = (Get-WmiObject -Query "Select * from Win32_OperatingSystem").OSArchitecture |
| 35 | + |
| 36 | +# Download Sysmon Update Script |
| 37 | +Invoke-WebRequest -Uri $sysmonUpdateConfig -OutFile $sysmonUpdatePath |
| 38 | + |
| 39 | +# Download Sysmon Config |
| 40 | +Invoke-WebRequest -Uri $sysmonConfigURL -OutFile $sysmonConfigPath |
| 41 | + |
| 42 | +# Depending on the OS Architecture, Download and Install Sysmon |
| 43 | +if ($OSArchitecture -eq "32-bit") { |
| 44 | + # Download Sysmon 32 bit |
| 45 | + Invoke-WebRequest -Uri $sysmon32URL -OutFile $sysmon32Path |
| 46 | + |
| 47 | + # Install Sysmon with Config |
| 48 | + Start-Process -FilePath $sysmon32Path -ArgumentList "-accepteula -i $sysmonConfigPath" -NoNewWindow -Wait |
| 49 | + |
| 50 | +} elseif ($OSArchitecture -eq "64-bit") { |
| 51 | + # Download Sysmon 64 bit |
| 52 | + Invoke-WebRequest -Uri $sysmon64URL -OutFile $sysmon64Path |
| 53 | + |
| 54 | + # Install Sysmon with Config |
| 55 | + Start-Process -FilePath $sysmon64Path -ArgumentList "-accepteula -i $sysmonConfigPath" -NoNewWindow -Wait |
| 56 | + |
| 57 | +} else { |
| 58 | + Write-Output "Unsupported architecture: $OSArchitecture" |
| 59 | +} |
| 60 | + |
| 61 | +# Create a New Scheduled Task |
| 62 | +Start-Process schtasks.exe -ArgumentList '/Create /RU SYSTEM /RL HIGHEST /SC HOURLY /TN Update_Sysmon_Rules /TR "powershell.exe -ExecutionPolicy Bypass -File "C:\Programdata\Sysmon\SysmonUpdateConfig.ps1"" /f' -Wait -WindowStyle Hidden |
| 63 | +Start-Process schtasks.exe -ArgumentList '/Run /TN Update_Sysmon_Rules' -Wait -WindowStyle Hidden |
| 64 | + |
| 65 | +# Define Sysmon service Name Based on OS Architecture |
| 66 | +$sysmonServiceName = if ($OSArchitecture -eq "64-bit") { "Sysmon64" } else { "Sysmon" } |
| 67 | + |
| 68 | +# Check if Sysmon Service Exists |
| 69 | +try { |
| 70 | + $service = Get-Service -Name $sysmonServiceName -ErrorAction Stop |
| 71 | + Write-Output "Sysmon service exists" |
| 72 | +} catch { |
| 73 | + Throw "Sysmon service does not exist" |
| 74 | +} |
| 75 | + |
| 76 | +# Check if Scheduled Task is Created Successfully |
| 77 | +try { |
| 78 | + $task = Get-ScheduledTask -TaskName "Update_Sysmon_Rules" -ErrorAction Stop |
| 79 | + Write-Output "Scheduled task created successfully" |
| 80 | +} catch { |
| 81 | + Throw "Scheduled task creation failed" |
| 82 | +} |
0 commit comments