简介

由于公司线上生产环境firewalld默认都是关闭的,所以只能依赖iptables的表来方便管理IP黑白名单.

提示线上生产环境不要随便重启iptables,会导致docker已经映射好的关系表都没有了.

不要执行iptables-save,会把当前临时的一些规则保存到/etc/sysconfig/iptables,如有变动,重启又会被load出来.

不要执行iptables -F 清空规则.

增删改表单后,不要重启iptables,规则都是立即生效.

本本主要内容:ipset表应用

ip set是linux内核的一个内部框架,可由ipset工具管理.

ipset 适用于以下几种场景:

  • 一次性存储大量的ip或者端口,用以iptables匹配
  • 在不影响性能的前提下,动态更新iptables规则(针对ip或者端口),表单更新时不用频繁重启iptabels服务,立即生效
  • 期望使用ipset的告诉匹配,或者在一条iptables规则中表达复杂的 ip/端口规则,使规则看起来更简单明了

安装ipset

#安装ipset,大多数系统7已经默认安装过了
yum install ipset #备份原有的iptabels到文件
iptables -nL >myiptabls.oldlist
iptables-save >./myiptables.old

定义hash:net类型黑白名单

任何端口协议

#白名单
ipset create opsallow hash:net maxelem 10000
iptables -A INPUT -m set --match-set opsallow src -j ACCEPT #黑名单
ipset create opsdeny hash:net maxelem 500000
iptables -A INPUT -m set --match-set opsdeny src -j DROP #查看表
ipset list 表名 #保存表
ipset save 表名 -f 表名bak.txt #恢复表
ipset restore -f 表名bak.txt #查看iptables表(这里注意优先级)
iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 match-set opsallow src
DROP all -- 0.0.0.0/0 0.0.0.0/0 match-set opsdeny src
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 match-set opsallowipport src,dst
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 match-set opsdenyipport src,dst
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 match-set denytemp1d src
#加白名单,把公司自己IP先加进去
ipset add opsallow 123.123.123 #加黑名单
#ipset add opsdeny 45.9.20.101 #删白名单
ipset del opsallow 1.1.1.1
#删黑名单
ipset del opsallow 1.1.1.1 #网段
ipset add opsallow 192.168.255.0/24
ipset add opsallow 172.17.0.0/16 #清空表单
ipset flush 表名 #删除表
ipset destroy 表名 #测试是否在表中匹配到
ipset test opsdeny 192.168.255.166

定义hash:ip,port类型黑白名单

指定端口

#白名单
ipset create opsallowipport hash:ip,port maxelem 10000
iptables -A INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT #黑名单
ipset create opsdenyipport hash:ip,port maxelem 500000
iptables -A INPUT -p tcp -m set --match-set opsdenyipport src,dst -j DROP #增删
ipset add opsallowipport 192.168.255.166,80
ipset del opsallowipport 192.168.255.166,80
ipset del opsallowipport 192.168.255.166,80-89
ipset del opsallowipport 192.168.255.166,udp:53
ipset add opsdenyipport 192.168.255.166,tcp:80
ipset add opsdenyipport 192.168.255.161-192.168.255.166,8080 #这里走了些弯路,特此整理成表,只告诉了怎么加到ipset表,缺很少告诉你src,dst都需要指定,只写src导致ipset无效
ipset type | iptables match-set | Packet fields
------------------+--------------------+---------------------------------
hash:ip,port | src,dst | src IP address, dst port
hash:net,port,net | src,dst,dst | src IP address, dst port, dst IP address
hash:net,port,net | dst,src,src | dst IP address, src port, src IP address
hash:ip,port,ip | src,dst,dst | src IP address, dst port, dst IP address
hash:ip,port,ip | dst,src,src | dst IP address, src port, src ip address
hash:mac | src | src mac address
hash:mac | dst | dst mac address
hash:ip,mac | src,src | src IP address, src mac address
hash:ip,mac | dst,dst | dst IP address, dst mac address
hash:ip,mac | dst,src | dst IP address, src mac address

其他


#规则优先级
#这里也很容易走弯路如果ipset的allow和deny里都有,iptables中opsallowipport在opsdenyipport前面,那么allow优先;
#也就是说ipables中谁在前面,谁先匹配到,谁就有优先权.
#因此根据实际需要,要注意iptables条目的顺序先后. #查看排序规则
#iptables -nL INPUT
[root@opstest zzw]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 match-set opsdenyipport src,dst
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 match-set opsallowipport src,dst
#加到最前面
iptables -I INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT
#追加到后面
iptables -A INPUT -p tcp -m set --match-set opsallowipport src,dst -j ACCEPT
#创建自动1d解封
ipset create denytemp1d hash:ip timeout 86400
iptables -A INPUT -p tcp -m set --match-set denytemp1d src -j DROP
#添加注释
[root@opstest zzw]# ipset create opsdenyipport hash:ip,port maxelem 500000 comment
[root@opstest zzw]# ipset add opsdenyipport 192.168.255.167,tcp:80 comment "bad man"
[root@opstest zzw]# ipset list opsdenyipport
Name: opsdenyipport
Type: hash:ip,port
Revision: 5
Header: family inet hashsize 1024 maxelem 500000 comment
Size in memory: 240
References: 0
Number of entries: 1
Members:
192.168.255.167,tcp:80 comment "bad man"

