愚人节快乐
#!/bin/bash
# auth:chenjf
# func:centos7_加固
# version:v5.0
# sys:CentOS Linux release 7.9.2009 (Core)
[ $(id -u) -gt 0 ] && echo "please use root to execute the script!" && exit 1
#definition environment variable.
export PATH=/sbin/:/usr/sbin/:/bin/:/usr/bin/
DATE=$(date +%Y%m%d-%H:%M:%S)
echo -e "###################################################################################"
echo -e "# #"
echo -e "# centos7基础系统基线加固V5 #"
echo -e "# #"
echo -e "###################################################################################"
###备份重要文件###
localpath=$(cd $(dirname $0); pwd)
mkdir $localpath/baseline-bak -p
mkdir $localpath/baseline-bak/source -p
if [[ ! -f "$localpath/baseline-bak/source/sshd_config" ]]; then
cp -a /etc/ssh/sshd_config $localpath/baseline-bak/source/sshd_config
fi
if [[ ! -f "$localpath/baseline-bak/source/sysctl.conf" ]]; then
cp -a /etc/sysctl.conf $localpath/baseline-bak/source/sysctl.conf
fi
if [[ ! -f "$localpath/baseline-bak/source/yum.conf" ]]; then
cp -a /etc/pam.d/system-auth $localpath/baseline-bak/source/system-auth
fi
if [[ ! -f "$localpath/baseline-bak/source/password-auth" ]]; then
cp -a /etc/pam.d/password-auth $localpath/baseline-bak/source/password-auth
fi
if [[ ! -f "$localpath/baseline-bak/source/grub" ]]; then
cp -a /etc/default/grub $localpath/baseline-bak/source/grub
fi
if [[ ! -f "$localpath/baseline-bak/source/profile" ]]; then
cp -a /etc/profile $localpath/baseline-bak/source/profile
fi
if [[ ! -f "$localpath/baseline-bak/source/bashrc" ]]; then
cp -a /etc/bashrc $localpath/baseline-bak/source/bashrc
fi
if [[ ! -f "$localpath/baseline-bak/source/yum.conf" ]]; then
cp -a /etc/yum.conf $localpath/baseline-bak/source/yum.conf
fi
if [[ ! -f "$localpath/baseline-bak/source/audit.rules" ]]; then
cp -a /etc/audit/rules.d/audit.rules $localpath/baseline-bak/source/audit.rules
fi
if [[ ! -f "$localpath/baseline-bak/source/auditd.conf" ]]; then
cp -a /etc/audit/auditd.conf $localpath/baseline-bak/source/auditd.conf
fi
if [[ ! -f "$localpath/baseline-bak/source/issue.net" ]]; then
cp -a /etc/issue.net $localpath/baseline-bak/source/issue.net
fi
if [[ ! -f "$localpath/baseline-bak/source/issue" ]]; then
cp -a /etc/issue $localpath/baseline-bak/source/issue
fi
echo "------------The original files are backed up"
echo "------------Start hardening the system"
read -p "Please enter ssh port number you want to change (default is 22):" sshport
read -p"Are you sure? This may cause the ssh connection to break!!!(e.g.[y/n])" choice
until [ "$choice" == "y" -o "$choice" == "n" ];do
echo "Please input y or n ..."
read choice
done
if [ "$choice" = "n" ];then
exit 0
fi
####加固ssh###
cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config-${DATE}.bak && cp -a /etc/ssh/sshd_config $localpath/baseline-bak/sshd_config-${DATE}.bak
egrep -q "^\s*.*Port\s+.+(\s*#.*)?\s*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*Port\s+.+(\s*#.*)?\s*$/Port $sshport/" /etc/ssh/sshd_config || echo "Port $sshport" >> /etc/ssh/sshd_config
echo "------------Change the ssh port to $sshport"
egrep -q "^\s*.*PermitRootLogin\s+.+(\s*#.*)?\s*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*PermitRootLogin\s+.+(\s*#.*)?\s*$/PermitRootLogin no/" /etc/ssh/sshd_config || echo "PermitRootLogin no" >> /etc/ssh/sshd_config
echo "------------Disable the remote login with the root permission"
egrep -q "^\s*.*LoginGraceTime\s+.+(\s*#.*)?\s*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*LoginGraceTime\s+.+(\s*#.*)?\s*$/LoginGraceTime 60/" /etc/ssh/sshd_config || echo "LoginGraceTime 60" >> /etc/ssh/sshd_config
echo "------------set LoginGraceTime 60 in /etc/ssh/sshd_config"
cat << EOF > /etc/issue.net
********************************************************************************
* SSH登录系统安全警告信息 *
********************************************************************************
欢迎使用本系统!您已成功通过SSH登录到服务器。
请注意:
1. 请确保您的登录行为符合公司/组织的安全政策,并仅用于授权操作。
2. 系统会记录所有登录活动,非法访问将被追踪并报告给相关部门。
3. 请勿在未经授权的情况下共享您的密码或密钥,定期更换密码以增强安全性。
4. 登录后请立即更新系统及应用程序至最新版本,保持系统补丁的及时安装。
5. 若发现任何可疑活动,请立即向IT部门报告。
感谢您对网络安全的贡献,让我们共同维护系统的稳定与安全!
---
系统管理员团队敬上
当前时间:$(date)
********************************************************************************
EOF
egrep -q "^\s*.*Banner\s+.+(\s*#.*)?\s*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*Banner\s+.+(\s*#.*)?\s*$/Banner \/etc\/issue.net/" /etc/ssh/sshd_config || echo "Banner /etc/issue.net" >> /etc/ssh/sshd_config
echo "------------set Alarm slogan of ssh login "
egrep -q "^\s*.*Protocol\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*Protocol\s\w+.*$/Protocol 2/" /etc/ssh/sshd_config || echo "Protocol 2 " >> /etc/ssh/sshd_config
echo "------------set Protocol 2 in /etc/ssh/sshd_config"
egrep -q "^\s*.*MACs\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*MACs\s\w+.*$/MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]/" /etc/ssh/sshd_config || echo "MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]" >> /etc/ssh/sshd_config
echo "------------Use approved MAC algorithms in /etc/ssh/sshd_config"
egrep -q "^\s*.*MaxAuthTries\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*MaxAuthTries\s\w+.*$/MaxAuthTries 4/" /etc/ssh/sshd_config || echo "MaxAuthTries 4" >> /etc/ssh/sshd_config
echo "------------set MaxAuthTries 4 in /etc/ssh/sshd_config"
egrep -q "^\s*.*LogLevel\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*LogLevel\s\w+.*$/LogLevel INFO/" /etc/ssh/sshd_config || echo "LogLevel INFO" >> /etc/ssh/sshd_config
echo "------------set LogLevel INFO in /etc/ssh/sshd_config"
egrep -q "^HostbasedAuthentication\s+.+(\s*#.*)?\s*$" /etc/ssh/sshd_config && sed -ri "s/^HostbasedAuthentication\s+.+(\s*#.*)?\s*$/HostbasedAuthentication no/" /etc/ssh/sshd_config || echo "HostbasedAuthentication no" >> /etc/ssh/sshd_config
echo "------------set HostbasedAuthentication no in /etc/ssh/sshd_config"
egrep -q "^\s*.*ClientAliveInterval\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*ClientAliveInterval\s\w+.*$/ClientAliveInterval 300/" /etc/ssh/sshd_config || echo "ClientAliveInterval 300" >> /etc/ssh/sshd_config
echo "------------set ClientAliveInterval 300 in /etc/ssh/sshd_config"
egrep -q "^\s*.*ClientAliveCountMax\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*ClientAliveCountMax\s\w+.*$/ClientAliveCountMax 0/" /etc/ssh/sshd_config || echo "ClientAliveCountMax 0" >> /etc/ssh/sshd_config
echo "------------set ClientAliveCountMax 0 in /etc/ssh/sshd_config"
egrep -q "^\s*.*IgnoreRhosts\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*IgnoreRhosts\s\w+.*$/IgnoreRhosts yes/" /etc/ssh/sshd_config || echo "IgnoreRhosts yes" >> /etc/ssh/sshd_config
echo "------------set IgnoreRhosts yes in /etc/ssh/sshd_config"
egrep -q "^\s*.*PermitEmptyPasswords\s\w+.*$" /etc/ssh/sshd_config && sed -ri "s/^\s*.*PermitEmptyPasswords\s\w+.*$/PermitEmptyPasswords no/" /etc/ssh/sshd_config || echo "PermitEmptyPasswords no" >> /etc/ssh/sshd_config
echo "------------set PermitEmptyPasswords no in /etc/ssh/sshd_config"
systemctl restart sshd &>/dev/null 2&>/dev/null
####加固yum###
cp -a /etc/yum.conf /etc/yum.conf-${DATE}.bak && cp -a /etc/yum.conf $localpath/baseline-bak/yum.conf-${DATE}.bak
egrep -q "^gpgcheck\s*=\s*.+\s*$" /etc/yum.conf && sed -ri "s/^gpgcheck\s*=\s*.+\s*$/gpgcheck=1/" /etc/yum.conf || echo "gpgcheck=1" >> /etc/yum.conf
echo "------------Set gpgcheck to 1 in /etc/yum.conf"
####加固系统内核参数###
cp -a /etc/sysctl.conf /etc/sysctl.conf-${DATE}.bak && cp -a /etc/sysctl.conf $localpath/baseline-bak/sysctl.conf-${DATE}.bak
#net.ipv4.conf.all.secure_redirects = 0
egrep -q "^\s*net\.ipv4\.conf\.all\.secure_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.secure_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.secure_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.all.secure_redirects = 0" >> /etc/sysctl.conf
#net.ipv4.conf.default.secure_redirects = 0
egrep -q "^\s*net\.ipv4\.conf\.default\.secure_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.secure_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.secure_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.default.secure_redirects = 0" >> /etc/sysctl.conf
echo "------------secure ICMP redirection is not accepted"
egrep -q "^\s*\*\s+hard\s+core\s+.+(\s*#.*)?\s*$" /etc/security/limits.conf && sed -ri "s/^\s*\*\s+hard\s+core\s+.+(\s*#.*)?\s*$/* hard core 0/" /etc/security/limits.conf || echo "* hard core 0" >> /etc/security/limits.conf
egrep -q "^\s*fs\.suid_dumpable\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*fs\.suid_dumpable\s*.+(\s*#.*)?\s*$/fs.suid_dumpable = 0/" /etc/sysctl.conf || echo "fs.suid_dumpable = 0" >> /etc/sysctl.conf
echo "------------The core dump is restricted"
egrep -q "^\s*net\.ipv4\.conf\.all\.log_martians\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.log_martians\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.log_martians = 1/" /etc/sysctl.conf || echo "net.ipv4.conf.all.log_martians = 1" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv4\.conf\.default\.log_martians\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.log_martians\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.log_martians = 1/" /etc/sysctl.conf || echo "net.ipv4.conf.default.log_martians = 1" >> /etc/sysctl.conf
echo "------------Log suspicious packets"
egrep -q "^\s*net\.ipv6\.conf\.all\.accept_ra\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv6\.conf\.all\.accept_ra\s*.+(\s*#.*)?\s*$/net.ipv6.conf.all.accept_ra = 0/" /etc/sysctl.conf || echo "net.ipv6.conf.all.accept_ra = 0" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv6\.conf\.default\.accept_ra\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv6\.conf\.default\.accept_ra\s*.+(\s*#.*)?\s*$/net.ipv6.conf.default.accept_ra = 0/" /etc/sysctl.conf || echo "net.ipv6.conf.default.accept_ra = 0" >> /etc/sysctl.conf
echo "------------IPv6 router notification is not accepted"
egrep -q "^\s*net\.ipv4\.conf\.all\.rp_filter\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.rp_filter\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.rp_filter = 1/" /etc/sysctl.conf || echo "net.ipv4.conf.all.rp_filter = 1" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv4\.conf\.default\.rp_filter\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.rp_filter\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.rp_filter = 1/" /etc/sysctl.conf || echo "net.ipv4.conf.default.rp_filter = 1" >> /etc/sysctl.conf
echo "------------Enable reverse path filtering"
egrep -q "^\s*net\.ipv4\.conf\.all\.accept_source_route\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.accept_source_route\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.accept_source_route = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.all.accept_source_route = 0" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv4\.conf\.default\.accept_source_route\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.accept_source_route\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.accept_source_route = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.default.accept_source_route = 0" >> /etc/sysctl.conf
echo "------------Source routing packets are not accepted"
egrep -q "^\s*net\.ipv4\.conf\.all\.send_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.send_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.send_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.all.send_redirects = 0" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv4\.conf\.default\.send_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.send_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.send_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.default.send_redirects = 0" >> /etc/sysctl.conf
echo "------------Packet redirection is disabled"
egrep -q "^\s*net\.ipv4\.ip_forward\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.ip_forward\s*.+(\s*#.*)?\s*$/net.ipv4.ip_forward = 0/" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 0" >> /etc/sysctl.conf
echo "------------Disabling IP forwarding"
egrep -q "^\s*net\.ipv6\.conf\.all\.accept_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv6\.conf\.all\.accept_redirects\s*.+(\s*#.*)?\s*$/net.ipv6.conf.all.accept_redirects = 0/" /etc/sysctl.conf || echo "net.ipv6.conf.all.accept_redirects = 0" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv6\.conf\.default\.accept_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv6\.conf\.default\.accept_redirects\s*.+(\s*#.*)?\s*$/net.ipv6.conf.default.accept_redirects = 0/" /etc/sysctl.conf || echo "net.ipv6.conf.default.accept_redirects = 0" >> /etc/sysctl.conf
echo "------------IPv6 redirection is not accepted"
egrep -q "^\s*net\.ipv4\.tcp_syncookies\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.tcp_syncookies\s*.+(\s*#.*)?\s*$/net.ipv4.tcp_syncookies = 1/" /etc/sysctl.conf || echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
echo "------------TCP SYN Cookies are enabled"
egrep -q "^\s*net\.ipv4\.conf\.all\.accept_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.all\.accept_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.all.accept_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.all.accept_redirects = 0" >> /etc/sysctl.conf
egrep -q "^\s*net\.ipv4\.conf\.default\.accept_redirects\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*net\.ipv4\.conf\.default\.accept_redirects\s*.+(\s*#.*)?\s*$/net.ipv4.conf.default.accept_redirects = 0/" /etc/sysctl.conf || echo "net.ipv4.conf.default.accept_redirects = 0" >> /etc/sysctl.conf
echo "------------ICMP redirection is not accepted"
egrep -q "^\s*kernel\.randomize_va_space\s*.+(\s*#.*)?\s*$" /etc/sysctl.conf && sed -ri "s/^\s*kernel\.randomize_va_space\s*.+(\s*#.*)?\s*$/kernel.randomize_va_space = 2/" /etc/sysctl.conf || echo "kernel.randomize_va_space = 2" >> /etc/sysctl.conf
echo "------------Address space layout Randomization (ASLR) enabled"
sysctl -p &>/dev/null 2&>/dev/null
###禁用模块###
cat << EOF > /etc/modprobe.d/CIS.conf
install rds /bin/true
install jffs2 /bin/true
install sctp /bin/true
install dccp /bin/true
install udf /bin/true
install hfsplus /bin/true
install squashfs /bin/true
install hfs /bin/true
install freevxfs /bin/true
install cramfs /bin/true
install tipc /bin/true
install vfat /bin/true
EOF
rmmod jffs2 &>/dev/null 2&>/dev/nul
rmmod udf &>/dev/null 2&>/dev/nul
rmmod hfsplus &>/dev/null 2&>/dev/nul
rmmod hfs &>/dev/null 2&>/dev/nul
rmmod freevxfs &>/dev/null 2&>/dev/nul
rmmod cramfs &>/dev/null 2&>/dev/nul
rmmod vfat &>/dev/null 2&>/dev/nul
echo "------------Disable RDS/jffs2/sctp/DCCP/udf/hfsplus/squashfs/hfs/freevxfs/cramfs/tipc"
###加固登录验证###
cp -a /etc/pam.d/system-auth /etc/pam.d/system-auth-${DATE}.bak && cp -a /etc/pam.d/system-auth $localpath/baseline-bak/system-auth-${DATE}.bak
sed -ri "/^\s*auth\s+required\s+pam_tally2.so\s+.+(\s*#.*)?\s*$/d" /etc/pam.d/system-auth
sed -ri "1a auth required pam_tally2.so deny=5 unlock_time=900 even_deny_root root_unlock_time=30" /etc/pam.d/system-auth
egrep -q "^\s*account\s+required\s+pam_tally2.so\s*(\s*#.*)?\s*$" /etc/pam.d/system-auth || sed -ri '/^account\s+required\s+pam_permit.so\s*(\s*#.*)?\s*$/a\account required pam_tally2.so' /etc/pam.d/system-auth
cp -a /etc/pam.d/password-auth /etc/pam.d/password-auth-${DATE}.bak && cp -a /etc/pam.d/password-auth $localpath/baseline-bak/password-auth-${DATE}.bak
sed -ri "/^\s*auth\s+required\s+pam_tally2.so\s+.+(\s*#.*)?\s*$/d" /etc/pam.d/password-auth
sed -ri "1a auth required pam_tally2.so deny=5 unlock_time=900 even_deny_root root_unlock_time=30" /etc/pam.d/password-auth
egrep -q "^\s*account\s+required\s+pam_tally2.so\s*(\s*#.*)?\s*$" /etc/pam.d/password-auth || sed -ri '/^account\s+required\s+pam_permit.so\s*(\s*#.*)?\s*$/a\account required pam_tally2.so' /etc/pam.d/password-auth
echo "------------Password try 5 locks for 900 seconds"
###grub加固###
cp -a /etc/default/grub /etc/default/grub-${DATE}.bak && cp -a /etc/default/grub $localpath/baseline-bak/grub-${DATE}.bak
if grep -q "ipv6.disable=1" /etc/default/grub; then
echo "GRUB_CMDLINE_LINUX already contains ipv6.disable=1"
else
sudo sed -i '/GRUB_CMDLINE_LINUX/s/"$/ ipv6.disable=1"/' /etc/default/grub
grub2-mkconfig > /boot/grub2/grub.cfg
fi
echo "------------Disable ipv6"
if grep -q "audit=1" /etc/default/grub; then
echo "GRUB_CMDLINE_LINUX already contains audit=1"
else
sudo sed -i '/GRUB_CMDLINE_LINUX/s/"$/ audit=1"/' /etc/default/grub
grub2-mkconfig > /boot/grub2/grub.cfg
fi
###umask加固###
cp -a /etc/profile /etc/profile-${DATE}.bak && cp -a /etc/profile $localpath/baseline-bak/profile-${DATE}.bak
cp -a /etc/bashrc /etc/bashrc-${DATE}.bak && cp -a /etc/bashrc $localpath/baseline-bak/bashrc-${DATE}.bak
egrep -q "^\s*umask\s+\w+.*$" /etc/profile && sed -ri "s/^\s*umask\s+\w+.*$/umask 027/" /etc/profile || echo "umask 027" >> /etc/profile
egrep -q "^\s*umask\s+\w+.*$" /etc/bashrc && sed -ri "s/^\s*umask\s+\w+.*$/umask 027/" /etc/bashrc || echo "umask 027" >> /etc/bashrc
egrep -q "^\s*(export|)\s*TMOUT\S\w+.*$" /etc/profile && sed -ri "s/^\s*(export|)\s*TMOUT.\S\w+.*$/export TMOUT=600/" /etc/profile || echo "export TMOUT=600" >> /etc/profile
egrep -q "^\s*(export|)\s*TMOUT\S\w+.*$" /etc/bashrc && sed -ri "s/^\s*(export|)\s*TMOUT.\S\w+.*$/export TMOUT=600/" /etc/bashrc || echo "export TMOUT=600" >> /etc/bashrc
echo "------------set umask 027"
###cron文件夹权限加固###
chown root:root /etc/cron.hourly
chmod og-rwx /etc/cron.hourly
echo "------------Narrow permissions /etc/cron.hourly"
#回滚chmod 755 /etc/cron.hourly
chown root:root /etc/cron.monthly
chmod og-rwx /etc/cron.monthly
echo "------------Narrow permissions /etc/cron.monthly"
#回滚chmod 755 /etc/cron.monthly
chown root:root /etc/cron.daily
chmod og-rwx /etc/cron.daily
echo "------------Narrow permissions /etc/cron.daily"
#回滚chmod 755 /etc/cron.daily
chown root:root /etc/cron.weekly
chmod og-rwx /etc/cron.weekly
echo "------------Narrow permissions /etc/cron.weekly"
#回滚chmod 755 /etc/cron.weekly
chown root:root /etc/cron.d
chmod og-rwx /etc/cron.d
echo "------------Narrow permissions /etc/cron.d"
#回滚chmod 755 /etc/cron.d
chown root:root /etc/crontab
chmod og-rwx /etc/crontab
echo "------------Narrow permissions /etc/crontab"
#回滚chmod 644 /etc/crontab
chown root:root /boot/grub2/grub.cfg
chmod og-rwx /boot/grub2/grub.cfg
echo "------------Narrow permissions /boot/grub2/grub.cfg"
#回滚chmod 644 /boot/grub2/grub.cfg
cat << EOF > /etc/issue
********************************************************************************
* SSH登录系统安全警告信息 *
********************************************************************************
欢迎使用本系统!您已成功通过SSH登录到服务器。
请注意:
1. 请确保您的登录行为符合公司/组织的安全政策,并仅用于授权操作。
2. 系统会记录所有登录活动,非法访问将被追踪并报告给相关部门。
3. 请勿在未经授权的情况下共享您的密码或密钥,定期更换密码以增强安全性。
4. 登录后请立即更新系统及应用程序至最新版本,保持系统补丁的及时安装。
5. 若发现任何可疑活动,请立即向IT部门报告。
感谢您对网络安全的贡献,让我们共同维护系统的稳定与安全!
---
系统管理员团队敬上
当前时间:$(date)
********************************************************************************
EOF
cat << EOF > $localpath/audit.rules
## This file is automatically generated from /etc/audit/rules.d
-D
-b 8192
-f 1
-w /etc/group -p wa -k identity
-w /etc/passwd -p wa -k identity
-w /etc/gshadow -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/security/opasswd -p wa -k identity
-w /etc/selinux/ -p wa -k MAC-policy
-w /usr/share/selinux/ -p wa -k MAC-policy
-w /var/run/utmp -p wa -k session
-w /var/log/wtmp -p wa -k logins
-w /var/log/btmp -p wa -k logins
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d/ -p wa -k scope
-w /var/log/sudo.log -p wa -k actions
-w /var/log/lastlog -p wa -k logins
-w /var/run/faillock/ -p wa -k logins
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d/ -p wa -k scope
-e 2
EOF
###加固audit###
cp -a /etc/audit/auditd.conf /etc/audit/auditd.conf-${DATE}.bak && cp -a /etc/audit/auditd.conf $localpath/baseline-bak/auditd.conf-${DATE}.bak
cp -a /etc/audit/rules.d/audit.rules /etc/audit/rules.d/audit.rules-${DATE}.bak && cp -a /etc/audit/rules.d/audit.rules $localpath/baseline-bak/audit.rules-${DATE}.bak
egrep -q "^\s*space_left_action\s+.+(\s*#.*)?\s*$" /etc/audit/auditd.conf && sed -ri "s/^\s*space_left_action\s+.+(\s*#.*)?\s*$/space_left_action = email/" /etc/audit/auditd.conf || echo "space_left_action = email" >> /etc/audit/auditd.conf
echo "------------set space_left_action = email in /etc/audit/auditd.conf"
egrep -q "^\s*action_mail_acct\s+.+(\s*#.*)?\s*$" /etc/audit/auditd.conf && sed -ri "s/^\s*action_mail_acct\s+.+(\s*#.*)?\s*$/action_mail_acct = root/" /etc/audit/auditd.conf || echo "action_mail_acct = root" >> /etc/audit/auditd.conf
echo "------------set action_mail_acct = root in /etc/audit/auditd.conf"
egrep -q "^\s*admin_space_left_action\s+.+(\s*#.*)?\s*$" /etc/audit/auditd.conf && sed -ri "s/^\s*admin_space_left_action\s+.+(\s*#.*)?\s*$/admin_space_left_action = halt/" /etc/audit/auditd.conf || echo "admin_space_left_action = halt" >> /etc/audit/auditd.conf
echo "------------set admin_space_left_action = halt in /etc/audit/auditd.conf"
egrep -q "^\s*max_log_file_action\s+.+(\s*#.*)?\s*$" /etc/audit/auditd.conf && sed -ri "s/^\s*max_log_file_action\s+.+(\s*#.*)?\s*$/max_log_file_action = keep_logs/" /etc/audit/auditd.conf || echo "max_log_file_action = keep_logs" >> /etc/audit/auditd.conf
echo "------------set max_log_file_action = keep_logs in /etc/audit/auditd.conf"
\cp -a $localpath/audit.rules /etc/audit/rules.d/audit.rules
echo "------------set rules into /etc/audit/rules.d/audit.rules"
systemctl restart auditd &>/dev/null 2&>/dev/nul
标签:sysctl,#.,整改,echo,centos7,etc,基线,conf,net
From: https://www.cnblogs.com/haiyoyo/p/18108855