APF(Advanced Policy Firewall)是 Rf-x Networks 出品的Linux环境下的软件防火墙,被大部分Linux服务器管理员所采用,使用iptables的规则,易于理解及使用。
适合对iptables不是很熟悉的人使用,因为它的安装配置比较简单,但是功能还是非常强大的。
一,下载,安装apf
root@linux:/home/zhangy# wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz
root@linux:/home/zhangy# tar -xvzf apf-current.tar.gz
root@linux:/home/zhangy# cd apf-9.7-1
root@linux:/home/zhangy/apf-9.7-1# ./install.sh
安装成功的提示信息如下:
root@linux:/home/zhangy/apf-9.7-1# ./install.sh
Installing APF 9.7-1: Completed.
Installation Details:
Install path: /etc/apf/
Config path: /etc/apf/conf.apf
Executable path: /usr/local/sbin/apf
Other Details:
Listening TCP ports: 22,25,111,3306,53976
Listening UDP ports: 111,917,936,5353,49640,54744
Note: These ports are not auto-configured; they are simply presented for information purposes. You must manually configure all port options.
二,配置apf
vim /etc/apf/conf.apf
IG_TCP_CPORTS="21,22,80,443,3306,8080" //设置服务器允许被访问的TCP端口
IG_UDP_CPORTS="53" //设置服务器允许被访问的UDP端口
EG_TCP_CPORTS="21,25,80,443,43,2089" //设置服务器允许对外访问的TCP端口
EG_UDP_CPORTS="20,21,53" //设置服务器允许对外访问的UDP端口
DEVEL_MODE="1" 改为 DEVEL_MODE="0"
DLIST_SPAMHAUS="0" 改为 DLIST_SPAMHAUS="1"
DLIST_DSHIELD="0" 改为 DLIST_DSHIELD="1"
配置过程中要注意以下几点:
1,根据不同的服务器开放不同的端口,web服务器根mysql服务器开放的端口肯定不一样。
2,DEVEL_MODE="1"表示在调试模式下,每五分钟重新刷空配置,这样能避免因为错误的配置把你自己也搞在了服务器外面进不去了。等配置好了,确定没有问题了,就改成0。
# Untrusted Network interface(s); all traffic on defined interface will be
# subject to all firewall rules. This should be your internet exposed
# interfaces. Only one interface is accepted for each value.
IFACE_IN="bond1"
IFACE_OUT="bond1"
# Trusted Network interface(s); all traffic on defined interface(s) will by-pass
# ALL firewall rules, format is white space or comma separated list.
IFACE_TRUSTED="bond0"
这里的bond1、bond0和eth0、eth1类似,都是名字而已,比如bond1表示外网,是不受信任的,所以所有的传输都会提交给firewall来审核;而bond0是内网,是受信任的。
IFACE_IN and IFACE_OUT - By default, these are set to eth0; however, SoftLayer has our eth0 set to private network and our public network set to eth0. Misconfiguring these variables might result in no network connectivity. Set both variables to eth1
IFACE_TRUSTED - This variable tells the firewall to trust the listed interfaces. In our instance, we trust our eth0. Set to eth0
3,设置只通许192.168.1.139远程连接22端口
// 在/etc/apf/allow_hosts.rules添加如下信息:
tcp:in:d=22:s=192.168.1.139
out:d=22:d=192.168.1.139
// 在/etc/apf/deny_hosts.rules添加如下信息:
tcp:in:d=22:s=0/0
out:d=22:d=0/0
开始的时候,我以为只要在allow_hosts.rules里面加就行了,改过一后,我换了一个IP,已然可以连接,搞得我很无语。后在deny_hosts.rules加上了上面的规则后,在连接时就提示超时了。allow_hosts.rules和deny_hosts.rules里面都加了规则后,重起apf会提示配置成功的信息,偶然发现的。
apf(12234): {trust} allow outbound 192.168.1.139 to port 22
apf(12234): {trust} allow inbound tcp 192.168.1.139 to port 22
三,apf的常用命令
<a target="_blank" href="http://blog.51yip.com/server/1347.html#">查看</a>
apf -s // 启动APF防火墙
apf -r // 重启APF防火墙
apf -f // 刷新APF防火墙配置文件
apf -l // 列出APF的过虑规则
apf -t // APF的日志信息。
apf -e // 将域名解释加入信认规则
apf -a // 将IP/IP段添加到白名单
apf -d // 将IP/IP段添加到黑名单
apf -u // 将IP/IP段从白/黑名单中删除
apf -o // 将IP/IP段从白/黑名单中删除
四,常用端口列表
21/tcp //ftp
22/tcp //ssh
25/tcp //smtp
53/udp //dns
80/tcp //http
110/tcp //pop3
143/tcp //imap
443/tcp //https
993/tcp //imaps
995/tcp //pop3
3306/tcp //mysql
5432/tcp //postgresql