关于 ConfigServer Security & Firewall (CSF)

如何在RHEL,CentOS和Fedora中安装CSF开源防火墙_centos

防火墙是增加服务器安全性的非常有用且非常强大的方法。

它可以保护您的服务器免受恶意软件、DDOS、暴力破解、后门、rootkit 和服务器上的本地漏洞的侵害。

CSF(ConfigServer Security & Firewall)是高级防火墙和安全应用程序。它是一个开源应用程序。


安装所需的 CSF 模块

CSF 脚本需要 perl 模块。

[root@linuxhelp ~]# yum install perl-libwww-perl
Loaded plugins: auto-update-debuginfo, fastestmirror, protectbase, refresh-
: packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: epel.mirror.net.in
* epel-debuginfo: epel.mirror.net.in
...
...
...
Installed:
perl-libwww-perl.noarch 0:5.833-2.el6

Complete!


下载 CSF

下载或安装任何新软件时使用 /tmp 目录。Wget 命令用于下载 CSF 脚本。

[root@linuxhelp ~]# cd /tmp/
[root@linuxhelp tmp]# wget http://www.configserver.com/free/csf.tgz
--2016-03-17 09:59:46-- http://www.configserver.com/free/csf.tgz
Resolving www.configserver.com... 109.70.137.78, 2a01:c0:2:22::3
Connecting to www.configserver.com|109.70.137.78|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
...
...
...

100%[======================================> ] 688,544 364K/s in 1.8s

2016-03-17 09:59:49 (364 KB/s) - “ csf.tgz”  saved [688544/688544]


删除现有防火墙

为了避免冲突,必须删除 APF(高级策略防火墙)+BFD(暴力检测)组合。运行以下命令以删除现有防火墙。

[root@linuxhelp tmp]# sh /tmp/csf/remove_apf_bfd.sh
sh: /tmp/csf/remove_apf_bfd.sh: No such file or directory

我之前没有安装APF和BFD,这就是为什么它说没有这样的文件或目录

安装 CSF

tar命令用于切换到新创建的CSF目录。要安装,请运行安装程序脚本。

[root@linuxhelp ~]# cd /tmp/
[root@linuxhelp tmp]# tar -xzf csf.tgz
[root@linuxhelp tmp]# cd csf
[root@linuxhelp csf]# sh install.sh
Selecting installer...

Running csf generic installer

Installing generic csf and lfd

Check we' re running as root
...
...
...
mode of `/etc/init.d/lfd'  retained as 0755 (rwxr-xr-x)
mode of `/etc/init.d/csf'  retained as 0755 (rwxr-xr-x)
`/etc/csf/csfwebmin.tgz'  ->  `/usr/local/csf/csfwebmin.tgz' 

Installation Completed


配置 CSF

上述命令将在测试模式下安装并启动 CSF。要禁用测试模式,请为 CSF 配置TCP_IN、TCP_OUT、UDP_IN 和 UDP_OUT 选项。对文件中的 /etc/csf/csf.conf 进行以下更改。

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

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

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

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

通过将变量 TESTING = 1 更改为 TESTING = 0 TESTING = “ 0” 来禁用测试模式

启动CSF

启用 csf 并加入启动中。

[root@linuxhelp csf]# chkconfig --level 60 csf on
[root@linuxhelp csf]# csf -r
Flushing chain `INPUT' 
Flushing chain `FORWARD' 
Flushing chain `OUTPUT' 
...
...
...
LOCALINPUT all opt -- in !lo out * 0.0.0.0/0 ->  0.0.0.0/0
LOCALOUTPUT all opt in * out !lo ::/0 ->  ::/0
LOCALINPUT all opt in !lo out * ::/0 ->  ::/0

CSF 配置用法和选项

csf.conf :用于控制 CSF 的主配置文件。
csf.deny :防火墙上被拒绝的 IP 和 CIDR 地址列表。
csf.allow :防火墙上允许的 IP 和 CIDR 地址列表。
csf.*ignore :用户、IP 的各种忽略文件列表。
csf.ignore :防火墙上被忽略的 IP 和 CIDR 地址列表。

CSF的命令和选项

选项 -r 用于重新加载所有规则。
选项 -d 用于拒绝 IP 地址
选项 -a 用于允许 IP 地址

# csf -d IP 地址
# csf -a IP 地址
# csf -r

拒绝主机地址

[root@linuxhelp csf]# csf -d 192.168.7.101
Adding 192.168.7.101 to csf.deny and iptables DROP...
DROP all opt -- in !lo out * 192.168.7.101 ->  0.0.0.0/0
LOGDROPOUT all opt -- in * out !lo 0.0.0.0/0 ->  192.168.7.101

[root@linuxhelp csf]# csf -r
Flushing chain `INPUT' 
Flushing chain `FORWARD' 
...
...
....
LOCALOUTPUT all opt in * out !lo ::/0 ->  ::/0
LOCALINPUT all opt in !lo out * ::/0 ->  ::/0


列出规则

[root@linuxhelp csf]# csf -l
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source des
.....
.....
.....
Chain DENYIN (1 references)
num pkts bytes target prot opt in out source destination

1 1 165 DROP all -- !lo * 192.168.7.101 0.0.0.0/0

...
...
...

验证输出

[root@linuxhelp csf]# ping 192.168.7.101
PING 192.168.7.101 (192.168.7.101) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

添加允许列表

[root@linuxhelp csf]# csf -a 192.168.7.101
Removing 192.168.7.101 from csf.deny...
Removing rule...
DROP all opt -- in !lo out * 192.168.7.101 ->  0.0.0.0/0
LOGDROPOUT all opt -- in * out !lo 0.0.0.0/0 ->  192.168.7.101
Adding 192.168.7.101 to csf.allow and iptables ACCEPT...
ACCEPT all opt -- in !lo out * 192.168.7.101 ->  0.0.0.0/0
ACCEPT all opt -- in * out !lo 0.0.0.0/0 ->  192.168.7.101

[root@linuxhelp csf]# csf -r
Flushing chain `INPUT' 
Flushing chain `FORWARD' 
...
...
...

列出规则

[root@linuxhelp csf]# csf -l
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- !lo * 8.8.8.8 0.0.0.0/0 tcp dpt:53

Chain ALLOWOUT (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- * !lo 0.0.0.0/0 192.168.7.101

.....

验证输出

[root@linuxhelp csf]# ping 192.168.7.101
PING 192.168.7.101 (192.168.7.101) 56(84) bytes of data.
64 bytes from 192.168.7.101: icmp_seq=1 ttl=128 time=1.40 ms
64 bytes from 192.168.7.101: icmp_seq=2 ttl=128 time=0.377 ms
64 bytes from 192.168.7.101: icmp_seq=3 ttl=128 time=0.321 ms
64 bytes from 192.168.7.101: icmp_seq=4 ttl=128 time=0.241 ms
64 bytes from 192.168.7.101: icmp_seq=5 ttl=128 time=0.341 ms
............

获取所有选项的列表

键入 CSF 以获取所有选项的列表

[root@linuxhelp csf]# csf
csf: v8.16 (generic)
csf(1)
...
...
...
NAME
csf - ConfigServer &  Security Firewall

SYNOPSIS
csf [OPTIONS]
.....

移除 CSF 防火墙

运行以下脚本,在/etc/csf/uninstall.sh 目录下删除CSF防火墙。

[root@linuxhelp csf]# /etc/csf/uninstall.sh
Uninstalling csf and lfd...

Flushing chain `INPUT' 
Flushing chain `FORWARD' 
Flushing chain `OUTPUT' 
...
...
...
removed directory: `/var/lib/csf/ui' 
removed directory: `/var/lib/csf' 

...Done

此命令将完全擦除所有文件和文件夹的 CSF 防火墙。