Apache 2.4.x ip黑名单

时间:2014-04-18 15:58:56

标签: apache apache2 ip

我正在寻找一种简单的方法来将Apache 2.4.x中的IP地址列入黑名单。我的网站将尝试非法操作的IP地址记录到文本文件中。我想在Apache中使用这个文本文件来拒绝所有访问此ip列表的所有vhosts。什么是最好的方式(最容易和最少资源消耗的方式)? 找到this但这只适用于2.2 ..不确定这是如何适用于2.4 .. 欢呼声。

编辑:这是运行apache x64的Windows x64框

2 个答案:

答案 0 :(得分:11)

@vastlysuperiorman称之为正确,csf / lfd是最好的。不幸的是,它们只能在linux上运行。

This free utility promises to provide the same functionality:动态监控访问尝试并自动阻止IP地址。如果出现误报,您可以使用命令取消阻止。当然值得一试。

另一种方法是创建一个VM(如果您的平台支持虚拟化)部署一个非常小的规范linux盒子,并将其用作代理。这应该很容易实现。顺便说一句,为什么不只是使用linux? ..: - )

(这应该是关于@ vastlysuperiorman的帖子的评论,但我没有足够的SO代表评论其他人的帖子)

Edited to suggest a possible apache 2.4 based solution:

在apache中转换2.2和2.4之间的ACL指令

2.2语法

order Deny,Allow
include conf/IPList.conf
Allow from all

2.4语法

DocumentRoot /some/local/dir

<Directory /some/local/dir/>
   <RequireAll>
      Require all granted
      Include conf/IPList.conf
   </RequireAll>
</Directory>

#this will also work
<Location />
   <RequireAll>
      Require all granted
      Include conf/IPList.conf
   </RequireAll>
</Directory>

# conf/IPLIst.com is actually in /etc/apache2/conf/IPList.conf 
#   (ie, paths are relative to where apache is installed.  
#    I guess you can also use the full path to the list.

在conf / IPList.conf中,您将拥有包含以下条目的单独行

Require not ip 10.10.1.23
Require not ip 192.168.22.199
Require not ip 10.20.70.100

使用mod-rewrite和IP列表进行禁止

  • 要使重定向到另一个页面起作用,您需要将RewriteRule保留在您要保护的基本URL之外。
  • 例如,重定向不能在DocumentRoot上的Directory指令或'/'上的Location指令下工作,因为禁令会影响我们想要显示的状态页面。
  • 因此,最好将其保留在目录或位置指令之外,或链接到另一个未受保护的Web服务器上的状态页。

#Required set of rewrite rules
RewriteEngine on
RewriteMap    hosts-deny  txt:/etc/apache/banned-hosts
RewriteCond   ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond   ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND
RewriteRule   ^  /why-am-i-banned.html

##  inside our banned hosts file, we have:
## /etc/apache2/banned-hosts (maintain the format .. its not just a plain text file)
## 

193.102.180.41 -
192.168.111.45 -
www.example.com -
www.sumwia.net -

# inside our status page, could be html as below or a plain text file with '.txt' extension
#/var/www/html/why-am-i-banned.html
#
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Why is my IP banned?</title>
</head>
<body>
<h2>Why is my IP address banned?</h2>
<p>
To manage spammers and for other security needs, our server automatically blocks      
suspicious IP address.  If however you reckon your IP address has been blocked 
wrongfully, please contact us.
</p>
</body>
</html>

当然,您可以解析日志文件并根据需要填充conf / IPList.conf或/ etc / apache2 / banned-hosts。

作为短期解决方案

允许您使用2.2语法的替代方法是安装mod_access_compat模块并继续使用弃用的2.2样式'Deny,Allow'指令。但这只是一个短期解决方案,因为该模块只是为了帮助转换,并且可能会在未来版本的apache 2.4中消失

答案 1 :(得分:8)

我也没有看到从Apache本身动态阻止访问的好方法。有“hacky”方法:您可以设置一个环境变量来包含IP列表,然后使用带有$ {REMOTE_ADDR}和env函数的模块,但这是一个延伸。 Details on the Expression Parser

但是,我使用了几个有助于保护Apache服务器的轻量级模块。

ConfigServer Firewall(CSF / LFD)是一个很好的Linux系统解决方案。它提供了一种管理iptables的简单方法,可以设置为执行强力检测和阻塞。信息here


编辑: 将以下行添加到/etc/csf/csf.deny以包括您的自定义IP阻止列表:

Include /var/www/example.deny

或者,更新脚本以直接将IP地址附加到csf.deny:

echo $badIP >> /etc/csf/csf.deny

或使用CSF命令行选项(首选):

csf -d 10.20.30.40

CSF自述文件here


mod_security是我最喜欢的Apache / nginx模块之一。它检测危险的GET和POST请求并相应地阻止访问。正确设置后,它将触发CSF阻止经常违反规则的IP地址。详情here