如何从TCP数据包打印数据
下面是一个完全满足您需求的示例:挂钩接收TCP数据包并打印其有效负载.如果要从接收的数据包中打印一些其他信息(如二进制数据),您只需要修改此注释下的部分:
/ * —–从收到的TCP数据包中打印所有需要的信息—— * /
如果您需要跟踪传输的数据包而不是接收的数据包,则可以替换此行:
nfho.hooknum = NF_INET_PRE_ROUTING;
这一个:
nfho.hooknum = NF_INET_POST_ROUTING;
保存下一个文件并发出make命令来构建内核模块.然后执行sudo insmod print_tcp.ko加载它.之后,您将能够使用dmesg命令查看嗅探信息.如果要卸载模块,请运行sudo rmmod print_tcp命令.
print_tcp.c:
#include
#include
#include
#include
#include
#define PTCP_WATCH_PORT 80 /* HTTP port */
static struct nf_hook_ops nfho;
static unsigned int ptcp_hook_func(const struct nf_hook_ops *ops,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct iphdr *iph; /* IPv4 header */
stru