gotr00t0day/LDFIR
GitHub: gotr00t0day/LDFIR
LDFIR 是一个 Linux 数字取证与事件响应指南,提供入侵后分析的完整步骤和命令,帮助安全团队应对安全事件。
Stars: 3 | Forks: 0
# Linux 数字取证与事件响应 (LDFIR)
### 入侵后分析指南
## 目录
1. [第一响应原则](#1-first-response-principles)
2. [易失性数据收集(请先执行此操作)](#2-volatile-data-collection-do-this-first)
3. [用户与认证分析](#3-user--authentication-analysis)
4. [进程与网络分析](#4-process--network-analysis)
5. [持久化机制](#5-persistence-mechanisms)
6. [文件系统与文件分析](#6-filesystem--file-analysis)
7. [日志分析](#7-log-analysis)
8. [内存取证](#8-memory-forensics)
9. [恶意软件与 Rootkit 检测](#9-malware--rootkit-detection)
10. [时间线重建](#10-timeline-reconstruction)
11. [遏制与修复](#11-containment--remediation)
12. [证据保全](#12-evidence-preservation)
## 1. 第一响应原则
**不要恐慌。不要重启。** 重启会销毁易失性证据(内存、活动连接、运行中的进程)。
### 立即决策
```
Is the system actively being used by an attacker right now?
├── YES → Isolate network first, preserve volatile data second
└── NO → Preserve volatile data before anything else
```
### 在不重启的情况下隔离网络
```
# 立即丢弃所有流量
sudo iptables -I INPUT -j DROP
sudo iptables -I OUTPUT -j DROP
sudo iptables -I FORWARD -j DROP
# 或关闭接口
sudo ip link set eth0 down
# 若命令失败,拔网线/禁用WiFi作为最后手段
```
### 在接触任何东西之前记录你的环境
```
date -u # timestamp everything in UTC
uname -a # kernel version
hostname
id # who you are running as
uptime # how long the system has been running
```
## 2. 易失性数据收集(请先执行此操作)
易失性数据在重启后会消失。请立即按此顺序收集。
### 系统时间与正常运行时间
```
date -u
uptime
cat /proc/uptime
```
### 运行中的进程
```
ps auxf # full process tree with hierarchy
ps -eo pid,ppid,user,stat,start,time,comm,args
ls -la /proc/*/exe 2>/dev/null # resolve actual binary for each PID
```
### 打开的网络连接
```
ss -antpue # all TCP/UDP connections with PIDs
netstat -antpue # alternative if ss unavailable
lsof -i # files/processes tied to network
cat /proc/net/tcp # raw kernel TCP table (hex)
cat /proc/net/tcp6
cat /proc/net/udp
```
### 已登录用户
```
who
w
last -a # login history
lastb # failed logins
```
### 已加载的内核模块
```
lsmod
cat /proc/modules
```
### ARP 与路由表
```
arp -a
ip neigh
ip route
ip addr
```
### 所有进程的环境变量
```
for pid in /proc/[0-9]*/environ; do
echo "=== $pid ===";
cat "$pid" 2>/dev/null | tr '\0' '\n';
done
```
### 打开的文件描述符
```
lsof -n # all open files
lsof +L1 # files open but deleted (common attacker trick)
```
## 3. 用户与认证分析
### 检查新建或修改过的用户
```
cat /etc/passwd
cat /etc/shadow # look for unusual hashes or accounts
cat /etc/group
# 查找除root外的UID 0账户
awk -F: '($3 == 0) {print}' /etc/passwd
```
### SSH 密钥
```
# 检查每个用户的authorized_keys
for home in /root /home/*; do
echo "=== $home ===";
cat "$home/.ssh/authorized_keys" 2>/dev/null;
done
# 查找最近修改的authorized_keys
find / -name "authorized_keys" -newer /etc/passwd 2>/dev/null
```
### Sudo 访问权限
```
cat /etc/sudoers
ls -la /etc/sudoers.d/
sudo -l -U # what can a specific user run
```
### 登录历史与认证日志
```
last -Faixw # full login history with IPs
lastb -Faixw # failed attempts
grep "Accepted\|Failed\|Invalid" /var/log/auth.log
grep "sudo" /var/log/auth.log
journalctl _SYSTEMD_UNIT=sshd.service --since "7 days ago"
```
### PAM 后门指标
```
# 检查PAM配置是否有未授权更改
ls -la /etc/pam.d/
find /lib/security/ -newer /etc/passwd 2>/dev/null
find /lib64/security/ -newer /etc/passwd 2>/dev/null
```
## 4. 进程与网络分析
### 查找没有关联二进制文件的进程(已删除/替换)
```
# 二进制文件已被删除的进程
ls -la /proc/*/exe 2>/dev/null | grep deleted
# 从/tmp、/dev/shm、/var/tmp运行的进程(重大警示)
ls -la /proc/*/exe 2>/dev/null | grep -E "tmp|shm"
```
### 监听异常端口的进程
```
ss -tlnp
# 与已知服务交叉验证
# 任何由root运行的1024以上端口均属可疑
```
### 检查父-子进程关系异常
```
pstree -p -a -l # full tree with PIDs and args
# 常见可疑模式:
# apache2 → bash → nc
# cron → sh → wget
# sshd → bash(但用户从未登录)
```
### 到外部 IP 的网络连接
```
ss -antpe | grep ESTABLISHED
# 获取远程IP地理定位(需curl/网络连接)
for ip in $(ss -tn | awk '/ESTABLISHED/ {print $5}' | cut -d: -f1 | sort -u); do
echo "$ip: $(curl -s ipinfo.io/$ip/country 2>/dev/null)"
done
```
### DNS 查询(如果使用 systemd-resolved)
```
journalctl -u systemd-resolved --since "24 hours ago"
cat /var/log/syslog | grep -i dns
```
## 5. 持久化机制
这些是攻击者最常留下后门的地方。
### Cron 计划任务
```
crontab -l # current user
crontab -l -u root
cat /etc/crontab
ls -la /etc/cron.*
find /var/spool/cron/ -type f # all user crontabs
```
### Systemd 服务
```
systemctl list-units --type=service --all
systemctl list-unit-files --type=service
# 查找最近创建或修改的单元文件
find /etc/systemd /usr/lib/systemd /lib/systemd -name "*.service" \
-newer /etc/passwd 2>/dev/null
```
### Init / rc 启动脚本
```
ls -la /etc/init.d/
ls -la /etc/rc.local
cat /etc/rc.local
ls -la /etc/rc*.d/
```
### Shell 启动文件
```
for f in /root/.bashrc /root/.bash_profile /root/.profile \
/etc/profile /etc/bash.bashrc /etc/environment; do
echo "=== $f ==="; cat "$f" 2>/dev/null;
done
find /home -name ".bashrc" -o -name ".profile" -o -name ".zshrc" | \
xargs grep -l "wget\|curl\|nc\|bash -i\|/dev/tcp" 2>/dev/null
```
### SUID/SGID 二进制文件(权限提升持久化)
```
find / -perm -4000 -type f 2>/dev/null | sort # SUID
find / -perm -2000 -type f 2>/dev/null | sort # SGID
# 如有已知安全基线,进行比对检查
```
### PATH 中的可写目录
```
echo $PATH | tr ':' '\n' | xargs ls -ld 2>/dev/null | grep -v "^d..x..x..x"
```
### /etc/ld.so.preload (LD_PRELOAD 后门)
```
cat /etc/ld.so.preload # should almost always be empty
ls -la /etc/ld.so.conf.d/
ldconfig -p # list cached shared libraries
```
### 内核模块(rootkit)
```
lsmod
# 检查是否存在于/lib/modules/$(uname -r)/中不存在的模块
find /lib/modules/$(uname -r)/ -name "*.ko" > /tmp/known_modules.txt
lsmod | awk '{print $1}' | tail -n +2 > /tmp/loaded_modules.txt
diff /tmp/known_modules.txt /tmp/loaded_modules.txt
```
## 6. 文件系统与文件分析
### 最近修改的文件
```
# 过去24小时内修改的文件
find / -xdev -mtime -1 -type f 2>/dev/null | grep -v proc | grep -v sys
# 过去7天内修改的文件(已排序)
find / -xdev -mtime -7 -type f -printf '%TY-%Tm-%Td %TH:%TM %p\n' \
2>/dev/null | sort | grep -v proc | grep -v sys
# 重点检查敏感位置
find /etc /bin /sbin /usr/bin /usr/sbin /lib /lib64 \
-newer /etc/passwd -type f 2>/dev/null
```
### 位于异常位置的文件
```
# /tmp、/var/tmp、/dev/shm中的可执行文件
find /tmp /var/tmp /dev/shm -type f -executable 2>/dev/null
# 隐藏文件和目录
find / -name ".*" -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null
# 不应如此之大的大文件
find / -xdev -size +100M -type f 2>/dev/null | grep -v proc
```
### 文件完整性检查
```
# 若已安装aide
aide --check
# 若已安装tripwire
tripwire --check
# 手动哈希校验关键二进制文件
for bin in /bin/ls /bin/ps /bin/netstat /usr/bin/ssh /usr/sbin/sshd; do
echo "$bin: $(sha256sum $bin 2>/dev/null)"
done
# 与软件包管理器的预期哈希值比对
rpm -Va 2>/dev/null # RPM-based systems
dpkg --verify 2>/dev/null # Debian-based systems
```
### 仍被打开的已删除文件(经典攻击者手法)
```
# 攻击者常删除其二进制文件但保持进程运行
lsof +L1 2>/dev/null
# 从/proc恢复二进制文件
cp /proc//exe /tmp/recovered_binary
file /tmp/recovered_binary
strings /tmp/recovered_binary | head -100
```
### 检查 Web Shell
```
find /var/www /srv /opt /usr/share/nginx /usr/share/apache2 \
-name "*.php" -o -name "*.jsp" -o -name "*.aspx" 2>/dev/null | \
xargs grep -l "exec\|system\|passthru\|shell_exec\|base64_decode" 2>/dev/null
```
## 7. 日志分析
### 检查日志是否被清除
```
ls -la /var/log/wtmp # should not be 0 bytes on an active system
ls -la /var/log/btmp
ls -la /var/log/lastlog
ls -la /var/log/auth.log
stat /var/log/auth.log # check modification time vs creation time
```
### Syslog / 系统消息
```
grep -i "error\|fail\|warn\|crit\|attack\|invalid" /var/log/syslog | tail -200
grep -i "segfault\|kernel\|oops\|panic" /var/log/kern.log
```
### 认证日志
```
grep "Accepted password\|Accepted publickey" /var/log/auth.log
grep "FAILED\|Failed\|Invalid user" /var/log/auth.log
grep "sudo\|su\[" /var/log/auth.log
```
### Bash 历史记录(可能已被清除)
```
cat /root/.bash_history
cat /home/*/.bash_history
# 同时检查其他历史记录文件
cat /root/.zsh_history
cat /root/.python_history
# 被截断(清空)的历史记录文件
ls -la /home/*/.bash_history /root/.bash_history
# 0字节或极新修改时间=可能已清除
```
### Journald (systemd 日志)
```
journalctl --since "7 days ago" --no-pager | grep -i "fail\|error\|denied"
journalctl -u ssh --since "24 hours ago"
journalctl -u cron --since "24 hours ago"
journalctl --list-boots # previous boot sessions
journalctl -b -1 # logs from last boot
```
### Apache / Nginx 日志
```
grep -E "POST|PUT|CONNECT" /var/log/apache2/access.log | tail -200
grep " 500 \| 403 \| 401 " /var/log/nginx/access.log | tail -200
# 查找Web shell活动
grep -E "cmd=|exec=|shell=|passthru" /var/log/apache2/access.log
```
## 8. 内存取证
### 转储进程内存
```
# 转储可疑进程的内存区域
cat /proc//maps # memory map
cat /proc//smaps # detailed memory usage
# 转储特定内存区域(从maps获取范围)
dd if=/proc//mem bs=1 skip=$((0xSTART)) count=$((0xEND - 0xSTART)) \
of=/tmp/memdump.bin 2>/dev/null
# 从进程内存中提取字符串
strings /proc//mem 2>/dev/null | grep -E "http|/tmp|bash|nc |wget|curl"
```
### 完整内存捕获(需要 LiME 或类似工具)
```
# 安装LiME内核模块(来自可信外部源)
insmod lime.ko "path=/mnt/usb/memory.lime format=lime"
# 使用Volatility3分析(在独立取证工作站上)
python3 vol.py -f memory.lime linux.pslist
python3 vol.py -f memory.lime linux.netstat
python3 vol.py -f memory.lime linux.bash
```
## 9. 恶意软件与 Rootkit 检测
### 检查隐藏进程(PID 间隙)
```
# 比对/proc PID与ps输出
diff <(ls /proc | grep -E '^[0-9]+$' | sort -n) \
<(ps ax | awk '{print $1}' | grep -E '^[0-9]+$' | sort -n)
# 存在于/proc但不在ps中的PID=被rootkit隐藏
```
### 检查隐藏的网络连接
```
# 比对ss与/proc/net
ss -antpe > /tmp/ss_output.txt
cat /proc/net/tcp > /tmp/proc_tcp.txt
# 在/proc/net/tcp中但不在ss中的条目=rootkit隐藏连接
```
### inotify / fanotify 监视器(EDR 或攻击者工具)
```
# 查找持有文件系统监视器的进程
for pid in /proc/[0-9]*/fdinfo/*; do
grep -l "inotify\|fanotify" "$pid" 2>/dev/null
done
# 解析PID:echo $pid | grep -oP '(?<=/proc/)\d+'
```
### 检查预加载的库(LD_PRELOAD rootkit)
```
cat /etc/ld.so.preload
cat /proc//environ | tr '\0' '\n' | grep LD_PRELOAD
```
### 使用 chkrootkit / rkhunter 扫描
```
# 理想情况下从可信只读介质运行
chkrootkit
rkhunter --check --skip-keypress
```
### 检测内核级钩子
```
# 检查系统调用表是否被修改(若有已知安全基线)
# 使用SystemTap或eBPF工具进行实时分析
# 或比对/proc/kallsyms地址与已知安全内核构建
sudo grep "sys_call_table" /proc/kallsyms
```
## 10. 时间线重建
### 构建文件系统时间线
```
# 使用find生成mactime式时间线
find / -xdev -printf "%A@ %T@ %C@ %m %n %u %g %s %p\n" 2>/dev/null \
> /tmp/filesystem_timeline.txt
# 按修改时间排序
sort -n /tmp/filesystem_timeline.txt | tail -500
# 转换为可读格式
find / -xdev -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\n" 2>/dev/null | \
sort | grep -v "/proc\|/sys" > /tmp/timeline_readable.txt
```
### 与日志时间戳关联
```
# 从认证日志中提取时间戳并与文件更改匹配
grep "May 17" /var/log/auth.log | head -50
# 查找可疑登录时间附近被访问的所有文件
find / -xdev -newermt "2026-05-17 02:00" ! -newermt "2026-05-17 04:00" \
2>/dev/null | grep -v proc | grep -v sys
```
### 检查 inode 更改时间 (ctime — 更难伪造)
```
# ctime在任何元数据更改时更新(重命名、chmod等)
# stat显示三个时间:atime、mtime、ctime
stat /bin/ls
stat /usr/bin/sudo
# 查找ctime在可疑时间窗口内的文件
find /bin /sbin /usr/bin /usr/sbin \
-ctime -7 2>/dev/null
```
## 11. 遏制与修复
### 立即遏制检查清单
```
# 1. 隔离网络
sudo iptables -I INPUT -j DROP
sudo iptables -I OUTPUT -j DROP
# 2. 终止攻击者会话(先找到其PID)
who -a # find their TTY
pkill -9 -t pts/1 # kill session on pts/1
# 3. 撤销被破解的SSH密钥
# 编辑/home//.ssh/authorized_keys并移除攻击者密钥
# 4. 更改所有密码
passwd root
passwd
# 5. 轮换SSH主机密钥
rm /etc/ssh/ssh_host_*
ssh-keygen -A
# 6. 封锁攻击者IP
sudo iptables -I INPUT -s -j DROP
```
### 移除持久化
```
# 删除恶意cron作业
crontab -r # remove ALL cron for current user
crontab -r -u # specific user
# 禁用恶意服务
systemctl stop
systemctl disable
rm /etc/systemd/system/.service
# 移除LD_PRELOAD条目
echo "" > /etc/ld.so.preload
ldconfig
# 删除恶意内核模块
rmmod
# 随后将其永久列入黑名单
echo "blacklist " >> /etc/modprobe.d/blacklist.conf
```
## 12. 证据保全
### 在触碰前对所有东西计算哈希值
```
sha256sum /var/log/auth.log > /tmp/evidence_hashes.txt
sha256sum /var/log/syslog >> /tmp/evidence_hashes.txt
sha256sum /etc/passwd >> /tmp/evidence_hashes.txt
sha256sum /etc/shadow >> /tmp/evidence_hashes.txt
```
### 磁盘镜像(在关闭系统前在在线系统上进行)
```
# 通过SSH镜像到远程主机
sudo dd if=/dev/sda bs=4M | gzip | ssh user@forensic-host "cat > /cases/disk.img.gz"
# 或镜像到本地USB(先挂载)
sudo dd if=/dev/sda bs=4M | gzip > /mnt/usb/disk.img.gz
# 验证镜像完整性
sha256sum /mnt/usb/disk.img.gz
```
### 打包易失性数据以便传输
```
mkdir /tmp/ir_$(date +%Y%m%d_%H%M%S)
IR_DIR="/tmp/ir_$(date +%Y%m%d_%H%M%S)"
ps auxf > "$IR_DIR/processes.txt"
ss -antpue > "$IR_DIR/network.txt"
lsmod > "$IR_DIR/modules.txt"
last -Faixw > "$IR_DIR/logins.txt"
find / -xdev -mtime -7 -type f 2>/dev/null > "$IR_DIR/recent_files.txt"
cp /var/log/auth.log "$IR_DIR/"
cp /var/log/syslog "$IR_DIR/"
cp /etc/passwd "$IR_DIR/"
cp /etc/shadow "$IR_DIR/"
sha256sum "$IR_DIR"/* > "$IR_DIR/HASHES.txt"
tar czf "/tmp/ir_evidence_$(date +%Y%m%d).tar.gz" "$IR_DIR"
```
## 快速参考:危险信号检查清单
| 指标 | 检查命令 |
|------|----------|
| 具有已删除二进制文件的进程 | `lsof +L1` |
| UID 为 0 但非 root 的账户 | `awk -F: '($3==0)' /etc/passwd` |
| /tmp 目录下的 SUID 文件 | `find /tmp -perm -4000` |
| 异常的内核模块 | `lsmod` 对比已知基线 |
| 到未知 IP 的出站连接 | `ss -antpe` |
| 被清除的日志文件 | `ls -la /var/log/wtmp` |
| 运行来自 /tmp 脚本的 Cron 任务 | `crontab -l` |
| /etc/ld.so.preload 文件不为空 | `cat /etc/ld.so.preload` |
| 未授权的 SSH 密钥 | `cat ~/.ssh/authorized_keys` |
| 隐藏的进程(PID 间隙) | 对比 `/proc` 和 `ps` 的输出 |
*尽可能在可信环境中工作。被入侵的内核可能会向标准工具隐藏进程、文件和连接。如有疑问,请从干净的可启动 USB 启动,并以只读方式挂载可疑的文件系统。*
标签:BurpSuite集成, rootkit检测, SecList, 内存取证, 后妥协分析, 安全指南, 应用安全, 持久化机制分析, 数字取证, 时间线重建, 易失性数据收集, 系统分析, 网络分析, 网络安全, 网络隔离, 自动化脚本, 自定义DNS解析器, 认证分析, 证据保留, 进程分析, 逆向工具, 隐私保护