Linux中生成testtcp.ko模块代码怎么写-创新互联

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

成都创新互联公司主要业务有网站营销策划、网站制作、网站建设、微信公众号开发、重庆小程序开发H5网站设计、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、营销型网站建设资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

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

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

一、代码

1.1 文件:testtcp_main.c

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/inetdevice.h>
#include <linux/string.h>
#include <net/route.h>
#include <linux/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/checksum.h>
#include <net/tcp.h>
#include <net/ip.h>

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模块代码怎么写了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。

分享标题:Linux中生成testtcp.ko模块代码怎么写-创新互联
本文来源:https://www.cdcxhl.com/article48/dpceep.html

成都网站建设公司_创新互联,为您提供做网站关键词优化全网营销推广外贸建站网站排名用户体验

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发