centos7 系统安全加固方案

常用组件

  1. # 换源
  2. yum -y install epel-release wget
  3. sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
  4. wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  5. wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/repo/epel-7.repo
  6. yum clean all
  7. yum makecache
  8. # 常用
  9. yum -y install ntpdate #同步时间
  10. yum -y install bash-completion # 对 systemctl 命令服务名称的补全
  11. yum -y install net-tools # TCP/IP工具箱
  12. yum -y install curl # 网络请求
  13. yum -y install vim # 编辑工具
  14. yum -y install lrzsz # 上传下载工具
  15. yum -y install sysstat # 系统监控工具
  16. yum -y install psmisc # 进程管理
  17. yum -y install iptraf-ng # 流量监控
  18. yum -y install nethogs # 流量监控2
  19. yum -y install fail2ban # 必装 防止ssh爆破

 基本优化

  1. sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config # 关闭反向DNS解析
  2. sed -i 's%GSSAPIAuthentication yes%GSSAPIAuthentication no%' /etc/ssh/sshd_config # 解决Linux之间使用SSH连接慢问题
  3. sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%' /etc/ssh/sshd_config #禁止空密码登录
  4. sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config && setenforce 0 # 禁用SELINUX

一 . 设置会话超时(1分钟)

默认配置:

加固方案:

  1. 1.备份配置文件:
  2. # cp -a /etc/profile /etc/profile.default
  3. 2.编辑配置文件 vi /etc/profile 在文件的末尾添加参数
  4. export TMOUT=60
  5. 3.保存配置文件

二. 设置history命令时间戳

默认配置:

加固方案:

  1. 1.备份配置文件:
  2. 2.编辑配置文件: vi /etc/profile 在文件的末尾添加参数
  3. export HISTTIMEFORMAT="%F %T `whoami` "
  4. 3.保存配置文件

 

 三.禁止Control-Alt-Delete 键盘重启系统命令

默认配置:

  1. ls /usr/lib/systemd/system/ctrl-alt-del.target

加固方案:

  1. 1.备份配置文件 cp -a /usr/lib/systemd/system/ctrl-alt-del.target /usr/lib/systemd/system/ctrl-alt-del.target.default
  2. 2.移除该原源文件 rm -rf /usr/lib/systemd/system/ctrl-alt-del.target

四. 隐藏系统版本信息

默认配置:

加固方案:

  1. 执行以下命令:
  2. mv /etc/issue /etc/issue.bak
  3. mv /etc/issue.net /etc/issue.net.bak

 

五.最大文件打开数(文件句柄数)

默认配置:

	
  1. [root@i-1y3we23j ~]# ulimit -n
  2. 1024

 

加固方案:

  1. 1.备份配置文件
  2. cp -a /etc/security/limits.conf /etc/security/limits.conf.default
  3. 2.添加以下两行配置到该文件最后
  4. *       soft    nofile  1000000  
  5. *       hard    nofile  1000000  
  6. *       soft    nproc  65535  
  7. *       hard    nproc  65535

 

六.用户最大进程数

默认配置:

	
  1. [root@i-1y3we23j ~]# cat /etc/security/limits.d/20-nproc.conf
  2. # Default limit for number of user's processes to prevent
  3. # accidental fork bombs.
  4. # See rhbz #432903 for reasoning.
  5.  
  6. * soft nproc 4096
  7. root soft nproc unlimited

加固方案:

	
  1. 1.备份配置文件
  2. cp -a /etc/security/limits.d/20-nproc.conf /etc/security/limits.d/20-nproc.conf.default
  3. 2.修改配置文件vim /etc/security/limits.d/20-nproc.conf
  4.  
  5. * soft nproc 65535
  6. * hard nproc 65535

 

备注:修改所有用户的最大进程数为65535

7. 禁止root用户直接登录

一、新建帐户,命令如下:

  1. useradd NewUser

二、设置帐户密码,命令如下:

  1. passwd NewUser

三、不允许root直接登陆,命令如下:

  1. vim /etc/ssh/sshd_config
  2. 找到 # PermitRootLogin yus
  3. 改为 PermitRootLogin no

四、禁止root登陆,命令如下:

  1. vim /etc/ssh/sshd_config
  2. sed -i 's%#Port 22%Port 66%' /etc/ssh/sshd_config
  3. #Port 22字段删掉#,将22改为其他不被使用的端口
  4. 服务器端口最大可以开到65536

五、使环境变量立即生效,命令如下 (将第一行80 更改成新SSH端口):

  1. firewall-cmd --zone=public --add-port=66/tcp --permanent
  2. firewall-cmd --reload
  3. systemctl restart sshd.service
  4. /etc/rc.d/init.d/sshd restart

 

