Linux 系统安全加固篇之安全加固脚本

该专栏内的脚本都会定期更新,请注意变化
脚本适用于Centos 7.x系列,同样支持Redhat 7.x系列
使用之前建议通读脚本注释,并确认不会影响你现在在用的业务
注意脚本内部包含一定的参数,这些参数比较重要,涉及用户、NTP第三放服务器地址等

#!/bin/bash

#################################################################
## Linux 设备合规配置脚本
## 基于 Centos7
## 在执行之前请务必先浏览一遍 有一些重要的信息需要留意
## 注意,此脚本有些部分只能执行一遍,如果执行多次会产生错误
## Update at: 20201208
## 增加了NTP 配置的部分
#################################################################
USER_NAME=
USER_PASSWD=
NTP_SERVER=


#################################################################
## 设置屏幕保护
echo "=== 正在设置屏幕保护"
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \
--type bool \
--set /apps/gnome-screensaver/idle_activation_enabled true
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \
--type bool \
--set /apps/gnome-screensaver/lock_enabled true
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \
--type string \
--set /apps/gnome-screensaver/mode blank-only
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory \
--type int \
--set /apps/gnome-screensaver/idle_delay 15


#################################################################
## 设置ssh警告banner
touch /etc/sshbanner
chown bin:bin /etc/sshbanner
chmod 644 /etc/sshbanner
echo " Authorized users only. All activity may be monitored and reported "   >/etc/sshbanner
echo "Banner /etc/sshbanner" >> /etc/ssh/sshd_config
echo "=== 正在重启 sshd 服务"
systemctl restart sshd


#################################################################
## 设置口令生存周期
echo "=== 正在设置口令生存周期"
cp -p /etc/login.defs /etc/login.defs_bak
sed -i "s/^PASS_MIN_LEN.*/PASS_MIN_LEN   8 /g" /etc/login.defs
sed -i "s/^PASS_MAX_DAYS.*/PASS_MAX_DAYS   90 /g" /etc/login.defs


#################################################################
## 设置ssh登录成功后的警告banner
echo "=== 正在设置ssh登录成功后的警告banner"
echo " Authorized users only. All activity may be monitored and reported " > /etc/motd


#################################################################
## 新建用户账户
echo "=== 正在创建自用账户"
useradd $USER_NAME
echo $USER_PASSWD | passwd $USER_NAME --stdin  &>/dev/null
echo "$USER_NAME    ALL=(ALL)    NOPASSWD: ALL">> /etc/sudoers


#################################################################
## 删除无用账户
echo "=== 正在删除无用账户"
userdel gdm
userdel listen
userdel webservd
userdel nobody4
userdel noaccess


#################################################################
## 设置用户缺省MASK
echo "=== 正在设置用户缺省MASK"
cp -p /etc/profile /etc/profile_bak
cp -p /etc/csh.login /etc/csh.login_bak
cp -p /etc/csh.cshrc /etc/csh.cshrc_bak
cp -p /etc/bashrc /etc/bashrc_bak
cp -p /root/.bashrc /root/.bashrc_bak
cp -p /root/.cshrc /root/.cshrc_bak
echo "umask 027" >> /etc/profile
echo "umask 027" >> /etc/csh.login
echo "umask 027" >> /etc/csh.cshrc
echo "umask 027" >> /etc/bashrc
echo "umask 027" >> /root/.bashrc
echo "umask 027" >> /root/.cshrc


#################################################################
## 设置登录超时
echo "=== 正在设置登录超时"
cp -p /etc/profile /etc/profile_bak
cp -p /etc/csh.cshrc /etc/csh.cshrc_bak
echo "TMOUT=180" >> /etc/profile
echo "export TMOUT" >> /etc/profile
echo "set autologout=30" >> /etc/csh.cshrc


#################################################################
## 添加用户组
echo "=== 正在添加用户组 ${USER_NAME}"
groupadd ${USER_NAME}
usermod -g ${USER_NAME} ${USER_NAME}                                                                                    


