Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 53 ยท Defense, Detection and Response

Incident Response and Digital Forensics Basics

Learn the incident response lifecycle, containment choices, evidence handling, chain of custody, timelines and hashing, with runnable Node stand-ins.

Sooner or later a defended system is attacked successfully, or an employee clicks something they should not. What matters then is how calmly and quickly the organization can find out what happened, stop it, recover, and learn. Incident response (IR) is the planned way of doing that. Digital forensics is the careful collection and analysis of evidence that supports it, in a way that keeps the evidence trustworthy.

This lesson explains the phases of a response, the choices behind containment, how to handle evidence, and how to build a timeline. Real incident work happens on real systems and cannot run here, so the checklists and commands are read-only. The runnable parts are Node stand-ins: a timeline builder and an evidence-hashing script working on made-up data in memory. They show the idea, not a forensic toolkit. Detection, the step before all of this, is covered in Logging, Monitoring and SIEM Basics.

The incident response cycle

Prepare Plans, contacts and tools ready.
Detect and analyze Is it real? How big is it?
Contain and eradicate Stop the spread, remove the cause.
Recover and learn Restore service, fix the gaps.

The last step feeds the first. Every incident improves the plan.

1 / 2

Phases of incident response

Most frameworks (NIST and SANS publish well-known ones) describe the same loop with slightly different names.

  1. Preparation. Done long before any incident. Write the plan, name an incident lead and a deputy, keep an out-of-band contact list (email may be compromised), set up logging, backups and a clean workstation for analysts, and rehearse with tabletop exercises.
  2. Detection and analysis. An alert, a user report or an outside notice arrives. Decide whether it is a real incident, what systems and data are affected, and how severe it is. Start a written log of every action and its time right now.
  3. Containment. Limit the damage without destroying evidence.
  4. Eradication. Remove the cause: malware, stolen credentials, the exploited vulnerability, rogue accounts.
  5. Recovery. Restore systems from known-good sources, watch closely for signs the attacker returns, and return to normal operation.
  6. Lessons learned. A blameless review within days: what happened, what worked, what did not, what changes. Update the plan, rules and training.
flowchart LR
    P["Prepare"] --> D["Detect and analyze"]
    D --> C["Contain"]
    C --> E["Eradicate"]
    E --> R["Recover"]
    R --> L["Lessons learned"]
    L --> P
    D -. "new facts" .-> D

In practice analysis keeps running during containment, because you learn more as you go.

Containment decisions

Containment is a trade-off between stopping the attacker and keeping the business running and the evidence intact. Short-term options include:

  • Isolate a host from the network (disable its switch port or apply a quarantine rule) while leaving it powered on, so memory contents survive for analysis.
  • Disable or reset accounts and revoke tokens, keys and sessions that may be stolen.
  • Block malicious addresses and domains at the firewall or DNS.
  • Take a service offline when continuing would cause more harm.

Pulling the power plug is sometimes the right call for fast-spreading destructive malware, but it loses volatile data such as running processes and network connections. Decide deliberately, write the decision down, and involve the people who own the system. After containment, check that the attacker did not already move on to other machines, as described in Persistence, Lateral Movement and Detection.

Only respond on systems you are authorized to handle. Analysts have been fired, or worse, for "helping" on machines that belonged to someone else. Follow your organization's legal and privacy rules, and bring in legal counsel early when personal data may be involved.

Handling evidence

Evidence can matter for insurance, regulators, employment action or court. If you cannot show it was not altered, it loses value. The core habits:

  • Order of volatility. Collect what disappears first: memory, running processes and network connections, then temporary files, then disk contents, then logs and backups.
  • Work on copies. Take a forensic image of a disk, and analyze the copy. Use a write blocker on physical media so the act of reading does not change it.
  • Hash everything. Compute a cryptographic hash (SHA-256) of the original and of each copy. If the hashes match, the copies are bit-for-bit identical, and a later match proves nothing changed.
  • Chain of custody. Keep a record for each item: what it is, who collected it, when and where, how it was stored, and every transfer between people. Gaps in the record weaken the evidence.
  • Store safely. Use tamper-evident bags or access-controlled storage, and keep the originals untouched.
  • Document as you go. Notes with time, command and result. Screenshots help but notes are better.

Typical read-only commands on a Linux analysis machine look like this, run against an image you have the right to examine:

Terminal
# Read-only example: fingerprint an evidence file and record the result
sha256sum disk-image.dd | tee disk-image.dd.sha256
# Later, verify the evidence is unchanged
sha256sum -c disk-image.dd.sha256

Building a timeline

A timeline puts events from many sources in one order: firewall logs, authentication records, file modification times, email gateway entries and user reports. It turns scattered facts into a story and helps answer "when did it start?", "what was reached?" and "is it over?". Two cautions:

  • Convert everything to UTC and note the clock offset of each source. A server five minutes slow can make cause appear after effect.
  • A timeline shows what is known. Mark guesses as guesses and revisit them as evidence grows.
Node.js Merge sources into one 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 node:readline or process.stdin), it gets the next line from this box.

What is real? Merging by normalized UTC time and measuring offsets from the first event is exactly the method analysts use. The three arrays stand in for log exports, and real tools handle millions of rows and many formats.

If you removed the time-zone conversion, the mail event would appear to happen after everything else, and the story would be wrong. Always normalize first.

Stand-in: hashing evidence and spotting tampering

This example creates a few "evidence files" in memory, computes SHA-256 fingerprints, records them in a custody log, then simulates one byte being changed and shows that verification fails.

Node.js Hash evidence and verify custody

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 node:readline or process.stdin), it gets the next line from this box.

What is real? SHA-256 and the compare-after-copy workflow are real, and a one-bit change really does produce a completely different digest. The in-memory buffers and the simple array standing in for a custody form are not: real custody records are signed documents or case-management entries, and real images come from disks or memory captures.

After the incident

The lessons-learned meeting is where the value is. Keep it blameless, factual and short. Typical outputs: a final timeline, the root cause (for example an unpatched server plus a reused password), what detection could have caught it earlier, and a dated action list with owners. Share a sanitized summary widely. Then feed the findings back into secure development, monitoring rules and training, because that is what makes the next incident smaller.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do analysts hash evidence files with SHA-256?

    A hash is a fingerprint. If it matches the recorded value, the data is identical. Any change produces a different hash.
  2. 2. Which evidence is usually collected first, according to the order of volatility?

    Volatile data vanishes when a machine is powered off or rebooted, so it is captured before more durable data.
  3. 3. What is the purpose of a chain of custody record?

    Without an unbroken record of handling, it is hard to show the evidence was not altered.
  4. 4. During analysis, a firewall log is in UTC and a mail log is in local time with an offset. What should you do before merging them?

    Mixed time zones produce a misleading order of events. Convert to one standard and note each source's clock error.

Summary

  • Incident response is a loop: prepare, detect and analyze, contain, eradicate, recover, learn.
  • Containment balances stopping the attacker against keeping evidence and service. Record every decision.
  • Preserve evidence by copying it, hashing it, and keeping a chain of custody record.
  • Build timelines in UTC and separate known facts from guesses.
  • A blameless lessons-learned review turns each incident into better defenses.

Next: Secure Coding and DevSecOps.