防火墙之ipset表应用的更多相关文章

  1. 期中架构&防火墙¥四表五链

    今日内容 架构图 包过滤防火墙 Iptables 新建虚拟机 内容详细 一.架构图 用户通过域名访问一个网站类比开车去饭店用餐 访问网站的流程 1.浏览器输入网站的域名(www.baidu.com), ...

  2. Openwrt上使用dnsmasq和ipset实现域名分流

    目标 部署一台自动代理路由器,实现根据域名来自动设定直连或者代理,而我要做的只是设置PC的默认网关为主路由器(192.168.0.1)还是自动代理路由器(192.168.0.254). 创建Openw ...

  3. dnsmasq-2.48没有ipset特性,安装dnsmasq-2.71来支持ipset

    iptables只能根据ip地址进行转发,不能识别域名,而dnsmasq-full不仅可以实现域名-IP的映射,还可以把这个映射关系存储在ipset中,所以使用dnsmasq+ipset就可以实现ip ...

  4. 6. Samba服务和防火墙配置笔记

    Samba文件服务器 (一)简介 内网文件服务器 windows-windows 文件共享服务 网络邻居linux-linux NFS windows-linux Samba (二)端口 smbd: ...

  5. [转] Linux下防火墙iptables用法规则详及其防火墙配置

    from: http://www.cnblogs.com/yi-meng/p/3213925.html 备注: 排版还不错,建议看以上的链接. iptables规则 规则--顾名思义就是规矩和原则,和 ...

  6. centos防火墙操作

    centos防火墙基本操作 #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT#/sbin/iptables -I INPUT -p tcp -- ...

  7. 防火墙和iptables

    本文目录: 6.1 为什么需要防火墙 6.2 数据传输流程 6.2.1 网络数据传输过程 6.2.2 本机数据路由决策 6.3 TCP三次握手.四次挥手以及syn攻击 6.3.1 三次握手建立tcp连 ...

  8. CentOS_6.5配置iptables防火墙策略

    ###############配置filter表防火墙############### #清除预设表filter中的所有规则链的规则iptables -F #清除预设表filter中使用者自定链中的规则 ...

  9. 企业防火墙之iptables

    1.1 企业中安全优化配置原则 尽可能不给服务器配置外网ip ,可以通过代理转发或者通过防火墙映射.并发不是特别大情况有外网ip,可以开启防火墙服务. 大并发的情况,不能开iptables,影响性能, ...

随机推荐

  1. 《自动控制原理》个人笔记(来自ppt课件)

    控制的含义 控制(CONTROL)----某个主体使某个客体按照一定的目的动作.主体–人:人工控制: 机器:自动控制客体–指一件物体,一套装置,一个物化过程,一个特定系统. 人工控制与自动控制 人在控 ...

  2. 排序 | 冒泡排序的优化与qsort快速排序

    冒泡排序 冒泡排序 Bubble_Sort,是极为简单的一种排序算法.虽然效率差一点,但好在具有结构简单,容易理解,易于操作等优点.冒泡排序就是把小的元素往前调或者把大的元素往后调.在相邻的两个元素间 ...

  3. Python中的numpy库介绍!

    转自:https://blog.csdn.net/codedz/article/details/82869370 机器学习算法中大部分都是调用Numpy库来完成基础数值计算的.安装方法: pip3 i ...

  4. [译]HTML&CSS Lesson5: 定位

    CSS最大的用处之一就是可以将内容和元素定位到任何我们想要的位置,使我们的设计具有结构,使内容更加易懂. CSS有好几种不同的定位属性,每种都有自己的使用场景.在这节课中我们会通过不同的案例--可复用 ...

  5. Vue3生命周期的理解

    beforeCreate():在实例生成之前 created():在实例生成之后 beforeMount():在模板已经被编译成函数之后,组件内容被渲染到页面之前 mounted():在组件内容被渲染 ...

  6. c++字符串替换

    #include <string> #include <iostream> using namespace std; string m_replace(string strSr ...

  7. Jquery中each的3种遍历方式

    学习目标: 参考博文: https://blog.csdn.net/honey_th/article/details/7404273 一.Jquery中each的几种遍历方法 1. 选择器+遍历 &l ...

  8. web.xml的作用及基本配置

    web工程中的web.xml文件有什么作用呢?它是每个web.xml工程都必须的吗? 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的.那什么时候需要,什 ...

  9. 设计模式之:享元模式FlyweightPattern的实现

    享元模式的理解: 享元模式的定义:运用共享技术支持大量细粒度对象的复用: Flyweight Pattern Definition:Use sharing to support large numbe ...

  10. spring源码编译完整步骤拿来即用!

    1.版本选择 1)源码版本:spring5.3.x 2)gradle版本:根据spring源码的工程路径:gradle/wrapper/gradle-wrapper.properties文件查看gra ...