Configserver Firewall and Security (CSF and LFD)

12-10-2014 | Remy van Elst


Table of Contents


This page covers my notes about csf and lfd. csf is an easy SPI iptables firewall suite. lfd is the login failure daemon, which scans log files for failed authentication and blocks the IP's doing that. This page covers installation, populair command line options and popular config file options.

CSF Installation

On CentOS systems, first install some required Perl packages:

 yum -y install perl-libwww-perl.noarch perl-Time-HiRes

Download CSF:

wget http://www.configserver.com/free/csf.tgz

Extract it:

tar -xf csf.tgz

Start the installation:

cd csf 
./install.sh

If you have a server with Directadmin:

cd csf
./install.directadmin.sh

Set some sane defaults:

perl -pi -w -e "s/TESTING = \"1\"/TESTING = \"0\"/" /etc/csf/csf.conf
perl -pi -w -e "s/SMTP_BLOCK = \"0\"/SMTP_BLOCK = \"1\"/" /etc/csf/csf.conf
perl -pi -w -e "s/LF_DSHIELD = \"0\"/LF_DSHIELD = \"86400\"/" /etc/csf/csf.conf
perl -pi -w -e "s/LF_SPAMHAUS = \"0\"/LF_SPAMHAUS = \"86400\"/" /etc/csf/csf.conf
perl -pi -w -e "s/LF_DIRWATCH = \"300\"/LF_DIRWATCH = \"120\"/" /etc/csf/csf.conf
perl -pi -w -e "s/LF_DIRWATCH_DISABLE = \"0\"/LF_DIRWATCH_DISABLE = \"1\"/" /etc/csf/csf.conf
perl -pi -w -e "s/PT_SKIP_HTTP = \"0\"/PT_SKIP_HTTP = \"1\"/" /etc/csf/csf.conf
perl -pi -w -e "s/PT_USERPROC = \"10\"/PT_USERPROC = \"15\"/" /etc/csf/csf.conf
perl -pi -w -e "s/CC_DENY = \"\"/CC_DENY = \"CN,KR,HK,IN,ID,MY,NG,PK,RU,SA,TW,SY,AE\"/" /etc/csf/csf.conf
perl -pi -w -e "s/IGNORE_ALLOW = \"0\"/IGNORE_ALLOW = \"1\"/" /etc/csf/csf.conf

Restart CSF:

csf -r

Done. Do note that the IP you installed from has been added to the allow list.

csf principle

The idea with csf, as with most iptables firewall configurations, is to block everything and then allow through only those connections that you want. This is done in iptables by DROPPING all connections in and out of the server on all protocols. Then allow traffic in and out from existing connections. Then open ports up in and outgoing for both TCP and UDP individually.

csf command line tips

Block an IP from the server

csf -d 192.168.1.2 
csf -d 192.168.0/24

Remove an IP from the blocklist

csf -dr 192.168.1.2 

Allow an IP

csf -a 172.16.20.1

Do note that when you allow an IP, it will still be blocked by lfd if it starts brute forcing. See below, "Don't block IP addresses from the allow list with lfd" to see how to disable it.

Search for an IP:

csf -g 192.168.1.2

View Ports listening for external connections and the executables running behind them

csf -p

Disable csf and lfd

csf -x

Enable csf and lfd

csf -e

Restart csf

csf -r

Mail yourself a general security check of the server

csf -m user@example.com

Advanced filters

These can be placed in either csf.allow or csf.deny.

Allow connections over port 443 to a range of IP addresses:

tcp|out|d=443|d=12.34.56.78/27 # csf.allow

Allow SSH from one IP:

tcp|in|d=22|s=23.45.67.88 # csf.allow

Block traffic to port 80 from a specific range:

tcp|out|d=80|d=12.34.56.78/24 #csf.deny

The syntax for the advanced rules:

tcp/udp|in/out|s/d=port|s/d=ip|u=uid

tcp/udp  : EITHER tcp OR udp OR icmp protocol
in/out   : EITHER incoming OR outgoing connections
s/d=port : EITHER source OR destination port number (or ICMP type)
           (use a _ for a port range, e.g. 2000_3000)
s/d=ip   : EITHER source OR destination IP address
u/g=UID  : EITHER UID or GID of source packet, implies outgoing connections,
           s/d=IP value is ignored

csf.conf highlights

The /etc/csf/csf/csf.conf file is huge and I recommend you read through it. Below I'll highlight some parts of the config I use more often.

Don't block IP addresses from the allow list with lfd

By default lfd will also block IP addresses listed in the csf.allow file. Change the following to make lfd ignore IP addresses on that list.

IGNORE_ALLOW = "1"

Do note that if one of those IP's starts brute forcing the server, they will not be blocked.

Open/Allow ports

To allow incoming connections to a specific port, edit the following part of the config:

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2222"

# Allow incoming UDP ports
UDP_IN = "20,21,53"

To allow outgoing ports:

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,443,2222"

# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list 
UDP_OUT = "20,21,53,113,123"

Allow incoming / outgoing ping / ICMP

To allow ICMP/ping, in/out, change the following:

# Allow incoming PING
ICMP_IN = "1"

# Allow outgoing PING
ICMP_OUT = "1"

To block it, change it to "0"

Block certain country's

To block all traffic coming from certain country's:

CC_DENY = "CN,KR,HK,IN,ID,MY,NG,PK,RU,SA,TW,SY,AE"

Use the 2 letter ISO code there.

Disable tracking of long running processes

PT_LIMIT = "0"

If that is set to 1 you will receive a lot of mails when process resource usage spikes or they run longer than a minute.

Enable a Web Management UI

To enable the CSF web management UI, without having a controlpanel (like cpanel/directadmin):

UI = "0"
UI_PORT = "6666"
UI_USER = "username"
UI_PASS = "password"

Send an email when a user logs in via ssh

LF_SSH_EMAIL_ALERT = "1"

The emails are sent to root by default. Change the below line to change that:

LF_ALERT_TO = "user@example.org"

Tags: centos, cpanel, csf, debian, directadmin, firewall, iptables, ubuntu,