Advertisement
Guest User

Install-Sysmon.ps1

a guest
May 16th, 2022
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Install Sysmon if it isn't installed, and if it is, update the config (if it needs it)
  2. # (C) Nathaniel Roach 2021
  3.  
  4. #Requires -RunAsAdministrator
  5. [cmdletbinding()]
  6. Param(
  7.     [Parameter(Mandatory=$false)] [Switch]$ForceUpdate = $false,
  8.     [Parameter(Mandatory=$false)] [Switch]$ForceConfig = $false,
  9.     [Parameter(Mandatory=$false)] [Switch]$ForceBinary = $false
  10.     )
  11.  
  12. Set-strictmode -version latest
  13.  
  14. $ErrorActionPreference = "Stop"
  15.  
  16. $SysmonRemotePath = "\\domain\DFS\ComputerAccessibleShare\sysmon"
  17. $SysmonExe32 = "sysmon\sysmon.exe"
  18. $SysmonExe64 = "sysmon\sysmon64.exe"
  19. $SysmonConfig = "sysmon-config.xml"
  20. $SysmonLocalPath = "C:\Windows"
  21.  
  22. $SysmonConfigRemotePath = ($SysmonRemotePath + "\" + $SysmonConfig)
  23. $SysmonConfigLocalPath = ($SysmonLocalPath + "\" + $SysmonConfig)
  24.  
  25. If ($ForceUpdate) {
  26.     $ForceConfig = $true
  27.     $ForceBinary = $true
  28. }
  29.  
  30. $IntConfigUpdated = $false
  31. $IntBinaryUpdated = $false
  32. $IntServiceRegistered = $false
  33.  
  34. $HostBitness = (gwmi win32_operatingsystem | select osarchitecture).osarchitecture
  35. if ($HostBitness -ne "64-bit")
  36. {
  37.     $SysmonRemoteExeFullPath = ($SysmonRemotePath + "\" + $SysmonExe32)
  38.     $SysmonServiceName = "sysmon"
  39. } else {
  40.     $SysmonRemoteExeFullPath = ($SysmonRemotePath + "\" + $SysmonExe64)
  41.     $SysmonServiceName = "sysmon64"
  42. }
  43.  
  44. Write-Verbose ("Using the following EXE for service install: " + $SysmonRemoteExeFullPath)
  45.  
  46. function Update-SysmonConfig {
  47.     Param(
  48.     [Parameter(Mandatory=$false)]
  49.     [Switch]$NoService = $false
  50.     )
  51.     if (! $IntConfigUpdated) {
  52.         try {
  53.             Write-Verbose "Copying config..."
  54.             Copy-Item -Path $SysmonConfigRemotePath -Destination $SysmonLocalPath -Force
  55.             $IntConfigUpdated = $true
  56.             if (! $NoService) {
  57.                 Write-Verbose "Informing service of update..."
  58.                 try {
  59.                     & $SysmonRemoteExeFullPath -c $SysmonConfigLocalPath
  60.                 } catch {
  61.                     Write-Warning "Error applying config"
  62.                 }
  63.             }
  64.         } catch {
  65.             Write-Error ("Error copying config: " + $_.Exception)
  66.         }
  67.     }
  68. }
  69.  
  70. function Update-SysmonBinary {
  71.     Param(
  72.     [Parameter(Mandatory=$false)]
  73.     [Switch]$NoService = $false
  74.     )
  75.     if (! $IntBinaryUpdated) {
  76.         try {        
  77.             Write-Verbose "Copying binary..."
  78.             Copy-Item -Path $SysmonRemoteExeFullPath -Destination ($SysmonLocalPath + "\Sysmon.exe" + ".new") -Verbose
  79.            
  80.             if (! $NoService) {
  81.                 Write-Verbose "Uninstalling and swapping files"
  82.                 try {
  83.                     Write-Verbose "Attempting uninstall..."
  84.                     & $SysmonRemoteExeFullPath -u force
  85.                     #Move-Item -Path ($SysmonLocalPath + "\Sysmon.exe") -Destination ($SysmonLocalPath + "\Sysmon.exe" + ".old") -Verbose -Force
  86.                     Move-Item -Path ($SysmonLocalPath + "\Sysmon.exe" + ".new") -Destination ($SysmonLocalPath + "\Sysmon.exe") -Verbose -Force
  87.  
  88.                     Write-Verbose "Installing update"
  89.                     & $SysmonRemoteExeFullPath -accepteula -i $SysmonConfigLocalPath
  90.                 } catch {
  91.                     Write-Warning "Error restarting Sysmon during Update-SysmonBinary"
  92.                 }
  93.             } else {  
  94.                 Move-Item -Path ($SysmonLocalPath + "\Sysmon.exe" + ".new") -Destination ($SysmonLocalPath + "\Sysmon.exe") -Verbose -Force
  95.             }
  96.             $IntBinaryUpdated = $true
  97.         } catch {
  98.             Write-Error ("Error copying binary: " + $_.Exception)
  99.         }
  100.     }
  101. }
  102.  
  103. function Install-SysmonService {
  104.     Write-Verbose "Starting install..."
  105.  
  106.     try { # making the directory if it doesn't exist
  107.         If (!(Test-Path -Path $SysmonLocalPath -PathType Container)){
  108.             Write-Verbose "Making directory"
  109.  
  110.             Remove-Item -Path $SysmonLocalPath -ErrorAction Ignore
  111.             New-Item -Path $SysmonLocalPath -ItemType Directory
  112.         }
  113.  
  114.         Update-SysmonConfig -NoService:$true
  115.         Update-SysmonBinary -NoService:$true
  116.                
  117.     } catch {
  118.         Write-Error ("Error copying files: " + $_.exception)
  119.     }
  120.    
  121.     try { # registering the service
  122.         & $SysmonRemoteExeFullPath -accepteula -i $SysmonConfigLocalPath
  123.         $IntServiceRegistered = $true
  124.     } catch {
  125.         Write-Error ("Error registring Sysmon service: " + $_.Exception)
  126.     }
  127. }
  128.  
  129. Try {
  130.     Write-Verbose "Checking presence of Sysmon service"
  131.     Get-Service $SysmonServiceName -ErrorAction Stop | Out-Null
  132.     Write-Verbose "Sysmon service already installed"
  133.  
  134.     Write-Verbose "Checking if binary needs updating, might take a second"
  135.     #If ((Get-FileHash $($SysmonLocalPath + "\Sysmon.exe")).hash -ne (Get-FileHash $SysmonRemoteExeFullPath).hash) {
  136.     If (!(Test-Path ($SysmonLocalPath + "\Sysmon.exe"))) {
  137.         Update-SysmonBinary
  138.     } elseif ((Get-Item ($SysmonLocalPath + "\Sysmon.exe")).LastWriteTimeUtc -lt (Get-Item $SysmonRemoteExeFullPath).LastWriteTimeUtc -or $ForceBinary) {
  139.         Write-Host "Binary being updated..."
  140.         Update-SysmonBinary
  141.     }
  142.  
  143.     Write-Verbose "Checking if config need updating"
  144.     If (!(Test-Path ($SysmonConfigLocalPath))) {
  145.         Update-SysmonConfig
  146.     } elseif ((Get-FileHash $SysmonConfigLocalPath).hash -ne (Get-FileHash $SysmonConfigRemotePath).hash -or $ForceConfig) {
  147.         Write-Host "Config being updated..."
  148.         Update-SysmonConfig
  149.     }
  150.  
  151. } catch [Microsoft.PowerShell.Commands.ServiceCommandException] {
  152.     Write-Host "Sysmon service missing, beginning install"
  153.     Install-SysmonService
  154. }
  155.  
  156. Write-Host "Done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement