https://www.ipdeny.com/ipblocks/

1. Ipset

ipset 的产生就是为了方便 iptables 的,ipset 可以减少 iptables 规则的冗余,尤其对于比较复杂的网络过滤时。iptables 使用 ipset 需要通过 -m set 指定,通过选项达到不同的效果。

ipset 命令说明:

ipset v7.15

Usage: ipset [options] COMMAND

Commands:
create SETNAME TYPENAME [type-specific-options]
        Create a new set
add SETNAME ENTRY
        Add entry to the named set
del SETNAME ENTRY
        Delete entry from the named set
test SETNAME ENTRY
        Test entry in the named set
destroy [SETNAME]
        Destroy a named set or all sets
list [SETNAME]
        List the entries of a named set or all sets
save [SETNAME]
        Save the named set or all sets to stdout
restore
        Restore a saved state
flush [SETNAME] # 清空 ipset 中指定或所有集合的 ip 条目(删条目,不删集合)
        Flush a named set or all sets
rename FROM-SETNAME TO-SETNAME
        Rename two sets
swap FROM-SETNAME TO-SETNAME
        Swap the contect of two existing sets
help [TYPENAME]
        Print help, and settype specific help
version
        Print version information
quit
        Quit interactive mode

Options:
-o plain|save|xml
       Specify output mode for listing sets.
       Default value for "list" command is mode "plain"
       and for "save" command is mode "save".
-s
        Print elements sorted (if supported by the set type).
-q
        Suppress any notice or warning message.
-r
        Try to resolve IP addresses in the output (slow!)
-!
        Ignore errors when creating or adding sets or
        elements that do exist or when deleting elements
        that don't exist.
-n
        When listing, just list setnames from the kernel.

-t
        When listing, list setnames and set headers
        from kernel only.
-f
        Read from the given file instead of standard
        input (restore) or write to given file instead
        of standard output (list/save).

Supported set types:
    list:set                3       skbinfo support
    list:set                2       comment support
    list:set                1       counters support
    list:set                0       Initial revision
    hash:mac                1       bucketsize, initval support
    hash:mac                0       Initial revision
    hash:ip,mac             1       bucketsize, initval support
    hash:ip,mac             0       Initial revision
    hash:net,iface  8       bucketsize, initval support
    hash:net,iface  7       skbinfo and wildcard support
    hash:net,iface  6       skbinfo support
    hash:net,iface  5       forceadd support
    hash:net,iface  4       comment support
    hash:net,iface  3       counters support
    hash:net,iface  2       /0 network support
    hash:net,iface  1       nomatch flag support
    hash:net,iface  0       Initial revision
    hash:net,port   8       bucketsize, initval support
    hash:net,port   7       skbinfo support
    hash:net,port   6       forceadd support
    hash:net,port   5       comment support
    hash:net,port   4       counters support
    hash:net,port   3       nomatch flag support
    hash:net,port   2       Add/del range support
    hash:net,port   1       SCTP and UDPLITE support
    hash:net,port,net       3       bucketsize, initval support
    hash:net,port,net       2       skbinfo support
    hash:net,port,net       1       forceadd support
    hash:net,port,net       0       initial revision
    hash:net,net    3       bucketsize, initval support
    hash:net,net    2       skbinfo support
    hash:net,net    1       forceadd support
    hash:net,net    0       initial revision
    hash:net                7       bucketsize, initval support
    hash:net                6       skbinfo support
    hash:net                5       forceadd support
    hash:net                4       comment support
    hash:net                3       counters support
    hash:net                2       nomatch flag support
    hash:net                1       Add/del range support
    hash:net                0       Initial revision
    hash:ip,port,net        8       bucketsize, initval support
    hash:ip,port,net        7       skbinfo support
    hash:ip,port,net        6       forceadd support
    hash:ip,port,net        5       comment support
    hash:ip,port,net        4       counters support
    hash:ip,port,net        3       nomatch flag support
    hash:ip,port,net        2       Add/del range support
    hash:ip,port,net        1       SCTP and UDPLITE support
    hash:ip,port,ip 6       bucketsize, initval support
    hash:ip,port,ip 5       skbinfo support
    hash:ip,port,ip 4       forceadd support
    hash:ip,port,ip 3       comment support
    hash:ip,port,ip 2       counters support
    hash:ip,port,ip 1       SCTP and UDPLITE support
    hash:ip,mark    3       bucketsize, initval support
    hash:ip,mark    2       skbinfo support
    hash:ip,mark    1       forceadd support
    hash:ip,mark    0       initial revision
    hash:ip,port    6       bucketsize, initval support
    hash:ip,port    5       skbinfo support
    hash:ip,port    4       forceadd support
    hash:ip,port    3       comment support
    hash:ip,port    2       counters support
    hash:ip,port    1       SCTP and UDPLITE support
    hash:ip         5       bucketsize, initval support
    hash:ip         4       skbinfo support
    hash:ip         3       forceadd support
    hash:ip         2       comment support
    hash:ip         1       counters support
    hash:ip         0       Initial revision
    bitmap:port             3       skbinfo support
    bitmap:port             2       comment support
    bitmap:port             1       counters support
    bitmap:port             0       Initial revision
    bitmap:ip,mac   3       skbinfo support
    bitmap:ip,mac   2       comment support
    bitmap:ip,mac   1       counters support
    bitmap:ip,mac   0       Initial revision
    bitmap:ip               3       skbinfo support
    bitmap:ip               2       comment support
    bitmap:ip               1       counters support
    bitmap:ip               0       Initial revision
  • SETNAME 是创建的 ipset 的名称,

  • TYPENAME 是 ipset 的类型:TYPENAME := method:datatype [,datatype [,datatype]],method 指定 ipset 中的 entry 存放的方式,随后的 datatype 约定了每个 entry 的格式。

    • 可以使用的 method:bitmap、hash、list

    • 可以使用的 datatype:ip、net、mac、port、iface、mark

