利用netfilter构建用户级防火墙

要求

  1. 设置iptables过滤规则为:所有从本机发出的icmp包全部到自己编写的应用程序
  2. 编写应用程序,功能如下:
    ·2.1 允许从本机出发,目的地址为win ip的icmp包;
    ·2.2 丢弃其他任何icmp包;
    ·2.3 当出现错误时,做错误处理,能够清理占用资源,退出程序。

实验环境

red hat 9.0

步骤

  1. 下载iptables-1.2.7a.tar.bz2.rar到red hat 9.0
  2. tar xjvf iptables-1.2.7a.tar.bz2.rar
  3. cd iptables-1.2.7a
  4. make
  5. make install-devel
  6. 加载过滤需要的内核模块
    modprobe iptable_filter
    modprobe ip_queue
  7. 设置过滤规则
    iptables -A OUTPUT -p icmp -j QUEUE

代码如下


#include <linux/netfilter.h>
#include <libipq.h>
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <netinet/ip.h>
#define BUFSIZE 2048 

static void die(struct ipq_handle *h)
{
	ipq_perror("passer");
	ipq_destroy_handle(h);
	exit(1);
}

int main(int argc, char **argv)
{
	int status;
	unsigned char buf[BUFSIZE];
	struct ipq_handle *h;
	struct iphdr *iphead;

	h = ipq_create_handle(0, PF_INET);
	if (!h)
	{
		die(h);
	}
	
	status = ipq_set_mode(h, IPQ_COPY_PACKET, BUFSIZE);
	if (status < 0)
	{
		die(h);
	}
		
	do{
		status = ipq_read(h, buf, BUFSIZE, 0);
		if (status < 0)
		{
			die(h);
		}
		
		switch (ipq_message_type(buf)) {
			case NLMSG_ERROR:
				fprintf(stderr, "Received error message %d\\n",
				        ipq_get_msgerr(buf));
				break;
				
			case IPQM_PACKET: {
				ipq_packet_msg_t *m = ipq_get_packet(buf);
				
				iphead = (struct iphdr *)m->payload;
				if(iphead->daddr == inet_addr(argv[1]))
				{
					status = ipq_set_verdict(h,m->packet_id,
							NF_ACCEPT,0,NULL);
					if(status < 0)
					{
						die(h);
					}
				}
				else
				{
					status = ipq_set_verdict(h,m->packet_id,
							NF_DROP,0,NULL);
					if(status < 0)
					{
						die(h);
					}
				}

				break;
			}
			
			default:
				fprintf(stderr, "Unknown message type!\\n");
				break;
		}
	} while (1);
	
	ipq_destroy_handle(h);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值