Skip to main content Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Configserver Firewall and Security (CSF and LFD)

Published: 12-10-2014 | Author: Remy van Elst | Text only version of this article

Table of Contents

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

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

CSF Installation

On CentOS systems, first install some required Perl packages:

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

Download CSF:


Extract it:

tar -xf csf.tgz

Start the installation:

cd csf ./

If you have a server with Directadmin:

cd csf./

Set some sane defaults:

perl -pi -w -e "s/TESTING = \"1\"/TESTING = \"0\"/" /etc/csf/csf.confperl -pi -w -e "s/SMTP_BLOCK = \"0\"/SMTP_BLOCK = \"1\"/" /etc/csf/csf.confperl -pi -w -e "s/LF_DSHIELD = \"0\"/LF_DSHIELD = \"86400\"/" /etc/csf/csf.confperl -pi -w -e "s/LF_SPAMHAUS = \"0\"/LF_SPAMHAUS = \"86400\"/" /etc/csf/csf.confperl -pi -w -e "s/LF_DIRWATCH = \"300\"/LF_DIRWATCH = \"120\"/" /etc/csf/csf.confperl -pi -w -e "s/LF_DIRWATCH_DISABLE = \"0\"/LF_DIRWATCH_DISABLE = \"1\"/" /etc/csf/csf.confperl -pi -w -e "s/PT_SKIP_HTTP = \"0\"/PT_SKIP_HTTP = \"1\"/" /etc/csf/csf.confperl -pi -w -e "s/PT_USERPROC = \"10\"/PT_USERPROC = \"15\"/" /etc/csf/csf.confperl -pi -w -e "s/CC_DENY = \"\"/CC_DENY = \"CN,KR,HK,IN,ID,MY,NG,PK,RU,SA,TW,SY,AE\"/" /etc/csf/csf.confperl -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 blockeverything and then allow through only those connections that you want. This isdone in iptables by DROPPING all connections in and out of the server on allprotocols. Then allow traffic in and out from existing connections. Then openports up in and outgoing for both TCP and UDP individually.

csf command line tips

Block an IP from the server

csf -d csf -d 192.168.0/24

Remove an IP from the blocklist

csf -dr 

Allow an IP

csf -a

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

Search for an IP:

csf -g

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

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= # csf.allow

Allow SSH from one IP:

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

Block traffic to port 80 from a specific range:

tcp|out|d=80|d= #csf.deny

The syntax for the advanced rules:

tcp/udp|in/out|s/d=port|s/d=ip|u=uidtcp/udp  : EITHER tcp OR udp OR icmp protocolin/out   : EITHER incoming OR outgoing connectionss/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 addressu/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.


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

Open/Allow ports

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

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

To allow outgoing ports:

# Allow outgoing TCP portsTCP_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 PINGICMP_IN = "1"# Allow outgoing PINGICMP_OUT = "1"

To block it, change it to "0"

Block certain country's

To block all traffic coming from certain country's:


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 usagespikes or they run longer than a minute.

Enable a Web Management UI

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

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

Send an email when a user logs in via ssh


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

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