Linux 系统安全加固实战

← 返回指南列表

Linux 系统安全加固实战

Linux 系统安全不仅是策略配置,更是持续对抗的过程。本文从攻击者的视角出发,覆盖入侵排查、文件权限、进程网络、日志审计、Rootkit 检测、内网安全等实战场景,提供可直接落地的命令和脚本。


一、入侵排查

1.1 入侵排查全景

MERMAID
115%
Ctrl + 滚轮缩放 · 拖动查看完整流程图

1.2 进程与资源排查

BASH
# 查看所有进程
ps -ef
ps auxf

# 按 CPU 排序(定位挖矿/高消耗进程)
ps aux --sort=-%cpu | head -20

# 按内存排序
ps aux --sort=-%mem | head -20

# 查看隐藏进程(ps 无法看到的)
ls -la /proc/[0-9]*/exe 2>/dev/null | grep -v "permission denied"

# 查看进程启动时间
ps -eo pid,user,lstart,etime,cmd | sort -k4

# 检查异常子进程(如 cron 启动的 sh)
pstree -ap

# 查看进程打开的文件
lsof -p PID
lsof +L1  # 查看已删除但仍在运行的文件(隐藏恶意文件常见手法)

# 查看进程映射的库文件
cat /proc/PID/maps

# 检查没有对应进程的端口(可能是内核态 Rootkit)
ss -antp | grep -v "users:"

1.3 网络连接排查

BASH
# 所有监听端口
ss -tlnp
ss -ulnp

# 已建立的连接
ss -tnp | grep ESTAB

# 查看哪个进程在连接外网
lsof -i -P -n | grep ESTABLISHED

# 对外连接 TOP 10(判断是否存在对外通信的恶意进程)
netstat -antp 2>/dev/null | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -10

# 查看 DNS 查询(挖矿池/ C2 通信)
tcpdump -i eth0 -n port 53 -c 50 2>/dev/null

# 检查 ARP 表(ARP 欺骗检测)
arp -a
ip neigh

# 查看路由表(异常的默认路由)
route -n
ip route

# 检查 iptables 规则(攻击者可能添加规则放行流量)
iptables -L -n -v
iptables -t nat -L -n -v

1.4 用户与登录排查

BASH
# 查看当前登录用户
who
w
last

# 查看最近登录记录
last -10
lastlog

# 查看登录失败记录(爆破检测)
lastb | head -20

# 检查 sudo 使用记录
cat /var/log/auth.log | grep sudo
journalctl _COMM=sudo

# 检查超级用户
awk -F: '($3 == 0) {print}' /etc/passwd

# 检查空口令用户
awk -F: '($2 == "" || $2 == "!") {print $1}' /etc/shadow

# 检查最近新增的用户
sort -t: -k3 -n /etc/passwd | tail -10
# 普通用户的 UID >= 1000,新增的恶意用户 UID=0(与 root 同级)

# 查看可登录的用户
cat /etc/passwd | grep -E '/bin/bash|/bin/sh'

# 检查 .ssh 目录异常
find /home/*/.ssh -name "authorized_keys" -exec cat {} \;
find /root/.ssh -name "authorized_keys" -exec cat {} \;
# 检查是否有非授权的 SSH 公钥

# 检查 SSH 配置(后门常用配置)
cat /etc/ssh/sshd_config | grep -v "^#" | grep -v "^$"
# PermitRootLogin yes  → 是否允许 root 登录
# PasswordAuthentication yes → 是否允许密码登录
# Port 2222 → 是否改了非默认端口

1.5 文件系统排查

BASH
# 查找最近修改的文件(最近 24 小时内)
find / -mtime -1 -type f ! -path "/proc/*" ! -path "/sys/*" ! -path "/run/*" | head -50

# 查找最近 7 天新增的可执行文件
find / -mtime -7 -type f -executable ! -path "/proc/*" ! -path "/sys/*" | head -50

# 查找隐藏文件(攻击者常用 . 开头隐藏恶意文件)
find / -name ".*" -type f ! -path "/proc/*" ! -path "/sys/*" | head -30

# 查找权限异常文件(SUID 文件)
find / -perm -4000 -type f 2>/dev/null
find / -perm -2000 -type f 2>/dev/null

# 查找所有可写且可执行的文件
find / -perm -o=w -type f -executable 2>/dev/null | head -20

# 查找所有用户可写的目录
find / -type d -perm -o=w 2>/dev/null | head -20

# 查找所有没有属主的文件
find / -nouser -o -nogroup 2>/dev/null | head -20

# 查找大文件(疑似数据窃取)
find / -type f -size +100M 2>/dev/null | head -20

# 检查 /tmp、/dev/shm、/var/tmp 等公共可写目录
ls -la /tmp/
ls -la /dev/shm/
ls -la /var/tmp/

# 检查系统关键文件是否被修改
rpm -Va  # RHEL/CentOS 验证所有 RPM 包
debsums -s  # Debian/Ubuntu 验证所有 DEB 包

# 查找 .so 文件劫持(LD_PRELOAD 后门)
cat /etc/ld.so.preload 2>/dev/null
ls -la /etc/ld.so.preload 2>/dev/null

1.6 持久化排查

BASH
# 计划任务排查
crontab -l                # 当前用户
cat /etc/crontab          # 系统级计划任务
ls -la /etc/cron.*/       # 各目录下脚本
ls -la /var/spool/cron/   # 用户计划任务文件

