gpt4 book ai didi

c - Netfilter 字符串模块使用示例

转载 作者:太空狗 更新时间:2023-10-29 15:27:05 27 4
gpt4 key购买 nike

任何人都可以指出一些将 xt_string 模块与 netfilter 结合使用的示例或提供示例。我想做的是编写 netfilter 模块,它将丢弃在 skb->data 字段中包含特定字符串的数据包。

我最初简单地尝试了 strnstr(skb->data, "mystring", strlen("mystring")) 但这似乎是解决这个问题的不正确方法(而且它似乎不是工作,因为我没有看到任何数据包被丢弃)。

提前致谢

最佳答案

如果你的意思是在用户空间使用 iptables 字符串匹配,这里有一个例子:

iptables -I INPUT 1 -p tcp --dport 80 -m string --string "domain.com" --algo kmp -j DROP

或者如果你的意思是在内核空间,你可以使用提供KMP/BM/FSM算法的textsearch API,下面的例子来自内核源码lib/textsearch.c:

int pos;
struct ts_config *conf;
struct ts_state state;
const char *pattern = "chicken";
const char *example = "We dance the funky chicken";
conf = textsearch_prepare("kmp", pattern, strlen(pattern),
GFP_KERNEL, TS_AUTOLOAD);
if (IS_ERR(conf)) {
err = PTR_ERR(conf);
goto errout;
}
pos = textsearch_find_continuous(conf, &state, example, strlen(example));
if (pos != UINT_MAX)
panic("Oh my god, dancing chickens at %d\n", pos);
textsearch_destroy(conf);

关于c - Netfilter 字符串模块使用示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13401096/

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