問題
最近不知道為什么,惡意代理的請求數(shù)越來越多,明明我返回的都是403Forbidden,但是由于數(shù)量實在龐大,還是消耗了我大量的帶寬和資源。之前的方法已經(jīng)沒有用了,想了半天還是研究研究防火墻吧,雖然僅僅靠Apache也能對某些IP進行黑名單設(shè)置,但是感覺還是有點麻煩的。比如最常見的用iptables,或者是ufw,雖然都能很好的做到管理,但是他們基本都需要一條一條的加,十分麻煩。
網(wǎng)上搜索了下,找到了一個挺方便的小工具–CSF(ConfigServer & Security Firewall),這個工具據(jù)說除了能夠方便的管理IP blacklist,而且也能稍加配置抵御一定量的DDOS攻擊。
安裝
工具本身可以在csf工具的官網(wǎng)上下載。
下載并解壓后可以參考其中的install.txt
的說明進行安裝,講的簡潔而且詳細,注意給權(quán)限就行。需要說明的是,這個工具其實也是基于iptables,只是簡化了命令而已。
關(guān)于ddos的防護
根據(jù)readme.txt的描述,進行ddos防護的功能主要是靠/etc/csf/csf.conf中的配置進行控制的,尤其是當中的PORTFLOOD參數(shù),一般都進行如下設(shè)置:
1
2
3
4
5
6
7
8
9
10
11
12
|
#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里,可以有多種書寫方式,在該文件的頂部描述的十分清楚:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
############################################################################### # Copyright 2006-2017, Way to the Web Limited # URL: http://www.configserver.com # Email: [email protected] ############################################################################### # 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
命令。
針對惡意代理請求的防護方案
當然,我用這個的目的是為了根本解決之前的惡意代理占用帶寬的問題。有了這個工具,就可以十分輕松的進行控制了,思路如下:
- 首先,搜索Apache的log(/var/log/apache2/access.log),查找所有應(yīng)被屏蔽的log條目(我這里指的是所有被403的請求)。
- 然后,提取每條Log記錄對應(yīng)的ip地址。
- 對結(jié)果進行排序去重,生成black list。
- blacklist 寫入csf.deny
- 重啟csf防護服務(wù)。
實現(xiàn)起來超級簡單:
root@server:~# cat /var/log/apache2/access.log |grep \ 403\ |awk '{print $1}'|sort|uniq >> /etc/csf/csf.deny
可以手動查看下結(jié)果是否正確,確認之后既可以csf -r
重啟服務(wù)了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.mythsman.com/2017/03/20/1/