(1) 借助iptables的recent模块限制IP连接数

可以限制瞬间连接数过大的恶意IP(比如web应用防护,但不适用于LVS+Keepalived集群环境)

防护指令如下

  1. # 允许一个客户端60秒内可以发起20个对web服务器的连接
  2. iptables -I INPUT -p tcp --dport 80 -m state --state NEW -m recent --name web --set
  3. # iptables默认将日志输出到/var/log/messages
  4. iptables -A INPUT -m recent --update --name web --seconds 60 --hitcount 20 -j LOG --log-prefix 'HTTP attack: '
  5. iptables -A INPUT -m recent --update --name web --seconds 60 --hitcount 20 -j DROP

效率的产生是因为netfilter在网络应用的低层级上就实现了封堵,调用资源少。这如同硬件防火墙在转发面就实现封堵,而不会上升到控制面。

(2) 防SSH暴力破解

使用工具DenyHosts,原理为:

python2写成,分析/var/log/secure日志文件,当发现同一IP在进行多次SSH登陆尝试时,就会将IP记录到tcpwrappers配置文件/etc/hosts.deny中。

  • 环境确认
  1. ldd /sbin/sshd | grep libwrap
  2. 回显
  3. libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f2844d53000)
  4. 说明你所使用的sshd支持TCP Wrappers
  5. python -V 查看版本是不是2.x.x
  6. 可以用如下命令统计一下可疑IPv4地址的数量
  7. cat /var/log/secure | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | uniq -c
  8. 可以结合iptables再限制:
  9. #每分钟对ssh的新连接只允许两个,已建立的连接不限制
  10. iptables -P INPUT DROP
  11. iptables -A INPUT -m state state ESTABLISHED -j ACCEPT
  12. iptables -A INPUT -p tcp dport 22 -m limit limit 2/minute limit-burst 2 -m state state NEW -j ACCEPT
  • 安装

    方式一:
  1. # Debian/Ubuntu
  2. $ sudo apt-get install denyhosts
  3. # RedHat/CentOS
  4. $ yum update
  5. $ yum install denyhosts
  6. 或者
  7. $ wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
  8. $ rpm -Uvh rpmforge-release*rpm
  9. $ yum install denyhosts
  10. $ systemctl status denyhosts
  11. 或者
  12. $ service denyhosts status
  13. $ chkconfig denyhosts on
  14. # Archlinux
  15. $ yaourt denyhosts
  16. # Gentoo
  17. $ emerge -av denyhosts
  18. //白名单配置
  19. $ vim /etc/hosts.allow
  20. //添加你的IP到白名单
  21. $ sshd: Your_IP
  22. //黑名单配置
  23. $ vim /etc/hosts.deny
  24. //如果要禁止所有连接,那就
  25. $ sshd: ALL **
  26. //查看denyhosts收集到的恶意ip
  27. $ cat /etc/hosts.deny

denyhosts配置详解

配置文件/etc/denyhosts.conf

或者

/etc/denyhosts/denyhosts.cfg

具体可以使用如下命令查看:

rpm -ql denyhosts

  1. PURGE_DENY: removed HOSTS_DENY entries that are older than this time
  2. when DenyHosts is invoked with the --purge flag
  3. format is: i[dhwmy]
  4. Where 'i' is an integer (eg. 7)
  5. 'm' = minutes
  6. 'h' = hours
  7. 'd' = days
  8. 'w' = weeks
  9. 'y' = years
  10. # yum install denyhosts -y
  11. # cp denyhosts.cfg denyhosts.cfg.bak
  12. # vim denyhosts.cfg
  13. ############ THESE SETTINGS ARE REQUIRED ############
  14. SECURE_LOG = /var/log/secure #sshd的登陆日志文件
  15. HOSTS_DENY = /etc/hosts.deny #将需要阻止的IP写入到hosts.deny,所以这个工具只支持调用TCP Wrapper的协议
  16. PURGE_DENY = 1h #过多久后清除已阻止的IP,即阻断恶意IP的时长(1小时)
  17. BLOCK_SERVICE = sshd #作用的服务名
  18. DENY_THRESHOLD_INVALID = 1 #允许无效用户(不在/etc/passwd中)登录失败的次数
  19. DENY_THRESHOLD_VALID = 5 #允许普通有效用户登录失败的次数
  20. DENY_THRESHOLD_ROOT = 3 #允许root登录失败的次数
  21. DENY_THRESHOLD_RESTRICTED = 1 #设定 deny host 写入到该文件夹
  22. WORK_DIR = /var/lib/denyhosts #将deny的host或ip记录到work_dir中
  23. SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES
  24. HOSTNAME_LOOKUP=NO #是否做域名反解
  25. LOCK_FILE = /var/lock/subsys/denyhosts #将DenyHost启动的pid记录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务
  26. ############ THESE SETTINGS ARE OPTIONAL ############
  27. ADMIN_EMAIL = wawa@163.com #设置管理员邮件地址
  28. SMTP_HOST = localhost
  29. SMTP_PORT = 25
  30. SMTP_FROM = DenyHosts <nobody@localhost>
  31. SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME]
  32. AGE_RESET_VALID=5d
  33. AGE_RESET_ROOT=25d
  34. AGE_RESET_RESTRICTED=25d
  35. AGE_RESET_INVALID=10d
  36. ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE ##########
  37. DAEMON_LOG = /var/log/denyhosts #denyhost服务日志文件
  38. DAEMON_SLEEP = 30s
  39. DAEMON_PURGE = 1h #该项与PURGE_DENY 设置成一样,也是清除hosts.deniedssh 用户的时间

方式二:

  1. cd /usr/local/src
  2. tar zxvf DenyHosts-2.6.tar.gz
  3. cd DenyHosts-2.6
  4. python setup.py install
  5. 程序脚本自动安装到/usr/share/denyhosts
  6. 库文件安装到/usr/lib/python2.3/site-packages/DenyHosts
  7. denyhosts.py 自动安装到/usr/bin
  8. 设置启动脚本
  9. cd /usr/share/denyhosts
  10. cp daemon-control-dist daemon-control
  11. chown root daemon-control
  12. chmod 700 daemon-control
  13. grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg
  14. echo "/usr/share/denyhosts/daemon-control start" >> /etc/rc.local
  15. cd ln -s /usr/share/denyhosts/daemon-control denyhosts
  16. chkconfig --add denyhosts
  17. chkconfig --level 35 denyhosts on
  18. service denyhosts start

注意CentOS7的开启

  1. /etc/rc.local -> rc.d/rc.local
  2. #!/bin/bash
  3. # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
  4. #
  5. # It is highly advisable to create own systemd services or udev rules
  6. # to run scripts during boot instead of using this file.
  7. #
  8. # In contrast to previous versions due to parallel execution during boot
  9. # this script will NOT be run after all other services.
  10. #
  11. # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
  12. # that this script will be executed during boot.
  13. touch /var/lock/subsys/local
  14. /usr/share/denyhosts/daemon-control start

在登陆过程中如果出现如下信息,表示/etc/hosts.deny已经生效:

  1. Permission denied (publickey,gssapi-with-mic,password)
  2. 或者
  3. ssh_exchange_identification: read: Connection reset

查看/etc/hosts.deny,发现有

  1. sshd: 192.168.30.1

(3) 作为应用程序执行者的用户

一定要将其Shell设定为 nologin

(4) 查看登陆记录

  1. 命令:last
  2. 涉及的log文件 /var/log/wtmp
  3. chmod 640 /var/log/wtmp

(5) 不定期检查硬件损害情况

  1. grep error /var/log/messages

(6) 使用chkrootkit检查rootkit

  1. chkrootkit -n| grep 'INFECTED'

或者使用脚本加入cron

  1. # cat chkrootkit.sh
  2. #!/bin/bash
  3. PATH=/usr/bin:/bin
  4. TMPLOG=`mktemp`
  5. # Run the chkrootkit
  6. /usr/bin/chkrootkit > $TMPLOG
  7. # Output the log
  8. cat $TMPLOG | logger -t chkrootkit
  9. # bindshe of SMTPSllHow to do some wrongs
  10. if [ ! -z "$(grep 465 $TMPLOG)" ] && \
  11. [ -z $(/usr/sbin/lsof -i:465|grep bindshell) ]; then
  12. sed -i '/465/d' $TMPLOG
  13. fi
  14. # If the rootkit have been found,mail root
  15. [ ! -z "$(grep INFECTED $TMPLOG)" ] && \
  16. grep INFECTED $TMPLOG | mail -s "chkrootkit report in `hostname`" root

