Lesson 2 of 10
Incident Triage & Initial Response
The first hours of incident response are the most critical. Rapid, accurate triage determines scope, contains the threat, and preserves evidence before attackers can cover their tracks or cause further damage.
First Response on a Compromised Linux Host
#!/bin/bash
# IR First Response Script - Run as ROOT on suspected compromised system
# PRESERVE EVIDENCE FIRST - run from read-only USB drive if possible
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
OUTPUT_DIR="/tmp/ir_${TIMESTAMP}"
mkdir -p "$OUTPUT_DIR"
echo "[*] Capturing volatile data (disappears on reboot)..."
# 1. Current network connections - who is the system talking to?
echo "=== NETWORK CONNECTIONS ===" > "$OUTPUT_DIR/network.txt"
ss -tulnp >> "$OUTPUT_DIR/network.txt" # Open ports + processes
netstat -antp >> "$OUTPUT_DIR/network.txt" # All TCP connections
ip route show >> "$OUTPUT_DIR/network.txt" # Routing table
# 2. Running processes - any suspicious processes?
echo "=== RUNNING PROCESSES ===" > "$OUTPUT_DIR/processes.txt"
ps auxf >> "$OUTPUT_DIR/processes.txt" # All processes with forest view
ls -la /proc/*/exe 2>/dev/null >> "$OUTPUT_DIR/processes.txt" # Process executables
# 3. Logged-in users and recent logins
echo "=== USERS ===" > "$OUTPUT_DIR/users.txt"
who >> "$OUTPUT_DIR/users.txt" # Currently logged in
w >> "$OUTPUT_DIR/users.txt" # Who + what they're doing
last -20 >> "$OUTPUT_DIR/users.txt" # Last 20 logins
lastb -20 >> "$OUTPUT_DIR/users.txt" # Last 20 failed logins
# 4. Recent file system changes (last 24 hours)
find / -mtime -1 -type f -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null >> "$OUTPUT_DIR/recent_files.txt"
# 5. Cron jobs and startup persistence
echo "=== PERSISTENCE ===" > "$OUTPUT_DIR/persistence.txt"
crontab -l >> "$OUTPUT_DIR/persistence.txt" 2>/dev/null
ls -la /etc/cron* >> "$OUTPUT_DIR/persistence.txt"
ls -la /etc/systemd/system/*.service >> "$OUTPUT_DIR/persistence.txt"
cat /etc/rc.local >> "$OUTPUT_DIR/persistence.txt" 2>/dev/null
# 6. Calculate hashes of key system binaries
sha256sum /bin/bash /usr/bin/sudo /usr/sbin/sshd > "$OUTPUT_DIR/binary_hashes.txt"
echo "[*] Evidence collected in $OUTPUT_DIR"Windows First Response
# PowerShell - First response on Windows
$OutputDir = "C:\IR_$(Get-Date -Format yyyyMMdd_HHmmss)"
New-Item -ItemType Directory -Path $OutputDir
# Network connections + process names
Get-NetTCPConnection | Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, State, OwningProcess |
Export-Csv "$OutputDir\network.csv"
# Running processes with full paths
Get-Process | Select-Object Name, Id, Path, CPU, WorkingSet |
Export-Csv "$OutputDir\processes.csv"
# Autorun / persistence locations
Get-ScheduledTask | Where-Object State -ne Disabled |
Export-Csv "$OutputDir\scheduled_tasks.csv"
Get-ItemProperty HKLM:SoftwareMicrosoftWindowsCurrentVersionRun |
Export-Csv "$OutputDir\registry_run.csv"
# Recent PowerShell history
Get-Content (Get-PSReadlineOption).HistorySavePath |
Out-File "$OutputDir\ps_history.txt"
# Windows Event Logs - last 1000 security events
Get-EventLog -LogName Security -Newest 1000 |
Export-Csv "$OutputDir\security_events.csv"
# Logon events (4624 = success, 4625 = failure, 4648 = explicit logon)
Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4624,4625,4648} -MaxEvents 200 |
Export-Csv "$OutputDir\logon_events.csv"Common Mistakes
- Rebooting or shutting down the compromised system immediately - reboots destroy volatile evidence (RAM contents, running processes, network connections, encryption keys in memory); always collect volatile data first
- Running IR tools from the compromised system's utilities - attackers may have replaced system binaries (rootkit); use tools from a clean USB or trusted remote server
- Not taking notes with timestamps - IR requires precise timelines; lack of timestamped notes makes post-incident reports and legal proceedings difficult
Tip
Tip
Practice Incident Triage Initial Response in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of Incident Triage Initial Response from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Incident Triage Initial Response is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- The first hours of incident response are the most critical.
- Rebooting or shutting down the compromised system immediately - reboots destroy volatile evidence (RAM contents, running processes, network connections, encryption keys in memory); always collect volatile data first
- Running IR tools from the compromised system's utilities - attackers may have replaced system binaries (rootkit); use tools from a clean USB or trusted remote server
- Not taking notes with timestamps - IR requires precise timelines; lack of timestamped notes makes post-incident reports and legal proceedings difficult
Finished this lesson?