CentOS7系统检测和加固脚本
脚本来源:
https://github.com/xiaoyunjie/Shell_Script
主要是为了Linux系统的安全,通过脚本对Linux系统进行一键检测和一键加固
公众号后台回复:【安全加固脚本】获取脚本下载链接
1、系统检测脚本:CentOS_Check_Script.sh
#包含2个文件
CentOS_Check_Script.sh
README.txt
操作说明
#执行CentOS-Check_Script.sh脚本文件进行检查,命令格式如下
sh CentOS_Check_Script.sh | tee check_`date +%Y%m%d_%H%M%S`.txt
检查说明
此脚本是按三级等保要求,编写的一键检查脚本,此脚本只适合linux分支中的redhat、centos,运行脚本将结果输出到自定义的文件中,脚本结果需要人为检查。
此检查脚本包含以下几块内容:
- 系统基本信息
- 资源使用情况
- 系统用户情况
- 身份鉴别安全
- 访问控制安全
- 安全审计
- 剩余信息保护
- 入侵防范安全
- 恶意代码防范
- 资源控制安全
效果如下
[root@centos ~]# ll
total 20
-rw-------. 1 root root 1599 Nov 21 14:47 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 14376 Apr 2 2020 CentOS_Check_Script.sh
[root@centos ~]#
[root@centos ~]#
[root@centos ~]#
[root@centos ~]# sh CentOS_Check_Script.sh | tee check_`date +%Y%m%d_%H%M%S`.txt
##########################################################################
# #
# Epoint health check script #
# #
#警告:本脚本只是一个检查的操作,未对服务器做任何修改,管理员可以根据此报告 #
#进行相应的安全整改 #
##########################################################################
##########################################################################
# #
# 主机安全检测 #
# #
##########################################################################
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统基本信息<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< : centos.walkingcloud.cn : centos linux : 7.9.2009core : 3.10.0-1160.el7.x86_64 : x86_64 ip: 192.168.31.8424 cpu: amd ryzen 7 5800x 8-core processor cpu: 8 : product name: vmware virtual platform product name: 440bx desktop reference platform : sun nov 29 11:24:25 cst 2020>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>资源使用情况<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< : 7.6g : 406m : 7.1g : up 3 min 1 : 0.01 0.02 0.01='============================dividing' line='=============================== 内存状态:' procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 7414316 12636 146176 0 0 90 4 86 97 0 1 99 0 0 0 0 0 7417228 12636 146260 0 0 0 0 121 148 0 0 100 0 0 0 0 0 7417228 12636 146260 0 0 0 0 67 106 0 0 100 0 0 0 0 0 7417228 12644 146252 0 0 0 16 68 110 0 0 100 0 0 0 0 0 7417228 12644 146260 0 0 0 0 68 111 0 0 100 0 0='============================dividing' line='=============================== 僵尸进程:'>>>无僵尸进程
=============================dividing line================================
耗CPU最多的进程:
root 559 0.6 0.0 48616 5196 ? Ss 11:21 0:01 /usr/lib/systemd/systemd-udevd
root 1 0.6 0.0 194100 7284 ? Ss 11:21 0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 1235 0.2 0.3 358836 29552 ? Ssl 11:21 0:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 97 0.0 0.0 0 0 ? S< 11:21 0:00 \_ deferwq='============================dividing' line='=============================== 耗内存最多的进程:' root 1235 0.2 0.3 358836 29552 ssl 11:21 0:00 usrbinpython2 -es usrsbinfirewalld --nofork --nopid root 1582 0.0 0.2 574284 19460 ssl 11:21 0:00 usrbinpython2 -es usrsbintuned -l -p root 1242 0.0 0.1 547912 8828 ssl 11:21 0:00 usrsbinnetworkmanager --no-daemon polkitd 1169 0.0 0.1 613004 12948 ssl 11:21 0:00 usrlibpolkit-1polkitd --no-debug user pid cpu mem vsz rss tty stat start time command='============================dividing' line='=============================== 环境变量:' xdg_session_id='1' hostname='centos.walkingcloud.cn' selinux_role_requested='SHELL=/bin/bash' term='xterm-256color' histsize='1000' ssh_client='192.168.31.47' 64074 22 selinux_use_current_range='SSH_TTY=/dev/pts/0' user='root' ls_colors='rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:' path='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin' mail='/var/spool/mail/root' pwd='/root' lang='en_US.UTF-8' selinux_level_requested='HISTCONTROL=ignoredups' home='/root' shlvl='2' logname='root' ssh_connection='192.168.31.47' 64074 192.168.31.84 22 lessopen='||/usr/bin/lesspipe.sh' s xdg_runtime_dir='/run/user/0' histtimeformat='%Y-%m-%d' h:m:s _='/usr/bin/env' line='=============================== 路由表:' kernel ip routing table destination gateway genmask flags metric ref use iface 0.0.0.0 192.168.31.1 0.0.0.0 ug 100 0 0 ens33 192.168.31.0 0.0.0.0 255.255.255.0 u 100 0 0 ens33='============================dividing' line='=============================== 监听端口:' active internet connections servers proto recv-q send-q local address foreign address state pidprogram name tcp 0 0 0.0.0.0:22 0.0.0.0: listen 1584sshd tcp 0 0 127.0.0.1:25 0.0.0.0: listen 1784master tcp6 0 0 :::22 ::: listen 1584sshd tcp6 0 0 ::1:25 ::: listen 1784master udp 0 0 0.0.0.0:68 0.0.0.0: 1402dhclient='============================dividing' line='=============================== 当前建立的连接:' established 1='============================dividing' line='=============================== 开机启动的服务:' abrt-ccpp.service enabled abrt-oops.service enabled abrt-vmcore.service enabled abrt-xorg.service enabled abrtd.service enabled auditd.service enabled autovt.service enabled crond.service enabled dbus-org.fedoraproject.firewalld1.service enabled dbus-org.freedesktop.nm-dispatcher.service enabled firewalld.service enabled getty.service enabled irqbalance.service enabled kdump.service enabled lvm2-monitor.service enabled microcode.service enabled networkmanager-dispatcher.service enabled networkmanager-wait-online.service enabled networkmanager.service enabled postfix.service enabled rhel-autorelabel-mark.service enabled rhel-autorelabel.service enabled rhel-configure.service enabled rhel-dmesg.service enabled rhel-domainname.service enabled rhel-import-state.service enabled rhel-loadmodules.service enabled rhel-readonly.service enabled rsyslog.service enabled sshd.service enabled sysstat.service enabled systemd-readahead-collect.service enabled systemd-readahead-drop.service enabled systemd-readahead-replay.service enabled tuned.service enabled vgauthd.service enabled vmtoolsd.service enabled dm-event.socket enabled lvm2-lvmetad.socket enabled lvm2-lvmpolld.socket enabled default.target enabled multi-user.target enabled remote-fs.target enabled runlevel2.target enabled runlevel3.target enabled runlevel4.target enabled>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>系统用户情况<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< : user tty from login idle jcpu pcpu what root pts0 192.168.31.47 11:21 9.00s 0.02s 0.00s tail -n 2='============================dividing' line='=============================== 系统所有用户:' root:x:0:0 bin:x:1:1 daemon:x:2:2 adm:x:3:4 lp:x:4:7 sync:x:5:0 shutdown:x:6:0 halt:x:7:0 mail:x:8:12 operator:x:11:0 games:x:12:100 ftp:x:14:50 nobody:x:99:99 systemd-network:x:192:192 dbus:x:81:81 polkitd:x:999:998 tss:x:59:59 abrt:x:173:173 sshd:x:74:74 postfix:x:89:89='============================dividing' line='=============================== 系统所有组:' root:x:0 bin:x:1 daemon:x:2 sys:x:3 adm:x:4 tty:x:5 disk:x:6 lp:x:7 mem:x:8 kmem:x:9 wheel:x:10 cdrom:x:11 mail:x:12 man:x:15 dialout:x:18 floppy:x:19 games:x:20 tape:x:33 video:x:39 ftp:x:50 lock:x:54 audio:x:63 nobody:x:99 users:x:100 utmp:x:22 utempter:x:35 stapusr:x:156 stapsys:x:157 stapdev:x:158 input:x:999 systemd-journal:x:190 systemd-network:x:192 dbus:x:81 polkitd:x:998 ssh_keys:x:997 tss:x:59 abrt:x:173 sshd:x:74 postdrop:x:90 postfix:x:89='============================dividing' line='=============================== 当前用户的计划任务:' no crontab for root>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>身份鉴别安全<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>密码复杂度:已设置
=============================dividing line================================
>>>(root) 是一个未被锁定的账户,请管理员检查是否是可疑账户--------[需调整]
=============================dividing line================================
>>>密码过期天数是99999天,请管理员改成90天------[需调整]
=============================dividing line================================
>>>登入失败处理:未开启,请加固登入失败锁定功能----------[需调整]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>访问控制安全<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< :>>>/etc/passwd里面的polkitd的UID为999,该账户非系统默认账户,请管理员确认是否为可疑账户--------[需调整]
=============================dividing line================================
系统特权用户:
root
=============================dividing line================================
系统中空口令账户:
systemd-network该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
dbus该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
polkitd该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
tss该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
abrt该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
sshd该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
postfix该账户为空口令账户,请管理员确认是否为新增账户,如果为新建账户,请配置密码-------[需调整]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>安全审计<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30: root pts0 192.168.31.47 sun nov 29 11:21 still logged in reboot system boot 3.10.0-1160.el7. sun nov 29 11:21 - 11:24 00:03 root pts0 192.168.31.47 sat nov 21 15:01 - crash 720:20 root pts0 192.168.31.47 sat nov 21 14:57 - 15:01 00:03 root tty1 sat nov 21 14:57 - 15:01 00:03 reboot system boot 3.10.0-1160.el7. sat nov 21 14:48 - 11:24 720:36 wtmp begins sat nov 21 14:48:03 2020='============================dividing' line='=============================== 查看syslog日志审计服务是否开启:' redirecting to binsystemctl status rsyslog.service active: active running since sun 2020-11-29 11:21:26 cst 3min 7s ago>>>经分析,syslog服务已开启
=============================dividing line================================
查看syslog日志是否开启外发:
>>>经分析,客户端syslog日志未开启外发---------[无需调整]
=============================dividing line================================
审计的要素和审计日志:
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg :omusrmsg:*
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
=============================dividing line================================
系统中关键文件修改时间:
>>>文件名:/bin/ls 最后修改时间:Aug 20 2019
>>>文件名:/bin/ps 最后修改时间:Oct 1 01:21
>>>文件名:/bin/login 最后修改时间:Oct 1 01:46
>>>文件名:/etc/shadow 最后修改时间:Nov 21 14:47
>>>文件名:/etc/passwd 最后修改时间:Nov 21 14:47
###############################################################################################
# ls文件:是存储ls命令的功能函数,被删除以后,就无法执行ls命令 #
# login文件:login是控制用户登录的文件,一旦被篡改或删除,系统将无法切换用户或登陆用户 #
# /etc/passwd是一个文件,主要是保存用户信息 #
# /bin/ps 进程查看命令功能支持文件,文件损坏或被更改后,无法正常使用ps命令 #
# /etc/shadow是/etc/passwd的影子文件,密码存放在该文件当中,并且只有root用户可读 #
###############################################################################################
=============================dividing line================================
检查重要日志文件是否存在:
>>>/var/log/secure日志文件存在
>>>/var/log/messages日志文件存在
>>>/var/log/cron日志文件存在
>>>/var/log/boot.log日志文件存在
>>>/var/log/dmesg日志文件存在
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>剩余信息保护<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< : --------- filesystem size used avail use mounted devtmpfs 3.8g 0 3.8g 0 dev tmpfs 3.9g 0 3.9g 0 devshm tmpfs 3.9g 12m 3.8g 1 run tmpfs 3.9g 0 3.9g 0 sysfscgroup devmappercentos-root 74g 2.1g 68g 3 devsda1 976m 121m 789m 14 boot devmappercentos-opt 24g 45m 23g 1 opt tmpfs 781m 0 781m 0 runuser0='============================dividing' line='=============================== 可用块设备信息:' name maj:min rm size ro type mountpoint sda 8:0 0 100g 0 disk sda1 8:1 0 1g 0 part boot sda2 8:2 0 99g 0 part centos-root 253:0 0 75g 0 lvm centos-opt 253:1 0 24g 0 lvm opt sr0 11:0 1 1024m 0 rom='============================dividing' line='=============================== 文件系统信息:' devmappercentos-root ext4 defaults 1 1 uuid='b76e122d-1235-43ac-835c-5b3de7d1678e' boot ext4 defaults 1 2 devmappercentos-opt opt ext4 defaults 1 2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>入侵防范安全<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< :>>>无入侵行为
=============================dividing line================================
用户错误登入列表:
>>>用户错误登入--------[需调整]
btmp begins Sun Nov 29 11:21:25 2020
=============================dividing line================================
ssh暴力登入信息:
>>>无ssh暴力登入信息
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>恶意代码防范<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< : no crontab for root>>>未安装ClamAV杀毒软件,请部署杀毒软件加固主机防护--------[无需调整]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>资源控制安全<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< xinetd:>>>xinetd服务未开启-------[无需调整]
=============================dividing line================================
查看是否开启了ssh服务:
Redirecting to /bin/systemctl status sshd.service
Active: active (running) since Sun 2020-11-29 11:21:26 CST; 3min 7s ago
Nov 29 11:21:26 centos.walkingcloud.cn sshd[1584]: Server listening on 0.0.0.0 port 22.
Nov 29 11:21:26 centos.walkingcloud.cn sshd[1584]: Server listening on :: port 22.
>>>SSH服务已开启
=============================dividing line================================
查看是否开启了Telnet-Server服务:
>>>Telnet-Server服务未开启--------[无需调整]
=============================dividing line================================
root 1235 0.2 0.3 358836 29552 ? Ssl 11:21 0:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
>>>防火墙已启用
=============================dividing line================================
查看系统SSH远程访问设置策略(host.deny拒绝列表):
>>>远程访问策略未设置--------[无需调整]
=============================dividing line================================
查看系统SSH远程访问设置策略(hosts.allow允许列表):
>>>远程访问策略未设置--------[无需调整]
=============================dividing line================================
当hosts.allow和host.deny相冲突时,以hosts.allow设置为准
=============================dividing line================================
>>>未设置登入超时限制,请设置,设置方法:在/etc/profile或者/etc/bashrc里面添加参数TMOUT=600 --------[需调整]
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2、 加固脚本:Protective_Script
#包含2个文件
CentOS_Protective_Script.sh
README.txt
操作说明
#执行CentOS_Protective_Script.sh脚本文件进行加固,命令格式如下
sh CentOS_Protective_Script.sh
#执行完成后,请按脚本提示重启相应服务
功能说明
- 一键进行全部加固
- 设置密码复杂度
- 添加eproot账号
- 禁止root远程登入
- 设置history保存行数以及命令时间,设置窗口超时时间
- 更改SSH端口
- 登入失败处理
- 还原配置文件
效果如下
sh CentOS_Protective_Script.sh
###########################################################################################
OS type is centos
###########################################################################################
###########################################################################################
Auto backup successfully
###########################################################################################
#########################################################################################
# Menu #
# 1:ALL protective #
# 2:Set Password Complexity Requirements #
# 3:Create eproot account #
# 4:Set Remote Login Configuration(SSH) #
# 5:Set Shell History and TMOUT #
# 6:Set SSH Port #
# 7:Set Logon failure handling #
# 8:Recover Configuration #
# 9:Exit #
#########################################################################################
Please choice[1-9]:1
#########################################################################################
2、 set password complexity requirements
#########################################################################################
密码修改重试3次机会,新密码与老密码必须有3字符不同,最小密码长度12个字符,包含大写字符至少一个,小写字母至少一个,数字至少一个,特殊字符至少一个
[Password complexity set success]
#########################################################################################
3、Create eproot account
#########################################################################################
Be sure to create an eproot account?[y/n]:y
Please enter your password:yuanfan@2019
eproot account created successfully
[Permissions set success]
#########################################################################################
4、Set Remote Login Configuration(SSH)
#########################################################################################
[Success: Set SSH Protocol to 2]
Disable root remote login?[y/n](Please make sure you have created at least one another account):n
#########################################################################################
5、set history and timeout
#########################################################################################
set history size, format, and TMOUT?[y/n]:y
HISTSIZE has been set to 10000
HISTTIMEFORMAT has been set to "Number-Time-User-Command"
set shell TMOUT?[300-600]seconds:180
[Success]
#########################################################################################
6、set ssh port
#########################################################################################
change ssh port?[y/n]:n
#########################################################################################
7、set logon failure handling
#########################################################################################
Are you sure set logon failure handling?[y/n]:y
#########################################################################################
[Logon failure handling set success]
限制登入失败三次,普通账号锁定5分钟,root账号锁定5分钟
#########################################################################################
截图如下
(图片可放大查看)
(图片可放大查看)
WalkingCloud公众号后台回复:【安全加固脚本】获取脚本下载链接