- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个基本代码。此代码丢弃并记录所有传入和传出的数据包。我想写一个 netfilter 内核模块来拦截数据包并将它们记录在内核日志中。它应该能够检测不同的(以 1 或 2 为例)基于 TCP 的侦察数据包。该模块应检测这些数据包并记录到内核日志中。我不想过滤数据包,只是识别并记录它们。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
static struct nf_hook_ops nfho; //struct holding set of hook function options
//function to be called by hook
unsigned int hook_func(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
printk(KERN_INFO "packet dropped\n"); //log to var/log/messages
return NF_DROP; //drops the packet
}
//Called when module loaded using 'insmod'
int init_module()
{
nfho.hook = hook_func; //function to call when conditions below met
nfho.hooknum = NF_IP_PRE_ROUTING; //called right after packet recieved, first hook in Netfilter
nfho.pf = PF_INET; //IPV4 packets
nfho.priority = NF_IP_PRI_FIRST; //set to highest priority over all other hook functions
nf_register_hook(&nfho); //register hook
return 0; //return 0 for success
}
//Called when module unloaded using 'rmmod'
void cleanup_module()
{
nf_unregister_hook(&nfho); //cleanup – unregister hook
}
最佳答案
首先,该模块只丢弃传入的数据包。原因是下一行:nfho.hooknum = NF_IP_PRE_ROUTING;
。关于你的问题:我不明白什么是“基于侦察数据包”,但你可以从数据包中提取所有数据并将它们显示在内核日志中。例如:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>
static struct nf_hook_ops nfho; //struct holding set of hook function options
//function to be called by hook
unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *))
{
struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb); //you can access to IP source and dest - ip_header->saddr, ip_header->daddr
struct tcphdr *tcp_header;
if (ip_header->protocol == 6) //TCP protocol
{
printk(KERN_INFO "TCP Packet\n");
tcp_header = (struct tcphdr *)(skb_transport_header(skb)+20); //Note: +20 is only for incoming packets
printk(KERN_INFO "Source Port: %u\n", tcp_header->source); //can access dest in the same way
}
return NF_ACCEPT; //accept the packet
}
//Called when module loaded using 'insmod'
int init_module()
{
nfho.hook = hook_func; //function to call when conditions below met
nfho.hooknum = NF_INET_PRE_ROUTING; //called right after packet recieved, first hook in Netfilter
nfho.pf = PF_INET; //IPV4 packets
nfho.priority = NF_IP_PRI_FIRST; //set to highest priority over all other hook functions
nf_register_hook(&nfho); //register hook
return 0; //return 0 for success
}
//Called when module unloaded using 'rmmod'
void cleanup_module()
{
nf_unregister_hook(&nfho); //cleanup – unregister hook
}
关于c - Netfilter 内核模块拦截数据包并记录它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426783/
如果传入数据包进入并在本地处理,是否有办法获得通知该事件? 我目前正在使用 NF_IP_LOCAL_IN Hook 。但看起来这会提供所有发往该接口(interface)的数据包。例如,假设 tcp
我目前正在尝试在用户空间中使用带有 libnetfilter_queue 的代码来修改在 iptables 的 NFQUEUE 目标中排队的数据包。但是,我不知道如何去做。 我已经将它设置为使用 NF
我正在构建一个简单的 Linux 防火墙,使用 netfilters 作为内核模块实现。我希望它能够进行深度数据包检查,为此我需要能够读取数据包的内容。我该如何继续这样做? 最佳答案 来自 netfi
在实现 netfiler Hook 时,我一直在尝试读取数据包内容。 我可以正确过滤 IP 字段或 TCP 字段,但是,出于好奇,我一直在尝试更进一步并显示应用程序级别有效负载的内容,但失败了。 我知
假设我从 Netfilter 拦截了一个数据包,随后返回 NF_STOLEN。在某些时候,我想将该数据包重新注入(inject)到它来自的 TCP 流中(在本例中)。我想从内核空间执行此操作。到目前为
我正在用 netfilter 的钩子(Hook)编写一些内核模块。钩子(Hook)函数: uint main_hook(uint hooknum, struct s
我正在编写一个内核模块,它需要在 IP 层进行一些数据包过滤工作。我需要做的是拦截所有 IP 数据包,并且在一些传出数据包上,我需要在发送它们之前将它们保留一小段时间(比如几十毫秒)进行分析。 我已经
任何人都可以指出一些将 xt_string 模块与 netfilter 结合使用的示例或提供示例。我想做的是编写 netfilter 模块,它将丢弃在 skb->data 字段中包含特定字符串的数据包
我想写一个Linux 2.6 netfilter module,可以检查传入的IP包信息,比如目标IP,源IP。之后将这些信息传递给 user space 应用程序,该应用程序(即 Socket 应用
我有一个基本代码。此代码丢弃并记录所有传入和传出的数据包。我想写一个 netfilter 内核模块来拦截数据包并将它们记录在内核日志中。它应该能够检测不同的(以 1 或 2 为例)基于 TCP 的侦察
我想在端口 80 上捕获所有要发送到转发地址的 tcp 数据包,并将它们传递给用户空间程序进行修改。我知道如何使用 IPtables 规则来做到这一点,例如 iptables -A FORWARD .
NF_INET_LOCAL_OUT 钩子(Hook)的 skb->sk 指向发送数据包的套接字的 struct sock。是否有一个钩子(Hook)为接收数据包的套接字设置了它?在 NF_INET_L
我正在研究内核模块中的 netfilter Hook 。我希望能够捕获由 scapy 创建的数据包。 钩子(Hook)以及通过 scapy 生成的数据包都在同一台物理主机上运行。似乎没有可用的 net
我希望能够在收到数据包时将其分成两个数据包(ip header 中的两个不同目的地)并将其传回 netfilter。如果有帮助,我正在使用 C++。 这有可能吗? 谢谢 最佳答案 我发现最好的方法是通
如何在内核模块中编译多个文件(文件调用其他文件中的函数)? 最佳答案 我将你的问题解释为,你想将多个编译单元链接到一个模块中? 在内核源代码中有很多这样的例子;它的一般要点是像这样编写 Makefil
我想把所有的流量封装在UDP中。我已经可以捕获它并转发了。但是现在我想做封装。对于此任务,我需要以原始形式获取完整数据包。但是我该怎么做呢?我的代码是 while ((rv = recv(fd, bu
谁能告诉我如何使用 Netfilter Hook 通过 linux 模块修改数据包数据? 谢谢! 最佳答案 不必编写自己的 netfilter 模块。您可以使用 iptables 中的 QUEUE 目
我在 Ubuntu 14.10 上使用 netfilter 队列。到目前为止一切正常,我看到了数据包并设置了判断,但我想更好地了解我从队列中读取的内容。 从此page开始. 这是有问题的代码: fd
我正在使用 Linux 4.13.x。我正在考虑在内核中发送数据包响应。 考虑在用户空间中运行的回显 TCP 或 UDP 服务器,还有另一个节点运行 TCP 或 UDP 客户端。客户端正在向服务器发送
请看代码片段 char ipAddr[] = {192, 168, 88, 2}; struct iphdr *ip_hdr = (struct iphdr*)(some_valid_eth_hdr_
我是一名优秀的程序员,十分优秀!