Skip to content

Instantly share code, notes, and snippets.

@scottglew
Created November 25, 2025 12:23
Show Gist options
  • Select an option

  • Save scottglew/3154b2929946f1bf56c42ea5aac3e6f7 to your computer and use it in GitHub Desktop.

Select an option

Save scottglew/3154b2929946f1bf56c42ea5aac3e6f7 to your computer and use it in GitHub Desktop.
PowerShell script to export Sophos Firewall Authentication events from Fastvue Syslog log files to a CSV report.
<#
extract-sophos-auth-events.ps1
Export Sophos Firewall Authentication events from Fastvue Syslog log files.
- Looks for yesterday's log file in $logPath with name pattern *-yyyy-MM-dd.log
- Filters for log_component="Firewall Authentication"
- Outputs a CSV with:
Time, Event Type, Severity, Message, User Name, Source, Status
#>
# ========================
# Configurable variables
# ========================
# Folder containing the Sophos log files for this firewall
# Example: 'C:\ProgramData\Fastvue\Syslog Server\Logs\192.168.20.100'
$logPath = 'C:\path\to\sophos\syslog\files'
# Folder to write the CSV reports into
$outputPath = 'C:\Firewall Authentication Reports'
# Optional: you can override this if you want a specific date
# By default it uses yesterday's date (for scheduling at 12:05am)
$reportDate = (Get-Date).AddDays(-1)
# ========================
# Derived values
# ========================
$logDateString = $reportDate.ToString('yyyy-MM-dd') # matches filename date portion
$logFilePattern = "*-$logDateString.log"
# Find the log file for the report date
$logFile = Get-ChildItem -LiteralPath $logPath -Filter $logFilePattern -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if (-not $logFile) {
Write-Error "No log file found matching '$logFilePattern' in '$logPath'."
exit 1
}
Write-Host "Using log file: $($logFile.FullName)"
# ========================
# Parse log and build data
# ========================
$events = @()
Get-Content -LiteralPath $logFile.FullName |
Where-Object { $_ -like '*log_component="Firewall Authentication"*' } |
ForEach-Object {
$line = $_
# Extract fields using regex
$timestamp = [regex]::Match($line, 'timestamp="([^"]+)"').Groups[1].Value
$logComponent = [regex]::Match($line, 'log_component="([^"]+)"').Groups[1].Value
$severity = [regex]::Match($line, 'severity="([^"]+)"').Groups[1].Value
$message = [regex]::Match($line, 'message="([^"]+)"').Groups[1].Value
$userName = [regex]::Match($line, 'user_name="([^"]+)"').Groups[1].Value
$srcIp = [regex]::Match($line, 'src_ip="([^"]+)"').Groups[1].Value
$status = [regex]::Match($line, 'status="([^"]+)"').Groups[1].Value
if (-not $timestamp) { return }
# Convert ISO timestamp with offset to local time, then format like 13/11/2025 0:45
$dt = [datetimeoffset]::Parse($timestamp).ToLocalTime()
$timeFormatted = $dt.ToString('dd/MM/yyyy H:mm')
$events += [PSCustomObject]@{
'Time' = $timeFormatted
'Event Type' = $logComponent
'Severity' = $severity
'Message' = $message
'User Name' = $userName
'Source' = $srcIp
'Status' = $status
}
}
if (-not $events.Count) {
Write-Warning "No Firewall Authentication events found in $($logFile.FullName)."
}
# ========================
# Output CSV
# ========================
if (-not (Test-Path $outputPath)) {
New-Item -ItemType Directory -Path $outputPath -Force | Out-Null
}
$outFileName = "FirewallAuthentication-$($reportDate.ToString('yyyy-MM-dd')).csv"
$outFilePath = Join-Path $outputPath $outFileName
$events | Export-Csv -Path $outFilePath -NoTypeInformation -Encoding UTF8
Write-Host "Exported $($events.Count) events to $outFilePath"
@scottglew

Copy link
Copy Markdown
Author

Sophos Firewall Authentication Export Script

This is a PowerShell script that extracts Firewall Authentication events from Sophos XG log files collected by Fastvue Syslog. Below is information on how to use edit and run the script, and how to schedule it to run automatically each day using Windows Task Scheduler.


Overview

The script:

  • Locates yesterday’s log file in a specified folder (for example: 10.1.2.3-2025-11-19.log)
  • Filters for log entries where log_component="Firewall Authentication"
  • Extracts key fields and writes them to a CSV with the following columns:
