linux利用CSF防火墻屏蔽惡意請求
問題
最近不知道為什么,惡意代理的請求數(shù)越來越多,明明我返回的都是403Forbidden,但是由于數(shù)量實在龐大,還是消耗了我大量的帶寬和資源。之前的方法已經(jīng)沒有用了,想了半天還是研究研究防火墻吧,雖然僅僅靠Apache也能對某些IP進(jìn)行黑名單設(shè)置,但是感覺還是有點麻煩的。比如最常見的用iptables,或者是ufw,雖然都能很好的做到管理,但是他們基本都需要一條一條的加,十分麻煩。
網(wǎng)上搜索了下,找到了一個挺方便的小工具–CSF(ConfigServer & Security Firewall),這個工具據(jù)說除了能夠方便的管理IP blacklist,而且也能稍加配置抵御一定量的DDOS攻擊。
安裝
工具本身可以在csf工具的官網(wǎng)上下載。
下載并解壓后可以參考其中的install.txt
的說明進(jìn)行安裝,講的簡潔而且詳細(xì),注意給權(quán)限就行。需要說明的是,這個工具其實也是基于iptables,只是簡化了命令而已。
關(guān)于ddos的防護(hù)
根據(jù)readme.txt的描述,進(jìn)行ddos防護(hù)的功能主要是靠/etc/csf/csf.conf中的配置進(jìn)行控制的,尤其是當(dāng)中的PORTFLOOD參數(shù),一般都進(jìn)行如下設(shè)置:
#Syntax for the PORTFLOOD setting: #PORTFLOOD is a comma separated list of: port;protocol;hit count*;interval seconds #So, a setting of PORTFLOOD = "22;tcp;5;300,80;tcp;20;5" means: #1. If more than 5 connections to tcp port 22 within 300 seconds, then block #that IP address from port 22 for at least 300 seconds after the last packet is #seen, i.e. there must be a "quiet" period of 300 seconds before the block is #lifted #2. If more than 20 connections to tcp port 80 within 5 seconds, then block #that IP address from port 80 for at least 5 seconds after the last packet is #seen, i.e. there must be a "quiet" period of 5 seconds before the block is #lifted
這個可以根據(jù)個人需要修改。
關(guān)于black list
blacklist 就在/etc/csf/csf.deny里,可以有多種書寫方式,在該文件的頂部描述的十分清楚:
############################################################################### # Copyright 2006-2017, Way to the Web Limited # URL: http://www.configserver.com # Email: sales@waytotheweb.com ############################################################################### # The following IP addresses will be blocked in iptables # One IP address per line # CIDR addressing allowed with a quaded IP (e.g. 192.168.254.0/24) # Only list IP addresses, not domain names (they will be ignored) # # Note: If you add the text "do not delete" to the comments of an entry then # DENY_IP_LIMIT will ignore those entries and not remove them # # Advanced port+ip filtering allowed with the following format # tcp/udp|in/out|s/d=port|s/d=ip # # See readme.txt for more information regarding advanced port filtering #
簡要概括就是每一行代表一個ip,也可以代表一個ip段(CIDR),而且我們也可以加注釋,甚至可以指定端口和協(xié)議。
最后,在做出修改后想要生效記得用csf -r
命令。
針對惡意代理請求的防護(hù)方案
當(dāng)然,我用這個的目的是為了根本解決之前的惡意代理占用帶寬的問題。有了這個工具,就可以十分輕松的進(jìn)行控制了,思路如下:
- 首先,搜索Apache的log(/var/log/apache2/access.log),查找所有應(yīng)被屏蔽的log條目(我這里指的是所有被403的請求)。
- 然后,提取每條Log記錄對應(yīng)的ip地址。
- 對結(jié)果進(jìn)行排序去重,生成black list。
- blacklist 寫入csf.deny
- 重啟csf防護(hù)服務(wù)。
實現(xiàn)起來超級簡單:
root@server:~# cat /var/log/apache2/access.log |grep \ 403\ |awk '{print $1}'|sort|uniq >> /etc/csf/csf.deny
可以手動查看下結(jié)果是否正確,確認(rèn)之后既可以csf -r
重啟服務(wù)了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。