DEV Community

lucanu
lucanu

Posted on

Auto‑Generate Security Incident Heatmaps from CSV with LLM Prompts

A recent internal audit of a Fortune 500 SOC showed that analysts spent an average of 42 minutes per day manually filtering CSV logs before they could draw a heatmap of suspicious activity. ## 1. Pull raw events into a structured CSV

Security devices, firewalls, IDS/IPS, and endpoint agents, export logs in CSV format. A typical row contains a timestamp, source IP, destination IP, protocol, and a numeric risk score.

# Example: extract the last 24 h from a Palo Alto CSV export
awk -F',' '$1 >= "$(date -d "-24 hours" +%Y-%m-%d)"' panos_traffic.csv > recent.csv
Enter fullscreen mode Exit fullscreen mode


plaintext

The resulting recent.csv is the only input the LLM will ever see. Keep the column order consistent; rename ambiguous headers to ts,src_ip,dst_ip,proto,score. Takeaway: Standardize every CSV export to the same header set before feeding it to a language model. ## 2. Prompt the LLM to produce a heatmap script

Large language models excel at turning plain text specifications into runnable code. The prompt below asks the model to generate a Python script that reads recent.csv and outputs a heatmap PNG showing incident density by hour and source subnet.

You are a security automation expert. Write a Python 3 script that:
- reads recent.csv (columns: ts,src_ip,dst_ip,proto,score)
- parses ts as ISO‑8601, groups events by hour (0‑23) and by the first two octets of src_ip (e.g., 10.0, 192.168)
- sums the score for each bucket
- creates a heatmap where the X‑axis is hour, Y‑axis is src subnet, and color intensity is the summed score
- saves the figure as heatmap.png using matplotlib and seaborn
- includes a command‑line argument `--output` to change the file name
Enter fullscreen mode Exit fullscreen mode

When you paste this prompt into a tool like OpenAI's ChatGPT or Claude, the model returns a complete script within seconds. Review the code for any hard‑coded paths, then run it directly. Takeaway: A single, well‑crafted prompt replaces dozens of manual copy‑paste steps and yields production‑ready code. ## 3. Execute the generated script in a CI pipeline

Integrate the script into a GitHub Actions workflow so the heatmap updates with every new CSV upload. Below is a minimal workflow that triggers on a new file in the logs/ directory.

name: Heatmap Generation
on:
 push:
 paths:
 - 'logs/*.csv'
jobs:
 build:
 runs-on: ubuntu-latest
 steps:
 - uses: actions/checkout@v3
 - name: Set up Python
 uses: actions/setup-python@v5
 with:
 python-version: '3.11'
 - name: Install deps
 run: pip install pandas matplotlib seaborn
 - name: Generate heatmap
 run: |
 python generate_heatmap.py --output heatmap.png
 - name: Upload artifact
 uses: actions/upload-artifact@v3
 with:
 name: heatmap
 path: heatmap.png
Enter fullscreen mode Exit fullscreen mode


markdown

The workflow runs in under two minutes for a 500 KB CSV, producing a PNG that can be attached to a ticket or posted to Slack. Takeaway: Automate the script execution with CI/CD to guarantee fresh visual summaries without manual intervention. ## 4. Visual tweaks that matter to responders

Analysts often complain that heatmaps are either too dark or lack context. Add these three tweaks to the generated script before committing:

  1. Log‑scale color map – prevents high‑score outliers from flattening the rest of the plot.

    import numpy as np
    sns.heatmap(matrix, cmap='viridis', norm=matplotlib.colors.LogNorm())
    
  2. Overlay event count – a small white number on each cell shows raw incident volume.

    for i in range(matrix.shape[0]):
    for j in range(matrix.shape[1]):
    plt.text(j+0.5, i+0.5, int(count_matrix[i, j]),
    ha='center', va='center', color='white', fontsize=8)
    
  3. Dynamic title – embed the date range pulled from the CSV.

    start = df['ts'].min().date()
    end = df['ts'].max().date()
    plt.title(f'Incident Heatmap {start} → {end}')
    

These three lines turn a generic PNG into a decision‑ready artifact that responders can reference while triaging. Takeaway: Small visual refinements dramatically increase the heatmap's operational value. ## 5. Validate and iterate with a feedback loop

After the first deployment, collect two metrics for the next week:

  • Mean Time to Visual Insight (MTVI): time from CSV arrival to heatmap availability. - False‑negative heat zones: count of high‑risk periods that the heatmap failed to highlight. If MTVI stays above 5 minutes, revisit the CI step that copies the CSV into the repo; a direct S3 sync can shave seconds. If false‑negative zones appear, adjust the grouping granularity from /16 subnets to /24. bash # Example: switch to /24 subnets in the generated script src_subnet = '.'.join(src_ip.split('.')[:3])

Iterating on the script is cheap because the LLM can regenerate the modified section on demand. Takeaway: Treat the heatmap pipeline as a feedback‑driven product; small parameter tweaks yield measurable security gains. ---

Deploy the prompt, script, and GitHub Action today, then schedule a 15‑minute review tomorrow to confirm the PNG appears in your incident channel. This single change cuts manual parsing time from dozens of minutes to under a minute per shift.


If this was useful: I packaged a deeper version as PHP 8.3 Upgrade Cheat Sheet for Developers.

This article was drafted with AI assistance.

Top comments (0)