前 言 Linux系统被应用于大部分企业的服务器上,因此在等保测评中主机加固也是必须要完成的一项环节。 由于在之后项目开始要进行主机加固,因此对linux的加固流程进行总结学习。 Linux的主机加固主要分为:账号安全、认证授权、协议安全、审计安全。简而言之,就是4A(统一安全管理平台解决方案)。 这边就使用我自己kali的虚拟机进行试验学习。 一、账户安全1、口令生存期gedit /etc/login.defs 在此处对密码的长度、时间、过期警告进行修改 PASS_MAX_DAYS 90 #密码最长过期天数 PASS_MIN_DAYS 10 #密码最小过期天数 PASS_WARN_AGE 7 #密码过期警告天数
如果修改设置有最小长度也需要修改 PASS_MIN_LEN 8 #密码最小长度 2、口令复杂度(很重要)password requisite pam_cracklib.so在文件中找到 password requisite pam_cracklib.so将其修改为: password requisite pam_cracklib.so try_first_pass retry=3 dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=8备注:至少包含一个数字、一个小写字母、一个大写字母、一个特殊字符、且密码长度>=8 3、版本信息cat /proc/version 4、限制xx用户登录/etc/hosts.deny 添加内容: sshd : 192.168.1.1
禁止192.168.1.1对服务器进行ssh的登陆 5、检查是否有其他uid=0的用户awk -F “:” '($3==0) {print $1} ' /etc/passwd6、登陆超时限制 cp -p /etc/profile /etc/profile_bak(备份)gedit /etc/profile增加 TMOUT=300export TMOUT或者 echo 'export TMOUT=300'>>/etc/profileecho 'readonly TMOUT' >>/etc/profilesource /etc/profile7、检查是否使用PAM认证模块禁止wheel组之外的用户su为rootgedit /etc/pam.d/su新增 auth sufficient pam_rootok.soauth required pam_wheel.so use_uid备注:auth与sufficient之间由两个tab建隔开,sufficient与动态库路径之间使用一个tab建隔开 8、禁用无用账户cat /etc/passwd #查看口令文件,确认不必要的账号。 passwd -l user # 锁定不必要的账号 9、账户锁定gedit /etc/pam.d/system-auth在文件中修改或者添加 auth required pam_tally.so onerr=fail deny=3 unlock_time=7200锁定账户举例: passwd -l binpasswd -l syspasswd -l adm10、检查系统弱口令 john /etc/shadow --singlejohn /etc/shadow --wordlist=pass.dic我这边有报错 就不展示了 使用passwd 用户 命令为用户设置复杂的密码 二、协议安全1、openssh升级(按需做)yum update openssh 2、定时任务(防止病毒感染)定时任务检查: crontab -l 一次性任务检查: at -l 3、限制ssh登录(看是否需要)首先cat /etc/ssh/sshd_config 查看PermitRootLogin是否为no gedit /etc/ssh/sshd_config PermitRootLogin no不允许root登陆 Protocol 2 修改ssh使用的协议版本 MaxAuthTries 3 修改允许密码错误次数 或echo "tty1" > /etc/securetty hmod 700 /root 4、限制su为root用户gedit /etc/pam.d/su 在头部添加 auth required /lib/security/pam_wheel.so group=wheel 5、禁止root用户登录ftp因为我的kali下没有这个文件,因此借鉴一下网上的 cat /etc/pam.d/vsftpdAuth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed#其中file=/etc/vsftpd/ftpusers即为当前系统上的ftpusers文件. echo “root” >> /etc/vsftpd/ftpusersv6、防止flood攻击gedit /etc/sysctl.conf增加net.ipv4.tcp_syncookies = 1 然后sysctl -p 7、禁pingecho 0 > /proc/sys/net/ipv4/icmp_echo_igore_all 8、检查异常进程ps aux|sort -rn -k +3|head #检查cpu占用前10 ps aux|sort -rn -k +4|head #检查内存占用前10 9、关闭无效的服务及端口比如邮箱 service postfix status chkconfig --del postfix chkconfig postfix off
比如cpus service cups status chkconfig --del cups chkconfig cups off 10、设置防火墙策略或者用防火墙策略: service iptables status echo '请根据用户实际业务端口占用等情况进行设置!' 例如: gedit /etc/sysconfig/iptables 添加如下策略 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT-A INPUT -m state --state NEW -m udp -p udp --dport 8080 -j ACCEPT以下举例: iptables -I INPUT -s 22.48.11.11 -j DROP # 22.48.11.11的包全部屏蔽 iptables -I INPUT -s 22.48.11.0/24 -j DROP #22.48.11.1到22.48.11.255的访问全部屏蔽 iptables -I INPUT -s 192.168.1.1 -p tcp --dport 80 -j DROP # 192.168.1.1的80端口的访问全部屏蔽 iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j DROP #192.168.1.1到192.168.1.255的80端口的访问全部屏蔽 service iptabels restart #重启防火墙 11、设置历史记录数量cp /etc/profile /etc/profile_xu_bak(备份) sed -i s/'HISTSIZE=1000'/'HISTSIZE=5000'/g /etc/profile(修改) cat /etc/profile |grep HISTSIZE|grep -v export(检查) 三、认证权限1、配置用户最小权限chmod 644 /etc/passwd chmod 400 /etc/shadow chmod 644 /etc/group 2、文件与目录缺省权限控制cp /etc/profile /etc/profile.bak(备份) gedit /etc/profile
增加 umask 027 source /etc/profile 四、日志审计1、启用远程日志功能gedit /etc/rsyslog.conf *.* @Syslog日志服务器IP ###注意:*和@之间存在的是tab键,非空格。 2、检查是否记录安全事件日志gedit /etc/syslog.conf 或者 /etc/rsyslog.conf 在文件中加入如下内容: *.err;kern.debug;daemon.notice /var/log/messages chmod 640 /var/log/messages service rsyslog restart 3、日志保留半年以上cp/etc/logrotate.conf /etc/logrotate.conf_xu_bak(备份) sed -i s/'rotate 4'/'rotate 12'/g /etc/logrotate.conf(修改) service syslog restart(重启) cat /etc/logrotate.conf |grep -v '#' |grep rotate(检查)
|