Netfilter字符串模块示例用法

Sne*_*min 4 c netfilter linux-kernel

任何人都可以指出一些使用xt_string模块和netfilter的例子或提供一个例子.我想要做的是编写netfilter模块,它将在skb-> data字段中丢弃包含某个字符串的数据包.

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

提前致谢

Con*_*ang 5

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

iptables -I INPUT 1 -p tcp --dport 80 -m string --string "domain.com" --algo kmp -j DROP
Run Code Online (Sandbox Code Playgroud)

或者,如果您在内核空间中指的是,您可以使用提供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);
Run Code Online (Sandbox Code Playgroud)