--- - name: Stop if the Sysmon checksum was not set ansible.builtin.assert: that: - sysmon_zip_sha256 | length == 64 fail_msg: >- Set sysmon_zip_sha256 in group_vars. Download Sysmon.zip once, check its signature, and use the SHA-256 from Get-FileHash. The file changes with every release. - name: Make a folder for Sysmon ansible.windows.win_file: path: "{{ sysmon_dir }}" state: directory - name: Download Sysmon, refusing it if the checksum differs ansible.windows.win_get_url: url: "{{ sysmon_zip_url }}" dest: "{{ sysmon_dir }}\\Sysmon.zip" checksum: "{{ sysmon_zip_sha256 }}" checksum_algorithm: sha256 register: sysmon_zip - name: Unpack Sysmon ansible.windows.win_powershell: parameters: Dir: "{{ sysmon_dir }}" Force: "{{ sysmon_zip.changed }}" script: | [CmdletBinding(SupportsShouldProcess)] param([string]$Dir, [bool]$Force) if ((Test-Path "$Dir\Sysmon64.exe") -and -not $Force) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess($Dir, 'unpack Sysmon.zip')) { Expand-Archive -Path "$Dir\Sysmon.zip" -DestinationPath $Dir -Force } - name: Write the Sysmon configuration ansible.windows.win_template: src: sysmonconfig.xml.j2 dest: "{{ sysmon_dir }}\\sysmonconfig.xml" register: sysmon_config # Sysmon checks the file against its schema when it loads it, and refuses a # bad one while keeping the configuration it already had. The exit code is # how that refusal reaches the run. - name: Install Sysmon, or load the new configuration ansible.windows.win_powershell: parameters: Dir: "{{ sysmon_dir }}" Reload: "{{ sysmon_config.changed or sysmon_zip.changed }}" script: | [CmdletBinding(SupportsShouldProcess)] param([string]$Dir, [bool]$Reload) $installed = [bool](Get-Service -Name Sysmon64 -ErrorAction SilentlyContinue) # -c only reloads the configuration, so a new release is installed # by removing the running one first. $running = (Get-Item "$env:windir\Sysmon64.exe" -ErrorAction SilentlyContinue).VersionInfo.FileVersion $fresh = (Get-Item "$Dir\Sysmon64.exe" -ErrorAction SilentlyContinue).VersionInfo.FileVersion $upgrade = $installed -and $running -and $fresh -and ($running -ne $fresh) if ($installed -and -not $upgrade -and -not $Reload) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess('Sysmon64', 'install, upgrade, or reload')) { if ($upgrade) { & "$Dir\Sysmon64.exe" -u | Out-Null if ($LASTEXITCODE -ne 0) { throw "Sysmon $running would not uninstall (exit $LASTEXITCODE)" } $installed = $false } if ($installed) { & "$Dir\Sysmon64.exe" -c "$Dir\sysmonconfig.xml" | Out-Null } else { & "$Dir\Sysmon64.exe" -accepteula -i "$Dir\sysmonconfig.xml" | Out-Null } if ($LASTEXITCODE -ne 0) { throw "Sysmon rejected $Dir\sysmonconfig.xml (exit $LASTEXITCODE)" } } - name: Make the Sysmon log big enough to survive a day without forwarding ansible.windows.win_powershell: parameters: Bytes: "{{ sysmon_log_max_bytes }}" script: | [CmdletBinding(SupportsShouldProcess)] param([long]$Bytes) $log = 'Microsoft-Windows-Sysmon/Operational' $line = wevtutil gl $log 2>$null | Where-Object { $_ -like '*maxSize:*' } # No log yet means Sysmon is not installed, as in a preview of a first run. if (-not $line) { if ($WhatIfPreference) { return }; throw "$log does not exist. Is Sysmon installed?" } $now = [long](($line -split ':')[1].Trim()) if ($now -ge $Bytes) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess($log, "raise maximum size to $Bytes bytes")) { wevtutil sl $log "/ms:$Bytes" if ($LASTEXITCODE -ne 0) { throw "wevtutil could not resize $log" } } - name: Confirm Sysmon is running and writing ansible.windows.win_powershell: script: | $Ansible.Changed = $false $svc = Get-Service -Name Sysmon64 -ErrorAction SilentlyContinue if (-not $svc -or $svc.Status -ne 'Running') { throw 'The Sysmon64 service is not running.' } $last = Get-WinEvent -LogName 'Microsoft-Windows-Sysmon/Operational' -MaxEvents 1 -ErrorAction Stop $Ansible.Result = @{ last_event = $last.TimeCreated.ToString('o') } when: not ansible_check_mode