# systemd 服务排查
systemctl list-units --type=service --state=running | head -30
systemctl list-units --type=timer --all  # systemd 定时器

# 查看所有 systemd 服务文件(包括已禁用的)
ls -la /etc/systemd/system/*.service
ls -la /lib/systemd/system/*.service

# 启动脚本排查
cat /etc/rc.local
ls -la /etc/init.d/
ls -la /etc/rc.d/

# 环境变量排查
cat /etc/profile
cat /etc/bash.bashrc
cat ~/.bashrc
cat ~/.bash_profile
cat ~/.profile

# LD_PRELOAD 排查
echo $LD_PRELOAD
cat /etc/ld.so.preload 2>/dev/null
cat /etc/ld.so.conf.d/*.conf

二、文件与权限安全

2.1 敏感文件权限清单

文件路径 推荐权限 说明
/etc/passwd 644 用户账户信息
/etc/shadow 600 或 640 密码哈希(必须 root 可读)
/etc/sudoers 440 sudo 配置
/etc/ssh/sshd_config 600 SSH 服务端配置
/etc/ssh/*_key 600 SSH 私钥
/etc/ssh/*_key.pub 644 SSH 公钥
~/.ssh/authorized_keys 600 授权公钥
~/.bash_history 600 Bash 历史
/var/log/ 640 日志目录
/boot/grub/grub.cfg 600 GRUB 引导配置

2.2 权限加固命令

BASH
# 修复系统关键文件权限
chmod 644 /etc/passwd
chmod 600 /etc/shadow
chmod 440 /etc/sudoers
chmod 600 /etc/ssh/sshd_config
chmod 600 /etc/ssh/*_key
chmod 644 /etc/ssh/*_key.pub
chmod 600 ~/.ssh/authorized_keys

# 锁定关键文件(防止修改)
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/sudoers
chattr +i /etc/ssh/sshd_config

# 查看被锁定的文件
lsattr /etc/passwd /etc/shadow /etc/sudoers

# 解锁
chattr -i /etc/passwd

# 限制 /tmp 分区的执行权限
mount -o remount,noexec,nosuid /tmp
# 或者写入 /etc/fstab
# tmpfs /tmp tmpfs defaults,noexec,nosuid,nodev 0 0

# 限制 /dev/shm 的执行权限
mount -o remount,noexec,nosuid /dev/shm

2.3 SUID/SGID 管理

BASH
# 查找所有 SUID 文件
find / -perm -4000 -type f 2>/dev/null

# 查找所有 SGID 文件
find / -perm -2000 -type f 2>/dev/null

# 移除不必要的 SUID 权限
# 常见的危险 SUID 程序
chmod u-s /usr/bin/passwd  # 不建议移除,passwd 需要 SUID
chmod u-s /usr/bin/su       # 需要时可保留
chmod u-s /usr/bin/sudo     # 需要时可保留
chmod u-s /usr/bin/mount
chmod u-s /usr/bin/umount
chmod u-s /usr/bin/pkexec   # 建议移除

# 批量移除所有非必要的 SUID
for file in $(find / -perm -4000 -type f 2>/dev/null); do
    # 除必要程序外都移除
    case $file in
        /usr/bin/passwd|/usr/bin/su|/usr/bin/sudo)
            continue;;
        *)
            chmod u-s "$file";;
    esac
done

2.4 文件完整性校验

BASH
# 使用 AIDE
apt install aide -y
aideinit
mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db

# 定期检查
aide --check

# 使用 Tripwire(更严格)
tripwire --init
tripwire --check

# 使用 osquery(实时文件监控)
osqueryi "SELECT * FROM file_events;"

# 对关键二进制文件做哈希记录
cat << 'EOF' > /root/file_hashes.txt
/etc/passwd: $(sha256sum /etc/passwd)
/etc/shadow: $(sha256sum /etc/shadow)
/bin/bash: $(sha256sum /bin/bash)
/usr/sbin/sshd: $(sha256sum /usr/sbin/sshd)
/usr/bin/ssh: $(sha256sum /usr/bin/ssh)
EOF

三、日志审计

3.1 Linux 日志体系

日志文件 内容 排查命令
/var/log/auth.log 认证日志(Ubuntu/Debian) cat /var/log/auth.log
/var/log/secure 认证日志(RHEL/CentOS) cat /var/log/secure
/var/log/syslog 系统日志 cat /var/log/syslog
/var/log/messages 系统消息 cat /var/log/messages
/var/log/kern.log 内核日志 cat /var/log/kern.log
/var/log/dmesg 内核环缓冲 dmesg
/var/log/btmp 登录失败记录 lastb
/var/log/wtmp 登录历史 last
/var/log/lastlog 最近登录 lastlog
/var/log/audit/audit.log auditd 审计 ausearch
/var/log/nginx/access.log Nginx 访问日志 按需查看
/var/log/nginx/error.log Nginx 错误日志 按需查看

3.2 日志分析命令

BASH
# SSH 登录成功的 IP 统计
grep "Accepted" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn | head -20

# SSH 登录失败的 IP 统计(爆破检测)
grep "Failed password" /var/log/auth.log | awk '{print $9}' | sort | uniq -c | sort -rn | head -20

# 从某个 IP 的登录尝试
grep "192.168.1.100" /var/log/auth.log

# 查看特定用户的操作
grep "root" /var/log/auth.log

# 看谁执行了 sudo
grep "sudo" /var/log/auth.log

# 查看 cron 任务执行日志
grep CRON /var/log/syslog
journalctl -u cron

# 系统错误日志
journalctl -p err -b

# 启动日志
journalctl -b -1  # 上次启动的日志

# 内核异常
dmesg | grep -iE "error|fail|oops|panic"

# 使用 journalctl 分析
# 最近一小时的日志
journalctl --since "1 hour ago"

# 特定时间范围
journalctl --since "2026-06-08 10:00:00" --until "2026-06-08 11:00:00"

# 按可执行文件过滤
journalctl /usr/sbin/sshd

# 实时跟踪日志
journalctl -f
tail -f /var/log/auth.log

3.3 auditd 配置

BASH
# 安装 auditd
apt install auditd audispd-plugins -y

# 审计关键文件变更
auditctl -w /etc/passwd -p wa -k passwd_changes
auditctl -w /etc/shadow -p wa -k shadow_changes
auditctl -w /etc/sudoers -p wa -k sudoers_changes
auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config
auditctl -w /root/.ssh -p wa -k root_ssh

# 审计特定系统调用
auditctl -a exit,always -S execve -k command_execution

# 查看审计日志
ausearch -k passwd_changes
ausearch -k command_execution
ausearch -ui root -ts today

# 生成审计报告
aureport -l  # 登录报告
aureport -x  # 执行报告
aureport -f  # 文件变更报告
aureport -m  # 账户修改报告

# 持久化配置(写入 /etc/audit/rules.d/)
cat << 'EOF' > /etc/audit/rules.d/security.rules
-w /etc/passwd -p wa -k passwd_changes
-w /etc/shadow -p wa -k shadow_changes
-w /etc/sudoers -p wa -k sudoers_changes
-w /etc/ssh/sshd_config -p wa -k sshd_config
-w /root/.ssh -p wa -k root_ssh
-a exit,always -S execve -k command_execution
EOF

四、Rootkit 检测

4.1 Rootkit 类型

类型 工作原理 检测难度
用户态 Rootkit 替换系统命令(ls/ps/netstat) 低
LD_PRELOAD 注入 通过环境变量劫持系统调用 中
LKM 内核模块 加载恶意内核模块 高
Bootkit 修改 MBR/EFI 引导 极高
Firmware Rootkit 修改固件 极高

4.2 检测命令

BASH
# 使用 chkrootkit
apt install chkrootkit -y
chkrootkit

# 使用 rkhunter
apt install rkhunter -y
rkhunter --check

# 检查常见 Rootkit 文件
find / -name "*rootkit*" -type f 2>/dev/null
find / -name "*kdb*" -type f 2>/dev/null
find / -name "*knark*" -type f 2>/dev/null

# 检查 ls 是否被替换
# 正常 ls 大小
ls -la /bin/ls
# 对比其他同发行版机器的 ls 大小

# 使用 stat 检查文件是否被修改
stat /bin/ls /bin/ps /bin/netstat /usr/sbin/lsof /usr/sbin/sshd

# 检查内核模块
lsmod
# 查看是否存在可疑内核模块
cat /proc/modules

# 检查 /dev 中的隐藏设备
ls -la /dev/ | grep -E '^c|^b'

# 检查系统调用表是否被修改(需要编译内核模块)
# 使用 systemtap 检测
stap -e 'probe kernel.function("sys_open") { printf("%s\n", execname()) }'

# 检查 /proc 异常
cat /proc/1/cmdline  # init 进程
ls -la /proc/ | grep "?" | head -10  # 隐藏进程

# 使用 unhide 工具检测隐藏进程
apt install unhide -y
unhide proc
unhide sys
unhide brute

4.3 快速自查脚本

BASH
#!/bin/bash
# Rootkit 快速检测脚本

echo "=== 1. 检查 SUID 文件变化 ==="
find / -perm -4000 -type f 2>/dev/null

echo "=== 2. 检查隐藏进程 ==="
ps auxf

echo "=== 3. 检查关键命令 Hash ==="
for cmd in /bin/ls /bin/ps /bin/netstat /bin/ss /usr/sbin/sshd /usr/bin/top /usr/sbin/lsof; do
    if [ -f "$cmd" ]; then
        echo "$cmd: $(sha256sum $cmd | cut -d' ' -f1)"
    fi
done

echo "=== 4. 检查环境变量 ==="
echo "LD_PRELOAD=$LD_PRELOAD"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"

echo "=== 5. 检查 /etc/ld.so.preload ==="
cat /etc/ld.so.preload 2>/dev/null || echo "文件不存在"

echo "=== 6. 检查计划任务 ==="
for user in $(cut -f1 -d: /etc/passwd); do
    crontab -u $user -l 2>/dev/null
done

echo "=== 7. 检查内核模块 ==="
lsmod | grep -vE "^(Module|$)"

五、SSH 安全保障

5.1 SSH 安全配置基线

BASH
# /etc/ssh/sshd_config 安全配置
Port 2222                        # 修改默认端口
PermitRootLogin no               # 禁止 root 直接登录
PasswordAuthentication no        # 禁用密码登录(仅密钥)
PubkeyAuthentication yes         # 启用密钥认证
AuthorizedKeysFile .ssh/authorized_keys  # 授权密钥文件
RSAAuthentication yes             # RSA 认证

# 限制登录用户
AllowUsers alice bob             # 仅允许 alice 和 bob
AllowGroups sshusers             # 仅允许 sshusers 组

# 登录控制
MaxAuthTries 3                   # 最大尝试次数
MaxSessions 2                    # 最大并发会话
LoginGraceTime 30                # 登录超时(秒)
ClientAliveInterval 300          # 心跳间隔
ClientAliveCountMax 2            # 最大心跳丢失

# 协议版本
Protocol 2                       # 仅使用 SSHv2

# 其他
UseDNS no                        # 不反向解析 DNS(加速登录)
X11Forwarding no                 # 禁用 X11 转发
AllowTcpForwarding no            # 禁用 TCP 转发(不需要时)
PrintMotd no                     # 不显示 MOTD
Banner /etc/issue.net            # 设置登录横幅(法律警告)

# 应用配置
systemctl restart sshd

5.2 SSH 密钥管理

BASH
# 生成强密钥(推荐 ed25519)
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519

# 或使用 RSA 4096 位
ssh-keygen -t rsa -b 4096 -a 100 -f ~/.ssh/id_rsa

# 禁止使用 DSA 和 ECDSA(已知弱点)
# 在 sshd_config 中
# HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256

# 使用 SSH 证书认证(推荐)
# CA 签名主机密钥
ssh-keygen -s ca_key -I host_name -h -n host1.example.com host_key.pub

# CA 签名用户密钥
ssh-keygen -s ca_key -I user_name -n username user_key.pub

# SSH 蜜罐登录告警
cat << 'EOF' >> ~/.bashrc
# SSH 登录告警
echo "ALERT: SSH Login $(date) from $(who am i | awk '{print $5}')" | \
  mail -s "SSH Login Alert" admin@example.com
EOF

5.3 fail2ban 自动封禁

BASH
apt install fail2ban -y

cat << 'EOF' > /etc/fail2ban/jail.local
[DEFAULT]
# 封禁时间:1 小时
bantime = 3600
# 检测时间窗口:10 分钟
findtime = 600
# 最大失败次数
maxretry = 5

[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
maxretry = 3

[sshd-ddos]
enabled = true
port = ssh
logpath = %(sshd_log)s
maxretry = 5

[apache-auth]
enabled = true
port = http,https
logpath = %(apache_access_log)s

[nginx-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log

[nginx-botsearch]
enabled = true
port = http,https
logpath = /var/log/nginx/access.log
maxretry = 10
EOF

systemctl restart fail2ban

# 查看封禁状态
fail2ban-client status
fail2ban-client status sshd

六、内网安全

6.1 本地网络攻击防御

BASH
# 防止 ARP 欺骗
# 设置静态 ARP
arp -s 192.168.1.1 aa:bb:cc:dd:ee:ff

# 启用内核 ARP 防护
sysctl -w net.ipv4.conf.all.arp_ignore=1
sysctl -w net.ipv4.conf.all.arp_announce=2

# 防止 IP 欺骗
sysctl -w net.ipv4.conf.all.rp_filter=1
sysctl -w net.ipv4.conf.default.rp_filter=1

# 防止 SYN Flood
sysctl -w net.ipv4.tcp_syncookies=1

# 防止 Smurf 攻击
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1

# 防止 ICMP 重定向
sysctl -w net.ipv4.conf.all.accept_redirects=0
sysctl -w net.ipv4.conf.all.secure_redirects=0
sysctl -w net.ipv4.conf.all.send_redirects=0

# 防止源路由
sysctl -w net.ipv4.conf.all.accept_source_route=0

# 记录马丁攻击
sysctl -w net.ipv4.conf.all.log_martians=1

6.2 端口敲门保护

BASH
# 端口敲门(Port Knocking)—— 隐藏 SSH 端口
# 只有按顺序发送特定端口的连接请求后才临时开放 SSH

apt install knockd -y

cat << 'EOF' > /etc/knockd.conf
[options]
    logfile = /var/log/knockd.log

[openSSH]
    sequence    = 7000,8000,9000
    seq_timeout = 5
    command     = /sbin/iptables -A INPUT -p tcp --dport 2222 -s %IP% -j ACCEPT
    tcpflags    = syn

[closeSSH]
    sequence    = 9000,8000,7000
    seq_timeout = 5
    command     = /sbin/iptables -D INPUT -p tcp --dport 2222 -s %IP% -j ACCEPT
    tcpflags    = syn
EOF

# 客户端敲门
knock target.com 7000 8000 9000
ssh -p 2222 user@target.com

6.3 全端口转发监控

BASH
# 监控出站连接——检测异常外连行为
# 使用 netstat 实时监控新连接
watch -n 1 'ss -tnp | grep ESTAB | head -30'

# 记录所有新连接的日志
iptables -A OUTPUT -m state --state NEW -j LOG --log-prefix "OUTPUT_NEW: "

# 限制出站连接频率
iptables -A OUTPUT -m state --state NEW -m recent --set
iptables -A OUTPUT -m state --state NEW -m recent --update --seconds 60 --hitcount 20 -j DROP

七、安全加固脚本

7.1 一键加固

BASH
#!/bin/bash
# Linux 系统一键加固脚本

set -e

echo "=== Linux 安全加固 ==="

# 1. 系统更新
echo ">>> 更新系统..."
apt update && apt upgrade -y

# 2. 内核参数加固
echo ">>> 内核参数加固..."
cat << 'EOF' >> /etc/sysctl.d/99-security.conf
# 网络
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

# 防止 fork 炸弹
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.kexec_load_disabled = 1

# 安全
fs.suid_dumpable = 0
kernel.randomize_va_space = 2
EOF
sysctl -p /etc/sysctl.d/99-security.conf

# 3. SSH 加固
echo ">>> SSH 加固..."
sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i 's/^X11Forwarding.*/X11Forwarding no/' /etc/ssh/sshd_config
sed -i 's/^MaxAuthTries.*/MaxAuthTries 3/' /etc/ssh/sshd_config
echo "Protocol 2" >> /etc/ssh/sshd_config

