当前位置: 移动技术网 > 网络运营>服务器>Linux > linux利用CSF防火墙屏蔽恶意请求

linux利用CSF防火墙屏蔽恶意请求

2019年04月23日  | 移动技术网网络运营  | 我要评论

问题
最近不知道为什么,恶意代理的请求数越来越多,明明我返回的都是403forbidden,但是由于数量实在庞大,还是消耗了我大量的带宽和资源。之前的方法已经没有用了,想了半天还是研究研究防火墙吧,虽然仅仅靠apache也能对某些ip进行黑名单设置,但是感觉还是有点麻烦的。比如最常见的用iptables,或者是ufw,虽然都能很好的做到管理,但是他们基本都需要一条一条的加,十分麻烦。

网上搜索了下,找到了一个挺方便的小工具–csf(configserver & security firewall),这个工具据说除了能够方便的管理ip blacklist,而且也能稍加配置抵御一定量的ddos攻击。

安装
工具本身可以在csf工具的上下载。

下载并解压后可以参考其中的install.txt的说明进行安装,讲的简洁而且详细,注意给权限就行。需要说明的是,这个工具其实也是基于iptables,只是简化了命令而已。

关于ddos的防护

根据readme.txt的描述,进行ddos防护的功能主要是靠/etc/csf/csf.conf中的配置进行控制的,尤其是当中的portflood参数,一般都进行如下设置:

#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

这个可以根据个人需要修改。

关于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),而且我们也可以加注释,甚至可以指定端口和协议。
最后,在做出修改后想要生效记得用csf -r命令。

针对恶意代理请求的防护方案

当然,我用这个的目的是为了根本解决之前的恶意代理占用带宽的问题。有了这个工具,就可以十分轻松的进行控制了,思路如下:

  1. 首先,搜索apache的log(/var/log/apache2/access.log),查找所有应被屏蔽的log条目(我这里指的是所有被403的请求)。
  2. 然后,提取每条log记录对应的ip地址。
  3. 对结果进行排序去重,生成black list。
  4. blacklist 写入csf.deny
  5. 重启csf防护服务。

实现起来超级简单:

复制代码 代码如下:

:~# cat /var/log/apache2/access.log |grep \ 403\ |awk '{print $1}'|sort|uniq >> /etc/csf/csf.deny

可以手动查看下结果是否正确,确认之后既可以csf -r重启服务了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网