作为IT工程师,我可以为您提供一套全面的CentOS安全配置方案,以防止恶意代码的自动执行。以下是详细的安全措施:
# 编辑/etc/fstab文件,为关键分区添加安全挂载选项
/dev/sda1 / ext4 defaults,nodev,nosuid,noexec 1 1
/dev/sda2 /home ext4 defaults,nodev,nosuid 1 2
/tmp /tmp tmpfs defaults,nosuid,nodev,noexec 0 0
noexec
: 禁止执行该分区上的二进制文件nosuid
: 忽略SUID/SGID权限nodev
: 禁止设备文件chmod 700 /root
chmod 1777 /tmp
chmod 755 /bin /usr/bin /sbin /usr/sbin
chmod 644 /etc/passwd /etc/group
chmod 600 /etc/shadow /etc/gshadow
# 限制普通用户访问关键目录
setfacl -R -m u:username:r-x /etc
setfacl -R -m u:username:r-x /bin /sbin /usr/bin /usr/sbin
# 禁用root远程登录
sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
systemctl restart sshd
# 编辑/etc/sudoers文件,限制用户权限
username ALL=(ALL) /usr/bin/yum, /usr/bin/systemctl restart httpd
# 确保SELinux处于强制模式
setenforce 1
sed -i 's/SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
# 安装管理工具
yum install -y policycoreutils-python setools-console
systemctl enable firewalld
systemctl start firewalld
# 只开放必要端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --remove-service=dhcpv6-client
firewall-cmd --reload
yum install -y rkhunter chkrootkit clamav
# 更新ClamAV病毒库
freshclam
# 定期扫描
rkhunter --update
rkhunter --propupd
rkhunter --check
yum install -y aide
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
# 设置定期检查
echo "0 5 * * * root /usr/sbin/aide --check" > /etc/cron.d/aide-check
systemctl mask atd.service
systemctl stop atd.service
# 限制cron使用
echo "ALL" > /etc/cron.allow
echo "root" >> /etc/cron.allow
# 防止/tmp目录执行脚本
mount -o remount,noexec /tmp
# 限制用户目录执行权限
find /home -type f -name "*.sh" -exec chmod -x {} \;
find /home -type f -name "*.py" -exec chmod -x {} \;
yum install -y yum-cron
sed -i 's/apply_updates = no/apply_updates = yes/' /etc/yum/yum-cron.conf
systemctl enable yum-cron
systemctl start yum-cron
yum install -y logwatch
echo "LogLevel INFO" >> /etc/ssh/sshd_config
systemctl restart rsyslog
systemctl restart sshd
每月检查:
每周任务:
每日任务:
通过以上配置,您的CentOS系统将具备较强的防御能力,能有效防止恶意代码的自动执行。请根据实际业务需求调整部分设置,并确保定期检查和更新安全策略。