简体   繁体   English

IP禁止 - 最有效的方式?

[英]IP Banning - most efficient way?

I run a large forum and like everyone else have issues with spammers/bots. 我经营一个大型论坛,像其他人一样有垃圾邮件发送者/机器人的问题。 There are huge lists of known spam IP's that you can download and use in htaccess form, but my only concern is the file size. 有大量已知的垃圾邮件IP列表,您可以下载并以htaccess形式使用,但我唯一关心的是文件大小。 So I suppose the question is how big is too big, given it's going to be loading in for every user. 所以我认为问题是有多大是太大了,因为它将为每个用户加载。 Adding all the IP's in it gets to about 100kb. 添加其中的所有IP可达到约100kb。

Is there an alternative that would have less overhead? 有没有可以减少开销的替代方案? Possibly doing it with php, or will that result in some heavy load too due to file size and checking ips etc? 可能使用php,或者由于文件大小和检查ips等导致一些重负载?

Any advice would be greatly appreciated. 任何建议将不胜感激。

Thanks, 谢谢,

Steve 史蒂夫

There are often more efficient ways than IP bans. 通常比IP禁令更有效。 For example, hidden fields in a form only bots will fill out, or requiring javascript or cookies for submitting forms. 例如,仅填写表单中的隐藏字段将填写,或者需要javascript或cookie来提交表单。

For IP banning, I wouldn't use .htaccess files. 对于IP禁止,我不会使用.htaccess文件。 Depending on your webserver it may read the htaccess files for each request. 根据您的Web服务器,它可能会读取每个请求的htaccess文件。 I'd definitely add the IP-bans into your webservers vhost configuration instead. 我肯定会将IP-bans添加到您的webservers vhost配置中。 That way I'd be sure the webserver will keep it in RAM and not read it again and again. 这样我就可以确定网络服务器会将它保存在RAM中而不是一次又一次地读取它。

Doing it via PHP would also be an option. 通过PHP完成它也是一种选择。 This way, you could also easily limit the bans to forms, like registration in your forum. 这样,您也可以轻松地将禁令限制为表单,例如在论坛中注册。

There are a few options: 有几个选择:

  • You can store the block list into the database. 您可以将阻止列表存储到数据库中。 It's more effecient to query there than with a loop in PHP. 在那里查询比在PHP中使用循环更有效。
  • You could pre-process the list with array_map(ip2long()) to save memory and possibly lookup time. 您可以使用array_map(ip2long())预处理列表以节省内存和可能的查找时间。
  • You could package the IP list into a regular expression, maybe run it though an optimizer (Perl Regexp::Optimizer). 您可以将IP列表打包成正则表达式,也可以通过优化器(Perl Regexp :: Optimizer)运行它。 PCRE testing would again be faster than a foreach and strpos tests. PCRE测试再次比foreach和strpos测试更快。 $regex = implode("|", array_map("preg_quote", file("ip.txt")));

But then, IP block lists are not often very reliable. 但是,IP阻止列表通常不太可靠。 Maybe you should implement the other two workarounds: hidden form fields to detect dumb bots. 也许你应该实现另外两个解决方法:隐藏表单字段来检测哑巴机器人。 Or captchas to block non-humans (not very user-friendly, but solves the problem). 或者用于阻止非人类的验证码(不是非常用户友好,但解决了问题)。

In .htaccess in your DocumentRoot, after: 在DocumentRoot中的.htaccess中,之后:

Order Deny,Allow

Append a line: 附加一行:

Deny from <black ip>

Well, you are building a database of addresses, right? 好吧,你正在建立一个地址数据库 ,对吗? Wouldn't it be useful to use a database product for it? 使用数据库产品不是很有用吗? If you don't have any yet, SQLite could be up to the task. 如果你还没有,SQLite可以胜任这项任务。

maybe you want to stop spam the good-ole-fashioned-way - Captcha ? 也许你想以优秀的方式阻止垃圾邮件 - Captcha

I believe that a Mr. Albert Einstein once said: Problems cannot be solved at the same level of awareness that created them :) 我相信阿尔伯特爱因斯坦先生曾经说过:问题无法在创造它们的同一意识水平上解决:)

Unless you already have problems with the load on your server you will probably not notice the difference from a 100K .htaccess file. 除非您的服务器上的负载已经存在问题,否则您可能不会注意到与100K .htaccess文件的区别。 There may be faster alternatives, perhaps including the use of iptables or the use of sorted ip lists that can be searched faster for matches, or even the use of a database (though the overhead of a single database query might crush the benefit of indexed tables) but it is probably not worth the effort unless you run a forum with high loads. 可能有更快的替代方案,可能包括使用iptables或使用排序的ip列表,可以更快地搜索匹配,甚至使用数据库(尽管单个数据库查询的开销可能会破坏索引表的好处) )但除非你运行一个高负荷的论坛,否则它可能不值得。

You can alternatively try to use captcha's or similar. 您也可以尝试使用验证码或类似的。 Everything in this direction comes at an expense and nothing is 100% reliable. 这个方向的一切都需要付出代价,没有什么是100%可靠的。

Don't use such IP lists. 不要使用此类IP列表。 They're likely to get outdated and you might block the wrong requests. 他们可能会过时,你可能会阻止错误的请求。 Just invest in good or better captchas and only block IPs from time to time if they're really doing some kind of denial of service attack. 如果他们真的在进行某种拒绝服务攻击,只需投资优质或更好的验证码,并且不时只阻止IP。

Why force the webserver to handle blocking users? 为什么强制Web服务器处理阻塞用户? I'd suggest using null routes (as using iptables will slow your server down if the amount of blocked IP entries grows). 我建议使用空路由(因为如果阻塞的IP条目数量增加,使用iptables会降低服务器速度)。

Read up on http://www.cyberciti.biz/tips/how-do-i-drop-or-block-attackers-ip-with-null-routes.html 阅读http://www.cyberciti.biz/tips/how-do-i-drop-or-block-attackers-ip-with-null-routes.html

http://php.net/manual/en/function.shell-exec.php http://php.net/manual/en/function.shell-exec.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM