CSF即(ConfigServer Security & Firewall)是一款優秀的Linux服務器防火牆軟件,是基於狀態包檢測(SPI,state packet inspection)的iptables防火牆,登陸/入侵檢測和安全的linux服務器的應用程序。具有全面、直接、方便,靈活配置。具有漏洞檢測、ip阻止、賬戶修改跟蹤、IDS(入侵檢測系統)、安全檢查等一系列功能。並且提供cPanel、DirectAdmin和Webmin面板的管理界面,功能強大,使用簡單。
一.下載解壓並安裝
[root@lhz ~]#wget https://download.configserver.com/csf.tgz
[root@lhz ~]# tar -zxvf csf.tgz
[root@lhz csf]# sh install.sh
安裝perl模塊
[root@lhz csf]#yum install perl-libwww-perl perl
測試一下csf iptable模塊:
[root@lhz csf]# perl /etc/csf/csftest.pl
Testing ip_tables/iptable_filter...OK
Testing ipt_LOG...OK
Testing ipt_multiport/xt_multiport...OK
Testing ipt_REJECT...OK
Testing ipt_state/xt_state...OK
Testing ipt_limit/xt_limit...OK
Testing ipt_recent...OK
Testing xt_connlimit...OK
Testing ipt_owner/xt_owner...OK
Testing iptable_nat/ipt_REDIRECT...OK
Testing iptable_nat/ipt_DNAT...OK
RESULT: csf should function on this server
二.配置csf
配置文件里面,可配置的東西很多,基本配置就不說了,網上有。在這里說一下,怎么配置防止少量的ddos,cc攻擊
1.端口洪水攻擊保護
[root@lhz csf]# vim /etc/csf/csf.conf
PORTFLOOD = "22;tcp;5;300,80;tcp;20;5"
解釋:
1),如果300秒內有5個以上連接到tcp端口22的連接,則至少在發現最后一個數據包300秒后阻止該IP地址訪問端口22,即在該阻止被取消前有300秒的"安靜" 期。
2), 如果5秒內有20個以上連接到tcp端口80的連接,則至少在發現最后一個數據包5秒后阻止該IP地址訪問端口80,即在該阻止被取消前有5秒的"安靜" 期.
給我感覺是,csf不光有一面牆,牆后面,還有一張網,動態防御。感覺這一點做的比較好。
2.啟動csf
[root@lhz ~]# /etc/init.d/csf start
Starting csf:Flushing chain `INPUT'
Flushing chain `FORWARD'
Flushing chain `OUTPUT'
Flushing chain `PREROUTING'
Flushing chain `POSTROUTING'
Flushing chain `OUTPUT'
Flushing chain `INPUT'
Flushing chain `FORWARD'
Flushing chain `OUTPUT'
csf: FASTSTART loading DROP no logging (IPv4)
csf: FASTSTART loading DROP no logging (IPv6)
LOG tcp opt -- in * out * 0.0.0.0/0 -> 0.0.0.0/0 limit: avg 30/min burst 5 LOG flags 0 level 4 prefix `Firewall: *TCP_IN Blocked* '
LOG tcp opt -- in * out * 0.0.0.0/0 -> 0.0.0.0/0 tcp flags:0x17/0x02 limit: avg 30/min burst 5 LOG flags 8 level 4 prefix `Firewall: *TCP_OUT Blocked* '
。。。。。。。。。。。
啟動時打印出很多信息,查看一下,有沒有fatal,以及warning,如果沒有就ok.
[root@lhz ~]# vim /etc/csf/csf.conf
TESTING = "0" //TESTING由1改為0
重啟一下csf,[root@rudder ~]# csf -r,重啟命令都和apf是一樣的。二個基於iptables的防火牆,真的有很多地方相似。
3.啟動lfd
[root@lhz ~]# /etc/init.d/lfd start
Starting lfd: Done
lfd這個模塊,有一個很重要的功能,就是記錄下防御的過程。
192.168.1.100被阻止四次后,被永久禁止訪問了。然后我查了一下,這個IP自動被放到了csf.deny下面去了。
從官網上找了一些參數說明:
-h, --help Show this message //顯示此消息
-l, --status List/Show iptables configuration //列出/顯示iptables配置
-l6, --status6 List/Show ip6tables configuration //列出/顯示ip6ables配置
-s, --start Start firewall rules //啟用防火牆規則
-f, --stop Flush/Stop firewall rules (Note: lfd may restart csf) //清除/停止防火牆規則(注意:lfd可能重新啟動csf)
-r, --restart Restart firewall rules //重新啟用防火牆規則
-q, --startq Quick restart (csf restarted by lfd) //快速重啟(lfd重啟csf)
-sf, --startf Force CLI restart regardless of LF_QUICKSTART setting //不顧 LF_QUICKSTART設置,強制CLI重新啟動
-a, --add ip Allow an IP and add to /etc/csf/csf.allow //允許一個IP並添加至/etc/csf/csf.allow
-ar, --addrm ip Remove an IP from /etc/csf/csf.allow and delete rule //從/etc/csf/csf.allow 刪除一個IP,刪除規則
-d, --deny ip Deny an IP and add to /etc/csf/csf.deny //拒絕一個IP並添加至/etc/csf/csf.deny
-dr, --denyrm ip Unblock an IP and remove from /etc/csf/csf.deny //解除對一個IP的阻止並從/etc/csf/csf.deny里刪除
-df, --denyf Remove and unblock all entries in /etc/csf/csf.deny //刪除並解除對/etc/csf/csf.deny里所有記錄的阻止
-g, --grep ip Search the iptables rules for an IP match (incl. CIDR) //查詢與某IP匹配的iptables規則(包括 CIDR)
-t, --temp Displays the current list of temp IP entries and their //TTL顯示當前臨時IP及其TTL的列表
-tr, --temprm ip Remove an IPs from the temp IP ban and allow list //從臨時禁止和允許IP列表刪除IPs
-td, --tempdeny ip ttl [-p port] [-d direction]
Add an IP to the temp IP ban list. ttl is how long to //添加一個IP至臨時禁止IP列表,
blocks for (default:seconds, can use one suffix of h/m/d) //ttl是指端口的阻止時間(默認:秒,可以使用一個h/m/d后綴)
Optional port. Optional direction of block can be one of: //可選端口。阻止方向可以是以下任意一種:進入,傳出或進出(默認:進入)
in, out or inout (default:in)
-ta, --tempallow ip ttl [-p port] [-d direction]
Add an IP to the temp IP allow list (default:inout) //添加一個IP至臨時允許IP列表(默認:進出)
-tf, --tempf Flush all IPs from the temp IP entries //清除所有臨時IP記錄
-cp, --cping PING all members in an lfd Cluster PINGlfd群的所有成員
-cd, --cdeny ip Deny an IP in a Cluster and add to /etc/csf/csf.deny //拒絕群里的某個IP,並添加到/etc/csf/csf.deny
-ca, --callow ip Allow an IP in a Cluster and add to /etc/csf/csf.allow //允許群里的某個IP,並添加到/etc/csf/csf.allow
-cr, --crm ip Unblock an IP in a Cluster and remove from /etc/csf/csf.deny //解除對群里某個IP的阻止,並從/etc/csf/csf.deny 刪除
-cc, --cconfig [name] [value]
Change configuration option [name] to [value] in a Cluster //將群里的配置選項[name]改為[value]
-cf, --cfile [file] Send [file] in a Cluster to /etc/csf/ //在群里發送[file]至/etc/csf/
-crs, --crestart Cluster restart csf and lfd //重新啟動群csf和lfd
-m, --mail [addr] Display Server Check in HTML or email to [addr] if present //在HTML顯示服務器檢查或發送郵件至[addr]地址,如果存在的話
-c, --check Check for updates to csf but do not upgrade //檢查csf更新但不更新
-u, --update Check for updates to csf and upgrade if available //檢查csf更新並更新,如果可以的話
-uf Force an update of csf //強制更新csf
-x, --disable Disable csf and lfd //禁用csf和lfd
-e, --enable Enable csf and lfd if previously disabled //啟用之前禁用的csf和lfd
-v, --version Show csf version //顯示csf版本
您可以通過這些選項方便快捷地控制和查看csf。所有的csf配置文件都在/etc/csf/ 里,包括:
csf.conf - 主要配置文件,它有說明每個選項用途的注釋
csf.allow - 防火牆始終允許通過的IP和CIDR地址列表
csf.deny - 防火牆始終不允許通過的IP和CIDR地址列表
csf.ignore- lfd應忽略,並且發現后不阻止的IP和CIDR地址列表
csf.*ignore- 列出了lfd應忽略的文件,用戶,IP地址的各種文件。具體參見每個文件。
如果修改上述任何文件,您要重新啟動csf才能生效。如果您使用命令行選項添加或拒絕IP地址,csf會自動生效。
csf.allow 和csf.deny都可以在列出的IP地址后做評論。該評論必須和IP地址在同一行,否則csf.deny的IP輪換會將其刪除。
如果直接編輯the csf.allow或csf.deny 文件,不論是從shell或WHM UI,您都要在IP地址與評論之間插入#,如下:
添加 11.22.33.44 # 因為我不喜歡它們
您也可以在使用the csf -a或csf -d命令時添加評論,不過不是插入 # ,而是:
添加csf -d 11.22.33.44 因為我不喜歡它們
三.csf 針對某一ip 開放 單個 多個端口
csf是一個其於iptables的防火牆,在/etc/csf/csf.conf中可以開放端口,但是在這里開放的端口,是所有IP都可以訪問的。如果想針對某一ip開放某一端口,或者多個端口怎么辦呢。
修改csf.allow文件,添加以下內容
[root@lhz ~]# vim /etc/csf/csf.allow
1,開放單個端口
tcp|in|d=3306|s=192.168.0.101
tcp|out|d=3306|d=192.168.0.101
2,開放多個端口
tcp|in|d=10000_10010|s=192.168.0.101
tcp|out|d=10000_10010|d=192.168.0.101
3,對局域網開放
udp|in|d=123|s=192.168.0.0/24
udp|out|d=123|d=192.168.0.0/24
4,重啟csf
[root@lhz ~# csf -r
四.csf ip 端口 映射
1.DNAT用法:
IPx|*|IPy|*|tcp/udp //IPX映射IPY
IPx|portA|IPy|portB|tcp/udp //IPX 端口portA 映射到ipY 端口portB
例子:
192.168.0.101|*|10.0.0.1|*|tcp
192.168.0.101|666|10.0.0.1|25|tcp
2.REDIRECT
IPx|portA|*|portB|tcp/udp //IPX的portA映射到portB
*|portA|*|portB|tcp/udp //portA映射到portB
例子
*|666|*|25|tcp
192.168.0.102|666|*|25|tcp
192.168.0.103|666|*|25|tcp