Kahibaro
Discord Login Register

Log and file recovery

Understanding Goals and Constraints

In incident response, “log and file recovery” has two distinct but related goals:

  1. Recovering evidence that was deleted, tampered with, rotated, or partially overwritten (forensic focus).
  2. Recovering operational data (e.g., configuration files, application data) to restore service or reconstruct events (response focus).

At this stage, you should:

This chapter focuses on how to recover and reconstruct logs and files, not on the overall incident workflow or general evidence collection (covered elsewhere in the parent section).

Types of Data Relevant to Recovery

For log and file recovery, you will primarily deal with:

Each has different recovery strategies and success probabilities depending on filesystem, time elapsed, and system activity.

Immediate Preservation Measures

Before attempting recovery:

    dc3dd if=/dev/sdX of=/evidence/disk.img hash=sha256 log=/evidence/dc3dd.log

All recovery work should be done on the image, not on the original disk.

Sources of Log Data Beyond Obvious Files

Attackers frequently try to clean or modify visible logs (e.g., auth.log), but logs or equivalents may exist elsewhere:

Before deep recovery, inventory all obvious and secondary log locations; many “lost logs” turn out to exist in a rotated or alternate location.

Recovering Rotated and Compressed Logs

Log rotation is not adversarial; it’s routine. But it can hide or fragment the timeline.

Identifying Rotated Logs

  ls -1 /var/log | egrep '(.1|.2|.gz|.xz|[0-9]{8})$'

Decompressing and Viewing

Common formats:

  zcat /var/log/auth.log.2.gz | less
  xzcat /var/log/journal-20251210.xz | less
  bzcat /var/log/someapp.log.3.bz2 | less

Use zgrep, xzgrep, etc. to filter without full decompression.

Reconstructing Log Timelines

To rebuild a continuous timeline:

  1. Order files by rotation policy, usually oldest has highest suffix (e.g., .7.gz).
  2. Concatenate in correct order:
   zcat /var/log/auth.log.7.gz /var/log/auth.log.6.gz ... \
        | cat - /var/log/auth.log.1 /var/log/auth.log \
        > /tmp/auth_full_timeline.log
  1. Adjust for overlapping or missing time windows:
    • Verify continuity with timestamps.
    • Note explicit gaps where compression or truncation removed data.

Be explicit in your notes where logs are missing due to rotation; do not silently infer actions in missing periods.

Recovering `systemd` Journals

If the system uses systemd journaling:

Viewing and Exporting

  journalctl --no-pager --output=short-iso > /tmp/journal_full.log
  journalctl -u ssh.service --since "2025-12-10 00:00" \
             --until "2025-12-11 00:00" > ssh_journal.log

Recovering Deleted or Truncated Journal Entries

If journalctl shows gaps or journals appear wiped:

Direct manual repair of journal binary files is non-trivial; focus on preservation and extraction rather than editing.

File Recovery from Filesystem Artifacts

When logs or other files were deleted (whether intentionally or due to normal rotation), recovery depends heavily on the filesystem.

EXT Family (ext2/ext3/ext4)

Common tools:

Workflow Example (ext4, using `extundelete`)

  1. Work on an image:
   losetup /dev/loop0 /evidence/disk.img
   mkdir /mnt/recovered
  1. Identify partition with fdisk -l /dev/loop0 (assume /dev/loop0p2).
  2. Run extundelete:
   extundelete /dev/loop0p2 --restore-file var/log/auth.log \
              --output-dir /mnt/recovered
  1. If path unknown, try:
   extundelete /dev/loop0p2 --restore-all --output-dir /mnt/recovered

Results may be partial if blocks are overwritten.

XFS/Btrfs/Other Filesystems

Each filesystem has different recovery tools and limitations:

In many non-ext cases, snapshots are more productive than raw undelete attempts.

File Carving for Logs and Text

When filesystem-level undelete fails, you can attempt file carving from unallocated space or raw images.

Basic Principles

Tools

Using Photorec

  1. Run on the image:
   photorec /log /d /mnt/recovered /cmd /evidence/disk.img options,search

(Interactive mode is typical; configure to search for log, txt, gz.)

  1. After carving:
    • Use file command to identify recovered files.
    • Use grep, zgrep to search for relevant event patterns.

Carving for Text-Based Logs

    grep -R "suspicious_user" /mnt/recovered | head

Remember that carved logs may not retain original names or paths; correlate with contents and timestamps where possible.

Recovering from Snapshots and Backups

Modern Linux systems frequently use snapshotting or regular backups. These can be invaluable when logs or files have been cleaned.

LVM Snapshots

If the system used LVM and snapshots existed before the incident:

  lvs
  lvcreate -s -n forensic_snap -L 10G /dev/vg0/root
  mount -o ro /dev/vg0/forensic_snap /mnt/forensic
  cp -a /mnt/forensic/var/log /analysis/logs_preincident

If historical snapshots are gone, this may not help, but on some systems, backup LVs or older snapshots persist.

Btrfs Snapshots

  btrfs subvolume list /
  btrfs subvolume show /.snapshots/123/snapshot
  mount -o ro /.snapshots/123/snapshot /mnt/snap123

Copy logs or configuration from these snapshots for comparison.

ZFS Snapshots

  zfs list -t snapshot
  zfs clone pool/root@pre-incident pool/forensics
  mount -o ro /pool/forensics /mnt/forensics

Traditional Backups

Backups might be:

For logs:

Reconstruction When Logs Are Missing or Tampered

Often, you will not fully “recover” logs, but rather reconstruct events from partial artifacts.

Cross-Correlation of Multiple Sources

If /var/log/auth.log is wiped, consider:

A single event (like an SSH login) usually leaves traces in several locations; one may be intact even if another is removed.

Using Metadata as Evidence

Even without file contents, metadata can indicate activity:

Detecting and Handling Log Tampering

Indicators:

For reconstruction:

Recovering Configuration and Application Files

Non-log files often matter as much as logs:

To recover:

Recovered versions can be used to:

Handling Partially Overwritten or Fragmented Files

Sometimes logs or files are only partially available:

Strategies:

Integrity, Hashing, and Documentation

All recovered content must be handled with forensic rigor.

Hashing Recovered Files

  sha256sum /mnt/recovered/auth.log > auth.log.sha256
  find /mnt/recovered -type f -exec sha256sum {} \; > recovered_hashes.txt

These hashes let you:

Documenting Recovery Steps

Maintain a clear record of:

Use a read-only operations log (e.g., plain text file under version control) and include it with the case artifacts.

Common Pitfalls and How to Avoid Them

Putting It Together: Practical Mini-Workflow

As a concise example for log and file recovery during an incident:

  1. Preserve:
    • Acquire a disk image with hashing.
    • Mount image read-only.
  2. Inventory existing logs and configs:
    • Plain logs, rotated/compressed, journals, application logs.
  3. Consolidate:
    • Decompress and merge rotated logs into full timelines.
    • Export systemd journal segments.
  4. Recover:
    • Attempt undelete (filesystem-appropriate tools) for key logs and config files.
    • Use snapshots or backups to retrieve earlier states.
    • Apply file carving to unallocated space for additional fragments.
  5. Correlate:
    • Combine logs, recovered files, metadata, and off-system sources to build an event timeline.
    • Mark explicit gaps or tampering suspicions.
  6. Verify and document:
    • Hash recovered artifacts.
    • Record commands and tools used.
    • Preserve both raw recovered files and any derived analysis products (e.g., combined timelines).

This structured approach maximizes the chance of recovering useful logs and files while maintaining forensic soundness.

Views: 21

Comments

Please login to add a comment.

Don't have an account? Register now!