8. 同步时区

同步时间服务器

  1. timedatectl set-timezone Asia/Shanghai
  2. ntpdate ntp.aliyun.com cn.pool.ntp.org

将系统时间写入到硬件时间

  1. hwclock -w

9. 关闭ICMP (ping)响应

永久修改方式(重启后有效)
禁止ICMP包通行

		
  1. echo net.ipv4.icmp_echo_ignore_all=1 >>/etc/sysctl.conf
  2. sysctl -p

允許ICMP包通行

		
  1. echo net.ipv4.icmp_echo_ignore_all=0 >>/etc/sysctl.conf
  2. sysctl -p

10. 安装Fail2ban 防止SSH爆破与CC攻击

  1. #CentOS内置源并未包含fail2ban,需要先安装epel源
  2. yum -y install epel-release
  3. #安装fial2ban
  4. yum -y install fail2ban
  5. # 启动
  6. systemctl start fail2ban
  7. # 开机自启
  8. systemctl enable fail2ban
  9. # 重新加载配置
  10. systemctl restart fail2ban
  11. 查看黑名单状态
  12. fail2ban-client status sshd(模块名)
  13. 黑名单移除IP
  14. fail2ban-client set sshd unbanip IP地址
  15. #查看日志
  16. tail /var/log/fail2ban.log

新建jail.local来覆盖fail2ban的一些默认规则:

  1. #新建配置
  2. vi /etc/fail2ban/jail.local
  3. #默认配置
  4. [DEFAULT]
  5. ignoreip = 127.0.0.1/8
  6. bantime = 86400
  7. findtime = 600
  8. maxretry = 5
  9. #这里banaction必须用firewallcmd-ipset,这是fiewalll支持的关键,如果是用Iptables请不要这样填写
  10. banaction = firewallcmd-ipset
  11. action = %(action_mwl)s

 

参数说明:

  1. ignoreipIP白名单,白名单中的IP不会屏蔽,可填写多个以(,)分隔
  2. bantime:屏蔽时间,单位为秒(s
  3. findtime:时间范围
  4. maxretry:最大次数
  5. banaction:屏蔽IP所使用的方法,上面使用firewalld屏蔽端口

 

防止SSH爆破

继续修改jail.local这个配置文件,在后面追加如下内容:

  1. [sshd]
  2. enabled = true
  3. filter = sshd
  4. port = 22
  5. action = %(action_mwl)s
  6. logpath = /var/log/secure

参数说明:

  1. [sshd]:名称,可以随便填写
  2. filter:规则名称,必须填写位于filter.d目录里面的规则,sshdfail2ban内置规则
  3. port:对应的端口
  4. action:采取的行动
  5. logpath:需要监视的日志路径

防止CC攻击

这里仅以Nginx为例,使用fail2ban来监视nginx日志,匹配短时间内频繁请求的IP,并使用firewalld将其IP屏蔽,达到CC防护的作用。

  1. #需要先新建一个nginx日志匹配规则
  2. vi /etc/fail2ban/filter.d/nginx-cc.conf
  3. #填写如下内容
  4. [Definition]
  5. failregex = -.*- .*HTTP/1.* .* .*$
  6. ignoreregex =

继续修改jail.local追加如下内容:

  1. [nginx-cc]
  2. enabled = true
  3. port = http,https
  4. filter = nginx-cc
  5. action = %(action_mwl)s
  6. maxretry = 20
  7. findtime = 60
  8. bantime = 3600
  9. logpath = /usr/local/nginx/logs/access.log

上面的配置意思是如果在60s内,同一IP达到20次请求,则将其IP ban 1小时,上面只是为了测试,请根据自己的实际情况修改。logpath为nginx日志路径。

防止Wordpress爆破

如果您经常分析日志会发现有大量机器人在扫描wordpress登录页面wp-login.php,虽然对方可能没成功,但是为了避免万一还是将他IP干掉为好。

  1. #需要先新建一个nginx日志匹配规则
  2. vi /etc/fail2ban/filter.d/wordpress.conf
  3. #填写如下内容
  4. [Definition]
  5. failregex = ^ -.* /wp-login.php.* HTTP/1\.."
  6. ignoreregex =

继续修改jail.local追加如下内容:

  1. [wordpress]
  2. enabled = true
  3. port = http,https
  4. filter = wordpress
  5. action = %(action_mwl)s
  6. maxretry = 20
  7. findtime = 60
  8. bantime = 3600
  9. logpath = /usr/local/nginx/logs/access.log

当然,别忘记输入systemctl restart fail2ban重启fail2ban使其生效。

总结

fail2ban已经内置很多匹配规则,位于/etc/fail2ban/filter.d/目录下,包含了常见的SSH/FTP/Nginx/Apache等日志匹配,如果都还无法满足您的需求,您也可以自行新建规则来匹配异常IP。使用fail2ban+Firewalld来阻止恶意IP是行之有效的办法,可极大提高服务器安全。

十一.设置grup密码

默认配置:无
加固方案:

1、输入grub密码。

  1. grub2-mkpasswd-pbkdf2

2、把输出纪录下来。

3、对/etc/grub.d/40_custom和/boot/grub2/grub.cfg进行备份。

  1. cp /etc/grub.d/40_custom /etc/grub.d/40_custom.backup
  2. cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg-bak

4、修改40_custom,在后面添加两行,把第一步的信息写到文件里去。

  1. vim /etc/grub.d/40_custom
  2. set superusers="root"
  3. password_pbkdf2 root grub.pbkdf2.sha512.10000.D83EEBCED27378122A16C68F775A8BF4F466CEF4A696F4733E5AC4029C3E574C766C95A8BCA991D4DB5257A7BF61242C36EEB071C81C97FE325CF43FAED7D61C.4905931D5723EB0C08E9714C0E9B3E7DDCD3B76E08996F7C88A1F742DB1656D74EE8DC83B2F3C58733C5C0F8A2DCBBEA96F2A0EC7CE4D49F786FEA411F631A80

5、重新启动grub菜单。

  1. grub2-mkconfig -o /boot/grub2/grub.cfg

.系统参数调优

 

默认配置:

加固方案:

	
  1. 1.备份配置文件
  2. cp -a /etc/sysctl.conf /etc/sysctl.conf.default
  3. 2.添加以下调优参数到该文件中
  4. net.ipv4.ip_forward = 1
  5. net.ipv4.conf.default.rp_filter = 1
  6. net.ipv4.conf.default.accept_source_route = 0
  7. kernel.sysrq = 0
  8. kernel.core_uses_pid = 1
  9. kernel.msgmnb = 65536
  10. kernel.msgmax = 65536
  11. kernel.shmmax = 68719476736
  12. kernel.shmall = 4294967296
  13. net.core.wmem_default = 8388608
  14. net.core.rmem_default = 8388608
  15. net.core.rmem_max = 16777216
  16. net.core.wmem_max = 16777216
  17. net.ipv4.route.gc_timeout = 20
  18. net.ipv4.tcp_retries2 = 5
  19. net.ipv4.tcp_wmem = 8192 131072 16777216
  20. net.ipv4.tcp_rmem = 32768 131072 16777216
  21. net.ipv4.tcp_mem = 94500000 915000000 927000000
  22. net.core.somaxconn = 32768
  23. net.core.netdev_max_backlog = 32764
  24. net.core.wmem_default = 8388608
  25. net.core.rmem_default = 8388608
  26. net.core.rmem_max = 16777216
  27. net.core.wmem_max = 16777216
  28. net.ipv4.ip_local_port_range = 1024 65535
  29. net.ipv4.tcp_retries2 = 5
  30. net.ipv4.tcp_timestamps = 0
  31. net.ipv4.tcp_tw_recycle = 1
  32. net.ipv4.tcp_keepalive_time = 1800
  33. net.ipv4.tcp_keepalive_probes = 3
  34. net.ipv4.tcp_keepalive_intvl = 30
  35. net.ipv4.tcp_max_orphans = 32768
  36. net.ipv4.tcp_wmem = 8192 131072 16777216
  37. net.ipv4.tcp_rmem = 32768 131072 16777216
  38. net.ipv4.tcp_mem = 94500000 915000000 927000000
  39.  
  40. fs.file-max = 1000000
  41. fs.inotify.max_user_instances = 8192
  42. kernel.pid_max = 65536
  43. net.ipv4.tcp_wmem = 4096 87380 8388608
  44. net.core.wmem_max = 8388608
  45. net.ipv4.tcp_window_scaling = 1
  46. net.ipv4.tcp_max_syn_backlog = 16384
  47. net.ipv4.tcp_syn_retries = 1
  48. net.ipv4.tcp_synack_retries = 1
  49.  
  50.  
  51. net.ipv4.tcp_syncookies = 1
  52. net.ipv4.tcp_tw_reuse = 1
  53. net.ipv4.tcp_fin_timeout = 30
  54.  
  55. net.ipv4.tcp_keepalive_time = 120
  56. net.ipv4.tcp_max_tw_buckets = 36000

 

  1. /sbin/sysctl -p /etc/sysctl.conf
  2. /sbin/sysctl -w net.ipv4.route.flush=1

 

版权说明: 本文为转载文章,源自互联网,由本站整编
文章分类: 技术教程
字数统计:本文共有 8690 个字
原文地址:

标签:

精彩评论

发表评论: