WebHostGear.com - the hosting resource for professionalshosting tutorials 
hosting howto webhost guide server managementMarch 14, 2010
server management, apache tutorials, hosting tutorials, cpanel, server security
Home / Hosting Tutorials / Server Security / How to install APF (Advanced Policy Firewall)

How to install APF (Advanced Policy Firewall)



Printer Friendly Printer Friendly Send to a friend Send to a friend
By : ramprage Rating : Average Rating : 8.26 From 271 Voter(s)

What is APF (Advanced Policy Firewall)? APF Firewall
APF is a policy based iptables firewall system designed for ease of use and configuration. It employs a subset of features to satisfy the veteran Linux user and the novice alike. Packaged in tar.gz format and RPM formats, make APF ideal for deployment in many server environments based on Linux. APF is developed and maintained by R-fx Networks: http://www.rfxnetworks.com/apf.php

This guide will show you how to install and configure APF firewall, one of the better known Linux firewalls available.10

Limit SSH connections to one IP with APF in this advanced tutorial

Requirements:

- Root SSH access to your server

Lets begin!
Login to your server through SSH and su to the root user.

1. cd /root/downloads or another temporary folder where you store your files.

2. wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz


Article provided by WebHostGear.com

3. tar -xvzf apf-current.tar.gz

4. cd apf-0.9.5-1/ or whatever the latest version is.

5. Run the install file: ./install.sh
You will receive a message saying it has been installed

Installing APF 0.9.5-1: Completed.

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.

6. Lets configure the firewall: pico /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"

7. Configuring Firewall Ports:

Cpanel Servers
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"



Ensim Servers
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: Ctrl+X then Y


8. Starting the firewall
/usr/local/sbin/apf -s

Other commands:
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


9. 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.

pico /etc/apf/conf.apf

FIND: DEVM="1"
CHANGE TO: DEVM="0"

10. 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!

pico /etc/apf/ad/conf.antidos

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="your@email.com"

Enter your email address to receive the alerts

Save your changes! Ctrl+X then press Y
Restart the firewall: /usr/local/sbin/apf -r

11. Checking the APF Log

Will show any changes to allow and deny hosts among other things.
tail -f /var/log/apf_log

Example output:
Aug 23 01:25:55 ocean apf(31448): (insert) deny all to/from 185.14.157.123
Aug 23 01:39:43 ocean apf(32172): (insert) allow all to/from 185.14.157.123


12. 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


13. 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.

A) /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 185.14.157.123 TESTING

pico /etc/apf/deny_hosts.rules

Shows the following:

# added 185.14.157.123 on 08/23/05 01:25:55
# TESTING
185.14.157.123

B) pico /etc/apf/deny_hosts.rules

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.

/etc/apf/apf -r

14. 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.
A) 
pico /etc/apf/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.

/etc/apf/apf -r

B) 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.
Example:

./apf -a 185.14.157.123 UNBLOCKING

pico /etc/apf/allow_hosts.rules

# added 185.14.157.123 on 08/23/05 01:39:43
# UNBLOCKING
185.14.157.123


Thanks to R-fx networks for developing and maintaining APF Firewall. Written by Steven Leggett of WebHostGear.com Need help with APF? Visit the WebHostGear Forums

New! - Need server help? Hire an Expert

Get professional help with your configuration, script installation or server issue.
Learn how we can help you with any server problem and make your server run like new. Professional staff will contact you, after submitting a quote request, by phone or email.

Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent

Related Articles


Guide to Chkrootkit - checking for intruders
Creating a Welcome message for SSH logins
Common SSH Commands - Linux Shell Commands
How to install BFD (Brute Force Detection)
Changing APF log for TDP/UDP drops
Ban an IP Address From The Server
Stop PHP nobody Spammers
Preventing Brute Force Attacks
How to install Shoutcast
APF Deny ALL for SSH Limit IP Connections


Discuss this article with others in our new hosting forums

Comments / Feedback

Edward
This worked great, but if your kernel is compiled with iptables statically instead of as a module you need to do this in the conf.apf MONOKERN="0" Set it to "1" and then try start APF again.
chris
the firewall should be set to 0 not 1 to be running allt eh time setting to 1 = turns off after 5 minutes ..i would fix it or you will have newbies with firewalls off everywhere
jdwh
Does this work for Burst.net servers? In the past there was a thread in their forums saying it was incompatible. Maybe this was fixed?
ryan
great howto document and covers the current release whereas most people cover the RPM release; great job.
Steve
jdwh - yes it works with any Linux servers, if you have any questions about firewall ports on the network then contact your provider.
Tom
Don't you need to type the following so APF starts after a reboot? chkconfig --level 2345 apf on
ryan
No; chkconfig is run during installation by APF's install.sh script.
greg
anyone know how to stop if from logging to the terminal (tty1?)
autoquartz
How about Plesk 7 Server?
BAMF
Make sure to add port #3306 to IG_TCP_CPORTS if you want to remotely administer your database.
Scott
Im kinda a newbie at this I use 4 diff Ipadresses on my machine could someone tell me how to add in so all the ip's use the same rights. Last firewall I tried blocked all my other Ip's on all ports thanx
chris
How do you block an Ip in APF?
Steve
To block and IP in APF go to /etc/apf and pico deny_hosts.conf. Scroll down and add the IP addresses you need to block, each on a separate line. Comments with more details are in the file.
Anton
Guys, what about ports for passive ftp?
Colin Myerscough
I can not use this firewall because feeds.dshield.org does not exist and I can not get the block.txt from there. Who can help me
Tim Rice
APF is great, except I sometimes have a hard time taking out a chain. For an example, I have the following loaded in the chain: 32 DROP all -- 83.0.0.0/8 anywhere I have tried to remove this, but can't for some strange reason. Is there a file that I can edit?
Khurrum Maqbool
I had to open port 2089 because it was having problems with the license. Apparently cpanel needs the license sync port 2089 not only for incoming but also for outgoing traffic open
Janos
I get an error message when I want to start APF: /usr/local/sbin/apf: line 1: ifconfig: command not found . What's wrong here?
For Janos from WHG
Run: cat /etc/apf/VERSION
You should see version: 0.9.3-rev3

Then before you run any APF commands make sure you're logged in as the root environment: su -

Make sure you use the "-" otherwise you won't be in the environment, this is why you're getting the error message I'm fairly sure.
DJALPHA
Which is correct? 3000_3500 or 30000_35000 ?
Please check how to thx.
reanncw
i get this,

/etc/apf/vnet/vnetgen: ip: command not found
lsmod: QM_MODULES: Function not implemented

iptables v1.2.6a: unknown protocol `ipv6-crypt' specified
Try `iptables -h' or 'iptables --help' for more information.
iptables: No chain/target/match by that name
iptables: No chain/target/match by that name
iptables: No chain/target/match by that name
Khurrum Maqbool
Also port 873 ingress is needed for Rsync... this is needed for /scripts/easyapache
Gareth
Is APF compatible with FreeBSD?
Andy
error as follows

iptables v1.2.9 invalild TCP / Port Service '=21' specified

