12-10-2014 | Remy van Elst | Text only version of this article
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.
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 -d 192.168.1.2 csf -d 192.168.0/24
csf -dr 192.168.1.2
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.
csf -g 192.168.1.2
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=126.96.36.199/27 # csf.allow
Allow SSH from one IP:
tcp|in|d=22|s=188.8.131.52 # 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.
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.
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"
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"
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.
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.
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"
LF_SSH_EMAIL_ALERT = "1"
The emails are sent to
root by default. Change the below line to change that:
LF_ALERT_TO = "email@example.com"