# 4. 锁定关键文件
echo ">>> 锁定关键文件..."
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/gshadow
chattr +i /etc/sudoers

# 5. 移除危险 SUID
echo ">>> 移除危险 SUID..."
chmod u-s /usr/bin/mount
chmod u-s /usr/bin/umount
chmod u-s /usr/bin/pkexec 2>/dev/null
chmod u-s /usr/bin/at
chmod u-s /usr/bin/chfn
chmod u-s /usr/bin/chsh
chmod u-s /usr/bin/newgrp

# 6. 安装安全工具
echo ">>> 安装安全工具..."
apt install -y fail2ban rkhunter chkrootkit aide auditd

# 7. 配置日志审计
echo ">>> 配置日志审计..."
systemctl enable auditd
auditctl -e 1
auditctl -w /etc/passwd -p wa -k passwd_watch
auditctl -w /etc/shadow -p wa -k shadow_watch
auditctl -w /etc/sudoers -p wa -k sudoers_watch

# 8. 限制 /tmp 执行权限
echo ">>> 限制 /tmp noexec..."
mount -o remount,noexec,nosuid /tmp 2>/dev/null

# 9. 清除不必要的服务
echo ">>> 禁用不必要的服务..."
systemctl disable avahi-daemon 2>/dev/null || true
systemctl disable cups 2>/dev/null || true
systemctl disable rpcbind 2>/dev/null || true