Try `iptables -h' or 'iptables --help' for more information

Bob
When I start the firewall I get a series of "uknown host" comments. What is this from and should I be concerned?
Sun Joo
After APF started, the sites are not accessed from the browser. The moment I stop APF, then sites are loaded. What am I missing ? Any clue ? Thanks.
JLchafardet
does this tutorial have ever been tested on RHEL ES3 Plesk Reloaded based servers?
Caz
I get "unknown host" when doing apf -r. Anyone know why?
Mtrafox
Hy.
After I installed APF, I have some problems.
When I get the mail from BDF, that someone atack my one of virtual IP, and I ceck with ifconfig, my virtual interface is down. Some time all of my virtual interface are down. Anyone know how to fix this ?
Randall_James
chris
How do you block an Ip in APF?

apf -d 12.345.67.89
----------------------
Bob and Caz
When I start the firewall I get a series of "uknown host" comments. What is this from and should I be concerned?

Check the deny_host.rules, there is probably a bad entry in the bottom half of file
________________________
INSTALL NOTE:
During the final phase of install you will recieve some output to screen, this will list the current listening ports! Generally these are going to be need added to the allowed ports in the conf.apf file. Also monitored services need open ports (eg, ThePlanet) check with your datacenter for a list of these ports.

Jafar
hello ,,
I have got a question ..I want your help for me please in this problem ..

Sometime : Network error(tcp_error) A communication error occurred: "Operation timed out"
Sometime : operatio timed out
Sometime : The page cannot be displayed
Sometime : Problem Report

Some of the tracts is not able to the entry this talk is visible to them by higher
An example ( UAE emirates . Syria . Egypt ..

this problem From , "Fairwell APF"

Stop the program of the protection "APF" they are able to the entry (/usr/local/sbin/apf -f"
Start,run the program of the protection "APF" They are not able to the entry (/usr/local/sbin/apf -s)


what's I want do on a this
By start this the problems From about 25 days .

thanks . I hope your help
apfwannabe
how much memory and cpu usage does APF consumes?

i have a vps with 199mb running whm/cpanel and has average load of 75% mem and 1 cpu load.

will apf makes the server unstable considering my current vps resources
above?
Henry
Hello,

I got this error during installation, please help,

Other Details:
cp: cannot stat `/etc/apf.bk.last/vnet/*.rules': No such file or directory
Imported options from 0.9.4-6 to 0.9.5-1.
Note: Please review /etc/apf/conf.apf for consistency, install default backed up to /etc/apf/conf.apf.orig
root@host [~/downloads/apf-0.9.5-1]# ./install.sh
Installing APF 0.9.5-1: Completed.
need-help
unknow host
Matt
I installed this successfully but then it locked me out! It blocked all IP's and I wasn't even able to get in via SSH - what do I have to mod for this to work? I had to have my datacenter disable firewall so I could have remote access again...
Tedd
I cant seem to configure this properly so that I can take it out of test mode. When running I cannot access my website.

I using WMH with cPanel. I got it installed okay. But no matter how I configure the options, Im denied from the website.

Any ideas?
Tapan
Hi,

When i did all i got this and its not working:

/usr/local/sbin/apf -s
eth0: error fetching interface information: Device not found
eth0: error fetching interface information: Device not found
eth0: error fetching interface information: Device not found
Development mode enabled!; firewall will flush every 5 minutes.
/etc/apf/firewall: line 1: /sbin/lsmod: No such file or directory
Unable to load iptables module (ip_tables), aborting.

Thanks.
Ankush
Please update the article to match the latest version settings of apf. The new apf has lot of new features.
Liam Glanfield
This was very detailed and spot on but I think I may have done somthing wrong. I have cpanel so used there port numbers given above. When i get to the part apf -s it works ok but I get "wget comand not found, try usinf wget --help for more info..." yet if I do apf -st it says it is working?! is this normal ??
Starfinder
Matt, Are you running SSH on a port other than 22? If so, you must also allow connections to this port.
nowayout
root@server1 [~/downloads]# /usr/local/sbin/apf -s
iptables v1.2.11: host/network `216-211-192-63.noviant.com' not found
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.2.11: host/network `216-211-192-63.noviant.com' not found
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.2.11: host/network `adsl_corporativo16760-74.etb.net.co' not found
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.2.11: host/network `adsl_corporativo16760-74.etb.net.co' not found
Try `iptables -h' or 'iptables --help' for more information

How I could fix that ?
Buddah
Hmm, now my webpages won't load, ftp/plesk/ssh all work fine but websites won't load :-?
Fernando
APF is dieing right after I call it. I don't know why. In logs I can only see:
Jan 24 00:42:13 nocserver apf(28525): {glob} loading postroute.rules
Jan 24 00:42:13 nocserver apf(28525): {glob} default (egress) output accept
Jan 24 00:42:13 nocserver apf(28525): {glob} default (ingress) input drop
Jan 24 00:42:13 nocserver apf(28488): firewall initalized

Do you know why?
NOTSUPPORT
THIS IS NOT TECHNICAL SUPPORT. GO SEEK SUPPORT ELSEWHERE.
Cleber
How to install APF in 2 or more interfaces?

ex: eth0, eth0.1 and others
Rainer
Howto config NAT (iptable) with APF?
Morons
this isn't a support page, go elsewhere! This is just about one of the easiest monitoring software out there.
Luis
If you have Cpanel be sure to also open port 26 if you have a copy of exim on that port for the clients that have isp's that block 25
enes
when using smbmount command with APF, smbmount does not working and gives "/bin/ls: .: Stale NFS file handle" error.

to solve this problem, use this;

./apf -a OTHER_SERVER_IP
./apf -r
Fred
I see cpanel is spoken about as it relates to this error but I am using monsterconls would it make a different.

lsmod: QM_MODULES: Function not implemented Unable to load iptables module (ip_tables), aborting.

I am have DDOS and install apf but can't get to load.

Any help would be welcomed -THANKSl
Steve
I get installed the apf on server but cannot control the DDOS attack.
Please let us know what to do.
I have configured everything.
The attack is now going on.
Calvin
How to remove this firewall after installation ?

Regards,
Calvin
Calvin Phan
How to uninstall (remove) this APF firewall ?
Steve
rm -fv /etc/cron.daily/fw
chkconfig --del apf
rm -frv /etc/apf
Muhammad Sajjad
i need ur help about configuring firewall on routers. please provide me some notes or tutorials.
FD
APF simply does not work correctly with 2.6 kernels due to module name changes and incompatibilities. You get the "Unable to load iptables module (ipt_state), aborting" message

Setting "MONOKERN" to 1 is not a solution as it causes problems with passive FTP (amongst others) and (as mentioned in the script) is NOT supported.

I have searched everywhere for a fix, but nothing is suitable. I have uninstalled APF and will wait for a release that addresses this issue.
M0Dy
How to uninstall (remove) this APF firewall ?
Nemesiz
remove /etc/apf dir. APF - its only a script to make firewall rukes, not firewall.
Fidoxd
Is normal this log?

ago 05 11:55:01 server2 apf(31989): flushing & zeroing chain policies
ago 05 11:55:01 server2 apf(31989): firewall offline
ago 05 12:00:03 server2 apf(32043): flushing & zeroing chain policies
ago 05 12:00:03 server2 apf(32043): firewall offline
David Yasmineh
heres the issue....APF / BFD are really nice if you have a fixed IP that you use to access your server from.

The thing is i get a million unauthorized login attempts a month. I read about this script called PYTHON SCRIPT which would automatically block the attacking ip address for a period of time if it failed to authorize more than 3 times for example.

Now APF / BFD do the same?????

Please let me know, email me

david_yasmineh@yahoo.com

kind of urgent for me pls.

thnks
Daniel
Hi,

I cant start my firewall..

Development mode enabled!; firewall will flush every 5 minutes.
Unable to load iptables module (ipt_state), aborting.

Please help..
el
i tried to install on debian ...
cp: cannot create regular file `/etc/rc.d/init.d/apf': No such file or directory

debian doesn't have an /etc/rc.d/init.d directory. I assume that I should just edit the install script to point to /etc/init.d ?
Joe
APF & BFD both installed successfully and apparently working correctly except for two issues.

1/ No email alerts are being received (I have configured as per instructions)

2/ For AntiDOS for APF the file /var/log/apfados_log is missing.

Finally, how do I set the time during which a brute attack is allowed before being stopped. For instance, my server was under attack for 10 minutes during which time a huge number of attempts were made before being stopped. How do I reduce this time or preferably set a number of instances (5 perhaps) before an attack is stopped.

Thanks

Joe
Alan
Antidos is intended to operate via cron. This is a critical setup point as if
not done, antidos will simply not operate.

The following string can be placed into /etc/crontab or similar file:
*/2 * * * * root /etc/apf/ad/antidos -a >> /dev/null 2>&1

