Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Defence and Detection

Logging and Incident Response

What to log, how to build a timeline from events, and the incident response phases that follow an exploited host, with a Python timeline and triage stand-in.

Even good hardening will not stop everything. When an exploitation attempt succeeds, the quality of your logs decides whether you can answer "what happened, when, how far did it go, and is it over?" Incident response is the discipline of answering those questions calmly and in order.

This lesson covers what to log, how to turn scattered events into a timeline, and the standard response phases. It closes the chapter that began with Detecting Metasploit Activity.

What to log

Logs you do not collect before an incident are lost. A minimum set:

Source Why
Authentication (success and failure) Brute force, spraying, stolen credentials
Process creation with command lines Spawned shells, encoded commands
Network flows, DNS and proxy logs Callbacks, scanning, data leaving
Web server and WAF logs Exploit attempts against applications
Firewall and VPN logs Reachability and entry points
Cloud audit logs Changes to identities and resources
File-integrity and configuration changes Persistence and tampering

Send logs to a central, write-protected system. An attacker with admin rights on a host will try to wipe that host's local logs, so a remote copy is the only copy you can trust. Synchronise clocks (NTP) on every system, or timelines will not line up.

The incident response phases

flowchart LR
    P["Prepare"] --> D["Detect and analyse"]
    D --> C["Contain"]
    C --> E["Eradicate"]
    E --> R["Recover"]
    R --> L["Lessons learned"]
    L --> P

The cycle follows the widely used NIST incident handling model.

  • Prepare: contacts, tools, runbooks, logging, backups, practice drills.
  • Detect and analyse: confirm it is real, scope it, record everything.
  • Contain: stop the spread (isolate the host, disable accounts, block addresses) while preserving evidence.
  • Eradicate: remove the cause: patch the hole, remove persistence, reset credentials.
  • Recover: restore from known-good state, monitor closely.
  • Lessons learned: write up, fix the process gaps, update detections.

Isolate, do not power off, a suspected host when you can: memory holds evidence (running processes, network connections) that disappears on shutdown. Capture it first if your procedures allow.

Building a timeline

A timeline merges events from many sources into one ordered story. This stand-in merges invented web, firewall and endpoint events, sorts them, and prints the chain.

Python Merge sources into a timeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The merged story is far clearer than any single log: an upload, a shell spawned a second later, a callback to port 4444, then persistence. Each step maps to a containment action (block the address, remove the cron entry, patch the upload handler).

Triage questions

When an alert fires, answer in order:

  1. Is it real? (Check the evidence, not just the alert title.)
  2. Which systems and accounts are involved?
  3. What did the attacker access or change?
  4. Is it still happening?
  5. What is the business impact, and who must be told?
Python A simple triage scorer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Evidence handling

  • Write down who did what and when; keep notes timestamped.
  • Collect copies, not originals, and record hashes of evidence files.
  • Limit who can access evidence (chain of custody) if legal action is possible.
  • Involve legal counsel and follow breach-notification laws that apply to you.

After the incident

A blameless review focuses on process: which control should have stopped it, which log was missing, how long did detection take? Turn each answer into a task: a rule (Writing Detection Signatures), a hardening step (Hardening Against Exploitation) or a drill.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why send logs to a central, protected system?

    A remote copy survives tampering on the compromised host.
  2. 2. Which phase comes right after containment?

    After containing, you remove the cause: patch, remove persistence, reset credentials.
  3. 3. Why synchronise clocks with NTP?

    Accurate, consistent timestamps are essential for correlating logs.
  4. 4. Why prefer isolating over powering off a suspect host?

    Running processes and connections exist only in memory.

Summary

  • Log authentication, processes, network, web and cloud activity centrally with synced clocks.
  • Follow the cycle: prepare, detect and analyse, contain, eradicate, recover, learn.
  • A merged timeline turns scattered events into a story.
  • Triage by asset value, confirmation and data exposure.
  • Preserve evidence and feed lessons back into detection and hardening.

Next: Law, Ethics and Authorization