作为IT工程师,我可以提供一套全面的CentOS系统安全加固方案,以防止恶意程序的自动加载。以下是具体措施:
# 禁用不必要的服务
systemctl disable cups
systemctl disable avahi-daemon
systemctl disable postfix # 如果不需要邮件服务
# 查看所有服务状态
systemctl list-unit-files --type=service
# 检查系统定时任务
crontab -l # 当前用户
crontab -u root -l # root用户
ls -la /etc/cron* # 系统定时任务目录
cat /etc/crontab
# 检查anacron任务
ls -la /etc/anacrontab
# 检查系统启动项
chkconfig --list
ls -la /etc/rc.d/rc[0-6].d/
# 检查用户启动项
ls -la ~/.config/autostart/ # 图形界面用户
# 安装aide进行文件完整性检查
yum install aide -y
aide --init
mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz
aide --check
# 确保SELinux处于enforcing模式
getenforce
# 如果未启用
vim /etc/selinux/config # 设置为SELINUX=enforcing
# 编辑sysctl.conf
vim /etc/sysctl.conf
# 添加以下内容
kernel.exec-shield = 1
kernel.randomize_va_space = 2
fs.suid_dumpable = 0
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.tcp_syncookies = 1
# 应用设置
sysctl -p
# 限制root直接登录
vim /etc/ssh/sshd_config
# 设置 PermitRootLogin no
# 设置umask
vim /etc/profile
# 添加 umask 027
# 限制su命令
vim /etc/pam.d/su
# 添加 auth required pam_wheel.so use_uid
usermod -G wheel your_admin_user
# 关键目录设置不可修改属性
chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/sudoers
chattr +i /etc/ssh/sshd_config
chattr +i /etc/resolv.conf
# 检查setuid/setgid文件
find / -perm -4000 -o -perm -2000 -type f -exec ls -ld {} \;
# 配置日志轮转
vim /etc/logrotate.conf
# 安装并配置logwatch
yum install logwatch -y
vim /etc/logwatch/conf/logwatch.conf
# 设置自动安全更新
yum install yum-cron -y
vim /etc/yum/yum-cron.conf
# 设置 update_cmd = security
# 设置 apply_updates = yes
systemctl enable yum-cron
systemctl start yum-cron
# 安装配置firewalld
yum install firewalld -y
systemctl enable firewalld
systemctl start firewalld
# 基本规则
firewall-cmd --permanent --remove-service=dhcpv6-client
firewall-cmd --permanent --zone=public --add-service=ssh
firewall-cmd --reload
# 安装配置OSSEC
yum install -y epel-release
yum install -y ossec-hids-server
/var/ossec/bin/ossec-control start
实施以上措施后,您的CentOS系统将大大降低被恶意程序自动加载的风险。建议定期审计系统状态,保持安全更新,并监控异常日志。