官网 http://www.chkrootkit.org/

(7) 开源IPS

官网 https://www.snort.org/

CentOS安全防护实例的更多相关文章

  1. centos 安装多实例数据库

    在Centos下安装多个MySql 5.7① 下载MySql 解压版安装包② 编写安装脚本③ 将脚本和安装包放置同一目录④ 编写my.cnf文件并放置在/etc/ 目录下⑤ 赋予脚本运行权限并运行⑥ ...

  2. CentOS 6 多实例 编译安装mariadb-5.5.59

    系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的源码包 http://mariadb.org/ mariadb- ...

  3. CentOS防火墙操作实例(启动、停止、开、闭端口)

    注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service   iptables status<回车>   停止防火墙: [root@localh ...

  4. centos网络配置实例

    1.配置DNS vim   /etc/resolv.conf nameserver 192.168.0.1 nameserver 8.8.8.8 nameserver 8.8.4.4 2.配置网关 r ...

  5. Centos网络配置

    网上搜索:centos网络配置的方法,主要包括dns.网关.IP地址,主要是配置resolv.conf\network\ifcfg-eth0这些网络配置文件. 稍后我会就centos7的网络配置进行实 ...

  6. centos 配置固定ip

    centos下网络配置方法(网关.dns.ip地址配置) 来源:互联网 作者:佚名 时间:07-13 00:32:07 [大 中 小] 本文介绍了centos网络配置的方法,centos网络配置主要包 ...

  7. centos 安装node js环境

    node.js支持多种平台安装,其中Win平台安装比较简单,下面重点讲解下Linux平台的安装步骤.本文以CentOS平台为实例,不准备讲 解采取源码编译安装方式,而是采取在node.js网站下载已经 ...

  8. centos下配置DNS

    centos网络配置实例 1,配置DNSvi /etc/resolv.conf加入: 代码如下: nameserver 192.168.0.1 nameserver 8.8.8.8 nameserve ...

  9. centos下配置dns,gateway,ip

    centos网络配置实例 1,配置DNS vi /etc/resolv.conf 加入:   代码如下: nameserver 192.168.0.1 nameserver 8.8.8.8 names ...

随机推荐

  1. 【学习心得】Link-cut Tree

    Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构.lct通过splay来维护每次的perferred path,说白了就是一个动态的树 ...

  2. BP算法演示

    本文转载自https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/ Background Backpropaga ...

  3. 3D Computer Grapihcs Using OpenGL - 12 Rotation Matrix

    为了证明我们上节渲染出来的是一个立方体而不是一个平面,我们决定将它旋转一定角度,这样我们就需要一个旋转矩阵(也属于ModelTransformMatrix的一部分) 上一节我们的ModelTransf ...

  4. linux sed 替换,使用变量,变量中含有特殊字符的处理

    文件 test.sh 内容如下: #!/bin/bash export JAVA_HOME=/data/jdk_1.9.0 echo $JAVA_HOME 想把 JAVA_HOME = 后面的内容替换 ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_18-练习三_按指定格式打印集合的方法

    主要锻炼用集合作为参数 使用if判断语句

  6. Git 实践

    最近也学习了Git的相关知识,现通过一个实例来记录Git使用流程,也方便日后使用. git的基础学习: https://www.yiibai.com/git/git-quick-start.html ...

  7. docker搭建harbor私有镜像库

    创建harbor私有镜像库 一.部署准备: harbor软件包   在部署节点上: 1)解压harbor的软件包将harbor目录下所有文件发送到/opt/目录下   tar zxvf harbor- ...

  8. C# picturebox 加载图片后透明显示在另一控件之上

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 【ABAP系列】SAP ABAP ALV中设置CHECKBOX同时选中事件

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP ALV中设置CHE ...

  10. js数组,运算符