暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

ipset 日常使用

生有可恋 2022-11-16
1105

ipset 结合 iptables 可以简化 iptables 规则书写。ipset 可以理解为 ip 集合,iptables 可以像处理 IP 一样处理 ipset。

我们通过一个例子来演示 ipset 日常如何使用。

首先创建一个 ipset

    $ ipset create denylist hash:ip

    我们将 denylist 作为黑名单,限制 denylist 中的 IP 访问服务器,此时 iptables 规则可以这么写:

      iptables -I INPUT -p tcp -m set --match-set denylist src -j DROP

      此时 iptables 中会有一条阻止策略,平时我们需要限制黑名单中的IP访问时,只需要操作 denylist ,而不需要去动 iptables 规则。

      向 denylist 中添加 IP 操作如下:

        $ ipset add denylist 8.219.119.144
        $ ipset add denylist 8.219.71.118
        $ ipset add denylist 87.116.72.24
        $ ipset add denylist 89.248.163.132
        ipset add denylist 91.240.118.187

        当重复添加相同IP进去时,命令会提示无法插入:

          bash$ ipset add denylist 91.240.118.187
          ipset v7.15: Element cannot be added to the set: it's already added

          查看 denylist 中的 IP :

            $ ipset list denylist

            当内容过多时,可以结合 grep 过滤目标 IP。

              $ ipset list denylist | grep 91.240.118.187

              从 ipset 中删除一个 IP :

                $ ipset del denylist 91.240.118.187

                这个例子仅仅用于简化 iptables 规则 ,除此之外,ipset 还可以实现自动封堵入侵 IP ,如果服务器存在容易被攻击的端口,比如 22 端口。可以将命中 22 端口的 IP 自动添加至 ipset 中,然后 DROP 掉数据包。具体操作命令为:

                  $ ipset create honey-pot hash:ip
                  $ iptables -I INPUT -p tcp --dport 22 -j SET --add-set honey-pot src
                  $ iptables -I INPUT -p tcp -m set --match-set honey-pot src -j DROP
                  $ 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 honey-pot src
                  SET tcp  --  0.0.0.0/0 0.0.0.0/0 tcp dpt:22 add-set honey-pot src

                  通过以上几步即可搭建一个简易的入侵阻断系统,具体操作说明可以参考之前写的一篇文章:


                  全文完。

                  如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。


                  文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                  评论