iptables 使用 ipset 的方法:

[ ! ] --match-set setname flag [,flag] ...

flag 可以通过指定为 src 或 dst,flag 最多数量为 6 个,中间使用逗号隔开。

1.1. 举例说明

  1. 屏蔽一组地址

    创建了一个叫“myset”的“net”网络地址的“hash”集合。
    # ipset create myset hash:net
    或
    # ipset -N myset nethash
    
    把希望屏蔽的地址网段添加到集合中。
    # ipset add myset 14.144.0.0/12
    # ipset add myset 27.8.0.0/13
    # ipset add myset 58.16.0.0/15

    丢弃来自源上述网段的数据包。

    # iptables -I INPUT -m set --match-set myset src -j DROP
  2. 屏蔽多个 IP 地址

    创建一个 IP 地址"集合",下面命令创建一个 "myset-ip" 散列集合。
    # ipset create myset-ip hash:ip,port
    
    将要屏蔽的地址加入此集合:
    # ipset add myset-ip 192.168.1.105,tcp:80
    # ipset add myset-ip 172.16.1.101,tcp:80
    
    丢弃来自源 IP 192.168.1.105、172.16.1.101,目的端口 80 的包
    # iptables -I INPUT -m set --match-set myset-ip src,dst -j DROP
    
    丢弃来自源 IP 192.168.1.105、172.16.1.101,源端口 80 的包
    # iptables -A INPUT -m set --match-set test src,src -j DROP
  3. 匹配 IP 和 Mac

    # 创建名称为test1的ipset
    ipset create test1 hash:ip,mac
    ipset add test1 192.168.1.105,3C:7C:3F:D7:94:AF
    
    # 丢弃来自源 IP 192.168.1.105,源 MAC 3C:7C:3F:D7:94:AF 的包
    iptables -A INPUT -m set --match-set test1 src,src -j DROP
  4. nomatch(hash:net 适用 )

    匹配被设置了 nomatch 标志的条目。iptables 可以用 –nomatch 选项,专门匹配 ipset 中带 nomatch 的条目。

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

    也就是说,这个通常与 hash:net 搭配使用,用来跳过 hash:net 指定的 ip netmask address.

    举例:

    # 创建类型为 hash:net 的 test3 ipset
    ipset create test3 hash:net
    
    # 添加 nomatch,匹配时跳过此条目
    ipset add test3 192.168.1.0/24 nomatch
    
    # iptables 匹配 test3 中,设置为 nomatch 的条目的网络段的 icmp 包丢弃
    iptables -I INPUT -m set --match-set test3 src --return-nomatch -p icmp -j DROP
  5. counters、packets、bytes(所有集合适用)

    如果指定了该选项,则使用每个元素支持的包和字节计数器创建集合。当元素(重新)添加到集合中时,除非包和字节选项显式指定包和字节计数器值,否则包和字节计数器将初始化为零。

  6. [!] –packets-eq value;–packets-lt value;–packets-gt value

    • 如果包匹配了 ipset 中的一个条目,并且包的数量等于(!不等于)设置的 value 值时,iptables 的动作将作用于此条目。

    • 用法同 –packets-eq value,但是是当包的数量小于 value

    • 用法同 –packets-eq value,但是是当包的数量大于 value

  7. [!] –bytes-eq value; –bytes-lt value; –bytes-gt value

    用法同 –packets-eq,–packets-lte,–packets-gt。只是匹配的是字节数。

    举例:

    创建 test ipset,记得要指定 counters 选项
    # ipset create test hash:ip counters
    
    添加 192.168.1.105 到 test ipset
    # ipset add test 192.168.1.105
    
    匹配到小于 3 个 icmp 包的 ipset 条目丢弃
    # iptables -I INPUT -m set --match-set test src --packets-lt 3 -p icmp -j DROP
    
    ipse list 命令可看到当前包的数量统计和字节统计
    # ipset list
    Name: test
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 counters
    Size in memory: 248
    References: 1
    Number of entries: 1
    Members:
    192.168.1.105 packets 4 bytes 240
    
    ping 的效果,前两个包没 ping 通,第三个包开始通
    # ping 192.168.1.40
    Pinging 192.168.1.40 with 32 bytes of data:
    Request timed out.
    Request timed out.
    Reply from 192.168.1.40: bytes=32 time<1ms TTL=64
    Reply from 192.168.1.40: bytes=32 time<1ms TTL=64
    
    Ping statistics for 192.168.1.40:
        Packets: Sent = 4, Received = 2, Lost = 2 (50% loss),
    Approximate round trip times in milli-seconds:
  8. ! –update-counters

    如果 ipset 命令指定了 counters 选项创建一个 ipset 时,匹配的条目的包和字节的数量会增加,如下所示:

    # ipset list
    Name: test
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 counters
    Size in memory: 248
    References: 1
    Number of entries: 1
    Members:
    192.168.1.105 packets 4 bytes 240
    
    # ipset list
    Name: test
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 counters
    Size in memory: 248
    References: 1
    Number of entries: 1
    Members:
    192.168.1.105 packets 6 bytes 360

    如果指定 !–update-counters,ipset 条目后的 packets 和 bytes 匹配到了包,数量不会增加。

  9. 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 选项只是一个小数。

  10. hashsize 集合的初始哈希大小(hash 集合适用)

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

    举例:

    [root@upel2-3 ~]# ipset
    ipset            ipset-translate
    [root@upel2-3 ~]# ipset
    ipset            ipset-translate
    [root@upel2-3 ~]# ipset create hashsizeset hash:ip
    [root@upel2-3 ~]# ipset list hashsizeset
    Name: hashsizeset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536
    Size in memory: 304
    References: 0
    Number of entries: 0
    Members:
    [root@upel2-3 ~]# ipset create hashsizeset1 hash:ip hashsize 2048
    [root@upel2-3 ~]#
    [root@upel2-3 ~]# ipset list hashsizeset1
    Name: hashsizeset1
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 2048 maxelem 65536
    Size in memory: 328
    References: 0
    Number of entries: 0
    Members:
  11. maxelem 集合存储最大数量(hash 集合适用)

    它定义了可以存储在集合中的元素的最大数量,默认值为 65536

    举例:

    [root@upel2-3 ~]# ipset create maxelemset hash:ip maxelem 100000
    [root@upel2-3 ~]# ipset list maxelemset
    Name: maxelemset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 100000
    Size in memory: 304
    References: 0
    Number of entries: 0
    Members:
  12. family {inet | inet6} IPv4/IPv6(适用 hash 集合(hash:mac 除外))

    这个参数对于除 hash:mac 之外的所有 hash 类型集的 create 命令都是有效的。它定义了要存储在集合中的 IP 地址的协议族

    举例:

    [root@upel2-3 ~]# ipset create familyset hash:ip  family inet6
    [root@upel2-3 ~]# ipset list familyset
    Name: familyset
    Type: hash:ip
    Revision: 4
    Header: family inet6 hashsize 1024 maxelem 65536        # inet6
    Size in memory: 312
    References: 0
    Number of entries: 0
    Members:
  13. forceadd 集合满时,随机删除(所有集合适用)

    当使用此选项创建的集合已满时,集合的下一个添加项可能成功并从集合中删除随机项。

  14. timeout 设置超时时间 / 生效时间(所有集合适用),如果设置为 0,表示永久生效,超时时间可以通过 -exist 来进行修改

    举例:

    [root@upel2-1 ~]# ipset create timeoutset hash:ip timeout 100
    [root@upel2-1 ~]# ipset list timeoutset
    Name: timeoutset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 timeout 100
    Size in memory: 304
    References: 0
    Number of entries: 0
    Members:
    [root@upel2-1 ~]#
    [root@upel2-1 ~]# ipset -exist add timeoutset 192.168.0.100
    [root@upel2-1 ~]#
    [root@upel2-1 ~]# ipset list timeoutset
    Name: timeoutset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 timeout 100
    Size in memory: 400
    References: 0
    Number of entries: 1
    Members:
    192.168.0.100 timeout 94
    [root@upel2-1 ~]#
    [root@upel2-1 ~]# ipset list timeoutset // 100s 后再查看
    Name: timeoutset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 timeout 100
    Size in memory: 400
    References: 0
    Number of entries: 0
    Members:
    [root@upel2-1 ~]#
    [root@upel2-1 ~]# ipset -exist add timeoutset 192.168.0.100 timeout 200
    [root@upel2-1 ~]# ipset list timeoutset
    Name: timeoutset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 timeout 100
    Size in memory: 400
    References: 0
    Number of entries: 1
    Members:
    192.168.0.100 timeout 198
    [root@upel2-1 ~]#
  15. comment 备注(所有集合适用)

    在 ipset 上启用此扩展可以使用任意字符串注释 ipset 条目。内核和 ipset 本身完全忽略这个字符串,纯粹是为了提供一种方便的方法来记录条目存在的原因。注释不能包含任何引号,通常的转义字符没有任何意义。

    举例:

    [root@upel2-1 ~]# ipset create commentset hash:ip comment
    [root@upel2-1 ~]# ipset list commentset
    Name: commentset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 comment
    Size in memory: 304
    References: 0
    Number of entries: 0
    Members:
    [root@upel2-1 ~]# ipset add commentset 10.250.1.100 comment "only ip"
    [root@upel2-1 ~]#
    [root@upel2-1 ~]# ipset list commentset
    Name: commentset
    Type: hash:ip
    Revision: 4
    Header: family inet hashsize 1024 maxelem 65536 comment
    Size in memory: 424
    References: 0
    Number of entries: 1
    Members:
    10.250.1.100 comment "only ip"

1.2. 使 ipset 持久化

上面创建的 ipset 存在于内存中,重启后将会消失。要使 ipset 持久化,你要这样做:

首先把 ipset 保存到 /etc/ipset.conf:

# ipset save > /etc/ipset.conf

然后启用 ipset.service, 与 iptables.service 相似,这个服务用于恢复 iptables 规则。

1.3. 其他命令

查看集合。
# ipset list

从集合中删除 IP 地址
ipset del myset {ip}

删除名为“myset”的集合。
# ipset destroy myset

删除所有集合。
# ipset destroy

导入 ipset 规则
ipset restore -f blacklist.txt

更多信息请参考 ipset 的 man 手册页。

1. Ipset — 尹晓剑知识库 1.0.0 文档