Linux 安全 (Linux Security)
一、安全概述
安全的重要性
- 数据保护:防止数据泄露、损坏
- 业务连续性:防止服务中断
- 合规要求:GDPR、个保法、PCI-DSS、HIPAA
- 声誉保护:安全事件影响品牌
- 法律责任:可能面临诉讼
安全的基本原则
- 最小特权 (Least Privilege):只给必要的权限
- 纵深防御 (Defense in Depth):多层防护
- 零信任 (Zero Trust):从不信任,始终验证
- 失效安全 (Fail Safe):失败时进入安全状态
- 默认拒绝 (Default Deny):默认拒绝所有
- 纵深防御:多层防御
攻击类型
- 被动攻击:嗅探、扫描
- 主动攻击:DoS/DDoS、注入
- 本地攻击:提权、密码破解
- 社会工程学:钓鱼、伪装
- 供应链:软件后门
- 零日漏洞:未知漏洞
- APT:高级持续威胁
攻击面
- 网络层:端口、服务、协议
- 应用层:Web 漏洞、API
- 系统层:内核、驱动
- 物理层:机房、U 盘
- 人:员工、内部威胁
二、认证与授权
1. PAM (Pluggable Authentication Modules)
PAM:Linux 认证框架
配置位置:
- /etc/pam.d/ 各种服务的 PAM 配置
- /etc/pam.d/login、sshd、sudo 等
配置结构:
类型 控制标志 模块 参数
auth required pam_unix.so
类型 (Type):
- auth: 认证 (验证你是谁)
- account: 账户 (账号是否过期)
- password: 密码 (修改密码)
- session: 会话 (登录/登出时操作)
控制标志 (Control):
- required: 必须成功,失败继续但最终失败
- requisite: 必须成功,失败立即失败
- sufficient: 成功则 OK,失败忽略
- optional: 仅供参考
- include: 包含其他文件
- substack: 子栈
示例 - /etc/pam.d/sshd:
auth required pam_env.so
auth sufficient pam_unix.so
auth required pam_deny.so
account required pam_unix.so
password required pam_cracklib.so
session required pam_limits.so
常用 PAM 模块:
- pam_unix.so:传统 Unix 认证
- pam_cracklib.so:密码强度检查
- pam_pwquality.so:密码质量
- pam_limits.so:资源限制
- pam_tally2.so / pam_faillock.so:登录失败锁定
- pam_google_authenticator.so:2FA
- pam_yubico.so:Yubikey
- pam_ssh_agent_auth.so:SSH agent
2. /etc/shadow
# 字段:用户名:密码哈希:最后修改天数:最小间隔:最大间隔:警告:不活动:过期:保留
# 例: user:$6$salt$hash:19000:0:99999:7:::
# $1$ = MD5, $5$ = SHA-256, $6$ = SHA-512
# 命令
passwd user # 改密码
chage -l user # 看
chage -d 0 user # 强制下次登录改密码
chage -M 90 user # 90 天过期
chage -W 7 user # 提前 7 天警告
chage -I 30 user # 不活动 30 天后过期
passwd -l user # 锁定
passwd -u user # 解锁
usermod -L user # 锁定(改 shadow)
usermod -U user # 解锁
3. 密码策略
# /etc/login.defs
PASS_MAX_DAYS 90
PASS_MIN_DAYS 1
PASS_MIN_LEN 12
PASS_WARN_AGE 7
# /etc/security/pwquality.conf
minlen = 12
minclass = 3
dcredit = -1 # 至少 1 个数字
ucredit = -1 # 至少 1 个大写
lcredit = -1 # 至少 1 个小写
ocredit = -1 # 至少 1 个特殊
4. 登录失败锁定
# /etc/pam.d/sshd 加:
auth required pam_faillock.so preauth deny=5 unlock_time=900
auth [default=die] pam_faillock.so authfail deny=5 unlock_time=900
# 查
faillock --user user
# 解锁
faillock --user user --reset
5. SSH 密钥认证
# 生成密钥
ssh-keygen -t ed25519 -C "comment"
# 旧版 RSA
ssh-keygen -t rsa -b 4096
# 复制公钥
ssh-copy-id user@host
# 手动
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
6. 暴力破解防护
- fail2ban: 自动封禁 IP
- denyhosts: 经典工具
- sshguard: 另一种
- 端口敲门 (port knocking)
# fail2ban
yum install fail2ban
systemctl enable --now fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure
maxretry = 5
bantime = 3600
三、SSH 安全
1. /etc/ssh/sshd_config 关键配置
# 端口
Port 22 # 可改非标准
# 协议
Protocol 2 # 禁用 1
# 禁用 root
PermitRootLogin no # 禁止 root 登录
# 或:PermitRootLogin prohibit-password # 仅密钥
# 密码登录
PasswordAuthentication no # 禁用密码,仅密钥
PermitEmptyPasswords no
# 密钥登录
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# 认证尝试
MaxAuthTries 3
MaxSessions 5
LoginGraceTime 30
# 限制用户
AllowUsers alice bob
AllowGroups sshusers
# 超时
ClientAliveInterval 300
ClientAliveCountMax 0
TCPKeepAlive no
# 日志
LogLevel VERBOSE
# 安全
X11Forwarding no
PermitTunnel no
AllowTcpForwarding local # 仅本地
AllowAgentForwarding no
AllowStreamLocalForwarding no
# 禁用弱加密
Ciphers -chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs -hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512
# 协议版本
Subsystem sftp internal-sftp
2. 重启 SSH
# 测试配置
sshd -t
# 重启
systemctl restart sshd
3. SSH 安全技巧
# 跳板机
ssh -J jumphost user@target
# 代理
ssh -D 1080 user@host # SOCKS 代理
ssh -L 8080:localhost:80 user@host # 本地转发
ssh -R 80:localhost:8080 user@host # 远程转发
# 代理命令
ProxyCommand ssh -W %h:%p jumphost
# 配置文件 ~/.ssh/config
Host myserver
HostName 1.2.3.4
User alice
Port 2222
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
# 跳板
Host *
ProxyCommand ssh -W %h:%p jumphost
4. SSH 端口敲门
# knockd
yum install knockd
# /etc/knockd.conf
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 5
command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 5
command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
四、用户与权限
1. 用户管理
# 列出
cat /etc/passwd
cat /etc/shadow
cat /etc/group
# 用户查询
id username
groups username
whoami
# 改属主
chown user:group file
chown -R user:group dir/
# 改属组
chgrp group file
2. sudo 权限
# /etc/sudoers (用 visudo 编辑)
# /etc/sudoers.d/ 子文件
# 格式
user host=(runas) NOPASSWD: commands
alice ALL=(ALL) ALL
bob ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx
%admins ALL=(ALL) ALL
# 关键配置
Defaults env_reset
Defaults timestamp_timeout=5
Defaults !authenticate
Defaults log_input, log_output
Defaults iolog_dir=/var/log/sudo
# 查
sudo -l
3. 文件权限
# 基础
chmod 755 file
chmod u+x file
chmod -R 644 dir/
# ACL
getfacl file
setfacl -m u:user:rw file
setfacl -m g:group:r file
setfacl -x u:user file
setfacl -b file # 全部移除
setfacl -d -m u:user:rx dir/ # 默认 ACL(目录)
# SUID/SGID/Sticky
chmod u+s file # SUID
chmod g+s dir # SGID (目录继承组)
chmod +t dir # Sticky (仅属主可删)
chmod 4755 file # SUID + 755
4. 特殊权限 (隐藏的危险)
- SUID root 文件 = 任何用户可 root 权限执行
- SGID = 临时提升组权限
- 找出 SUID:
find / -perm -u+s -type f 2>/dev/null
find / -perm -g+s -type f 2>/dev/null
5. 默认权限 (umask)
umask # 看
umask 022 # 文件 644, 目录 755
# 持久化
echo "umask 027" >> /etc/profile
6. 能力 (Capabilities)
# 看进程能力
getpcaps PID
cat /proc/PID/status | grep Cap
# 设能力
setcap cap_net_bind_service=+ep /usr/bin/python3
# 常用能力
cap_net_bind_service # 绑定 < 1024 端口
cap_net_admin # 网络管理
cap_sys_admin # 系统管理
cap_dac_override # 绕过文件权限
cap_chown # 改属主
五、防火墙 (Firewall)
详见 20-Linux网络管理.md 第七节,这里补充安全策略。
1. nftables 安全模板
# 默认拒绝,白名单
nft add table inet filter
nft add chain inet filter input { type filter hook input priority 0 ; policy drop ; }
# 允许本地回环
nft add rule inet filter input iif lo accept
# 允许已建立/相关
nft add rule inet filter input ct state established,related accept
# 允许 SSH (限速防爆破)
nft add rule inet filter input tcp dport 22 ct state new limit rate 4/minute accept
# 允许 HTTP/HTTPS
nft add rule inet filter input tcp dport { 80, 443 } accept
# 允许 ICMP (ping)
nft add rule inet filter input icmp type echo-request accept
# 记录丢弃
nft add rule inet filter input log prefix "nft-drop: " level warn
2. fail2ban (自动封禁)
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = nftables-multiport
chain = INPUT
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
maxretry = 3
findtime = 600
bantime = 3600
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
# 状态
fail2ban-client status
fail2ban-client status sshd
# 解封
fail2ban-client set sshd unbanip 1.2.3.4
六、SELinux 与 AppArmor
1. SELinux (Security-Enhanced Linux)
SELinux:Linux 内核的强制访问控制 (MAC) 机制
- 由 NSA 开发
- 标签化 (Label)
- 策略 (Policy)
- 强制 (Enforcing) / 许可 (Permissive) / 禁用 (Disabled)
状态:
getenforce # 当前模式
sestatus # 详细
setenforce 0 # 临时切 Permissive
setenforce 1 # 切回 Enforcing
配置 (/etc/selinux/config):
SELINUX=enforcing
SELINUXTYPE=targeted
文件上下文:
# 看
ls -Z file
ps auxZ
id -Z
# 改
chcon -t httpd_sys_content_t file
restorecon -R dir/ # 恢复默认
semanage fcontext -a -t httpd_sys_content_t '/mydir(/.*)?'
布尔值:
getsebool -a | grep httpd
setsebool -P httpd_can_network_connect on
SELinux 故障排查:
# 安装
yum install setroubleshoot
# 看拒绝日志
sealert -a /var/log/audit/audit.log
ausearch -m avc,user_avc -ts recent
audit2allow -M mymodule < /var/log/audit/audit.log
semodule -i mymodule.pp
2. AppArmor (Ubuntu/Debian 默认)
AppArmor:Ubuntu 的 MAC 机制,比 SELinux 简单
# 状态
aa-status
# 模式
/etc/apparmor.d/ # 配置文件
enforce # 强制
complain # 警告
disable # 禁用
# 工具
apt install apparmor-utils
aa-genprof program
aa-complain program
aa-enforce program
示例 (/etc/apparmor.d/usr.bin.nginx):
#include <tunables/global>
/usr/sbin/nginx {
#include <abstractions/base>
#include <abstractions/nginx>
capability dac_override,
capability setuid,
capability setgid,
/etc/nginx/** r,
/var/log/nginx/** w,
/var/www/** r,
/run/nginx.pid w,
deny /etc/shadow r,
}
七、内核安全加固
1. 内核参数
# /etc/sysctl.conf
# IP 转发 (路由器开,其他关)
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0
# SYN 攻击防护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 4096
# ICMP 重定向(防中间人)
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# 源路由(防 IP 欺骗)
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# 反 IP 欺骗
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# ICMP 忽略广播
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 启用 BPF JIT
net.core.bpf_jit_enable = 1
# kptr 限制(防内核地址泄露)
kernel.kptr_restrict = 2
# dmesg 限制
kernel.dmesg_restrict = 1
# usermodehelper
kernel.usermodehelper.bset = 0
# modules 自动加载限制
# 阻止未授权模块
# /etc/modprobe.d/blacklist.conf 加黑名单
2. 内核模块安全
# 加载模块
modprobe module_name
# 黑名单
echo "blacklist module" >> /etc/modprobe.d/blacklist.conf
# 禁用自动加载
echo "install module /bin/false" >> /etc/modprobe.d/disable.conf
# 列出已加载
lsmod
3. Secure Boot
mokutil --sb-state # 看状态
mokutil --list-enrolled # 列出已注册
4. 内核 lockdown (LSM)
# 5 = confidentiality
# 4 = integrity
# 1 = confidentiality + integrity
echo 5 > /sys/kernel/security/lockdown
# 持久化
# /etc/sysctl.d/10-lockdown.conf
kernel.lockdown_mode = 5
八、文件完整性检查
1. AIDE (Advanced Intrusion Detection Environment)
yum install aide
# 初始化数据库
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 检查
aide --check
# 更新
aide --update
# 配置
vi /etc/aide.conf
# /etc/ Full
# /boot/ Full
# /bin/ Full
# /sbin/ Full
2. Tripwire
yum install tripwire
# 初始化
tripwire --init
# 检查
tripwire --check
# 更新
tripwire --update
3. mtree (NetBSD 工具)
# 跨 BSD 工具
4. 手动检查
# 找 SUID root
find / -perm -4000 -type f 2>/dev/null
# 找没属主
find / -nouser -o -nogroup 2>/dev/null
# 找大文件
find / -type f -size +100M 2>/dev/null
# 找最近改动的关键文件
find /etc /bin /sbin -mtime -1
九、防病毒与恶意软件
1. ClamAV (开源杀毒)
yum install clamav clamav-update
# 更新病毒库
freshclam
# 扫描
clamscan -r /home
clamscan --infected --remove /tmp
# 守护进程
systemctl enable --now clamd
clamscan -l scan.log -r /var/www
2. rkhunter (Rootkit 检测)
yum install rkhunter
rkhunter --update
rkhunter --check
# 看警告
rkhunter --check --sk
3. chkrootkit
yum install chkrootkit
chkrootkit
4. Malware 检测
- YARA:模式匹配
- Volatility:内存取证
- TheHive / Cortex:事件响应平台
十、网络安全
1. TLS/SSL
# 证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout key.pem -out cert.pem
# 看证书
openssl x509 -in cert.pem -text -noout
openssl s_client -connect example.com:443 -showcerts
# 强配置 (Mozilla 建议)
# 禁用 SSLv3, TLS 1.0/1.1
# 仅 TLS 1.2 / 1.3
# 强加密套件
2. nginx SSL 配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
3. SSH 跳板机
- Bastion Host: 堡垒机
- Teleport: 现代方案
- Apache Guacamole: Web 跳板
- JumpServer: 国产开源
- Tailscale / Nebula: 零信任网络
4. VPN
- WireGuard: 现代, 极快
- OpenVPN: 经典
- IPSec / IKEv2: 企业
- SSH Tunnel: 临时
5. 内网监控
- Snort / Suricata: IDS
- Zeek (Bro): 网络分析
- Wireshark: 抓包
- ntopng: 流量分析
十一、DNS 安全
1. DNS 解析安全
# 阻止 DNS 泄露
# /etc/resolv.conf
nameserver 1.1.1.1
options edns0
2. DoH / DoT
# DoH (DNS over HTTPS)
# systemd-resolved
DNSOverTLS=yes
DNS=https://1.1.1.1/dns-query
# Stubby (DoT)
yum install stubby
3. 本地 DNS 安全
# dnsmasq 防 DNS rebinding
stop-dns-rebind
# bind
allow-recursion { trusted; };
allow-query { trusted; };
十二、加密与解密
1. GPG
# 生成密钥
gpg --full-generate-key
gpg --list-keys
gpg --list-secret-keys
# 加密
gpg -e -r user@example.com file
gpg -o file.gpg -e -r user@example.com file
# 解密
gpg -d file.gpg
gpg -o file -d file.gpg
# 签名
gpg --sign file
gpg --detach-sign file # 分离签名
gpg --verify file.sig file
# 导出公钥
gpg --export --armor user > pubkey.asc
gpg --import pubkey.asc
2. 加密文件系统
# LUKS 加密磁盘
cryptsetup luksFormat /dev/sda1
cryptsetup open /dev/sda1 my_crypt
mkfs.ext4 /dev/mapper/my_crypt
mount /dev/mapper/my_crypt /mnt
# 单文件加密
gpg -c secret_file # 对称加密
openssl enc -aes-256-cbc -salt -in file -out file.enc
3. SSH 密钥
# 生成
ssh-keygen -t ed25519
# 转换格式
ssh-keygen -p -f key # 改密码
ssh-keygen -i -m PKCS8 -f key > key_pem # 转换
# 看
ssh-keygen -l -f key
ssh-keygen -y -f key > pub.key # 提取公钥
4. 哈希
md5sum file
sha1sum file
sha256sum file
sha512sum file
# 看 /etc/shadow
getent shadow user | cut -d: -f2 | base64 -d | sha512sum
十三、容器安全
1. 容器安全最佳实践
- 最小镜像:alpine、distroless
- 非 root 用户:USER 1000
- 只读根文件系统:--read-only
- 能力最小化:--cap-drop ALL
- seccomp:--security-opt seccomp=unconfined
- AppArmor / SELinux:策略
docker run -d \
--read-only \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--security-opt no-new-privileges \
--security-opt seccomp=profile.json \
--user 1000 \
--read-only \
nginx
2. 镜像扫描
- Trivy: 开源, 多平台
- Clair: Quay 配套
- Snyk: 商业
- Aqua: 商业
- Docker Scout: Docker 官方
- Anchore: 商业
trivy image nginx:latest
trivy fs /path/to/code
trivy k8s cluster
3. K8s 安全
- Pod Security Standards: Restricted / Baseline / Privileged
- Network Policies: 网络隔离
- RBAC: 权限控制
- OPA / Kyverno: 策略引擎
- Falco: 运行时安全
- gVisor / Kata: 安全沙箱
十四、安全审计与合规
1. Lynis (系统审计)
yum install lynis
lynis audit system
# 输出报告
cat /var/log/lynis-report.dat
2. OpenSCAP (合规扫描)
yum install openscap-scanner
oscap info /usr/share/openscap/scap-yyyy/.../ssg-rhel9-ds.xml
oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis \
--results results.xml \
--report report.html \
/usr/share/openscap/scap-yyyy/.../ssg-rhel9-ds.xml
3. CIS Benchmarks
- CIS Benchmarks: 全球公认安全基线
- DISA STIG: 美国国防部
- NIST: 美国国家标准
4. 合规框架
- PCI-DSS: 支付卡
- HIPAA: 美国医疗
- GDPR: 欧盟隐私
- ISO 27001: 信息安全
- SOC 2: 服务组织控制
- 等保 2.0: 中国
十五、应急响应
1. 应急响应流程
- 准备 (Preparation): 预案、工具
- 识别 (Identification): 检测事件
- 遏制 (Containment): 限制损害
- 根除 (Eradication): 清除威胁
- 恢复 (Recovery): 恢复正常
- 事后 (Lessons Learned): 总结改进
2. 应急响应工具
- GRR: Google 远程响应
- OSQuery: Facebook 系统查询
- TheHive: 事件管理
- Cortex: 响应引擎
- MISP: 威胁情报
- Velociraptor: 端点可见性
3. 取证检查清单
# 进程
ps auxf
ps aux | grep -v '\['
ls /proc/*/exe 2>/dev/null | xargs -I {} readlink {} | sort -u
# 网络
ss -tulnp
netstat -tulnp
arp -a
ip neigh
# 账号
cat /etc/passwd
cat /etc/shadow
last
lastb
lastlog
who -a
# 文件
find / -mtime -3 -type f 2>/dev/null
find / -size +100M -type f 2>/dev/null
find / -name "*.sh" -mtime -7
find / -perm -u+s 2>/dev/null
ls -la /tmp /var/tmp /dev/shm
# 日志
journalctl --since "-1d" -p err
grep -i "error\|fail\|warning" /var/log/messages | tail
last -100
4. 关键检查点
- 账号: 不明用户、UID=0
- 进程: 不明进程
- 网络: 异常连接、监听端口
- 文件: 修改、可疑、SUID
- 日志: 异常
- 计划任务: cron, systemd timer
- 启动项: rc.local, systemd
- 内核模块: lsmod, modprobe
十六、密码与认证
1. 强密码原则
- 长度 > 复杂度: 12+ 字符
- 多样字符: 大小写数字特殊
- 不重用: 不同系统不同密码
- 定期改: 90 天
- 用密码管理器: KeePass、Bitwarden
- 不记在纸上: 用工具
2. 多因素认证 (MFA)
- TOTP: Google Authenticator
- 硬件 token: Yubikey
- 生物识别:指纹、面部
- 推送通知: 钉钉、企微
# Google Authenticator
google-authenticator
# /etc/pam.d/sshd
auth required pam_google_authenticator.so
3. SSO / 联合身份
- SAML: 企业 SSO
- OAuth 2.0: 授权
- OIDC: 身份认证
- LDAP / AD: 目录
- Kerberos: 票据认证
- FreeIPA: 综合
- Keycloak: 开源 IDM
- Auth0 / Okta: 商业
十七、Linux 漏洞与更新
1. 软件更新
# RHEL/CentOS
yum update
yum update --security # 仅安全更新
yum check-update # 查更新
# Debian/Ubuntu
apt update && apt upgrade
apt update && apt full-upgrade
unattended-upgrade # 自动更新
# SUSE
zypper update
zypper patch
2. 内核升级
# 查当前
uname -r
# RHEL
yum install kernel
# 选新版启动
# Ubuntu
apt install linux-image-generic
# 旧内核清理
yum remove $(yum list installed 'kernel*' | grep -v "$(uname -r)")
package-cleanup --oldkernels --count=2
3. 漏洞扫描
- OpenVAS: 开源漏洞扫描
- Nessus: 商业
- Qualys: 商业
- Trivy: 容器
- lynis: 系统
# OpenVAS
openvas-start
# Web UI: https://localhost:9392
十八、零信任 (Zero Trust)
1. 零信任原则
- 从不信任,始终验证
- 最小特权
- 持续验证(不只是登录时)
- 微分段(细粒度网络隔离)
- 加密所有
2. 零信任实现
- BeyondCorp (Google): 基于设备/身份/上下文
- Tailscale / ZeroTier: 简单 VPN 替代
- Cloudflare Access: 边缘零信任
- Cloudflare Tunnel: 内网穿透
- Tailscale: 简单, 适合小团队
- Headscale: Tailscale 自托管
- OpenZiti: 开源零信任
- Teleport: 基础设施访问
3. 设备信任
- 设备证书
- 设备态势
- MDM (移动设备管理)
- 持续评估
十九、Linux 加固清单 (CIS)
1. 初始加固
# 系统更新
yum update -y
# 启用防火墙
systemctl enable --now firewalld
firewall-cmd --add-service=ssh
firewall-cmd --reload
# 启用 SELinux
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1
# 启用 auditd
yum install audit
systemctl enable --now auditd
2. SSH 加固
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 0
X11Forwarding no
AllowTcpForwarding no
3. 用户与认证
# 锁定不需要的账号
for user in $(awk -F: '($3 < 1000) {print $1}' /etc/passwd); do
if [ "$user" != "root" ]; then
usermod -L $user
usermod -s /sbin/nologin $user
fi
done
# 设置密码策略
# /etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
lcredit = -1
ocredit = -1
# 失败锁定
# /etc/pam.d/system-auth
auth required pam_faillock.so preauth deny=5 unlock_time=900
4. 文件权限
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
chmod 644 /etc/passwd
chmod 644 /etc/group
chmod 600 /etc/ssh/ssh_host_*_key
chmod 644 /etc/ssh/ssh_host_*_key.pub
5. 内核参数
# /etc/sysctl.d/99-security.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.tcp_syncookies = 1
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
6. 日志和审计
# auditd 规则
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k identity
-w /var/log/audit/ -p wa -k audit
7. 服务最小化
# 禁用不需要的服务
for service in telnet rsh rlogin rexec ypbind; do
systemctl disable $service
systemctl stop $service
done
二十、安全工具速查
| 任务 | 工具 |
|---|---|
| 漏洞扫描 | OpenVAS、Nessus |
| 入侵检测 | AIDE、Tripwire、OSSEC |
| 防病毒 | ClamAV |
| Rootkit 检测 | rkhunter、chkrootkit |
| 系统审计 | Lynis、OpenSCAP |
| 日志分析 | journalctl、ELK、Loki |
| 网络嗅探 | tcpdump、Wireshark、Zeek |
| 入侵检测(网络) | Snort、Suricata |
| 密码破解 | hashcat、John the Ripper |
| 渗透测试 | Metasploit、Kali |
| 镜像安全 | Trivy、Clair、Snyk |
| K8s 安全 | Falco、OPA、Kyverno |
| 零信任 | WireGuard、Teleport、Tailscale |
| 沙箱 | Firejail、bubblewrap |
| 取证 | Volatility、TheHive |
| 威胁情报 | MISP、AlienVault OTX |
二十一、核心要点速记
- 最小特权 = 给必要的权限
- 纵深防御 = 多层防护
- 默认拒绝 = 防火墙默认 DROP
- SSH:禁 root、禁密码、用密钥、改端口
- 防火墙 = 必备,iptables/nftables/firewalld/ufw
- SELinux = 强制,关键服务器必开
- PAM = 认证框架,改密码策略
- fail2ban = 自动封禁暴力破解
- AIDE = 文件完整性监控
- ClamAV = 杀病毒
- rkhunter/chkrootkit = Rootkit 检测
- 审计:auditd + ausearch + aureport
- 日志 = journalctl + ELK
- 密码:12+ 字符、定期改、用密码管理器
- MFA = 多因素,必开
- SSH 密钥 = ed25519, 4-8 KB 安全
- 更新 = 及时装安全更新
- 零信任 = 永不信任,持续验证
- 等保 2.0 = 中国合规必过
- CIS Benchmarks = 加固基线
- /etc/shadow 600,/etc/passwd 644
- find -perm -u+s = 找 SUID
- kptr_restrict = 2 = 防内核地址泄露
- rp_filter = 1 = 反 IP 欺骗
- accept_source_route = 0 = 禁源路由
- sshd_config:PermitRootLogin no
- PermitEmptyPasswords no
- PasswordAuthentication no
- MaxAuthTries 3
- ClientAliveInterval 300
- firewalld/nftables 必备
- Lynis audit system = 系统自检
- tcpdump -i any -w file.pcap = 抓包