Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 80 ยท Security Operations and Incident Response

Digital Forensics Basics

Learn the basics of digital forensics: order of volatility, evidence handling, chain of custody, file hashes and building an event timeline in Python.

When an incident happens, two questions follow quickly: what exactly occurred? and can we prove it? Digital forensics is the discipline of collecting, preserving and analyzing digital evidence so that those questions can be answered reliably. The answers may be used to fix the problem, to file an insurance claim, to discipline an employee or, in the worst case, to support a court case.

This lesson covers the habits that make evidence trustworthy: collecting the most fragile data first, never working on the original, recording who touched what, proving with hashes that nothing changed, and merging many sources into one timeline. It builds on The Incident Response Process and on hashing from Hash Functions and Integrity. All data in the demos is made up.

Forensics on a system you do not own or are not authorized to examine can be illegal. In a company, ask legal counsel or the incident lead before you collect anything, especially from personal devices.

Principles of good forensics

  1. Preserve the original. Work on copies. Anything you do to the original, even opening a file, can change timestamps.
  2. Document everything. Write down what you did, when, with which tool and version, and why. Another examiner should be able to repeat your steps and reach the same result.
  3. Prove integrity. Compute cryptographic hashes of evidence when you collect it and verify them whenever it moves.
  4. Keep a chain of custody. A record of every person who handled the evidence, and when.
  5. Stay objective. Report what the evidence shows, including facts that do not fit your first theory.

Order of volatility

Some data disappears when power is lost or time passes. Collect the most volatile first.

flowchart TD
    A["CPU registers and cache"] --> B["Memory (RAM): processes,<br/>connections, keys"]
    B --> C["Network state and routing tables"]
    C --> D["Running processes and logged-in users"]
    D --> E["Disk contents and file system"]
    E --> F["Remote logs and monitoring data"]
    F --> G["Backups and archives"]

From most volatile (top) to least volatile (bottom). Evidence collection normally follows this order.

Memory is the classic example. It can hold decrypted data, encryption keys, command lines and network connections that exist nowhere on disk. A machine that is powered off for "safety" loses all of it. That is why the response plan often says isolate from the network, but keep it running until a forensic lead decides.

Collecting and preserving evidence

For a disk or a file set the typical steps are:

  • Photograph and label the device; note its state, serial numbers and location.
  • Create a forensic image, a bit-for-bit copy of the storage, using a tool that cannot write to the original. Hardware or software write blockers prevent accidental changes.
  • Hash the original and the image and check that they match.
  • Store the original safely (sealed, access-controlled) and analyze only the image copy.
  • Record all of this.

A hash is a fingerprint. If even one bit of the evidence changes, the SHA-256 digest changes completely (the avalanche effect from the hashing lesson). The demo below hashes an "image" held in memory, then shows what a single flipped byte does.

Python Hash evidence and detect a change

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.

Hash the evidence the moment you collect it, record the digest in your notes (ideally also in a second place), and re-check it every time the file is copied or moved. Remember that MD5 and SHA-1 are no longer collision resistant, so use SHA-256 or better for new work, as explained in the hashing lesson.

Chain of custody

The chain of custody is a documented history showing who collected the evidence, where it was stored and who handled it, with dates and reasons. If there is a gap, the other side in a dispute can argue that the evidence might have been altered. A custody form usually lists: a unique item ID, a description, hash values, the collector, the date and time, and every transfer with signatures.

You can make a digital custody log tamper-evident by chaining the entries with hashes: each entry stores the hash of the previous one, so changing an old entry breaks every later link. This is the same idea as a blockchain, used on a tiny scale.

Python A tamper-evident chain-of-custody log

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.

A real custody log also needs a trusted place to keep it, because an attacker who can rewrite every entry and recompute every hash defeats this scheme. Practical systems publish the latest hash somewhere the editor cannot reach (a signed message, a separate system), which is why the lesson on HMAC and Message Authentication matters: a keyed MAC or a digital signature prevents recomputation by someone who lacks the key.

What examiners look for

Evidence comes in many shapes. Common sources are:

  • Log files from servers, applications, firewalls and identity providers.
  • File system metadata: names, sizes, and the MAC times (modified, accessed, created/changed). These tell when files appeared and were touched.
  • Memory captures: running processes, network connections, injected code.
  • Browser and email artifacts: history, downloads, attachments.
  • Network captures and flow records: who talked to whom.
  • Cloud audit logs: API calls, console logins, permission changes.

Deleted files are often still recoverable until their space is reused, and attackers sometimes alter timestamps ("timestomping") to hide, which is why examiners compare several independent sources instead of trusting one.

Building a timeline

A timeline puts events from many sources in a single chronological list. It is usually the most useful forensic product: it shows the order of the attacker's steps and reveals gaps. Building one has a few traps:

  • Time zones. Log A might be in UTC, log B in local time. Convert everything to UTC before sorting.
  • Clock skew. Machines drift. Note the known offset of each source and correct for it.
  • Formats. Timestamps come as text, epoch seconds or other forms. Normalize them first.

The demo merges three fabricated sources: a web server log in UTC, a Windows-style event log in local time (UTC+2), and file times in epoch seconds. Fixed offsets are used so no time-zone database is needed.

Python Merge three log sources into one UTC 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.

Read the merged list as a story: a login, an export of customer data, a new account on the server, an archiving tool, an archive file. No single log tells it. Had the local-time entries been sorted as if they were UTC, the story would have looked like the account was created two hours after the archive. Time-zone mistakes are one of the most common causes of wrong conclusions in investigations.

Reporting

A good forensic report is written for its reader (executives, lawyers, engineers). It states the question, the evidence examined (with hashes), the methods used, the findings and their confidence, and the limits of the analysis. Separate facts ("the file was created at 09:13 UTC") from interpretation ("this suggests data staging") and say how sure you are.

Evidence collection phases, acquisition choices and data carving

The steps above describe the collection of one item. A whole investigation is usually organized into five phases, and the choices you make during acquisition decide what evidence you can prove later.

The five phases

  1. Identification: recognize that an incident needs investigation and decide which systems, accounts and devices might hold evidence. Define scope and legal authority.
  2. Preservation: stop evidence from changing. Isolate devices, suspend log rotation, secure the scene, and begin the chain of custody.
  3. Collection (acquisition): copy the data in a forensically sound way, starting with the most volatile items.
  4. Analysis: examine the copies: recover files, parse artifacts, search, correlate and build the timeline.
  5. Reporting and presentation: document what was found, how, with which tools and limits, so another examiner could repeat it.

Acquisition choices

  • Live acquisition (the system is running): captures memory, network connections and running processes, and avoids losing data that is encrypted when the machine is switched off. It necessarily changes the system a little, so record every command you run and when.
  • Dead acquisition (the system is off): the storage is removed or booted from trusted media and imaged through a write blocker. It changes nothing on the disk, but memory is gone, and a full-disk-encrypted drive may be unreadable without the key.
  • Logical acquisition copies the files and folders the system presents. It is quick, but it misses deleted data and unallocated space.
  • Physical (bit-for-bit) acquisition copies every sector of the storage, including free space where traces of deleted files may remain.
  • Targeted collection gathers only specific files, mailboxes or cloud data, which is often appropriate when the whole disk is too large or contains unrelated private data.

After imaging, verify the copy by comparing hashes of the source and the image, and record the tool name, version, time and operator. A common pattern is to compute two hashes (for example SHA-256 and a second algorithm) so a theoretical collision in one does not undermine the proof.

Why SSDs and TRIM change the rules

On an old magnetic disk, deleting a file marks its space as free, and the bytes often stay until something overwrites them. Solid-state drives behave differently. Flash memory must be erased before it is rewritten, so the drive's controller runs garbage collection in the background, and the operating system sends TRIM commands telling the drive which blocks no longer hold live data. The drive may then erase those blocks at any time, even while the machine sits switched on, and reads of trimmed blocks commonly return zeros. Wear levelling also moves data around internally. The practical result for examiners is that deleted data on an SSD is often already gone, powering a drive on can change evidence, and you should image it promptly and document the model and TRIM behaviour. Absence of recovered data on an SSD is weaker evidence than on a hard disk.

Data carving

When file system records are damaged or deleted, examiners can still search raw bytes for file signatures: files of many types begin with a fixed pattern ("magic bytes") and sometimes end with one. A JPEG image starts with FF D8 FF and ends with FF D9; a PNG starts with 89 50 4E 47; a PDF starts with %PDF. Data carving scans unallocated space or an image for these patterns and cuts out the bytes between them, without needing the file system. Limits: fragmented files are not recovered whole, carved files have no names or timestamps, and false positives occur, so results must be validated.

The demo builds a small synthetic "disk" with two embedded image-like files and some noise, then carves them by signature. The "files" are plain bytes, not real pictures.

Python Carve files from raw bytes by signature

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.

Real carvers add maximum-length rules, content checks (does the data actually decode as an image?) and support hundreds of formats. Treat carved output as leads that need corroboration.

Forensic imaging with a disk-imaging tool

The core technique of disk forensics is to image a drive, not to browse it. A graphical imaging tool such as FTK Imager (free for Windows) shows the workflow.

  1. Connect the evidence drive through a hardware or software write blocker, so nothing is written to it.
  2. Choose the source (physical drive, logical volume or folder) and a destination on a separate, clean disk.
  3. Choose an image format: raw (dd-style, widest compatibility) or E01 (compressed, with case metadata and built-in hashes).
  4. Enter case number, evidence number and examiner name, then start the capture.
  5. After the capture, verify: the tool computes a hash (MD5 and SHA-1, or SHA-256) of the source and the image; they must match, and the hash goes into the chain-of-custody record.
  6. Work only on copies of the image, never on the original.

Limitations: imaging a live system changes it; encrypted drives need keys captured while running; SSDs with TRIM and wear-levelling can destroy deleted data on their own; huge disks take hours; and the tool only captures what the drive exposes (hidden areas or remapped sectors may need special hardware).

Remediation planning after an attack

After containment, a remediation plan turns lessons into changes: remove attacker persistence, reset every credential the attacker may have seen, patch the entry point, rebuild affected hosts from known-good images rather than "cleaning" them, and add detection for the technique used. Include man-in-the-browser (MITB) malware in your threat list when the incident involved banking or admin sessions: it lives in the browser and alters pages and transactions after login, so it defeats passwords and some one-time codes. The usual fixes are transaction signing, hardware tokens, up-to-date browsers and endpoint detection. Close the plan with owners, dates and a verification test.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. According to the order of volatility, which should you collect first?

    Memory and live network state vanish at shutdown or quickly change, while disks, remote logs and backups persist, so the most fragile data is collected first.
  2. 2. Why do examiners work on a forensic image instead of the original disk?

    Preserving the original unchanged keeps the evidence reliable. All analysis happens on verified copies.
  3. 3. What does a matching SHA-256 hash of a file before and after transfer show?

    A hash only demonstrates integrity: the bytes are identical. It says nothing about who created the file or whether it is malicious.
  4. 4. Which timeline mistake can make events appear in the wrong order?

    Different sources use different zones and clocks. Convert everything to one reference (UTC) and correct known skew before sorting.

Summary

  • Forensics aims at reliable, repeatable and documented answers; preserve originals and work on verified copies.
  • Collect in order of volatility: memory and network state first, then disks and logs.
  • Hashes prove that evidence did not change; a chain of custody records who handled it and when.
  • Tamper-evident logs chain entry hashes, and keys or signatures stop someone from recomputing them.
  • A UTC timeline built from several sources tells the story, but only if time zones, skew and formats are normalized.
  • Reports separate facts from interpretation and state limits.

Next: Vulnerability Management and Patching, the routine work that stops many incidents before they start.