| name | linux-forensics |
| description | Perform Linux digital forensics investigations. Use this skill whenever the user needs to investigate a Linux system for security incidents, malware, unauthorized access, or suspicious activity. This includes gathering system information, analyzing logs, checking for persistence mechanisms, examining file systems, recovering deleted files, and documenting findings. Trigger on requests involving Linux forensics, incident response, malware investigation, system compromise analysis, or security auditing of Linux systems. |
Linux Forensics Investigation
A systematic approach to investigating Linux systems for security incidents, malware, and unauthorized access.
Investigation Workflow
Follow this sequence to ensure comprehensive coverage:
- Preserve evidence - Set up clean environment, avoid modifying the target
- Gather initial information - System state, processes, network connections
- Capture memory (if live) - Use LiME before shutdown
- Create disk image - Forensic copy with hash verification
- Analyze artifacts - Logs, autostart locations, user accounts, file system
- Document findings - Timeline, suspicious indicators, evidence chain
1. Initial Information Gathering
Set Up Clean Environment
Before touching the target system, prepare a trusted environment:
export PATH=/mnt/usb/bin:/mnt/usb/sbin
export LD_LIBRARY_PATH=/mnt/usb/lib:/mnt/usb/lib64
Collect System State
Run these commands to capture the current system state:
date
uname -a
ifconfig -a || ip a
netstat -anp
netstat -rn; route
ps -ef
lsof -V
df; mount
free
w
last -Faiwx
lsmod
cat /etc/passwd
cat /etc/shadow
find / -type f -mtime -1 -print 2>/dev/null
Red Flags to Watch For
- Root processes with high PIDs - Normal root processes have low PIDs
- Users without shells in /etc/passwd - Check for password hashes in /etc/shadow
- Unexpected network connections - Especially outbound to unknown IPs
- Modified system binaries - Compare against known-good versions
2. Memory Acquisition (Live Systems)
Using LiME
LiME (Linux Memory Extractor) captures volatile memory:
apt-get install lime-forensics-dkms
make -C /lib/modules/$(uname -r)/build M=$PWD
sudo insmod lime.ko "path=/path/to/mem_dump.bin format=lime"
Important: Do not install anything on the victim machine if possible. Use a USB with pre-compiled LiME matching the kernel version.
LiME Formats
- raw - Concatenated segments
- padded - Raw with zero-padding
- lime - Recommended, includes metadata
Network Transfer
sudo insmod lime.ko "path=tcp:4444 format=lime"
3. Disk Imaging
Shutdown Strategy
- Normal shutdown - Allows filesystem sync but malware may destroy evidence
- Pull the plug - Risk of data loss but prevents malware cleanup
If malware suspected: Run sync then power off immediately.
Create Forensic Image
dd if=/dev/sdX of=/path/to/image.img bs=512
dcfldd if=/dev/sdX of=/path/to/image.img bs=512 hash=sha256 hashwindow=1M hashlog=/path/to/hashes.log
Always mount images as read-only to prevent modification.
4. Disk Image Analysis
Identify Image Type
file disk.img
img_stat -t evidence.img
img_stat -i list
Filesystem Information
fsstat -i raw -f ext4 disk.img
fls -i raw -f ext4 disk.img
fls -i raw -f ext4 disk.img <inode_number>
icat -i raw -f ext4 disk.img <inode_number>
5. Malware Detection
System File Integrity
rpm -Va
dpkg --verify
apt-get install debsums
debsums | grep -v "OK$"
Search Installed Programs
cat /var/lib/dpkg/status | grep -E "Package:|Status:"
cat /var/log/dpkg.log | grep installed
rpm -qa --root=/mntpath/var/lib/rpm
find /sbin/ -exec dpkg -S {} \; | grep "no path found"
find /sbin/ -exec rpm -qf {} \; | grep "is not"
find / -type f -executable 2>/dev/null | grep <pattern>
Check Common Installation Directories
ls /usr/local /opt /usr/sbin /usr/bin /bin /sbin
6. Recover Deleted Binaries
If a process was executed from a deleted file:
cd /proc/<PID>/
head -1 maps
dd if=mem bs=1 skip=<address> count=<size> of=/path/to/recovered
7. Autostart and Persistence Locations
Scheduled Tasks
cat /var/spool/cron/crontabs/*
cat /var/spool/cron/atjobs
cat /var/spool/anacron
cat /etc/cron*
cat /etc/at*
cat /etc/anacrontab
cat /etc/incron.d/*
cat /var/spool/incron/*
for d in /etc/cron.*; do [ -f "$d/0anacron" ] && stat -c '%n %y %s' "$d/0anacron"; done
find /etc/cron.* -type f -exec grep -l -E 'curl|wget|/bin/sh|python|bash -c' {} \;
SSH and Account Backdoors
grep -E '^\s*PermitRootLogin' /etc/ssh/sshd_config
awk -F: '($7 ~ /bin\/(sh|bash|zsh)/ && $1 ~ /^(games|lp|sync|shutdown|halt|mail|operator)$/) {print}' /etc/passwd
cat ~/.ssh/authorized_keys
cat ~/.ssh/known_hosts
Cloud C2 Indicators
ps aux | grep -E '[c]loudflared|trycloudflare'
systemctl list-units | grep -i cloudflared
Service Autostart Locations
Check these directories for malicious services:
/etc/inittab
/etc/rc.d/ and /etc/rc.boot/
/etc/init.d/
/etc/inetd.conf and /etc/xinetd/
/etc/systemd/system/
/etc/systemd/system/multi-user.target.wants/
/usr/local/etc/rc.d/
~/.config/autostart/
/lib/systemd/system/
Kernel Modules
lsmod
ls /lib/modules/$(uname -r)
cat /etc/modprobe.d/*
cat /etc/modprobe*
cat /etc/modprobe.conf
User Login Scripts
ls /etc/profile.d/
cat /etc/profile
cat /etc/bash.bashrc
cat /etc/rc.local
cat ~/.bashrc
cat ~/.bash_profile
cat ~/.profile
ls ~/.config/autostart/
8. Log Analysis
System Logs
cat /var/log/syslog
cat /var/log/auth.log
cat /var/log/messages
cat /var/log/secure
grep -iE "session opened for|accepted password|new session|not in sudoers" /var/log/auth.log
cat /var/log/boot.log
cat /var/log/kern.log
cat /var/log/dmesg
cat /var/log/daemon.log
cat /var/log/cron
cat /var/log/faillog
cat /var/log/btmp
cat /var/log/httpd/*
cat /var/log/mysqld.log
cat /var/log/xferlog
User History Files
cat ~/.bash_history
cat ~/.zsh_history
ls ~/.zsh_sessions/
cat ~/.python_history
cat ~/.mysql_history
cat ~/.viminfo
cat ~/.lesshst
cat ~/.ftp_history
cat ~/.sftp_history
ls ~/.mozilla/firefox/
ls ~/.config/google-chrome/
cat ~/.gitconfig
ls .git/logs/
USB Device History
pip3 install usbrip
usbrip ids download
usbrip events history
usbrip events history --pid 0002 --vid 0e0f
usbrip ids search --pid 0002 --vid 0e0f
Privilege Escalation Indicators
cat /etc/sudoers
ls /etc/sudoers.d/
cat /etc/groups
cat /etc/passwd
cat /etc/shadow | grep -E '^([^:]+):$|^[^:]+:!!|^([^:]+):!'
9. File System Analysis
Timeline and Anti-Forensics Detection
find / -user root -perm -04000 -print 2>/dev/null
ls -laR --sort=time /bin
ls -laR --sort=time /sbin
ls -lai /bin | sort -n
find / -name ".. *" -o -name "..^G" 2>/dev/null
ls -la /dev/
lsof +L1
lsof | grep '(deleted)'
df -i
Inode Analysis
find / -xdev -inum <inode_number> 2>/dev/null
sudo debugfs -R "stat <inode_number>" /dev/sdX
Key inode fields:
- Links: 0 means no directory entry references this inode
- dtime: Deletion timestamp
- ctime/mtime: Metadata and content modification times
Filesystem Version Comparison
git diff --no-index --diff-filter=A path/to/old/ path/to/new/
git diff --no-index --diff-filter=M path/to/old/ path/to/new/ | grep -E '^\+' | grep -v "Installed-Time"
git diff --no-index --diff-filter=D path/to/old/ path/to/new/
10. Documentation and Reporting
Evidence Chain
Document:
- Case information - Date, time, investigator, case number
- System information - Hostname, IP, OS version, kernel
- Acquisition details - Tools used, hash values, timestamps
- Findings - Suspicious files, processes, network connections
- Timeline - Chronological sequence of events
- Recommendations - Remediation steps
Hash Verification
Always verify evidence integrity:
sha256sum /path/to/evidence
md5sum /path/to/evidence
sha256sum -c hashes.txt
Quick Reference Scripts
Use the bundled scripts for common tasks:
scripts/gather_system_info.sh - Collect initial system state
scripts/check_autostart.sh - Scan all persistence locations
scripts/analyze_logs.sh - Extract key log entries
scripts/find_suspicious.sh - Identify potential malware indicators
Safety Reminders
- Never modify the target system - Work from forensic images
- Verify tool integrity - Use known-good binaries from USB
- Document everything - Maintain chain of custody
- Preserve timestamps - Note when each action was performed
- Verify hashes - Ensure evidence integrity throughout investigation