#################################################################
## 防syn攻击 增加主机访问控制
## 这里需要增加特定的IP地址段
echo "=== 正在设置主机IP地址限制"
cp -p /etc/hosts.allow /etc/hosts.allow_bak
cp -p /etc/hosts.deny /etc/hosts.deny_bak
## 注意该地址范围 别把自己墙到外面
## 这里sshd允许10段网络访问
## 我们加入的这个限制会影响4A登录,一定注意
## 如过sshd没有tcp_wrapper库,那么这里的配置没有用
echo "sshd:all:allow" >> /etc/hosts.allow
echo "sshd:all:allow" >> /etc/hosts.allow
echo "telnet:all:allow" >> /etc/hosts.allow
echo "sshd:234.234.234.234:deny" >> /etc/hosts.deny
echo "telnet:234.234.234.234:deny" >> /etc/hosts.deny


#################################################################
## 更改telnet警告banner
echo "=== 正在更改telnet警告banner"
echo " Authorized users only. All activity may be monitored and reported " > /etc/issue
echo " Authorized users only. All activity may be monitored and reported " > /etc/issue.net
systemctl restart xinetd


#################################################################
## 设置用户口令密码复杂度策略
echo "=== 正在修改用户口令密码复杂度策略"
cp -p /etc/pam.d/system-auth /etc/pam.d/system-auth_bak
sed -i "s/^password    requisite.*/password    requisite     pam_cracklib.so dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 minclass=2 minlen=8 /g" /etc/pam.d/system-auth
sed -i "s/^password    sufficient.*/password    sufficient    pam_unix.so md5 shadow nullok try_first_pass use_authtok /g" /etc/pam.d/system-auth


#################################################################
## 设置口令重复次数限制
echo "=== 设置口令重复次数限制"
touch /etc/security/opasswd
chown root:root /etc/security/opasswd
chmod 600 /etc/security/opasswd
sed -i "s/^password    required.*/password    required      pam_unix.so     remember=5 /g" /etc/pam.d/system-auth


#################################################################
## 设置口令锁定策略
echo "=== 正在设置口令锁定策略"
sed -i "5 s/^/auth        required    pam_tally2.so deny=6 onerr=fail no_magic_root unlock_time=120\n/"  /etc/pam.d/system-auth


#################################################################
## 设置禁止imcp重定向
echo "=== 正在禁止ICMP重定向"
cp -p /etc/sysctl.conf /etc/sysctl.conf_bak
grep "net.ipv4.conf.all.accept_redirects=0" /etc/sysctl.conf
if [ $? == "1" ]; then
  echo "net.ipv4.conf.all.accept_redirects=0" >> /etc/sysctl.conf
fi
sysctl -p


#################################################################
## 锁定无用账户
echo "=== 正在锁定无用账户"
user_arr=("lp" "nobody" "uucp" "games" "rpm" "smmsp" "nfsnobody")
for user in ${user_arr[@]}
do
  res=`egrep -w "$user" /etc/shadow | awk -F: '( $2 !~ "!" ) {print $1":"$2}'`
  if [ -n "$res" ]; then
    echo "**正在锁定用户 $user"
    sed -i "s/$user:/$user:\!/g" /etc/shadow
  fi
done


#################################################################
## 配置ntp服务器
echo "=== 正在配置NTP服务"
if [ ! -f "/etc/ntp.conf" ];then
  echo "**** 没有安装ntp服务器!请手动安装ntp服务器!"
else
  echo "restrict ${NTP_SERVER} nomodify notrap" >> /etc/ntp.conf
  echo "server ${NTP_SERVER}" >> /etc/ntp.conf
  echo "=== 正在重启NTP服务"
  systemctl restart ntpd
fi


#################################################################
## 设置日志记录
## 这个位置主要是处理 日志收集和日志发送的问题
## 一般根据部署环境的不通 产生的要求可能也不通

#################################################################
## 关闭Portmap服务 如果不使用nfs的情况下
echo "=== 正在关闭rpcinfo 探测"
systemctl stop rpcbind.socket
systemctl stop rpcbind
systemctl disable rpcbind.socket
systemctl disable rpcbind
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值