Advanced Policy Firewall (APF) is an iptables(netfilter) based firewall system designed around the essential needs of today’s Internet deployed servers and the unique needs of custom deployed Linux installations. The configuration of APF is designed to be very informative and present the user with an easy to follow process, from top to bottom of the configuration file. The management of APF on a day-to-day basis is conducted from the command line with the ‘apf’ command, which includes detailed usage information and all the features one would expect from a current and forward thinking firewall solution. APF is developed and maintained by R-fx Networks: http://www.rfxnetworks.com/apf.php
Download and Install
- tar -xzvf apf-current.tar.gz
- cd apf-X.X
Installation Details: Install path: /etc/apf/ Config path: /etc/apf/conf.apf Executable path: /usr/local/sbin/apf AntiDos install path: /etc/apf/ad/ AntiDos config path: /etc/apf/ad/conf.antidos DShield Client Parser: /etc/apf/extras/dshield/
Other Details: Listening TCP ports: 1,21,22,25,53,80,110,111,143,443,465,993,995,2082,2083,2086,2087,2095,2096,3306 Listening UDP ports: 53,55880
Note: These ports are not auto-configured; they are simply presented for information purposes. You must manually configure all port options.
Lets configure the firewall: vi /etc/apf/conf.apf
We will go over the general configuration to get your firewall running. This isn’t a complete detailed guide of every feature the firewall has. Look through the README and the configuration for an explanation of each feature.
We like to use DShield.org’s “block” list of top networks that have exhibited suspicious activity.
FIND: USE_DS="0" CHANGE TO: USE_DS="1"
Configuring Firewall Ports
We like to use the following on our Cpanel Servers
Common ingress (inbound) ports # Common ingress (inbound) TCP ports -3000_3500 = passive port range for Pure FTPD IG_TCP_CPORTS="21,22,25,53,80,110,143,443,2082,2083, 2086,2087, 2095, 2096,3000_3500" # # Common ingress (inbound) UDP ports IG_UDP_CPORTS="53" Common egress (outbound) ports # Egress filtering [0 = Disabled / 1 = Enabled] EGF="1" # Common egress (outbound) TCP ports EG_TCP_CPORTS="21,25,80,443,43,2089" # # Common egress (outbound) UDP ports EG_UDP_CPORTS="20,21,53"
We have found the following can be used on Ensim Servers – although we have not tried these ourselves as I don’t run Ensim boxes.
Common ingress (inbound) ports # Common ingress (inbound) TCP ports IG_TCP_CPORTS="21,22,25,53,80,110,143,443,19638" # # Common ingress (inbound) UDP ports IG_UDP_CPORTS="53" Common egress (outbound) ports # Egress filtering [0 = Disabled / 1 = Enabled] EGF="1" # Common egress (outbound) TCP ports EG_TCP_CPORTS="21,25,80,443,43" # # Common egress (outbound) UDP ports EG_UDP_CPORTS="20,21,53"
Save the changes :wq
Starting the firewall
usage ./apf [OPTION] -s|--start ......................... load firewall policies -r|--restart ....................... flush & load firewall -f|--flush|--stop .................. flush firewall -l|--list .......................... list chain rules -st|--status ....................... firewall status -a HOST CMT|--allow HOST COMMENT ... add host (IP/FQDN) to allow_hosts.rules and immediately load new rule into firewall -d HOST CMT|--deny HOST COMMENT .... add host (IP/FQDN) to deny_hosts.rules and immediately load new rule into firewall
After everything is fine, change the DEV option
Stop the firewall from automatically clearing itself every 5 minutes from cron. We recommend changing this back to “0” after you’ve had a chance to ensure everything is working well and tested the server out.
FIND: DEVM="1" CHANGE TO: DEVM="0"
Configure AntiDOS for APF
Relatively new to APF is the new AntiDOS feature which can be found in: /etc/apf/ad
The log file will be located at /var/log/apfados_log so you might want to make note of it and watch it!
There are various things you might want to fiddle with but I’ll get the ones that will alert you by email.
# [E-Mail Alerts]
Under this heading we have the following:
# Organization name to display on outgoing alert emails CONAME="Your Company"
Enter your company information name or server name..
# Send out user defined attack alerts [0=off,1=on] USR_ALERT="0"
Change this to 1 to get email alerts
# User for alerts to be mailed to USR="email@example.com"
Enter your email address to receive the alerts
Save your changes! :wq
Restart the firewall:
Checking the APF Log
Will show any changes to allow and deny hosts among other things.
tail -f /var/log/apf_log
Aug 23 01:25:55 ocean apf(31448): (insert) deny all to/from 18.104.22.168 Aug 23 01:39:43 ocean apf(32172): (insert) allow all to/from 22.214.171.124
New – Make APF Start automatically at boot time
To autostart apf on reboot, run this:
chkconfig --level 2345 apf on
To remove it from autostart, run this:
chkconfig --del apf
Denying IPs with APF Firewall (Blocking)
Now that you have your shiny new firewall you probably want to block a host right, of course you do! With this new version APF now supports comments as well. There are a few ways you can block an IP, I’ll show you 2 of the easier methods.
/etc/apf/apf -d IPHERE COMMENTHERENOSPACES
- The -d flag means DENY the IP address
- IPHERE is the IP address you wish to block
- COMMENTSHERENOSPACES is obvious, add comments to why the IP is being blocked
These rules are loaded right away into the firewall, so they’re instantly active. Example:
./apf -d 10.0.1.25 TESTING
Shows the following:
# added 126.96.36.199 on 08/23/05 01:25:55 # TESTING 10.0.1.25
You can then just add a new line and enter the IP you wish to block. Before this becomes active though you’ll need to reload the APF ruleset.
Allowing IPs with APF Firewall (Unblocking)
I know I know, you added an IP now you need it removed right away! You need to manually remove IPs that are blocked from deny_hosts.rules.
Find where the IP is listed and remove the line that has the IP. After this is done save the file and reload apf to make the new changes active.
If the IP isn’t already listed in deny_hosts.rules and you wish to allow it, this method adds the entry to allow_hosts.rules
/etc/apf/apf -a IPHERE COMMENTHERENOSPACES
- The -a flag means ALLOW the IP address
- IPHERE is the IP address you wish to allow
- COMMENTSHERENOSPACES is obvious, add comments to why the IP is being removed These rules are loaded right away into the firewall, so they’re instantly active.
./apf -a 188.8.131.52 UNBLOCKING
# added 184.108.40.206 on 08/23/05 01:39:43 # UNBLOCKING 10.0.1.25
Important configuration Options
- DEVM=”1″ — Leave DEVEL_MODE enabled until you are SURE that you have a working configuration. This option will flush/clear your firewall every 5 minutes, so if you lock yourself out, just wait 5 minutes and you can get back in 🙂
Config Options Specific to VPSLink VPS Systems
- SET_MONOKERN=”1″ — This option normally does not need to be enabled, BUT, on a VPSLink VPS instance, it MUST be enabled (at least on CentOS4). Without this enabled, APF will complain that it can’t find the iptables module and will abort immediately.
- IFACE_IN — Set to “venet0” or whatever you see in the output of the “ifconfig” command.
- IFACE_OUT — Same as IFACE_IN.
General Config Options
- IG_TCP_CPORTS — These are the INBOUND TCP ports you want to open. Separate the values with space, E.g. “22 80 443” would open SSH, HTTP, and HTTPS
- IG_UDP_CPORTS — These are the INBOUND UDP ports you want open. E.g. “53” if you run a DNS server.
- EG_TCP_CPORTS — These are the OUTBOUND TCP ports you want to open. By default, ALL outbound connections are allowed, but if you enable EGF=”1″, then outbound will be restricted as well. I highly recommend this for a more secure system.
- EG_UDP_CPORTS — These are the OUTBOUND UDP ports you want open. Same comment as above EG_TCP_CPORTS.
Thanks to R-fx networks for developing and maintaining APF Firewall.