代码来之:http://blog.chinaunix.net/uid-23069658-id-3245853.html

洞悉linux下的Netfilter&iptables:开发自己的hook函数【实战】

原来的代码在3.x上有些问题,主要是内核更新了,一些函数接口和数据结构变了,以下代码在3.0.5上测试通过:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <net/udp.h>
#include <linux/netdevice.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/inet.h>
#include <linux/netfilter.h>
#include <net/route.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv4/ip_tables.h>MODULE_LICENSE("GPL");
MODULE_AUTHOR("koorey KING");
MODULE_DESCRIPTION("My hook test");#define ETH "eth1"
#define SIP "192.168.81.238"
#define DIP "192.168.81.211"
#define SPORT 39804
#define DPORT 6980
unsigned char SMAC[ETH_ALEN]={0x00,0x0B,0x82,0x27,0xFD,0xE4};
unsigned char DMAC[ETH_ALEN]={0x10,0x78,0xD2,0xC6,0x2F,0x89};static int build_and_xmit_udp(char * eth, u_char * smac, u_char * dmac,u_char * pkt, int pkt_len,u_long sip, u_long dip,u_short sport, u_short dport)
{struct sk_buff * skb = NULL;struct net_device * dev = NULL;struct ethhdr * ethdr = NULL;struct iphdr * iph = NULL;struct udphdr * udph = NULL;u_char * pdata = NULL;if(NULL == smac || NULL == dmac)goto out;if(NULL == (dev= dev_get_by_name(&init_net,eth)))goto out;skb = alloc_skb(pkt_len + sizeof(struct iphdr) + sizeof(struct udphdr) + LL_RESERVED_SPACE(dev), GFP_ATOMIC);printk(KERN_INFO"dean: %s =>> %s ==>>%d\n",__FILE__,__FUNCTION__,__LINE__);if(NULL == skb)goto out;skb_reserve(skb, LL_RESERVED_SPACE(dev));skb->dev = dev;skb->pkt_type = PACKET_OTHERHOST;skb->protocol = __constant_htons(ETH_P_IP);skb->ip_summed = CHECKSUM_NONE;skb->priority = 0;//skb->nh.iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));skb->network_header = skb_put(skb, sizeof(struct iphdr));//skb->h.uh = (struct udphdr*)skb_put(skb, sizeof(struct udphdr));skb->transport_header =  skb_put(skb, sizeof(struct udphdr));pdata = skb_put(skb, pkt_len);{if(NULL != pkt)memcpy(pdata, pkt, pkt_len);}udph = (struct udphdr *)skb->transport_header;memset(udph, 0, sizeof(struct udphdr));udph->source = sport;udph->dest = dport;skb->csum = 0;udph->len = htons(sizeof(struct udphdr)+pkt_len);udph->check = 0;iph = (struct iphdr*)skb->network_header;iph->version = 4;iph->ihl = sizeof(struct iphdr)>>2;iph->frag_off = 0;iph->protocol = IPPROTO_UDP;iph->tos = 0;iph->daddr = dip;iph->saddr = sip;iph->ttl = 0x40;iph->tot_len = __constant_htons(skb->len);iph->check = 0;iph->check = ip_fast_csum((unsigned char *)iph,iph->ihl);skb->csum = skb_checksum(skb, iph->ihl*4, skb->len - iph->ihl * 4, 0);udph->check = csum_tcpudp_magic(sip, dip, skb->len - iph->ihl * 4, IPPROTO_UDP, skb->csum);skb->mac_header = skb_push(skb, 14);ethdr = (struct ethhdr *)skb->mac_header;memcpy(ethdr->h_dest, dmac, ETH_ALEN);memcpy(ethdr->h_source, smac, ETH_ALEN);ethdr->h_proto = __constant_htons(ETH_P_IP);if(0 > dev_queue_xmit(skb))goto out;printk(KERN_INFO"dean: %s =>> %s ==>>%d\n",__FILE__,__FUNCTION__,__LINE__);
out:if(NULL != skb){dev_put (dev);//kfree_skb (skb);}return(NF_ACCEPT);
}static int pktcnt=0;static 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 *))
{const struct iphdr *iph = (struct iphdr *)skb->network_header;int ret = NF_ACCEPT;printk(KERN_INFO"dean: %s =>> %s ==>>%d\n",__FILE__,__FUNCTION__,__LINE__);if(iph->protocol == IPPROTO_ICMP){pktcnt++;if(pktcnt%5 == 0){printk(KERN_INFO "Sending the %d udp pkt !\n",pktcnt/5);ret = build_and_xmit_udp(ETH,SMAC,DMAC,"hello",5,in_aton(SIP),in_aton(DIP),htons(SPORT),htons(DPORT));}}return ret;
}static struct nf_hook_ops nfho={.hook           = hook_func,.owner          = THIS_MODULE,.pf             = PF_INET,.hooknum        = NF_INET_LOCAL_OUT,.priority       = NF_IP_PRI_FIRST,
};static int __init myhook_init(void)
{printk(KERN_INFO"dean: %s =>> %s ==>>%d\n",__FILE__,__FUNCTION__,__LINE__);return nf_register_hook(&nfho);
}static void __exit myhook_fini(void)
{printk(KERN_INFO"dean: %s =>> %s ==>>%d\n",__FILE__,__FUNCTION__,__LINE__);nf_unregister_hook(&nfho);
}module_init(myhook_init);
module_exit(myhook_fini);

netfilter实现内核重构skb来发送udp包相关推荐

  1. 使用netcat(nc命令)发送udp包

    向192.168.31.65的2055端口发送udp测试数据 echo "Hello World\!" | nc -4u 192.168.31.65 2055 可以在192.168 ...

  2. Windows下VS发送UDP包100个,并接受显示出来

    在VS上开发,还需要做链接好"WS2_32.lib" 就在项目属性–> 链接器 –> 输入 –> 编辑 –> 输入"WS2_32.lib" ...

  3. linux sendto 源码,Linux内核源代码解析——用户发送数据包的起源之sendto

    Jack:我想知道用户如何把数据发送到内核空间的? 我:你觉得哪里比较难理解呢? Jack:一般程序员会在程序里通过socket变量获得一个文件描述符,然后通过write把定义好的字符串写入到该描述符 ...

  4. linux 命令发送udp包,linux – 如何创建UDP数据包?

    当我执行以下Netcat命令并使用Wireshark查看数据包时,它表示UDP数据包格式错误. $echo "this is a test" | nc -u 127.0.0.1 5 ...

  5. python心跳包原理_Python 用心跳(UDP包)探测不活动主机

    Python 用心跳(UDP包)探测不活动主机 计算机周期性的发送一个代表心跳的UDP包到服务器,服务器跟踪每台计算机在上次发送心跳之后尽力的时间并报告那些沉默时间太长的计算机. 客户端程序:Hear ...

  6. tcp/ip 协议栈Linux内核源码分析13 udp套接字发送流程二

    内核版本:3.4.39 继续UDP套接字发送,上一篇讲到了sock_sendmsg,这里继续,下面是sock_sendmsg的相关代码 int sock_sendmsg(struct socket * ...

  7. ue4打包安卓发送udp报文_内核udp报文截取、修改和发送

    本文档的Copyleft归necofang所有,使用GPL发布,可以自由拷贝,转载,转载时请保持文档的完整性,严禁用于任何商业用途. msn : necofang@hotmail.com 近来做一个产 ...

  8. tcp/ip 协议栈Linux内核源码分析12 udp套接字发送流程一

    内核版本:3.4.39 因为过往的开发工作中既包括内核网络层模块的开发,又包括应用层程序的开发,所以对于网络数据的通信有那么一些了解.但是对于网络通信过程中,内核和应用层之间接口是如何运作的不是很清楚 ...

  9. tcp/ip 协议栈Linux内核源码分析15 udp套接字接收流程二

    内核版本:3.4.39 上篇我们分析了UDP套接字如何接收数据的流程,最终它是在内核套接字的接收队列里取出报文,剩下的问题就是谁会去写入这个队列,当然,这部分工作由内核来完成,本篇剩下的文章主要分析内 ...

最新文章

  1. Tangram base的设计思路
  2. 写了一个好玩的小软件, 监视鼠标以及键盘的动作, 全局钩子. HowTired
  3. rsync远程数据同步工具的使用
  4. 通过lseek产生空洞文件
  5. TF-卷积函数 tf.nn.conv2d 介绍
  6. mfc 监控文件操作_商务办公好伴侣 兄弟MFC系列多功能一体机
  7. html写弹出告警状态,基于日志报警插件 elastalert 实现告警(示例代码)
  8. Jedis连接数据库
  9. 以修改注册表的方式避免ACK确认机制带来的延时现象
  10. python打开excel指定的sheet_pandds指定一个或多个sheet读取excel(sheet_name参数)
  11. 第二阶段冲刺第八天,6月7日。
  12. Win32汇编——内存管理
  13. 高通QCA9531 2.4GHz电梯监控无线CPE
  14. 国科大数字图像处理(复习与整理)
  15. 几种有趣的电路设计(阻容降压,防过压,LLC,PFC,正激和反激电路)
  16. python+selenium 拉勾网信息获取,主要是定位元素的练习
  17. 有量子计算机的山西高能小说,5本超火的诸天万界流小说,剧情高能,值得细细品味...
  18. 民航计算机初级职称有哪些,各系列专业职称资格分类一览表
  19. (2018-2021年)Uncertainty 相关SOTA文献笔记整理
  20. 计算机毕业论文选题推荐|软件工程|系列十

热门文章

  1. java访问linux共享目录_在Windows上访问linux的共享文件夹
  2. Android画圆环,水波移动的效果
  3. win10很多软件显示模糊_显示字体小到有些模糊?高分屏别忘了这些设置
  4. 常见的网页布局解决方案
  5. 某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。副教授的固定工资为3000元,每个课时补贴30元。讲师的固定工资为2000元,每个课时补贴20元
  6. ElasticSearch启动报错RollingFileManager (/xxx/xxx) java.io.FileNotFoundException
  7. SFB 项目经验-08-Polycom CX700-4.0.X-能登录SFB 2015-能更新为中文
  8. 瑞芯微rk356x板子快速上手
  9. 使用redis实现防止重复提交,成功解决方案 春风化作秋雨 2018-09-13 18:09:52 13787 收藏 6 分类专栏: 解决方案 版权 1、业务场景 业务开发中,常常涉及对前端操作
  10. nfc读卡java开发,分享一段飞天R502读卡器的JAVA读卡代码