echo "=== 加固完成!==="
touch /root/.hardened

7.2 使用 CIS Benchmark

CIS(Center for Internet Security)提供最权威的 Linux 安全基线:

BASH
# 使用 CIS 自动化工具
git clone https://github.com/CISOfy/lynis
cd lynis
./lynis audit system

# Lynis 报告内容
# - 系统信息
# - 漏洞警告
# - 加固建议
# - 扫描得分

# 使用 oscap(OpenSCAP)进行合规性扫描
apt install libopenscap8 scap-security-guide -y
oscap xccdf eval \
  --profile xccdf_org.ssgproject.content_profile_cis_level1_server \
  --results results.xml \
  --report report.html \
  /usr/share/xml/scap/ssg/content/ssg-ubuntu-2004-ds.xml

八、应急响应

8.1 应急响应流程

MERMAID
115%
Ctrl + 滚轮缩放 · 拖动查看完整流程图

8.2 取证命令

BASH
# 内存取证(先拔网线再操作)
# 保存进程信息
ps aux > forensic_ps.txt
lsof -n > forensic_lsof.txt
ss -antp > forensic_connections.txt

# 保存网络状态
netstat -antp > forensic_netstat.txt
iptables-save > forensic_iptables.txt

# 保存系统时间
date > forensic_time.txt
uptime >> forensic_time.txt

# 保存登录记录
w > forensic_w.txt
last > forensic_last.txt
lastb > forensic_lastb.txt

# 保存用户信息
cat /etc/passwd > forensic_passwd.txt
cat /etc/shadow > forensic_shadow.txt
cat /etc/group > forensic_group.txt

# 保存计划任务
crontab -l > forensic_crontab.txt 2>/dev/null
cat /etc/crontab > forensic_crontab_sys.txt

# 保存审计日志
journalctl -xb > forensic_journal.txt
cp /var/log/auth.log forensic_auth.log
cp /var/log/syslog forensic_syslog.log

# 保存所有运行服务
systemctl list-units --type=service > forensic_services.txt

# 保存内核信息
uname -a > forensic_uname.txt
lsmod > forensic_lsmod.txt
cat /proc/modules > forensic_modules.txt

# 内存镜像(需要 LiME 或 fmem)
# 如果已预装
lime -i /dev/mem -o forensic_memory.lime

# 收集关键文件
tar czf forensic_files.tar.gz \
  /var/log/ \
  /etc/ \
  /root/.bash_history \
  /home/*/.bash_history \
  /tmp/ \
  /var/tmp/

九、总结

MERMAID
115%
Ctrl + 滚轮缩放 · 拖动查看完整流程图

Linux 系统安全不是一次性配置,而是持续对抗的过程。本文涵盖的排查命令和加固脚本可在日常运维中直接使用。


参考