⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 local_in_hook.c

📁 linux netfilterNF_IP_LOCAL_IN处将发给本机的包用netlink重定向到用户空间
💻 C
字号:
#define __KERNEL__#define MODULE#include <linux/module.h>#include <linux/kernel.h>#include <linux/skbuff.h>#include <linux/ip.h>                  /* For IP header */#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>#include <linux/if_packet.h>#include <linux/netdevice.h>#include <linux/netlink.h>#include <net/sock.h>#define NETLINK_RECV_1 30typedef struct TagRedirectData{    unsigned long dwPortID;    unsigned long dwRedirectDataLength;    unsigned char pvRedirectData[1024];}TRedirectData;u32 pidl;static struct nf_hook_ops nfho;MODULE_LICENSE("GPL");struct sock *nl_skl = NULL;void nl_data_ready (struct sock *sk, int len){  wake_up_interruptible(sk->sleep);}static int send_to_user(TRedirectData *ptTempRedirData, int DataLength){	int size, i;	unsigned char  buff[1029]; 	unsigned char *old_tail;  	struct sk_buff *skb;  	struct nlmsghdr *nlh;  	size = NLMSG_SPACE(DataLength);	skb = alloc_skb(size, GFP_ATOMIC);  	old_tail = skb->tail;	nlh = NLMSG_PUT(skb, 0, 0, 0, size-sizeof(*nlh));		memcpy(NLMSG_DATA(nlh), ptTempRedirData, DataLength);	memcpy(buff, NLMSG_DATA(nlh), DataLength); 	printk(" dwRedirectDataLength: %lu\n",  ptTempRedirData->dwRedirectDataLength);	nlh->nlmsg_len = skb->tail - old_tail;	printk( "nlh->nlmsg_len: %u\n", nlh->nlmsg_len);	for(i=0;i<DataLength;i++)                {                      if(i%16 == 0)                              printk( "\n");                      printk( "%02x ", buff[i]);                }	NETLINK_CB(skb).groups = 0; /* not in mcast group */ 	NETLINK_CB(skb).pid = 0;      /* from kernel */ 	NETLINK_CB(skb).dst_pid = pidl; 	NETLINK_CB(skb).dst_groups = 0;  /* unicast */ 	return netlink_unicast(nl_skl, skb, pidl, MSG_DONTWAIT);	nlmsg_failure:	if(skb)   	 kfree_skb(skb);  	return -1;}unsigned int hook_localhost(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;	TRedirectData tTempRedirData;	int RedirectDataLength;
	struct sk_buff *sb = *skb;	int i;	iph = ( struct iphdr * )sb->data;    printk( "\nPacket info from");    printk( " %s\n", in->name);
    printk("sb->pkt_type:%d\n", sb->pkt_type);	if(in->name[0] == 'l')		return NF_DROP;	else if((iph->saddr) == 0x010aa8c0)		return NF_ACCEPT;	else	{			for(i=0;i<sb->len;i++)		{			if(i%16 == 0)				printk( "\n");			printk( "%02x ", *(sb->data+i));		}		tTempRedirData.dwPortID = in->name[3]-48;		tTempRedirData.dwRedirectDataLength = sb->len;		memset(tTempRedirData.pvRedirectData, 0, sizeof(tTempRedirData.pvRedirectData));		memcpy(tTempRedirData.pvRedirectData, sb->data, sb->len);		RedirectDataLength = tTempRedirData.dwRedirectDataLength + 2*sizeof(unsigned long);		printk(" dwRedirectDataLength: %lu\n",  tTempRedirData.dwRedirectDataLength);		printk(" RedirectDataLength: %d\n", RedirectDataLength);		send_to_user(&tTempRedirData, RedirectDataLength); 		return NF_DROP;	}		}int init_module(){	nfho.hook = hook_localhost;	nfho.hooknum  = NF_IP_LOCAL_IN;	nfho.pf = PF_INET;    nfho.priority = NF_IP_PRI_FIRST;	struct sk_buff *skb = NULL;	struct nlmsghdr *nlh = NULL;	int err;       	nl_skl = netlink_kernel_create(NETLINK_RECV_1, nl_data_ready); /* wait for message coming down from user-space */ 	skb = skb_recv_datagram(nl_skl, 0, 0, &err); 	nlh = (struct nlmsghdr *)skb->data; 	pidl = nlh->nlmsg_pid; /*pidl of sending process */	return nf_register_hook(&nfho); }void cleanup_module(){    sock_release(nl_skl->socket);     nf_unregister_hook(&nfho);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -