PowerShell to Download All Critical/High Lacework Alerts From the Last 24 Hours

Not sure how many folks here would use this but I needed this as part of a larger project so I thought I would share:

# Define Lacework API key, endpoint, and time range
$APIKey = "your_lacework_api_key"
$BaseURI = "https://api.lacework.net/api/v1"
$TimeRange = (Get-Date).AddHours(-24).ToString("yyyy-MM-ddTHH:mm:ssZ")

# Create authentication headers
$Headers = @{
    'Content-Type' = 'application/json'
    'x-api-key' = $APIKey
}

# Define function to get alerts
function Get-LaceworkAlerts($severity, $since) {
    $URI = "$BaseURI/external/vulnerabilities/container?start_time=$since&severity=$severity"

    try {
        $Response = Invoke-WebRequest -Uri $URI -Headers $Headers -Method Get
        $Alerts = ($Response.Content | ConvertFrom-Json).data
    }
    catch {
        Write-Error "Error fetching Lacework alerts: $_"
        $Alerts = $null
    }

    return $Alerts
}

# Get critical and high alerts from the past 24 hours
$CriticalAlerts = Get-LaceworkAlerts -severity "Critical" -since $TimeRange
$HighAlerts = Get-LaceworkAlerts -severity "High" -since $TimeRange

# Output the alerts
$CriticalAlerts
$HighAlerts

# Save the alerts to JSON files
if ($CriticalAlerts) {
    $CriticalAlerts | ConvertTo-Json | Set-Content -Path "CriticalAlerts.json"
}
if ($HighAlerts) {
    $HighAlerts | ConvertTo-Json | Set-Content -Path "HighAlerts.json"
}

Comments

Popular posts from this blog

Unveiling the Power of PowerShell Regions: A Comprehensive Guide

PowerShell Script to Remotely Update Firmware on Brother Printers Microsoft

PowerShell Script to Reset Permissions on all Documents in a Document Library in SharePoint Online.