nftables防火墙
本文最后更新于 104 天前,如有失效请评论区留言。

nftables

nftables是Linux核心的一个子系统,用于过滤和分类网络数据包/数据报/帧。从Linux内核3.13(于2014年1月19日发布)以后可用

nftables取代了Netfilter的传统iptables部分,nftables相较于iptables的优势在于代码重复更少,对新协议的扩展更容易

nftables和iptables区别

nftables相对于iptables的主要优势在于,简化了Linux内核ABI,减少了代码的重复,改进了错误报告,以及更高效的过滤规则的执行,存储和增量更改。

脚本配置

可以配合雷池进行ip的封禁,当你发现有某个ip不是正常用户,并且长期访问你的网站漏洞,你就可以把他加入ip黑名单中,可以从网络层直接阻断请求,更加底层处理性能更好

先创建两个文件

一个 blocked_ips.txt存放你需要拉入黑名单的ip,以换行分割

image-20240206205943387

touch blocked_ips.txt

再创建一个脚本存放把这些ip加入到nftables拒绝策略

vim block_ips.sh
#!/bin/bash
FILE="blocked_ips.txt"
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
    exit 1
fi
chain_exists=$(nft list chain ip filter blocklist 2>/dev/null)
if [ -z "$chain_exists" ]; then
    nft create chain ip filter blocklist { type filter hook forward priority 0 \; policy drop \; }
fi

# Add a rule for each address in the file that is not in the blocklist chain
while IFS= read -r ip || [[ -n "$ip" ]]; do
    if ! nft list chain ip filter blocklist | grep -q "$ip"; then
        nft add rule ip filter blocklist ip saddr $ip counter drop
    fi
done <"$FILE"

# Remove rules for addresses no longer in the file from the blocklist chain
blocked_ips=$(nft list chain ip filter blocklist | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
while IFS= read -r ip; do
    if ! grep -q "$ip" "$FILE"; then
        nft delete rule ip filter blocklist ip saddr $ip counter drop
    fi
done <<<"$blocked_ips"

执行脚本

bash block_ips.sh

查看规则

nft list chain ip filter blocklist

删除单个规则

先使用

nft -a list chain ip filter blocklist

查看blocklist下所有的规则并且带有句柄id

然后使用最后的handle 数字 删除对应的ip

nft delete rule ip filter blocklist handle 1138

image-20240206210235353

清空blocklist下所有规则

nft flush chain ip filter blocklist

删除blocklist

nft delete chain ip filter blocklist
版权声明:本文为BIMiracle原创,依据CC BY-SA 4.0许可证进行授权,转载请附上出处链接及本声明。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