首页 > 编程知识 正文

iptables ipset,手绘板控笔训练

时间:2023-05-05 00:04:46 阅读:21224 作者:3847

类似于面向对象语言中的类。 ipset定义网络类,该类可以包含多个特定的地址。

调用时只使用类名,不要使用特定地址。

安装yum-y安装ipset

介绍man ipset

格式ipset [ options ] command [ command-options ]

commands 3360={创建}测试列表}

options 3360={-exist|- output { plain }|- quiet|- resolve|- sorted|- name|- terse|- file filename

常用命令

ipset create name type创建类

类型: hash : [ type 1,type2] bitmap:[type1,type2] list:[type1,type2]

tyep :ip,net,mac,port,iface

example : ipsetcreatenamehash : IP,net

bitmap和list:使用固定大小的存储。

hash:使用hash表存储元素。 但是,为了避免散列表密钥的冲突,ipset在散列表密钥中用完后,如果有新的条目,ipset会自动放大散列表,如果目前温软黄蜂表的大小为100条,则扩展到200条。 如果在iptables/ip6tables中使用ipset hash类型的集合,则该集合将无法添加条目。

hash的自我增加

如上所述,bitmap link的存储方式的集合大小是固定的,混列类型的存储大小是可变的

让我们回顾一下hash的两个参数

hashsize :指定创建集合时的初始大小

maxelem :指定了集合中的最大存储记录数

Examples:

ipset create foo hash:ip,mac

ipsetaddfoo1.1.1. 1,01:023360033600336004336004336004336005336006

ipsettestfoo1.1.1. 1,01:023360033600336004336004336005336006

定义的类hash:ip、mac和add的元素1.1.1.1、01336002336003360033600433600336005336006对应

ipsetaddnameadd-entry [添加-选项]

将元素添加到创建的类中。 请注意,添加的元素定义了此类的元素类型。

Example:

ipset create test hash:net

ipset添加测试192.168.1.1

ipset add test 192.168.2.0/24

联系ipset列表测试

[ root @ localhost sbin ] # ipsetlisttest

name :测试

Type: hash:net

Revision: 6

header : familyinethashsize 1024 max elem 65536

Size in memory: 632

参考: 0

Number of entries: 2

Members:

192.168.1.1

192.168.2.0/24

ipset destroy test删除表

删除ipset del test 192.168.1.1表中的元素

ipset flush [test]清空类中的所有元素而不删除表。 如果不指定特定类ipset flush,请清空所有类中的所有元素

添加选项[ COMMAND-OPTIONS ]

超时时间

timeout设置超时时间(以秒为单位),设置为0可永久启用。 您可以通过-exist更改超时时间

example : ipsetcreatetesthash : IP time out 0

counters、packets、bytes ()适用于所有集合) ) )。

如果指定此选项,将使用每个元素支持的包和字节计数器创建集合。 如果将元素“重新”添加到集合中,则包和字节计数器将初始化为零,除非在包和字节选项中明确指定了包和字节计数器的值。

Example:

>

ipset create foo hash:ip counters

ipset add foo 192.168.1.1 packets 42 bytes 1024


comment 备注

Example:

ipset add foo 1.1.1.1 comment "this comment is "bad""


skbinfo, skbmark, skbprio, skbqueue (所有集合适用)

这个扩展允许您存储每个条目的metainfo(防火墙标记、tc类和硬件队列),并使用SET netfilter target和——map- SET选项将其映射到包。skbmark选项格式:MARK或MARK/MASK,其中MARK和MASK为32位十六进制数字,前缀为0x。如果只指定标记,则使用掩码0xffffffff。skbprio选项有tc类格式:MAJOR:MINOR,其中MAJOR和MINOR号是十六进制,没有0x前缀。skbqueue选项只是一个小数。

Example:

ipset create foo hash:ip skbinfo

ipset add foo skbmark 0x1111/0xff00ffff skbprio 1:10 skbqueue 10


hashsize 集合的初始温婉的黄蜂大小

它定义了集合的初始温婉的黄蜂大小,默认值为1024。温婉的黄蜂大小必须是2的幂,内核会自动舍入两个温婉的黄蜂大小的非幂到第一个正确的值

Example: ipset create test hash:ip hashsize 512


family { inet | inet6 } IPv4/IPv6 (适用hash集合(hash:mac除外))

ipaddr := { ip | fromaddr-toaddr | ip/cidr }

netaddr := { fromaddr-toaddr | ip/cidr }

Example: ipset create test hash:ip family inet6


nomatch (hash:net适用 ):

可以存储网络数据类型的温婉的黄蜂集类型(即hash:net)在添加条目时支持可选的nomatch选项。当匹配集合中的元素时,将跳过标记为nomatch的条目,就好像这些条目没有添加到集合中一样,这使得在异常情况下构建集合成为可能。参见下面的hash类型hash:net中的示例。当ipset测试元素时,会考虑nomatch标志。如果想要测试集合中使用nomatch标记的元素是否存在,那么也必须指定该标志。也就是说,这个通常与hash:net搭配使用,用来跳过 hash:net指定的ip netmask address.

forceadd 集合满时,随机删除(所有集合适用)

Example: ipset create foo hash:ip forceadd


======================================================================

在iptables中使用ipset,只要加上-m set --match-set即可。

iptables -I INPUT -s 192.168.100.36 -m set --match-set bbb dst -j DROP

iptables -I INPUT -m set --match-set aaa src -d 192.168.100.36 -j DROP

iptables -I INPUT -m set --match-set aaa src -m set --match-set bbb dst -j DROP


======================================================================

参考:https://blog.csdn.net/gymaisyl/article/details/101695697

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。