FreeBSD之防火墙配置

目录

简介 

使能 IPFW

编辑防火墙脚本文件,增加防火墙规则集

管理防火墙服务

参考


简介 

在FreeBSD里面, 有三个防火墙组件可以选择:PF、IPF、IPFW。今天我们介绍的是IPFW。

IPFILTER,也称为 IPF,是一种跨平台的开放源代码防火墙,已移植到多个操作系统,包括FreeBSD,NetBSD,OpenBSD和Solaris ™。

IPFILTER是一种内核端防火墙和NAT机制,它可以被用户层程序控制和监视。使用ipf可以设置或删除防火墙规则,使用ipnat可以设置或删除NAT规则,使用ipfstat可以打印IPFILTER内核部分的运行时统计信息,使用ipmon可以将IPFILTER操作记录到系统日志文件中。

IPFW为FreeBSD编写的有状态防火墙,它同时支持IPv4和 IPv6。它由几个组件组成:内核防火墙过滤器规则处理器及其集成的数据包记帐工具,日志记录工具, NAT,虚拟网络流量整形器,转发工具,桥接工具和ipstealth工具。

PF是从OpenBSD移至过来的:从FreeBSD 5.3开始,OpenBSD的PF防火墙的移植版本已经作为FreeBSD基本系统的一个集成部分被包括进来。PF是一个完整的、功能齐全的防火墙,可选地支持提供服务质量(QoS)的ALTQ(备用队列)。

使能 IPFW

添加firewall_enable="YES" 到 /etc/rc.conf:

注:直接编辑文件添加,或者执行命令sysrc firewall_enable="YES"

 使用firewall_script 而不是firewall_type,同样添加到 /etc/rc.conf:

sysrc firewall_script="/etc/ipfw.rules"

编辑防火墙脚本文件,增加防火墙规则集

cat /etc/ipfw.rules 
#!/bin/sh
# Flush out the list before we begin.
ipfw -q -f flush

# Set rules command prefix
cmd="ipfw -q add"
pif="vmx0"     # interface name of NIC attached to Internet

# 设置放开内部网络vmx1和回环网卡
$cmd 00005 allow all from any to any via vmx1
$cmd 00010 allow all from any to any via lo0

# 设置状态检查,如果它匹配动态规则表中的现有条目,允许数据包通过
$cmd 00101 check-state

# Allow outbound ping
$cmd 00250 allow icmp from any to any out via $pif keep-state

# Allow outbound SSH
$cmd 00280 allow tcp from any to any 23,22 out via $pif setup keep-state

# Deny all inbound traffic from non-routable reserved address spaces
#$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif     #RFC 1918 private IP
#$cmd 00301 deny all from 172.16.0.0/12 to any in via $pif      #RFC 1918 private IP
$cmd 00302 deny all from 10.0.0.0/8 to any in via $pif         #RFC 1918 private IP
$cmd 00303 deny all from 127.0.0.0/8 to any in via $pif        #loopback
$cmd 00304 deny all from 0.0.0.0/8 to any in via $pif          #loopback
$cmd 00305 deny all from 169.254.0.0/16 to any in via $pif     #DHCP auto-config
$cmd 00306 deny all from 192.0.2.0/24 to any in via $pif       #reserved for docs
$cmd 00307 deny all from 204.152.64.0/23 to any in via $pif    #Sun cluster interconnect
$cmd 00308 deny all from 224.0.0.0/3 to any in via $pif        #Class D & E multicast

# Deny public pings
$cmd 00310 deny icmp from any to any in via $pif

# Deny ident
$cmd 00315 deny tcp from any to any 113 in via $pif

# Deny all Netbios services.
$cmd 00320 deny tcp from any to any 137 in via $pif
$cmd 00321 deny tcp from any to any 138 in via $pif
$cmd 00322 deny tcp from any to any 139 in via $pif
$cmd 00323 deny tcp from any to any 81 in via $pif

# Deny fragments
$cmd 00330 deny all from any to any frag in via $pif

# Deny ACK packets that did not match the dynamic rule table
$cmd 00332 deny tcp from any to any established in via $pif

# Allow inbound SSH connections
$cmd 00410 allow tcp from any to me 23,22 in via $pif setup limit src-addr 2

管理防火墙服务

## 开启、查看、停止、重启防火墙

service ipfw start|status|stop|restart

## 使防火墙规则生效

sh /etc/ipfw.rules

 参考

https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/firewalls-ipfw.html

你可能感兴趣的:(操作系统,freebsd,ipfw)