Column Description
Time Event time in dd/MM/yyyy H:mm (local time)
Event Type log_component (e.g. Firewall Authentication)
Severity Log severity (e.g. Information, Notice)
Message Full firewall authentication message
User Name User account name (user_name)
Source Source IP (src_ip)
Status Authentication status (status, e.g. Successful)

Example output row:

Time,Event Type,Severity,Message,User Name,Source,Status
13/11/2025 0:45,Firewall Authentication,Information,"User Bob of group Sales logged in successfully to Firewall through LDAP authentication mechanism from 10.1.2.3",Bob,10.1.2.3,Successful

Script Location and Configuration

Assumed script path (adjust as needed):

C:\scripts\extract-sophos-auth-events.ps1

At the top of the script you’ll find the configurable variables:

# Folder containing the Sophos log files for this firewall
$logPath    = 'C:\path\to\sophos\syslog\files'

# Folder to write the CSV reports into
$outputPath = 'C:\Firewall Authentication Reports'

# By default it uses yesterday's date (for scheduling at 12:05am)
$reportDate = (Get-Date).AddDays(-1)

Edit:

  • $logPath to point to the folder that contains your Sophos XG log files written by Fastvue Syslog. Log files should use the defalt {host}-{date}.log format.
  • $outputPath to the folder where you want CSV reports to be written.
  • Optionally override $reportDate if you want to generate a report for a specific date.

Running the Script Manually

Copy the above script into a new text file and save it as extract-sophos-auth-events.ps1. We'll assume you've saved it into C:\scripts\extract-sophos-auth-events.ps1.

  1. Open Windows PowerShell as Administrator.

  2. Run:

    C:\scripts\extract-sophos-auth-events.ps1
  3. Check the output folder (e.g. C:\Firewall Authentication Reports) for a CSV named:

    FirewallAuthentication-YYYY-MM-DD.csv
    

Scheduling the Script with Windows Task Scheduler (12:05 AM Daily)

These steps create a scheduled task that runs the script each night at 12:05 AM, exporting authentication events for yesterday.

1. Open Task Scheduler

  1. Press Start and type Task Scheduler.
  2. Open Task Scheduler.

2. Create a New Task

  1. In the right-hand Actions pane, click Create Task… (not Create Basic Task).
  2. On the General tab:
    • Name: Export Sophos Firewall Authentication
    • Description: Exports yesterday's Sophos firewall authentication events to CSV.
    • Security options:
      • Choose an account with permission to read the logs and write to the output folder (e.g. Administrator).
      • Tick “Run whether user is logged on or not”.
      • Tick “Run with highest privileges”.

3. Configure the Trigger (12:05 AM Daily)

  1. Go to the Triggers tab and click New….
  2. Set:
    • Begin the task: On a schedule
    • Settings: Daily
    • Start: set the date and time to 12:05:00 AM.
    • Recur every: 1 days.
  3. Make sure Enabled is ticked.
  4. Click OK.

4. Configure the Action (Run PowerShell Script)

  1. Go to the Actions tab and click New….

  2. Set:

    • Action: Start a program

    • Program/script:

      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
      
    • Add arguments (optional):

      -NoProfile -ExecutionPolicy Bypass -File "C:\scripts\extract-sophos-auth-events.ps1"
      
    • Start in (optional):

      C:\scripts\
      

      (No quotes in the Start in field.)

  3. Click OK.

5. Conditions & Settings (Recommended)

Conditions tab (optional):

  • Untick “Start the task only if the computer is on AC power” if this is not relevant.
  • Tick “Wake the computer to run this task” if you want it to run from sleep.

Settings tab (recommended):

  • Tick “Allow task to be run on demand”.
  • Tick “Run task as soon as possible after a scheduled start is missed”.

Click OK to save the task. You may be prompted for the account password.


Testing the Scheduled Task

  1. In Task Scheduler, locate your task under Task Scheduler Library.
  2. Right-click the task and select Run.
  3. Confirm that a new CSV file is created in your output folder.
  4. If the file appears and contains data, the task is correctly configured and will run automatically each night at 12:05 AM.

Troubleshooting

  • No log file found
    Check that:

    • The logPath is correct.
    • The log file for yesterday exists and follows the naming pattern *-YYYY-MM-DD.log.
  • No Firewall Authentication events found
    Ensure your Sophos XG is generating Firewall Authentication logs and that they are being sent to the Fastvue Syslog server. Open on of the Fastvue Syslog log files and search for 'Firewall Authentication'.

  • Script doesn’t run from Task Scheduler but works manually

    • Confirm the full path to PowerShell is correct:
      C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    • Ensure the account used by the task has read/write permissions to both the log folder and the output folder.
    • Check the History tab of the task for error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment