Tuesday, November 15, 2011

CSF: Config Server Firewall Installation

Filed under: Technical Support
An alternative firewall to APF is the Config Server Firewall, or CSF.
CSF is generally considered a more advanced firewall as there are more configuration options compared to other firewalls, while still being simple enough to install and configure that even novice administrators can use it. This article will give you a simple overview about how to install and configure CSF and its security plugin LFD (Login Failure Daemon).

Note: This post assumes that you are familiar with SSH and basic command line navigation. These instructions apply primarily to customers who have VPS or Dedicated servers. If you do not have root-level access to your server account you will not be able to make these changes.

Removing Your Current Firewall

To prevent any conflicts in operation we will need to remove your current firewall. The most common software firewalls on our dedicated servers are APF and CSF, so we have provided instructions on how to remove APF below. If you are using a different software firewall be sure to follow that programs uninstall directions before continuing. After completing the uninstall continue with the CSF installation below.

Using Yum to Remove APF

If it was installed via yum, which is most likely, you will first need to identify the name of your APF package:
rpm -qa |grep -i apf
In this case:
[root@host ~]# rpm -qa | grep apf

In order to remove that package, and get your server ready for CSF installation, run the following command:
[root@host ~]# rpm -e apf-9.6_5-1
If you see this:
[root@host ~ ]# rpm -e apf-9.6_5-1
error: Failed dependencies:
apf >= 9.6_5-1 is needed by (installed) bfd-1.2-1.noarch

It means you have BFD installed, and it will need to be removed before you can proceed to removing APF:
[root@host ~ ]# rpm -e bfd-1.2-1.noarch
Then attempt to remove apf again:
[root@host ~ ]# rpm -e apf-9.6_5-1
Any further dependency errors can be handled in the same way as long as you are aware of what you are removing. If you have any questions or get stuck at any point feel free to open up a support request with us by logging in to PIMS.

Removing Source Installed APF

This can be a bit trickier, and if you are not sure what you’re doing you may want to submit a ticket to our support team. If you are confident and wish to proceed you will find a list of commands below that you can use to remove APF if it is installed in the most common CentOS directories.
Stopping APF and iptables clears all of the rules from your firewall and ensures that removing the APF installation won’t cause access problems:
[root@host ~ ]# /etc/init.d/iptables stop
[root@host ~ ]# /etc/init.d/apf stop
Remove all of the APF related files:
[root@host ~ ]# rm -Rfv /etc/apf
[root@host ~ ]# rm -fv /etc/cron.daily/fw
[root@host ~ ]# rm -fv /etc/init.d/apf

Remove APF from the list of programs that start at boot:
[root@host ~ ]# chkconfig apf off
If you get an error on any of those commands that you don’t understand please open a ticket with our support team and we will do everything we can to assist you.

Installing CSF

Installing CSF should be as simple as downloading the source file to your server and installing it. All commands below should be executed on your server via SSH, not on your local computer.
The first few steps of the installation are the same whether it is a cPanel server or a non-cPanel server.

Retrieving the Package

Best practice when installing any software from source: Use a temporary directory on a partition with plenty of space.
On our dedicated cPanel servers you will already have a directory that we generally use for such things called ‘temp’ on the home partition:
[root@host ~ ]# cd /home/temp/
It is a good idea to check your disk space usage before proceeding, just to be sure there is plenty available:
[root@host ~ ]# df -h
Use ‘wget’ to retrieve CSF install code:
[root@host ~ ]# wget http://www.configserver.com/free/csf.tgz
Once the download is complete, you will see something similar to the following, and be given a command prompt again:
14:53:02 (410.05 KB/s) - `csf.tgz' saved [487272/487272]
Next: Decompress the CSF install files and change directories to the newly created ‘csf’ directory:
[root@host ~ ]# tar zxvf csf.tgz
[root@host ~ ]# cd csf

This is where the paths diverge: cPanel server, or non-cPanel server.
[root@host /home/temp/csf/ ]# ./install.cpanel.sh
If you are running a non-cpanel redhat server:
[root@host /home/temp/csf/ ]# ./install.sh
Either way you can look through the output of the script as it is running and it will tell you everything it has done. At the end, you will see something similar to the following:
TCP ports currently listening for incoming connections:

UDP ports currently listening for incoming connections:

Note: The port details above are for information only, csf hasn't
been auto-configured.

Don't forget to:
1. Configure the TCP_IN, TCP_OUT, UDP_IN and UDP_OUT options in
the csf configuration to suite your server
2. Restart csf and lfd
3. Set TESTING to 0 once you're happy with the firewall

Adding current SSH session IP address to the csf whitelist in csf.allow:
Adding to csf.allow only while in TESTING
mode (not iptables ACCEPT)
*WARNING* TESTING mode is enabled
- do not forget to disable it in the configuration

Installation Completed
To start testing CSF, start it up:
[root@host ~ ]# /etc/init.d/csf restart
Once you have completed your testing be sure to take CSF out of testing mode by changing the flag in the csf.conf:
Edit the configuration with your favorite editor, in this case we will use vi:
[root@host ~ ]# vi /etc/csf/csf.conf
Find this block of text near the top of the configuration file:
# Testing flag - enables a CRON job that clears iptables incase of
# configuration problems when you start csf. This should be enabled until you
# are sure that the firewall works - i.e. incase you get locked out of your
# server! Then do remember to set it to 0 and restart csf when you're sure
# everything is OK. Stopping csf will remove the line from /etc/crontab

Edit the last line of that block of text so that it reflects testing being disabled:
Finally, restart CSF:
[root@host ~ ]# /etc/init.d/csf restart
Your CSF firewall is up and running! Congratulations!

Common Installation Errors

If you see an error about libwww not being installed you can install it with yum or cpan:
yum install perl-libwww-perl
perl -MCPAN -e 'install Bundle::LWP'

Beginning CSF: Making Changes Using the Command Line.

Updating CSF using the command line interface is simple by design!
Here are the most common commands you will be using:
csf -d IPADDRESS will deny an IP.
csf -a IPADDRESS will allow an IP.
csf -r will reload all rules.
If you forget the command you are looking for just type 'csf' on the command line and you will receive an list of all of your options:

[root@host ~ ]#  csf
ConfigServer Security & Firewall (http://www.configserver.com/cp/csf/)
csf: v4.17
(c)2006, Way to the Web Limited (http://www.waytotheweb.com)

Usage: /usr/sbin/csf [option] [value]

Option              Meaning
-h, --help          Show this message
-l, --status        List/Show iptables configuration
-s, --start         Start firewall rules
-f, --stop          Flush/Stop firewall rules
-r, --restart       Restart firewall rules
-a, --add ip        Add an IP address to be whitelisted to /etc/csf.allow
-d, --deny ip       Add an IP address to be blocked to /etc/csf.deny
-dr, --denyrm ip    Remove and unblock an IP address in /etc/csf.deny
-c, --check         Checks for updates to csf+lfd but does not perform an upgrade
-g, --grep ip       Search the iptables rules for an IP match (incl. CIDR)
-t, --temp          Displays the current list of temporary IP bans and their TTL
-tr, --temprm ip    Remove an IP address from the temporary IP ban list
-td, --tempdeny ip ttl [-p port] [-d direction]
                    Add an IP address to the temporary IP ban list. ttl is how
                    long to blocks for in seconds. Optional port. Optional
                    direction of block can be one of in, out or inout. Default
                    is in
-tf, --tempf        Flush all IP addresses from the temporary IP ban list
-u, --update        Checks for updates to csf+lfd and performs an upgrade if
-x, --disable       Disable csf and lfd
-e, --enable        Enable csf and lfd if previously disabled
-v, --version       Show csf version
CSF is an increasingly popular alternative to the stock firewalls on some servers. Should you require any other help or have any questions about CSF please open a ticket with our support team by logging in to your PIMS account, or you can also visit the CSF home page where you can find other documentation and FAQs.

No comments: