Configserver Firewall and Security (CSF and LFD)
Published: 12-10-2014 | Author: Remy van Elst | Text only version of this article
❗ This post is over seven years old. It may no longer be up to date. Opinions may have changed.
Table of Contents
- CSF Installation
- csf principle
- csf command line tips
- csf.conf highlights
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.
I'm developing a desktop monitoring app, Leaf Node Monitoring, open source, but paid. For Windows, Linux & Android, go check it out.
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.
On CentOS systems, first install some required Perl packages:
yum -y install perl-libwww-perl.noarch perl-Time-HiRes
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
Done. Do note that the IP you installed from has been added to the allow list.
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
Disable csf and lfd
Enable csf and lfd
Mail yourself a general security check of the server
csf -m firstname.lastname@example.org
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=184.108.40.206/27 # csf.allow
Allow SSH from one IP:
tcp|in|d=22|s=220.127.116.11 # csf.allow
Block traffic to port 80 from a specific range:
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
/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
lfd will also block IP addresses listed in the
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.
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:
Tags: articles , centos , cpanel , csf , debian , directadmin , firewall , iptables , ubuntu
LF_ALERT_TO = "email@example.com"