This will run antidos every two minutes. I dont recommend running it once a
minute as it may cause a bottleneck for itself and the CPU. Likewise running it
beyound a period of once every 5 minutes is not recommended either, for obviouse
reasons.
Jeff
Port 37 also needs to be open on Cpanel servers. The rdate function uses 37 to connect to the time servers
Steve
APF Plesk Ports

IG_TCP_CPORTS="20,21,22,25,53,80,110,143,443,465,993,995,8443"
IG_UDP_CPORTS="37,53,873"

EGF="1"
EG_TCP_CPORTS="20,21,22,25,53,37,43,80,113,443,465,873,5224"
EG_UDP_CPORTS="53,873"
Raptor
Great How to! Worked 1st time.
Steve
I cant start my firewall..

Development mode enabled!; firewall will flush every 5 minutes.
Unable to load iptables module (ipt_state), aborting.

any ideas ?
Beth
Has anyone gotten this to work to deny traffic from hosts that are perpetrating dictionary attacks against an FTP server? For example, several times a week, we have people running a database of usernames against our ftp servers. Sometimes, so much that my logs are 15-50 MB larger from logging the attempts. It would be great if we could stop a user at 5 attempts or so. The FTP server itself stops the user after 3 failed logins, but they go on with more from the list, so it could end up being thousands of names.

Thanks!
Beth
binoy
How can I unblock an ip through apf.

email me at binoy1983@yahoo.com
Senthil
Are there any ways in adding apf in whm panel (doing all the operations in the whm panel)?.
Rich
Is this guide still current? I only ask because it was 3 years ago that this article was originally published. Have any instructions changed?
Shaun
Works fine as of December 2007
vinyas
Works super as of January 2008
Valeriu Palos
This tool also runs beautifully on Ubuntu based machines (maybe even Debian) using this patch:

http://codeblog.palos.ro/2007/09/08/apf-firewall-on-ubuntu-704/
pankaj dhingra
cannot stat `/etc/apf.bk.last/vnet/*.rules':

facing this problem while installing apf
devil2005
i issue the command apf -d 192.168.1.64 . and also another ipddress for a webproxy to see if apf blocks it . but it doesnt . i have doen apf -r . and the webpage is still displayed to the blocked hosts . whats wrong
Milos
Starting with version 0.9.6-3 APF completely replaced Antidos with RAB (Reactive Address Blocking). So there is no more additional AntiDos cron job, everything is managed by APF and the RAB options are in conf.apf as expected.

It would be nice that you update this tutorila. :)
Albert
i have some probs.. :

root@viper [/apf-9.6-5]# ./install.sh
Installing APF 9.6-5: eth0: error fetching interface information: Device not found
Completed.

Installation Details:
Install path: /etc/apf/
Config path: /etc/apf/conf.apf
Executable path: /usr/local/sbin/apf

Other Details:
eth0: error fetching interface information: Device not found
cp: cannot stat `/etc/apf.bk.last/vnet/*.rules': No such file or directory
Imported options from 9.6-5 to 9.6-5.
Note: Please review /etc/apf/conf.apf for consistency, install default backed up to /etc/apf/conf.apf.orig

i'm using :

cPanel 11.24.4-R32603 - WHM 11.24.2 - X 3.9
CENTOS 5.2 i686 on virtuozzo
Ethernet Device : venet0:0
petrepaul
when i do nano /etc/apf/ad/conf.antidos

or pico /etc/apf/ad/conf.antidos

creates a new file? everything else installed and works fine, any suggestions?

really need to update this information with current version apf-9.7-1

 Add Comment
Name
Email
Image Code
Refresh Image

Comments / Feedback



Our site offers free hosting tutorials, cpanel tutorial, web hosting news, shell commands, running a web hosting business, dedicated guides, linux tutorial, apache install, home web server, web server guide, ssh commands, dedicated servers, DNS nameservers, chkrootkit, apf firewall, exim configuration, server compromised, cron backup solution, ftp backup script

Server Tutorials


WebHostGear Reviewed by Ping Zine - Click here

Links:
Lunarpages Coupon