APF(Advanced Policy Firewall)是Rf-x Networks 出品的Linux环境下的软件防火墙,被大部分Linux服务器管理员所采用,使用iptables的规则,易于理解及使用.可算是Linux使用较多的防火墙.APF的配置参数很多,有效利用这些配置参数可加强你的服务器安全,APF应该在每一台Linux服务器中得到应用。

一、介绍

  Advanced Policy Firewall (APF)是一个基于 iptables(netfilter)的防火墙系统,主要是根据当今互联网服务器的部署极其linux安装部署的基本需求来进行设计的,APF的的配置设计的非常丰富,并且目前来看配置文件整体都设计的易于通俗易懂,容易配置。APF的通过命令行使用'apf'命令进行日常管理,其中包含了详细的使用信息以及大家当前所期望防火墙解决方案的的所用功能。
  APF的技术方面包含了基于 iptables(netfilter)项目最新的稳定功能并提供了一个非常强有力的防火墙。APF通过三种方式进行数据过滤:
1)基于策略的静态规则(不要和“静态防火墙”混为一谈)
2)基于连接状态的策略
3)基于有效性的策略

  首先,基于策略的静态规则是防火墙最常用的一种方法,这就意味着防火墙有一个不变的指令集(规则)来确定在一定的条件下流量该如何处理。一个简单的例子通过配置conf.apf文件你可以允许/拒绝一个地址(客户端)访问信任的系统服务器或者打开的一个新的端口,因此这中策略就意味着在防火墙运行期间他是一个在短期内很少或者从不修改的规则。
  第二,基于连接状态的策略,理解为根据不同类型的连接区分合法的数据包,当数据包能匹配一个已知的连接的情况才允许通过防火墙,其他的数据包将被拒绝。举例说明:当使用ftp传送数据时,按照以前防火墙的配置你将不得不定义一系列复杂的静态策略允许数据包的传输,但是基于连接状态的策略就不是这样处理数据了,当防火墙发现了一个地址已经建立了到21端口的连接,那么该地址数据传输部分将和此连接想关联并且动态的改变防火墙允许此数据通过。
  第三、基于有效性的策略,是指防火墙能够匹配各种不同流量数据模式的能力,去检查已知的攻击或者检查流量数据是否能符合Internet标准。一个简单的例子,当一个可能的攻击者伪装数据的源ip并发送给你,APF将简单的丢弃这个数据或者选择先记录攻击然后再弃之~。另外一个例子,当网络上一个有问题的的路由器开始向你发送畸形数据包,APF将简单的丢弃这个数据或者返回数据给路由器,告诉路由器停止向你发送新的数据包。
  这三种被APF采用关键的过滤方法只是告诉你一个简单的概念,APF防火墙是构件在一个什么样的技术水平之上。APF有更多的特性可以使用。所有APF功能的详细描述可以查看/etc/apf/conf.apf这个配置文件,文件里面有以上所有功能的很好的描述。下面是APF许多功能的摘要:

  • 配置文件的详细注释和说明- 精细的网络出站和入站数据过滤
  • user id based outbound network filtering
  • 应用层的网络过滤
  • trust based rule files with an optional advanced syntax
  • global trust system where rules can be downloaded from a central management server- reactive address blocking (RAB), next generation in-line intrusion prevention
  • debug mode provided for testing new features and configuration setups
  • fast load feature that allows for 1000+ rules to load in under 1 second
  • inbound and outbound network interfaces can be independently configured
  • global tcp/udp port & icmp type filtering with multiple methods of executing filters (drop, reject, prohibit)
  • configurable policies for each ip on the system with convenience variables to import settings
  • packet flow rate limiting that prevents abuse on the most widely abused protocol, icmp
  • prerouting and postrouting rules for optimal network performance
  • dshield.org block list support to ban networks exhibiting suspicious activity
  • spamhaus Don't Route Or Peer List support to ban known "hijacked zombie" IP blocks
  • any number of additional interfaces may be configured as firewalled (untrusted) or trusted (not firewalled)
  • additional firewalled interfaces can have there own unique firewall policies applied
  • intelligent route verification to prevent embarrassing configuration errors
  • advanced packet sanity checks to make sure traffic coming and going meetsthe strictest of standards
  • filter attacks such as fragmented UDP, port zero floods, stuffed routing,arp poisoning and more
  • configurable type of service options to dictate the priority of different typesof network traffic
  • intelligent default settings to meet every day server setups
  • dynamic configuration of your servers local DNS revolvers into the firewall
  • optional filtering of common p2p applications
  • optional filtering of private & reserved IP address space
  • optional implicit blocks of the ident service
  • configurable connection tracking settings to scale the firewall to the size of your network
  • configurable kernel hooks (ties) to harden the system further to syn-flood attacks & routing abuses
  • advanced network control such as explicit congestion notification and overflow control
  • special chains that are aware of the state of FTP DATA and SSH connections top revent client side issues
  • control over the rate of logged events, want only 30 filter events a minute?300 a minute?
  • you are the boss
  • logging subsystem that allows for logging data to user space programs or standard syslog files
  • logging that details every rule added and a comprehensive set of error checks to prevent config errors
  • if you are familiar with netfilter you can create your own rules in any of the policy files
  • pluggable and ready advanced use of QoS algorithms provided by the Linux
  • 3rd party add-on projects that compliment APF features
    准备做的功能列表:
  • full support for NAT/MASQ including port forwarding
  • cluster oriented round-robin packet or port forwarding
  • in-line firewall reactive address blocking of connction floods
  • and much more...

二、系统要求

  APF被设计运行于安装了iptables(netfilter)包的linux操作系统之上,The iptables (netfilter)包支持Linux kernels 2.4及以上版本,你能在netfilter项目(http://www.netfilter.org)上找到更多信息。
  如果你使用的linux版本已经包含了iptables,版本如果比较高,那么将可能包含了APF所需要的模块。如果你是自定义内核编译iptables,那么你应该检查并确保如下的模块被编译到核心里面。
ip_tables、iptable_filter、iptable_mangle、ip_conntrack、ip_conntrack_irc、ip_conntrack_ftp、ipt_state、ipt_multiport、ipt_limit、ipt_recent、ipt_LOG、ipt_REJECT、ipt_ecnipt_length、ipt_macipt_multiport、ipt_owneript_state、ipt_ttl、ipt_TOS、ipt_TCPMSSipt_ULOG
  如果你想确认是否有这些模块,可在/lib/modules/kernelver/kernel/net/ipv4/netfilter/目录中查找
  #ls /lib/modules/$(uname -r)/kernel/net/ipv4/netfilter/
  APF支持的多个linux平台,如下是一个简单的支持清单:

  • Redhat Enterprise AS/ES 2+

  • CentOS Any

  • Fedora Core Any

  • Slackware 8.0+

  • Debian GNU/Linux 3.0+

  • Suse Linux 8.1+

  • Unbuntu Any

  • TurboLinux Server 9+

  • TurboLinux Fuji (Desktop)

  • RedHat Linux 7.3,8,9

    满足apf正常操作最基本的系统要求,你可以很容易的把包放到几乎任何情况,有一个linux2.4+内核、iptables和一个标准的bash shell(grep,awk,sed等等),如下是一个推荐系统的表格:
    DEVICE MIN RECOMMENDED
    CPU: 300Mhz 600Mhz
    MEM: 64MB 96MB
    DISK: OS OS
    NETWORK: Any Any

三、安装

  APF的安装设置非常的简单,其中有一个install.sh的脚本,为您安装apf软件并将执行所有的任务。
  Begin Install:
  # sh install.sh
  你可以通过编辑install.sh脚本文件里面的变量来自定义APF的配置文件的路径(conf.apf和internals.conf等文件),但是这是不推荐的,并且默认的路径应该可以满足所有用户的需求,默认的路径分别是:
Install Path: /etc/apf
Bin Path: /usr/local/sbin/apf
  这个包包含了两个非常有用的脚本,第一个是importconf,能将之前版本的apf的所有变量设置导出到新的安装文件,第二个是get_ports,此脚本将输出系统当前正在使用的服务器端口以便在安装过程中能更好的配置端口设置。
  在新的版本安装后,所有之前的APF版本都被保留并且存储在/etc/apf.bkDDMMYY-UTIME格式的位置,此外还将有一个/etc/apf.bk.last的sym-link连接到你已经安装的最后一个版本。
  在安装完成以后文档和方便的脚本分贝被拷贝到 /etc/apf/docs 和 /etc/apf/extras目录。

四、引导加载

  安装apf将会安装一个初始化脚本/etc/init.d/apf并配置此脚本为启动加载。如果你正在设置apf自定义的情况,那么你将可能要按照如下的指令进行。
  我们系统中的防火墙有三种工作模式,这三种方法除了能定制自己的需求外没有其他的任何真实的好处。
  第一种是使用chkconfig设置apf在系统初始化的时候启动,具体细节如下:
  chkconfig --add apf
  chkconfig --level 345 apf on
  第二种方法是在/etc/rc.local文件的最后增加一个sh -c "/etc/apf/apf -s" &的字符串。
  不建议同时使用以上两种启动方式,对大部分系统来说通过chkconfig的初始化脚本应该是很好滴。
  第三种方法也就是最后一种途径,是一种简单的按需的方式运行APF。使用apf -s 命令开始运行apf,使用apf -f关闭apf。

五、配置

  默认的参数适合大多数场合,其他的选项按照需要进行修改即可,配置文件里面都有详细的说明
  下面说明几个一般会用到的选项:
  DEVEL_MODE:设置为1的时候为开发者模式,运行在此模式下apf运行5分钟将自动关闭,防止配置错误引起连接不上服务器的情况,在配置测试完成后记得修改为0;
  SET_MONOKERN:随内核直接编译的iptables就将此选项设置为1;
  RAB:动态地址阻塞,修改为1,开启;
  RAB_PSCAN_LEVEL:安全等级,设置为3,高安全等级;
  TCR_PASS:是否允许Traceroute数据通过,根据自己的情况设置;
  DLIST_PHP、DLIST_SPAMHAUS、DLIST_DSHIELD、DLIST_RESERVED这几个选项是通过网络获取一些不安全的、僵死网络等ip地址进行屏蔽,一般情况建议设置为1;
  IG_TCP_CPORTS、IG_UDP_CPORTS:定义入站的TCP和UDP端口;
  EGF:设置为1打开出站端口检测;
  EG_TCP_CPORTS、EG_UDP_CPORTS:定义出站的TCP和UDP端口;
  EG_ICMP_TYPES:定义ICMP包过滤;