zoukankan      html  css  js  c++  java
  • Linux使用iptables设置黑白名单

    使用ipset工具

    1,下面我先说下iptables的基本配置规则,然后再说ipset
    以下使用C7 x86_64为实验环境
    CentOS7默认的防火墙不是iptables,而是firewalle.
    如果你没有安装iptables的话,你可以使用以下命令进行安装
    systemctl stop firewalld
    systemctl disable firewalld
    systemctl mask firewalld
    上面的意思是先屏蔽掉原有的firewall防火墙,下面我们就开始安装iptables,至于为什么要安装IPtables我就不讲了
    yum install iptables iptables-services -y

    设置规则
    #查看iptables现有规则
    iptables -L -n
    #先允许所有,不然有可能会杯具
    iptables -P INPUT ACCEPT
    #清空所有默认规则
    iptables -F
    #清空所有自定义规则
    iptables -X
    #所有计数器归0
    iptables -Z
    #允许来自于lo接口的数据包(本地访问)
    iptables -A INPUT -i lo -j ACCEPT
    #开放22端口
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    #开放21端口(FTP)
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    #开放80端口(HTTP)
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    #开放443端口(HTTPS)
    iptables -A INPUT -p tcp --dport 443 -j ACCEPT
    #允许ping
    iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
    #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
    iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    #其他入站一律丢弃
    iptables -P INPUT DROP
    #所有出站一律绿灯
    iptables -P OUTPUT ACCEPT
    #所有转发一律丢弃
    iptables -P FORWARD DROP

    其他规则设定
    #如果要添加内网ip信任(接受其所有TCP请求)
    iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
    #过滤所有非以上规则的请求
    iptables -P INPUT DROP
    #要封停一个IP,使用下面这条命令:
    iptables -I INPUT -s ... -j DROP
    #要解封一个IP,使用下面这条命令:
    iptables -D INPUT -s ... -j DROP

    #保存上述规则
    service iptables save
    开启iptables服务
    #注册iptables服务
    #相当于以前的chkconfig iptables on
    systemctl enable iptables.service
    #开启服务
    systemctl start iptables.service
    #查看状态
    systemctl status iptables.service

    2,现在我们介绍ipset
    ipset是iptables的扩展,它允许你创建 匹配整个地址集合的规则。而不像普通的iptables链只能单IP匹配, ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找,除了一些常用的情况,比如阻止一些危险主机访问本机,从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置.官网:http://ipset.netfilter.org/

    ipset的安装
    首先先安装依赖
    yum provides '*/applydeltarpm'
    yum install deltarpm -y
    yum install ipset -y
    创建一个ipset
    ipset create xxx hash:net (也可以是hash:ip ,这指的是单个ip,xxx是ipset名称)
    ipset默认可以存储65536个元素,使用maxelem指定数量
    ipset create blacklist hash:net maxelem 1000000 #黑名单
    ipset create whitelist hash:net maxelem 1000000 #白名单
    查看已创建的ipset
    ipset list
    在创建的黑名单中添加一个ip
    ipset add blacklist 192.168.4.175
    删除黑名单ip
    ipset del blacklist 192.168.4.175
    创建防火墙规则,与此同时,allset这个IP集里的ip都无法访问80端口(如:CC×××可用)
    iptables -I INPUT -m set --match-set blacklist src -p tcp -j DROP
    iptables -I INPUT -m set --match-set whitelist src -p tcp -j DROP
    iptables -I INPUT -m set --match-set setname src -p tcp --destination-port 80 -j DROP
    service iptables save
    将ipset规则保存到文件
    ipset save blacklist -f blacklist.txt
    ipset save whitelist -f whitelist.txt
    删除ipset
    ipset destroy blacklist
    ipset destroy whitelist
    导入ipset规则
    ipset restore -f blacklist.txt
    ipset restore -f whitelist.txt
    备注:ipset的一个优势是集合可以动态的修改,即使ipset的iptables规则目前已经启动,新加的入ipset的ip也生效

    3,应用场景

    例:某服务器被CC×××,经过抓包或者一序列手段发现有一批IP是源×××ip,因此我们需要封掉这些IP,如果用iptables一条一条加就麻烦些了。
    #对TIME_WAIT的外部ip以及此对ip出现的次数经行求重排序。
    netstat -ptan | grep TIME_WAIT | awk '{print $5}' | awk -F: '{print $1}' |sort |uniq -c | sort -n -r
    #tcpdump 抓取100个包,访问本机80的ip进行求重排序 只显示前20个,数量多的ip可能为×××源IP,我们需要封掉它
    tcpdump -tnn dst port 80 -c 100 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -n -r |head -20
    #新建一个setname.txt文件,以如下格式加入这些ip (有多少个ip就多少行)
    vim setname.txt
      add setname xxx.xxx.xxx.xxx
    #导入setname.txt文件到ipset集
    ipset restore -f setname.txt
    #查看是否导入成功 (成功的话会发现一个新ipset名为 sername,且Members里就是那些×××IP)
    ipset list
    #建立一条iptables规则,拦截这些×××ip访问服务器80,也可以直接禁止这些ip的所有访问
    iptables -I INPUT -m set --match-set setname src -p tcp --destination-port 80 -j DROP

  • 相关阅读:
    Windows如何自定义U盘盘符、文件夹图标、文件夹背景
    BIN文件如何打开
    Windows COM Surrogate 已停止工作怎么办
    EasyBoot使用方法
    SRS Audio Sandbox没有声音怎么办
    [Compose] 20. Principled type conversions with Natural Transformations
    [Compose] 19. Leapfrogging types with Traversable
    [Compose] 17. List comprehensions with Applicative Functors
    [Compose] 15. Applicative Functors for multiple arguments
    [Algorithm] Coding Interview Question and Answer: Longest Consecutive Characters
  • 原文地址:https://www.cnblogs.com/klb561/p/10924314.html
Copyright © 2011-2022 走看看