最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

运维大镖客:Linux 合规配置脚本第一版

IT圈 admin 2浏览 0评论

运维大镖客:Linux 合规配置脚本第一版

还在为Linux合规配置检查不过关而发愁吗?
最新的配置合规脚本来啦!
20220609 作者注:之后的脚本将会在新的网络空间安全付费专栏发布,如需要更新的版本,请移步专栏观看

#!/bin/bash#################################################################
## Linux 设备合规配置脚本
## 基于 Centos7
## 在执行之前请务必先浏览一遍 有一些重要的信息需要留意
## 注意,此脚本有些部分只能执行一遍,如果执行多次会产生错误
## Update at: 20201208#################################################################
USER_NAME=demo
USER_PASSWD=demopass!@
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#################################################################
## 设置用户口令密码复杂度策略
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#################################################################
## 设置用户缺省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登录,一定注意
echo "sshd:all:allow" >> /etc/host.allow
echo "sshd:10.:allow" >> /etc/host.allow
echo "telnet:10.:allow" >> /etc/host.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 "=== 设置口令重复次数限制"
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 "2 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" ]; thenecho "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[@]}
dores=`egrep -w "$user" /etc/shadow | awk -F: '( $2 !~ "!" ) {print $1":"$2}'`if [ -n "$res" ]; thenecho "**正在锁定用户 $user"sed -i "s/$user:/$user:\!/g" /etc/shadowfi
done#################################################################
## 关闭Portmap服务 如果不使用nfs的情况下
echo "=== 正在关闭rpcinfo 探测"
systemctl stop rpcbind.socket
systemctl stop rpcbind
systemctl disable rpcbind.socket
systemctl disable rpcbind

运维大镖客:Linux 合规配置脚本第一版

还在为Linux合规配置检查不过关而发愁吗?
最新的配置合规脚本来啦!
20220609 作者注:之后的脚本将会在新的网络空间安全付费专栏发布,如需要更新的版本,请移步专栏观看

#!/bin/bash#################################################################
## Linux 设备合规配置脚本
## 基于 Centos7
## 在执行之前请务必先浏览一遍 有一些重要的信息需要留意
## 注意,此脚本有些部分只能执行一遍,如果执行多次会产生错误
## Update at: 20201208#################################################################
USER_NAME=demo
USER_PASSWD=demopass!@
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#################################################################
## 设置用户口令密码复杂度策略
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#################################################################
## 设置用户缺省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登录,一定注意
echo "sshd:all:allow" >> /etc/host.allow
echo "sshd:10.:allow" >> /etc/host.allow
echo "telnet:10.:allow" >> /etc/host.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 "=== 设置口令重复次数限制"
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 "2 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" ]; thenecho "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[@]}
dores=`egrep -w "$user" /etc/shadow | awk -F: '( $2 !~ "!" ) {print $1":"$2}'`if [ -n "$res" ]; thenecho "**正在锁定用户 $user"sed -i "s/$user:/$user:\!/g" /etc/shadowfi
done#################################################################
## 关闭Portmap服务 如果不使用nfs的情况下
echo "=== 正在关闭rpcinfo 探测"
systemctl stop rpcbind.socket
systemctl stop rpcbind
systemctl disable rpcbind.socket
systemctl disable rpcbind
发布评论

评论列表 (0)

  1. 暂无评论