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

📄 firewall.c

📁 简单的Linux包过滤防火墙设计,RedHat Linux 9.0上编译运行。
💻 C
字号:
/*		基于netfilter包过滤防火墙		*//*		作者:	胡辛遥			*//*		作者: 胡辛遥			*//*		Data: 2004.11.23			*//*		Version: 1.01			*//*		Email: MontagueHu@Hotmail	.com	*/		#include <linux/kernel.h>#include <linux/ip.h>#include <linux/module.h>#include <linux/netdevice.h>#include <linux/netfilter.h>#include <linux/netfilter_ipv4.h>#include <linux/skbuff.h>#include <linux/tcp.h>#if LINUX_VERSION_CODE>=KERNEL_VERSION(2,4,9)MODULE_LICENSE("GPL");MODULE_AUTHOR("XXX@hotmail.com");#endifconst int DEV_NUMBER=100;	/* 设备号, 用来mknod */const int FTP_PORT=5376;		/* 设默认端口号码为21 */static int in_use=0;static int major=0;		/*用来判定注册设备是否成功 */static unsigned int defaultFTPPort;	/* 用户设置的FTP端口, 默认21 */static unsigned int deny_ip[1000];	/* 被拒绝的IP列表 */static unsigned int deny_port[1000];	/* 被拒绝的port列表 */static unsigned int deny_ftp_ip[1000];	/* 被FTP拒绝的IP列表 */unsigned int current_deny_ip=0;	/* 当前有多少个被拒绝的IP列表 */unsigned int current_deny_port=0;	/* 当前有多少个被拒绝的port列表 */unsigned int current_deny_ftp_ip=0;	/* 当前有多少个被FTP拒绝的IP列表 *//* IP */static int add_ip(unsigned int ip);	/* 增加一个IP到列表 */ static int release_ip(unsigned int ip);	/* 取消列表中的一个IP */static int check_ip(struct sk_buff* sb); 	/* 检查IP的函数 */static void show_current_ip_list();	/* 显示当前的IP列表 *//* port */static int add_port(unsigned int port);		/* 增加一个port到列表 */static int release_port(unsigned int port);	/* 取消列表中的一个port */static int check_port(struct sk_buff* sb);	/* 检查port的函数 */static void show_current_port_list();		/* 显示当前的port列表 *//* ftp */static int add_ftp_ip(unsigned int ip);		/* 增加一个IP到FTP列表 */static int release_ftp_ip(unsigned int ip);	/* 取消FTP列表中的一个IP */static int check_ftp_ip(struct sk_buff* sb);	/* 检查FTP的函数 */static void show_current_ftp_ip_list();		/* 显示当前的FTP拒绝列表 */static void show_current_ftp_port();		/* 显示当前的FTP端口 */static void set_ftp_port(unsigned int port);	/* 改变默认的FTP端口 *//* 其他 */static int	fw_ioctl(struct inode*,struct file* file,	/* 给外部调用的函数 */	unsigned int cmd,unsigned long arg);static int fw_open(struct inode* inode,struct file* file);	/* 设备开启时调用的函数 */static int fw_release(struct inode* inode,struct file* file);	/* 设备关闭时调用的函数 */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*));/* 用于注册我们的函数的数据结构 */static struct nf_hook_ops nfho;/* 设备驱动接口 file_operations */struct file_operations fw_fops={	NULL,	NULL,	NULL,	NULL,	NULL,	NULL,	fw_ioctl,	NULL,	fw_open,	NULL,	fw_release,	NULL};/* 增加一个IP到列表 */static int add_ip(unsigned int ip){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_ip;++i)	{		if (ip==deny_ip[i])		{			printk("This ip is already in the ip list\n");			return 0;		}	}	deny_ip[current_deny_ip++]=ip;	printk("Add ip %d.%d.%d.%d to the ip list successfully\n",ip&0x000000FF,		(ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,(ip&0xFF000000)>>24);		return 1;}/* 取消列表中的一个IP */static int release_ip(unsigned int ip){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_ip;++i)	{		if (ip==deny_ip[i])		{			printk("Remove ip %d.%d.%d.%d from the ip list successfully\n",ip&0x000000FF,				(ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,				(ip&0xFF000000)>>24);			deny_ip[i]=deny_ip[--current_deny_ip];			return 1;		}	}	printk("This ip is not in the ip list\n");	return 0;}/* 显示当前的IP列表 */static void show_current_ip_list(){	printk("----------------------\n");	int i;	printk("Here are the deny_ips!\nPlease check them!\n");	for (i=0;i<current_deny_ip;++i)	{		printk("%d:  %d.%d.%d.%d\n",i+1,			deny_ip[i]&0x000000FF,(deny_ip[i]&0x0000FF00)>>8,			(deny_ip[i]&0x00FF0000)>>16,(deny_ip[i]&0xFF000000)>>24);	}}		/* 检查IP的函数 */static int check_ip(struct sk_buff* sb){	if(!sb) return NF_ACCEPT;	if(!(sb->nh.iph)) return NF_ACCEPT;	int i;	for (i=0;i<current_deny_ip;++i)	{		if (sb->nh.iph->saddr==deny_ip[i])		{			printk("Firewall: Dropped packet from IP: %d.%d.%d.%d\n",			deny_ip[i]&0x000000FF,(deny_ip[i]&0x0000FF00)>>8,			(deny_ip[i]&0x00FF0000)>>16,(deny_ip[i]&0xFF000000)>>24);			return NF_DROP;		}	}	return NF_ACCEPT;}/* 增加一个port到列表 */static int add_port(unsigned int port){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_port;++i)	{		if (port==deny_port[i])		{			printk("This port is already in the port list\n");			return 0;		}	}	printk("Add port %d to the port list successfully\n",((port&0xFF00)>>8|(port&0x00FF)<<8));	deny_port[current_deny_port++]=port;	return 1;}/* 取消列表中的一个port */static int release_port(unsigned int port){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_port;++i)	{		if (port==deny_port[i])		{			printk("Remove port %d from the port list successfully\n",((port&0xFF00)>>8|(port&0x00FF)<<8));			deny_port[i]=deny_port[--current_deny_port];			return 1;		}	}	printk("This port is not in the port list\n");	return 0;}/* 显示当前的port列表 */static void show_current_port_list(){	printk("----------------------\n");	printk("Here are the deny_ports!\nPlease check them!\n");	int i;	for (i=0;i<current_deny_port;++i)	{		printk("%d: %d\n",i+1,		((deny_port[i]&0xFF00)>>8|(deny_port[i]&0x00FF)<<8));	}}/* 检查port的函数 */static int check_port(struct sk_buff* sb){	if(!sb) return NF_ACCEPT;	if(!(sb->nh.iph)) return NF_ACCEPT;	struct tcphdr* thread=(struct tcphdr*)(sb->data+(sb->nh.iph->ihl*4));		int i;		for (i=0;i<current_deny_port;++i)	{		if (thread->dest==deny_port[i])		{			printk("Firewall: Dropped packet from TCP port: %d\n",				((deny_port[i]&0xFF00)>>8|(deny_port[i]&0x00FF)<<8));			return NF_DROP;		}	}		return NF_ACCEPT;}/* 增加一个IP到FTP列表 */static int add_ftp_ip(unsigned int ip){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_ftp_ip;++i)	{		if (ip==deny_ftp_ip[i])		{			printk("This ip is already in the ftp ip list\n");			return 0;		}	}	deny_ftp_ip[current_deny_ftp_ip++]=ip;	printk("Add ip %d.%d.%d.%d to the ftp ip list successfully\n",ip&0x000000FF,		(ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,(ip&0xFF000000)>>24);		return 1;}/* 取消FTP列表中的一个IP */static int release_ftp_ip(unsigned int ip){	printk("----------------------\n");	int i;	for (i=0;i<current_deny_ftp_ip;++i)	{		if (ip==deny_ftp_ip[i])		{			printk("Remove ip %d.%d.%d.%d from the ftp ip list successfully\n",ip&0x000000FF,				(ip&0x0000FF00)>>8,(ip&0x00FF0000)>>16,				(ip&0xFF000000)>>24);			deny_ftp_ip[i]=deny_ftp_ip[--current_deny_ftp_ip];			return 1;		}	}	printk("This ip is not in the ftp ip list\n");	return 0;}/* 检查FTP的函数 */static int check_ftp_ip(struct sk_buff* sb){	if(!sb) return NF_ACCEPT;	if(!(sb->nh.iph)) return NF_ACCEPT;	struct tcphdr* thread=(struct tcphdr*)(sb->data+(sb->nh.iph->ihl*4));		int i;		for (i=0;i<current_deny_ftp_ip;++i)	{		if (thread->dest==defaultFTPPort && sb->nh.iph->saddr==deny_ftp_ip[i])		{			printk("Firewall: Dropped packet from IP: %d.%d.%d.%d\n",			deny_ftp_ip[i]&0x000000FF,(deny_ftp_ip[i]&0x0000FF00)>>8,			(deny_ftp_ip[i]&0x00FF0000)>>16,(deny_ftp_ip[i]&0xFF000000)>>24);			return NF_DROP;		}	}		return NF_ACCEPT;}/* 显示当前的FTP拒绝列表 */static void show_current_ftp_ip_list(){	printk("----------------------\n");	int i;	printk("Here are the deny_ftp_ips!\nPlease check them!\n");	for (i=0;i<current_deny_ftp_ip;++i)	{		printk("%d:  %d.%d.%d.%d\n",i+1,			deny_ftp_ip[i]&0x000000FF,(deny_ftp_ip[i]&0x0000FF00)>>8,			(deny_ftp_ip[i]&0x00FF0000)>>16,(deny_ftp_ip[i]&0xFF000000)>>24);	}}/* 改变默认的FTP端口 */static void set_ftp_port(unsigned int port){	defaultFTPPort=port;}/* 显示当前的FTP端口 */static void show_current_ftp_port(){	printk("----------------------\n");	printk("The current ftp port is %d\n",((defaultFTPPort&0xFF00)>>8|(defaultFTPPort&0x00FF)<<8));}/* 设备开启打开的函数 */static int fw_open(struct inode* inode,struct file* file){	if (in_use)	{		return -EBUSY;	}	else	{		MOD_INC_USE_COUNT;		++in_use;	}	return 0;}/* 设备关闭打开的函数 */static int fw_release(struct inode* inode,struct file* file){	in_use^=in_use;	MOD_DEC_USE_COUNT;	return 0;}/* 给外部调用的函数,开给外部的接口 */static int fw_ioctl(struct inode* inode,struct file* file,	unsigned int cmd,unsigned long arg){	int ret=0;	switch(cmd)	{		case 1:		{			/* 增加一个IP到屏蔽列表		*/			add_ip((unsigned int)arg);			break;		}		case 2:		{			/* 释放屏蔽列表中的一个IP	*/			release_ip((unsigned int)arg);			break;		}		case 3:		{			/* 显示当前被屏蔽的IP列表	*/			show_current_ip_list();			break;		}		case 4:		{			/* 增加一个端口到屏蔽列表	*/			add_port((unsigned short)arg);			break;		}		case 5:		{			/* 释放屏蔽列表中的一个端口	*/			release_port((unsigned short)arg);			break;		}		case 6:		{			/* 显示当前被屏蔽的服务端的端口	*/			show_current_port_list();			break;		}		case 7:		{			/* 增加一个IP到FTP屏蔽列表	*/			add_ftp_ip((unsigned int)arg);			break;		}		case 8:		{			/* 释放FTP屏蔽列表中的一个IP	*/			release_ftp_ip((unsigned int)arg);			break;		}		case 9:		{			/* 显示FTP屏蔽列表	*/			show_current_ftp_ip_list();			break;		}		case 10:		{			/* 显示当前设置的FTP端口	*/			show_current_ftp_port();			break;		}		case 11:		{			/* 设置当前的FTP端口, 默认21	*/			set_ftp_port((unsigned short)arg);			break;		}								default:			ret=-EBADRQC;	}	return ret;}/* 注册的hook函数的实现 */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*)){	struct sk_buff* sb=*skb;		/* 检查 ip */	if (check_ip(sb)==NF_DROP)	{		return NF_DROP;	}	else	{		/* 检查 port */		if (check_port(sb)==NF_DROP)		{			return NF_DROP;		}		else		{			/* 检查被FTP拒绝的IP列表 */			if (check_ftp_ip(sb)==NF_DROP)			{				return NF_DROP;			}		}	}	return NF_ACCEPT;}int init_module(){	SET_MODULE_OWNER(&fw_fops);	/* 注册这个设备 */	printk("----------------------\n");	if ((major=register_chrdev(DEV_NUMBER,"firewall",&fw_fops))<0)	{			/* 注册设备失败, 打印错误消息, 返回 */		printk("Firewall: Failed registering control device!\n");		printk("Firewall: Module installation aborted\n");		return 0;	}	/* 注册成功 */	in_use^=in_use;	printk("Firewall: Control device successfully registered.\n");	printk("Author: MontagueHu.\n");	/* 填充我们的hook数据结构 */	nfho.hook=hook_func;	/* 处理函数 */	nfho.hooknum=NF_IP_PRE_ROUTING;	/* 使用IPV4的第一个hook */	nfho.pf=PF_INET;	/*IPV4*/	nfho.priority=NF_IP_PRI_FIRST;	/* 让我们的函数首先执行 */		nf_register_hook(&nfho);	/* 注册 */		/* 初始化FTP的端口 */	defaultFTPPort=FTP_PORT;		return 0;}void cleanup_module(){		nf_unregister_hook(&nfho);	/* 注消 */	int ret;		printk("----------------------\n");	/* 注消设备 */	if((ret=unregister_chrdev(DEV_NUMBER,"firewall"))!=0)	{		/* 注消设备失败 */		printk("Firewall: Removal of module failed!\n");	}	/* 注消设备成功 */	printk("Firewall: Removal of module successfule\n");}

⌨️ 快捷键说明

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