🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Cybersecurity › Scripting for Cybersecurity › Lesson 10
TEC-CYBE44-05 · Lesson 10 · 16 clock hours · lab

Scripting Incident-Response Tasks: Triage Automation

Objective. Students write a Python triage script that collects volatile system data from a live Linux host — running processes, active network connections, logged-in users, and recent auth events — and writes a time-stamped forensic snapshot report.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Scripted Triage Matters

When a security analyst responds to a suspected compromise, the clock is running against them. Volatile data — information that exists only while the system is powered on — disappears the moment a machine reboots or an attacker clears their tracks. A triage script gives you a fast, repeatable, documented snapshot of a live system before that data is gone. This is a core skill tested in the CompTIA CySA+ incident-response domain and directly supports the ISC2 CC concepts of evidence preservation.

What Volatile Data Is and Why Collection Order Matters

Volatile data is any system state stored in memory or active kernel tables rather than on disk. Examples include:

Because some of this data changes faster than others, analysts follow an order-of-volatility principle. Collect the most transient data first: RAM contents (out of scope for this script but addressed in Course 7), then the process list, then network state, then on-disk log excerpts. If you collect in the wrong order you may miss a process that exited or a connection that dropped.

The Python Script Structure

You will build a single Python 3 script using three standard-library modules:

Capturing Commands Safely

Use `subprocess.run()` with `capture_output=True` and `text=True`. Always pass commands as a list of strings rather than a single shell string — this avoids shell-injection risks, which is an important security practice even in internal tooling.

```python

result = subprocess.run(["ps", "aux"], capture_output=True, text=True)

```

Check `result.returncode` before writing output. If the command fails, write the error (`result.stderr`) to your report so you know something went wrong rather than silently missing data.

The Four Commands

| Command | What it collects |

|---|---|

| `ps aux` | All running processes with PID, user, CPU, and command line |

| `ss -tulnp` | Listening TCP/UDP sockets with process names and PIDs |

| `who` | Currently logged-in users and their login times |

| `tail -n 50 /var/log/auth.log` | Last 50 lines of authentication log |

`ss` replaces the older `netstat` on most modern Linux distributions and is the tool referenced in current CySA+ objectives.

Naming the Report File

Name the output file using the hostname and a UTC timestamp so reports from different machines or different collection runs never overwrite each other:

```

triage_webserver01_20250601T143022Z.txt

```

Generate this with `datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")`.

Report Structure

Each section of the plain-text report should follow this pattern:

```

========================================

SECTION: Running Processes

Collected: 2025-06-01T14:30:22Z

========================================

<output here>

```

This structure makes the report readable by a human analyst and parseable by a SIEM or other tool later.

Safety and Ethical Boundaries

These tools are only to be run against systems you are authorized to examine. Running triage scripts against systems without written authorization may violate the Computer Fraud and Abuse Act (CFAA) and equivalent state laws. In this course you run your script only against the assigned lab VM. Never run it against shared infrastructure, cloud instances outside your lab account, or any system belonging to another person without documented approval.

From a system-safety standpoint, `subprocess` commands that write to disk should target a designated evidence directory, not system directories. Writing large files to `/` or `/var` on a compromised host could trigger disk-full conditions that interfere with other evidence or with the running system.

The Red-Team/Blue-Team Exercise

Your instructor will plant a netcat listener on the lab VM before the exercise. Netcat (`nc`) can open a listening port that an attacker uses as a backdoor or command-and-control channel. Your `ss -tulnp` output in the report will show every listening port and the process behind it. An unexpected port — especially one bound to `0.0.0.0` on a non-standard port number and owned by a process named `nc` or similar — is your indicator of compromise. Document it in your findings as you would in a real incident.

Connecting to Incident-Response Procedures

The workflow you practice here maps directly to the detection and analysis and containment phases of the NIST SP 800-61 incident-response lifecycle. CySA+ candidates are expected to understand how scripted data collection supports timely analysis, reduces analyst error, and produces defensible documentation. The report file you generate is the beginning of an incident case file.

Key terms8

Volatile dataSystem information — such as running processes and active network connections — that exists only while the system is powered on and is lost when the machine reboots or shuts down.
Order of volatilityThe principle that incident responders should collect the most transient data first (RAM, then processes, then network state, then disk artifacts) to minimize evidence loss.
subprocess moduleA Python standard-library module that allows a script to launch external programs, capture their standard output and error streams, and check their return codes.
ss (socket statistics)A Linux command-line utility that displays information about network sockets, including listening ports, established connections, and the processes that own them; the modern replacement for netstat.
Triage scriptAn automated script run during the early stage of incident response to quickly collect key system state information and record it in a structured, time-stamped report.
UTC timestampA time value expressed in Coordinated Universal Time, used in forensic reports so that evidence collected across different time zones can be compared without ambiguity.
Indicator of compromise (IoC)An observable artifact on a system or network — such as an unexpected listening port or an unknown process — that suggests the system may have been compromised.
auth.logA Linux system log file (typically at /var/log/auth.log on Debian-based distributions) that records authentication events including successful and failed login attempts and sudo usage.
In class: Class discussion: review what volatile data is lost on reboot and why scripted collection order matters (RAM artifacts, then process list, then network, then disk), connecting to incident-response procedures from course 7 preview. · Lab exercise: students write a Python script using os, subprocess, and datetime that runs ps aux, ss -tulnp, who, and tail of auth.log, captures each output with a UTC timestamp, and writes everything to a structured plain-text report file named with the hostname and timestamp. · Red-team/blue-team scenario: instructor plants a netcat listener on a lab VM; students run their triage script against it and must identify the suspicious listening port in their report, demonstrating that the script captures actionable data.
Materials: Ubuntu lab VM (with planted netcat listener), Python 3 docs, ss/ps/who man pages, course lab guide chapter 10, CompTIA CySA+ objectives (incident response domain)
Check: Report file is produced with UTC timestamps, contains output from all four commands, and correctly includes the planted netcat listener in the network-connections section; all three criteria required to pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

CySA+ — Incident Response
1. Why should volatile data be collected before examining files on disk during an incident response?
Security+ — Threats, Attacks, and Vulnerabilities
2. A triage script runs `subprocess.run(["ss", "-tulnp"], capture_output=True, text=True)`. What is the primary security reason for passing the command as a list rather than a single string with `shell=True`?
CySA+ — Threat and Vulnerability Management
3. During the red-team/blue-team exercise a student's triage report shows a listening entry from `ss -tulnp` on an unexpected high-numbered port owned by a process named `nc`. What does this most likely indicate?
Security+ — Operations and Incident Response
4. Which Python module is used in the triage script to generate a UTC timestamp for the report filename?
CySA+ — Incident Response
5. A student's triage script completes but the `auth.log` section of the report is blank. What is the most likely cause?
CySA+ — Incident Response
6. Which of the following NIST SP 800-61 incident-response lifecycle phases does scripted volatile-data collection most directly support?
Security+ — Governance, Risk, and Compliance
7. What ethical and legal requirement must be met before running a triage script on any system?
CySA+ — Incident Response
8. A student names their triage report file `triage_2025-06-01.txt` for every run on every host. What problem does this naming scheme create?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.