Linux中生成testtcp.ko模块代码怎么写
创始人
2024-12-26 04:21:07
0次

生成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包。

四、注意事项

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...