Skip to main content
Linux Forensics: Logs, Bash History, Persistence & Timelines
Lesson 2 of 21

Linux Forensics: Logs, Bash History, Persistence & Timelines

Investigate compromised Linux hosts by analyzing systemd journals, authentication logs, shell history, persistence locations, and ext4 MACB filesystem timelines.

Advanced 28 min Reverse engineering
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Query offline systemd journals and authentication logs to reconstruct SSH and privilege-escalation activity
  • Evaluate Bash history and related session artifacts while accounting for evasion and buffering limits
  • Inspect common Linux persistence locations including systemd units, cron directories, and SSH authorized_keys
  • Detect timestamp tampering with stat and build a filesystem timeline using Sleuth Kit fls and mactime

Recommended first: Disk & Deleted-File Forensics with Autopsy & Sleuth KitWindows Forensics: Registry, Prefetch, Amcache & $MFT

#Why this matters

Linux hosts power web servers, cloud workloads, and network appliances, yet they lack a centralized Registry or Prefetch cache. Instead, evidence is distributed across plain-text and binary logs, per-user shell files, service definitions, and ext4 inode metadata.

Because intruders on Linux routinely clear /var/log/auth.log or unset shell history, responders must know how to query binary systemd journals offline, spot hidden persistence entries, and detect timestamp forgery using inode ctime values and Sleuth Kit timelines.

  1. Logsjournald, auth.log, wtmp/btmp
  2. Shell.bash_history and rc files
  3. Persistencesystemd, cron, authorized_keys
  4. Timelinestat and Sleuth Kit mactime

#System and authentication logs

Modern Linux distributions (Debian, Ubuntu, RHEL, AlmaLinux) run systemd-journald alongside or instead of traditional text logs in /var/log/. Knowing both formats prevents missed evidence when one log stream is rotated or truncated.

Artifact Path What it records
Auth log /var/log/auth.log or /var/log/secure SSH logins, sudo invocations, su, PAM failures
Journald store /var/log/journal/<machine-id>/ Structured binary logs for kernel, systemd units, and services
Login records /var/log/wtmp and /var/log/btmp Successful (last) and failed (lastb) login sessions
Web/app logs /var/log/nginx/ or /var/log/apache2/ HTTP requests, source IPs, user agents, and status codes

You can inspect a mounted forensic image's binary journal directly from an analysis workstation without booting the suspect system by passing -D (or --directory) and --utc to journalctl.

Querying an offline journal directory in UTC
$ journalctl -D /mnt/evidence/var/log/journal --utc -u ssh.service --since "2025-10-18 01:00:00"
Oct 18 01:14:22 app01 sshd[1842]: Accepted publickey for deploy from 198.51.100.44 port 51204 ssh2
Oct 18 01:15:09 app01 sudo[1890]: deploy : TTY=pts/0 ; PWD=/home/deploy ; USER=root ; COMMAND=/usr/bin/systemctl daemon-reload

Binary login files should also be read offline with last -f /mnt/evidence/var/log/wtmp and lastb -f /mnt/evidence/var/log/btmp. If an attacker deleted auth.log but forgot wtmp or the binary journal, those entries still confirm source IP addresses and session durations.

#Shell history and session context

Interactive Bash sessions record commands in ~/.bash_history, usually capped by HISTSIZE and HISTFILESIZE. However, Bash normally holds commands in memory until the shell exits cleanly. If a session is killed, still open, or deliberately ended with kill -9 $$, recent commands never reach disk.

Attackers also use simple evasion tricks such as export HISTFILE=/dev/null, unset HISTFILE, or leading spaces when HISTCONTROL=ignorespace is set. Look for gaps in command context and corroborate with secondary user artifacts:

  • ~/.lesshst and ~/.viminfo (or ~/.local/state/nvim/shada/), which record files viewed or edited.
  • ~/.ssh/known_hosts and ~/.ssh/authorized_keys, which expose outbound lateral movement and inbound backdoor keys.
  • Shell startup scripts (~/.bashrc, ~/.profile) modified to alter PATH or run commands on login.

#Persistence mechanisms

When an intruder establishes persistence on Linux, they typically target locations executed automatically at boot, on a schedule, or upon user authentication:

  1. Systemd services and timers: Custom .service or .timer files placed in /etc/systemd/system/ override vendor units in /lib/systemd/system/. Inspect ExecStart lines and recently modified unit files.
  2. Cron jobs: Check /etc/crontab, /etc/cron.d/, /etc/cron.hourly/, /etc/cron.daily/, and per-user crontabs in /var/spool/cron/crontabs/ (or /var/spool/cron/).
  3. SSH authorized keys: An unexpected public key appended to /root/.ssh/authorized_keys or a service account's authorized_keys grants persistent remote access without passwords.
  4. Dynamic linker hijacking: An entry in /etc/ld.so.preload forces every dynamically linked binary to load a specified shared library.

#Ext4 MACB timestamps and Sleuth Kit timelines

On ext4 filesystems, stat exposes four key timestamps for every file:

  • Access (atime): Last read time (often restricted by relatime mount options).
  • Modify (mtime): Last time the file's data content changed.
  • Change (ctime): Last time the inode metadata (permissions, ownership, link count, or timestamps) changed.
  • Birth (crtime): Original inode creation time recorded on ext4.

An attacker can forge atime and mtime using touch -d to disguise a newly dropped script as an old system file. However, user-space tools cannot set ctime or crtime backward on a normal mounted filesystem; altering mtime updates ctime to the current kernel clock.

Detecting timestamp manipulation with stat on a mounted read-only image
$ stat /mnt/evidence/usr/local/bin/healthcheck.sh
  File: /mnt/evidence/usr/local/bin/healthcheck.sh
  Size: 642       	Blocks: 8          IO Block: 4096   regular file
Access: 2021-04-02 10:00:00.000000000 +0000
Modify: 2021-04-02 10:00:00.000000000 +0000
Change: 2025-10-18 01:14:55.412890112 +0000
 Birth: 2025-10-18 01:14:55.408112004 +0000

Here, Modify reads 2021-04-02, but both Change and Birth expose the true creation and tampering time of 2025-10-18 01:14:55 UTC.

To reconstruct activity across the entire partition, including unallocated deleted files, extract a Sleuth Kit bodyfile with fls and sort it with mactime:

Generating a filesystem timeline with Sleuth Kit
$ fls -r -m / /cases/app01/root.dd > /cases/app01/bodyfile.txt
$ mactime -b /cases/app01/bodyfile.txt -d -z UTC 2025-10-18 > /cases/app01/timeline.csv
$ head -n 5 /cases/app01/timeline.csv
Date,Size,Type,Mode,UID,GID,Meta,File Name
Sat Oct 18 2025 01:14:55,642,.c.b,-rwxr-xr-x,0,0,131402,/usr/local/bin/healthcheck.sh
Sat Oct 18 2025 01:15:02,318,m.cb,-rw-r--r--,0,0,131409,/etc/systemd/system/healthcheck.service

The Type flags (m Modify, a Access, c Change, b Birth) immediately show healthcheck.sh having only c and b at 01:14:55 (because m was backdated to 2021), followed seven seconds later by creation of /etc/systemd/system/healthcheck.service.

#Common mistakes and analyst pitfalls

  • Contaminating live timestamps: Mounting a forensic image read-write or running interactive searches on a live host alters atime and overwrites shell history buffers. Always mount evidence images with -o ro,noexec,noload (or parse images directly with Sleuth Kit).
  • Relying on ls -l alone: ls -l shows mtime by default, which is trivially spoofed with touch. Always inspect ctime and Birth via stat or mactime.
  • Ignoring local vs UTC differences: mactime -d defaults to the examiner workstation's timezone unless -z UTC is specified, causing off-by-hours errors when correlating against UTC logs.
  • Overlooking deleted files in unallocated space: Plain stat only sees active files; running fls -r -m / against the raw disk image includes deleted entries still referenced in unallocated inodes.

#Summary

  • Linux evidence spans systemd-journald binary logs, text logs (/var/log/auth.log or secure), binary login records (wtmp/btmp), shell artifacts, and persistence directories.
  • Attackers can suppress ~/.bash_history or alter mtime with touch, so investigators rely on journalctl -D, secondary artifacts like authorized_keys, and ext4 ctime/Birth metadata.
  • Sleuth Kit fls and mactime combine active and deleted inode timestamps into a single chronological timeline normalized to UTC.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge