关于 hardening-linux-endpoint-with-cis-benchmark
面向管理员的 Linux 端点加固技能,依据 CIS 基准提供操作流程。涵盖文件系统、服务、网络、访问控制、审计和日志设置,并介绍使用 OpenSCAP 评估及可选的 Ansible 修复。
主要功能
- 01符合 CIS 的 Linux 加固流程
- 02文件系统与服务配置
- 03SSH 和访问控制设置
- 04审计与日志配置
- 05OpenSCAP 基准评估
- 06可选的 Ansible 修复
适用场景
- 01按 CIS 基准加固 Linux 服务器
- 02为 Linux 端点合规审查做准备
- 03修复安全审计发现的问题
- 04使用 Ansible 自动化基线变更
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill hardening-linux-endpoint-with-cis-benchmark安装后在 Claude Code 中输入/hardening-linux-endpoint-with-cis-benchmark确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
使用 CIS Benchmark 加固 Linux 端点
何时使用
在以下情况下使用本技能:
- 按照 CIS benchmarks 加固 Linux 服务器(Ubuntu、RHEL、CentOS、Debian)
- 使用 Ansible、OpenSCAP 或 shell 脚本自动化 Linux 安全基线
- 满足 Linux 端点的合规要求(PCI DSS、HIPAA、SOC 2)
- 修复漏洞扫描或安全审计发现的问题
不要使用于 Windows 加固(请使用 hardening-windows-endpoint-with-cis-benchmark)。
前提条件
- 对目标 Linux 端点具有 root 或 sudo 访问权限
- 目标发行版的 CIS Benchmark PDF(来自 cisecurity.org)
- 用于自动化评估的 OpenSCAP 或 CIS-CAT
- 用于企业级修复的 Ansible(可选)
工作流
步骤 1:文件系统配置(第 1 节)
# 1.1.1 Disable unused filesystems
cat >> /etc/modprobe.d/CIS.conf << 'EOF'
install cramfs /bin/true
install freevxfs /bin/true
install jffs2 /bin/true
install hfs /bin/true
install hfsplus /bin/true
install squashfs /bin/true
install udf /bin/true
EOF
# 1.1.2 Ensure /tmp is a separate partition with nodev,nosuid,noexec
# /etc/fstab entry:
# tmpfs /tmp tmpfs defaults,rw,nosuid,nodev,noexec,relatime 0 0
systemctl unmask tmp.mount
systemctl enable tmp.mount
# 1.1.8 Ensure nodev option on /dev/shm
mount -o remount,nodev,nosuid,noexec /dev/shm
echo "tmpfs /dev/shm tmpfs defaults,nodev,nosuid,noexec 0 0" >> /etc/fstab
# 1.4 Secure boot settings
chown root:root /boot/grub/grub.cfg
chmod 600 /boot/grub/grub.cfg
# Set GRUB password
grub-mkpasswd-pbkdf2 # Generate hash, add to /etc/grub.d/40_custom
步骤 2:服务和网络(第 2-3 节)
# 2.1 Disable unnecessary services
systemctl disable --now avahi-daemon
systemctl disable --now cups
systemctl disable --now rpcbind
systemctl disable --now xinetd
# 2.2 Ensure NTP is configured
apt install chrony -y # or systemd-timesyncd
systemctl enable --now chrony
# 3.1 Network parameters (host only, not router)
cat >> /etc/sysctl.d/99-cis.conf << 'EOF'
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.tcp_syncookies = 1
net.ipv6.conf.all.accept_ra = 0
net.ipv6.conf.default.accept_ra = 0
EOF
sysctl --system
# 3.4 Configure firewall (UFW or firewalld)
ufw enable
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
步骤 3:访问控制(第 4-5 节)
# 5.2 SSH Server Configuration (/etc/ssh/sshd_config)
sed -i 's/#Protocol 2/Protocol 2/' /etc/ssh/sshd_config
cat >> /etc/ssh/sshd_config << 'EOF'
LogLevel VERBOSE
MaxAuthTries 4
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
X11Forwarding no
MaxStartups 10:30:60
LoginGraceTime 60
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 3
EOF
systemctl restart sshd
# 5.3 Password policy (PAM)
# /etc/security/pwquality.conf
minlen = 14
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
# 5.4 User account settings
# /etc/login.defs
PASS_MAX_DAYS 365
PASS_MIN_DAYS 1
PASS_WARN_AGE 7
# Lock inactive accounts
useradd -D -f 30
步骤 4:审计和日志记录(第 4 节)
# Install and configure auditd
apt install auditd audispd-plugins -y
systemctl enable --now auditd
# /etc/audit/rules.d/cis.rules
cat > /etc/audit/rules.d/cis.rules << 'EOF'
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d/ -p wa -k scope
-w /var/log/sudo.log -p wa -k actions
-a always,exit -F arch=b64 -S adjtimex -S settimeofday -k time-change
-a always,exit -F arch=b64 -S sethostname -S setdomainname -k system-locale
-w /etc/group -p wa -k identity
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /var/log/faillog -p wa -k logins
-w /var/log/lastlog -p wa -k logins
-a always,exit -F arch=b64 -S chmod -S fchmod -S fchmodat -k perm_mod
-a always,exit -F arch=b64 -S unlink -S rmdir -S rename -k delete
-w /sbin/insmod -p x -k modules
-w /sbin/modprobe -p x -k modules
-e 2
EOF
augenrules --load
# Configure rsyslog for remote logging
echo "*.* @@syslog-server.corp.com:514" >> /etc/rsyslog.d/50-remote.conf
systemctl restart rsyslog
步骤 5:使用 OpenSCAP 进行评估
# Install OpenSCAP
apt install openscap-scanner scap-security-guide -y
# Run CIS benchmark assessment
oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_level1_server \
--results /tmp/cis_results.xml \
--report /tmp/cis_report.html \
/usr/share/xml/scap/ssg/content/ssg-ubuntu2204-ds.xml
# View HTML report in browser for detailed results
关键概念
| 术语 | 定义 |
|---|---|
| OpenSCAP | 用于自动化合规的开源 SCAP(Security Content Automation Protocol)扫描器 |
| auditd | 用于监控系统调用和文件访问的 Linux 审计框架 |
| PAM | 可插拔认证模块;Linux 的可配置认证框架 |
| sysctl | 用于网络和系统安全调优的 Linux 内核参数配置 |
| AIDE | Advanced Intrusion Detection Environment;Linux 的文件完整性检查器 |
工具和系统
- OpenSCAP:用于 Linux 的自动化 CIS benchmark 评估
- Ansible Lockdown:用于自动化 CIS benchmark 修复的 Ansible 角色
- Lynis:用于 Linux/Unix 系统的开源安全审计工具
- AIDE:用于 Linux 端点的文件完整性监控
- auditd:用于系统调用监控的 Linux 审计框架
常见陷阱
- 将服务器 benchmark 应用于工作站:CIS 为服务器和工作站配置文件提供了单独的 benchmark。服务器 benchmark 会禁用桌面服务。
- 破坏 SSH 访问:错误配置 sshd_config(尤其是 PermitRootLogin、PasswordAuthentication)可能会将管理员锁在外面。始终从第二个会话测试 SSH 配置更改。
- 未测试防火墙规则:在未先允许 SSH 的情况下启用 UFW 将永久断开远程会话。
- 未测试就更改内核参数:某些 sysctl 设置可能会破坏应用程序网络。请先在预发布环境中测试。
评论
支持 Markdown