windows加固脚本_Linux主机自动加固脚本

    该脚本加固效果符合等保测评、风险评估、基线检查等安全标准的要求。我以某云主机CentOS7.2系统为例进行演示,在进行加固前我们简单检查一下系统原始配置信息,执行命令

cat /etc/login.defs

d8e2e00fa213978dc74a96704426a0a7.png    可以看到虽然有配置密码策略,但不符合合规标准里“密码最长使用期限90天”的要求。继续执行命令

cat /etc/pam.d/system-auth

a3e30a01fbe508ebfe866f53b797f574.png    默认未配置登陆失败处理策略。


    由此可见系统版本较新的云主机虽然默认配置了一定的安全策略,但依然存在不少不符合安全合规标准要求的地方。分别执行以下命令完成脚本创建和执行

touch firm.sh         #创建一个名为firm的.sh文件vi firm.sh            #编辑文件,在此我们需将脚本内容复制进文件chmod +x firm.sh      #给脚本赋予可执行权限sh firm.sh            #执行脚本

a96bc9e42f25bc83ade030ddf59b9d6e.png

    加固完成,个别修改项需重启系统生效。因为脚本内容禁用了22端口,所以下面我只能通过VNC方式来完成登录,在使用过程中我们需要根据实际需求对脚本内容进行增删。

    抽查加固效果,执行命令

cat /etc/pam.d/system-auth

7196c87992ae52428168fe47df30bec8.png    可以看到连续登录失败三次,锁定5分钟,密码至少10位,且须由字母数字下划线三种类型组成。继续执行命令

cat /etc/login.defs

4e8a96c97ed453fc91c9f5c7d52d7db2.png

    可以看到密码最长使用期限已改为90天。


    以下为脚本内容,Linux系统没有Windows复杂,所以要加固的内容也相对较少。

#!/bin/bash    #1.修改默认密码策略#备份文件cp /etc/login.defs   /etc/login.defs.baksed -i 's/99999/90/' /etc/login.defs  #2.设置3次输错密码锁定用户#备份文件cp /etc/pam.d/system-auth /etc/pam.d/system-auth.baksed -e '5 i\auth        required      /lib64/security/pam_tally2.so deny=3 unlock_time=300' -i /etc/pam.d/system-auth#3.密码复杂度设置sed -e "14 i\password    requisite     pam_cracklib.so minlen=10 difok=3 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=-1 try_first_pass retry=3" -i /etc/pam.d/system-authsed -e '15d'  -i /etc/pam.d/system-auth  #4.修改/etc/profile#备份文件cp /etc/profile /etc/profile.baksed -e 's/HISTSIZE=1000/HISTSIZE=3000/' -i /etc/profilesed -i "/HISTSIZE=3000/a HISTTIMEFORMAT=\"%F %T \"" /etc/profilesed -i "/HISTSIZE=3000/a TMOUT=1800" /etc/profilesed -i "$ a\export HISTORY_FILE=/var/log/useraudit.log" /etc/profilesed -i "$ a\export PROMPT_COMMAND=\'{ h=\`history 1\`;w=\`who am i\`;echo -e \$(date \"+%Y-%m-%d %H:%M:%S\") --- \$w ---\$h;} >> \$HISTORY_FILE\'" /etc/profile  /bin/touch /var/log/useraudit.log/bin/chmod 777 /var/log/useraudit.log/usr/bin/chattr +a /var/log/useraudit.log  #5.修改sshd配置#备份文件cp /etc/ssh/sshd_config   /etc/ssh/sshd_config.baksed -e 's/\#PermitRootLogin yes/PermitRootLogin no/' -i /etc/ssh/sshd_configsed -e 's/\#Port 22/Port 2222/' -i /etc/ssh/sshd_configsed -e 's/\#UseDNS yes/UseDNS no/' -i /etc/ssh/sshd_configsed -e 's/X11Forwarding yes/X11Forwarding no/' -i /etc/ssh/sshd_configsed -e 's/GSSAPIAuthentication yes/\#GSSAPIAuthentication yes/' -i /etc/ssh/sshd_configsed -e 's/GSSAPICleanupCredentials yes/\#GSSAPICleanupCredentials yes/' -i /etc/ssh/sshd_config#/etc/init.d/sshd restart  #6.禁用不需要的服务/sbin/chkconfig telnet off/sbin/chkconfig ip6tables off/sbin/chkconfig iptables off/sbin/chkconfig avahi-daemon off/sbin/chkconfig avahi-dnsconfd off  #7.数据库服务器这里请酌情配置#备份文件#cp /etc/pam.d/login /etc/pam.d/login.bak#cp /etc/security/limits.conf  /etc/security/limits.conf.bak#sed -i "$ a\session    required     /lib64/security/pam_limits.so" /etc/pam.d/login#sed -i "$ a\*  soft  nofile          65535" /etc/security/limits.conf#sed -i "$ a\*  hard  nofile          65535" /etc/security/limits.conf  echo "please restart the server for the settings to take effect,press enter exit" read pauseexit 1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值