gpt4 book ai didi

c - 如何从 debian 64 位上的 tcphdr (sk_buff) 结构访问数据/有效负载?

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

我在一个小型防火墙上工作,我必须从端口 80 (http) 的每个 tcp 数据包中检索数据以解析它们。这段代码在 debian 32 位虚拟机上运行良好,我能够打印每个网页的标题,但是当我尝试加载我的内核模块并通过 http 端口传输一些数据时,它没有打印任何数据。

当我编译时,它只在我的 64 位计算机上显示这些警告:

/home/dev3/C/FIREWALL/firewall.c: In function ‘hook_func’:
/home/dev3/C/FIREWALL/firewall.c:179: warning: cast from pointer to integer of different size
/home/dev3/C/FIREWALL/firewall.c:179: warning: cast to pointer from integer of different size

有人有什么想法吗?

谢谢。

代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>

#undef __KERNEL__
#include <linux/netfilter_ipv4.h>
#define __KERNEL__

#include <linux/ip.h>
#include <linux/tcp.h>

static struct nf_hook_ops nfho;

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 * iph;
struct tcphdr * tcph;
unsigned char * http_port = "\x00\x50";
char * data;

if (skb)
{
iph = ip_hdr(skb);

if (iph && iph->protocol && (iph->protocol == IPPROTO_TCP))
{
tcph = (struct tcphdr *)((__u32 *)iph + iph->ihl);

if ((tcph->source) == *(unsigned short *)http_port)
{
data = (char *)((int)tcph + (int)(tcph->doff * 4));

printk(KERN_DEBUG "TCP source : %hu, TCP dest : %hu\n", ntohs(tcph->source), ntohs(tcph->dest));
printk(KERN_DEBUG "TCP seq : %u, TCP ack_seq : %u\n", ntohl(tcph->seq), ntohl(tcph->ack_seq));
printk(KERN_DEBUG "TCP doff : %hu, TCP window : %hu\n", ntohs(tcph->doff), ntohs(tcph->window));
printk(KERN_DEBUG "TCP check : 0x%hx, TCP urg_ptr : %hu\n", ntohs(tcph->check), ntohs(tcph->urg_ptr));
printk(KERN_DEBUG "FLAGS=%c%c%c%c%c%c\n\n",
tcph->urg ? 'U' : '-',
tcph->ack ? 'A' : '-',
tcph->psh ? 'P' : '-',
tcph->rst ? 'R' : '-',
tcph->syn ? 'S' : '-',
tcph->fin ? 'F' : '-');
//printk(KERN_DEBUG "sending packet to : %pI4\n", &iph->daddr);
printk(KERN_DEBUG "data len : %d\ndata : \n", (int) strlen(data));
printk(KERN_DEBUG "%s\n", data);
}
}
}

return NF_ACCEPT;
}

int init_module()
{
int result;

nfho.hook = (nf_hookfn *) hook_func;
nfho.hooknum = NF_IP_POST_ROUTING;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;

result = nf_register_hook(&nfho);

if(result)
{
printk(KERN_DEBUG "firewall : erreur nf_register_hook !\n");
return 1;
}

printk(KERN_DEBUG "firewall : module charge.\n");

return 0;
}

void cleanup_module()
{
nf_unregister_hook(&nfho);
printk(KERN_DEBUG "firewall : module decharge.\n");
}

最佳答案

(char *)((int)tcph + (int)(tcph->doff * 4));错了,应该是

(char *)((unsigned char *)tcph + (tcph->doff * 4));

请注意 printk(KERN_DEBUG "data len : %d\ndata :\n", (int) strlen(data)); 根本不是一件安全的事情。您不知道数据是否包含文本,如果包含,它可能不包含 strlen(data) 需要按您预期工作的 0 终止字符串。

在比较端口和可能的其他字段时,您可能还需要关心字节顺序。

关于c - 如何从 debian 64 位上的 tcphdr (sk_buff) 结构访问数据/有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12073963/

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