千家信息网

Linux中生成testtcp.ko模块代码怎么写

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这期内容当中小编将会给大家带来有关Linux中生成testtcp.ko模块代码怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。生成testtcp.ko模块,添加
千家信息网最后更新 2024年09月22日Linux中生成testtcp.ko模块代码怎么写

这期内容当中小编将会给大家带来有关Linux中生成testtcp.ko模块代码怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

生成testtcp.ko模块,添加到内核。

添加该模块后,每个由该机器发出的数据包,如果是TCP协议,且源端口为81,将其改为RST包发出。

一、代码

1.1 文件:testtcp_main.c

#include #include #include #include #include #include #include #include #include #include #include #include #include #include unsigned int hook_mark1(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;    struct sk_buff *sk = skb;    u16 src_port,dst_port;    u16 datalen;    iph = ip_hdr(sk);    tcph = (struct udphdr*)((u_int8_t*)iph + (iph->ihl << 2));    src_port = ntohs(tcph->source);    dst_port = ntohs(tcph->dest);        if(src_port == 81 || dst_port == 81)            printk("<0>""src_port:%d, dst_port:%d, protocol:%d, rst:%d\n",src_port, dst_port, iph->protocol, tcph->rst);    if (iph->protocol == 6 && src_port == 81)    {          printk("<0>""---000---src_port:%d, dst_port:%d, protocol:%d, rst:%d\n",src_port, dst_port, iph->protocol, tcph->rst);        tcph->rst = 1;        iph->check = 0;        iph->check = ip_fast_csum((unsigned char*)iph, iph->ihl);        datalen = ntohs(iph->tot_len) - (iph->ihl << 2);        tcph->check = 0;        tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,                                            iph->protocol, csum_partial((unsigned char*)tcph,  datalen, 0));        skb->ip_summed = CHECKSUM_NONE;                return NF_ACCEPT;    }    return NF_ACCEPT;}static struct nf_hook_ops nfho_marker1;static int init_marker(void){        nfho_marker1.hook=hook_mark1;    nfho_marker1.hooknum=NF_INET_LOCAL_OUT;    nfho_marker1.pf=PF_INET;    nfho_marker1.priority=NF_IP_PRI_LAST;    nf_register_hook(&nfho_marker1);    return 0;}static void exit_marker(void){    nf_unregister_hook(&nfho_marker1);       }module_init(init_marker);module_exit(exit_marker);

1.2 文件Makefile:

obj-m := testtcp.o
testtcp-objs := testtcp_main.o

KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
install:
cp inerdns.ko ../

注意事项:

1. 第一行的testtcp.o 与第二行的testtcp_main.o 不能重复。

2. 第一行的testtcp.o 与第二行的testtcp-objs 前缀必须相同。

3. "default:"、"clean: "、"install:" 下一行的内容,行前面必须有tab键。

二、编译、添加模块到内核

2.1 编译

执行make,即可编译代码,并生产模块testtcp.ko。

2.2 添加模块到内核

lsmod 查看linux内核模块。

insmod testtcp.ko 将testtcp.ko模块添加到内核。

(rmmod testtcp 从内核中删除testtcp.ko模块。)

三、测试模块功能

3.1 测试代码

可以参照以下文章代码修改:http://blog.csdn.net/guowenyan001/article/details/11742621

3.2 linux下访问URL

curl 192.168.9.200:81

3.3 抓包查看

用tcpdump抓包查看,相关数据包是否已经被修改为RST包。

四、注意事项

内核模块代码,可能会造成系统崩溃,需要重启,所以最好在测试机上测试内核代码。

上述就是小编为大家分享的Linux中生成testtcp.ko模块代码怎么写了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

0