gpt4 book ai didi

c - 是否可以使用 libnetfilter_queue 访问 ip 片段

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:40 25 4
gpt4 key购买 nike

我正在使用 libnetfilter_queue在C中捕获数据包。我正在设置一个 iptable 规则来排队传入的数据包,这些数据包稍后将由用户空间实现处理,如下所示:iptables -A INPUT -j NFQUEUE --queue-num 0。我用了nfqnl_test示例作为实现捕获的框架。一切都按预期工作。但是,我注意到不可能在 ip 片段级别检查队列。也就是说,如果一个数据包以碎片的形式出现,它会在被放入队列之前首先被重新组合。但我想使用片段。那么有没有办法强制执行这种行为呢?我想要的是一个队列,我可以在其中观察原始传入数据包(碎片和未碎片),以便我能够相应地对它们采取行动。

我读到过重组确实发生过。另一方面,对于 iptables,有可用的 -f 标志,因此应该有我正在寻找的“碎片粒度”。我也尝试调整 iptable 规则(例如 iptables -t raw -D PREROUTING -i eth0 -j NFQUEUE --queue-num 0),但结果还是一样。我只能观察已经重新组装的数据包,我肯定知道它是以碎片形式到达的。

非常感谢任何帮助。

最佳答案

所以我找到了解决问题的方法,我在这里分享它以防有人感兴趣。感谢来自 netfilter 邮件列表的 Adel,他提出了可能的解决方法。基本上,解决方案是使用 nftables 并设置一条优先级低于碎片整理的链。我已经用 C 代码测试了这个设置,它似乎工作得很好(我没有注意到任何副作用)。但是,我不得不提一下,我只是用它来观察 IP 碎片,并没有对它们进行篡改。

下面有两个函数来设置 nftables 然后删除它们。

void set_nftable() {

int status = 0;

// Create a nftable
status = system("nft add table ip filter");

// Add a chain to the nftable called "predefrag" which has lower priority than the defragmentation -450 < -400
status = system("nft add chain ip filter predefrag { type filter hook prerouting priority -- -450 \\; }");

// Set the nftable rule (queue packets to be accessed by a user-space application)
status = system("nft add filter predefrag meta iif eth0 counter queue num 0 bypass");
}

void remove_nftable() {

int status = 0;

// Flush the rules that are stored in the chains that belong to the nftable
status = system("nft flush table ip filter");

// Delete the chain from the nftable
status = system("nft delete chain ip filter predefrag");

// Delete the nftable
status = system("nft delete table ip filter");
}

有了这些功能,nfqnl_test代码可用于捕获IP片段。下面是设置 nftables 和了解它们如何工作的有用链接(一旦熟悉了 nftables 手册,函数中的注释就非常不言自明了)。

http://wiki.nftables.org/wiki-nftables/index.php/Building_and_installing_nftables_from_sources

http://wiki.nftables.org/wiki-nftables/index.php/Configuring_tables

http://wiki.nftables.org/wiki-nftables/index.php/Configuring_chains

http://wiki.nftables.org/wiki-nftables/index.php/Simple_rule_management

http://wiki.nftables.org/wiki-nftables/index.php/Queueing_to_userspace

关于c - 是否可以使用 libnetfilter_queue 访问 ip 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com