实施 Web 请求速率限制算法的最佳方法是什么?
可能/部分重复:
我我正在寻找为 Web 应用程序实现移动时间窗口速率限制算法的最佳方法,以减少垃圾邮件或暴力攻击。
使用示例为“过去 5 分钟内给定 IP 的最大失败登录尝试次数”、“过去 N 分钟内的最大数量(帖子/投票/等...)”。
我更喜欢使用移动时间窗口算法,而不是每 X 分钟硬重置一次统计数据(如 twitter api)。
这适用于 C#/ASP.Net 应用程序。
Possible/partial duplicates:
- What’s a good rate limiting algorithm?
- Throttling method calls to M requests in N seconds
- Best way to implement request throttling in ASP.NET MVC?
I am looking for the best way to implement a moving time window rate limiting algorithm for a web application to reduce spam or brute force attacks.
Examples of use would be "Maximum number of failed login attempts from a given IP in the last 5 minutes", "Maximum number of (posts/votes/etc...) in the last N minutes".
I would prefer to use a moving time window algorithm, rather than a hard reset of statistics every X minutes (like twitter api).
This would be for a C#/ASP.Net app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我刚刚添加了问题的答案 如果 API 速率超出限制,则阻止 API 请求 5 分钟。
我使用 HttpRuntime.Cache 每分钟只允许 60 个请求。超过限制将在接下来的 5 分钟内阻止 API。
I just added the answer to the question Block API requests for 5 mins if API rate limit exceeds.
I used
HttpRuntime.Cache
to allow only 60 requests per minute. Exceeding the limit will block the API for next 5 minutes.我一直在研究一种新的基于 redis 的速率限制方法: http://blog.jnbrymn.com/2021/03/18/estimated-average-recent-request-rate-limiter.html
它比我的许多其他方法更简单我们已经看到,它不需要您不断创建新的 Redis 密钥(例如,不是每个用户每分钟窗口一个,而是每个用户一个)。它具有一些关于“健忘和宽恕”的良好特性,例如,施虐者无法在下一分钟内再次犯罪。它还有一个很好的解释,因为速率限制器的状态对应于用户最近请求速率的估计。
I have been working on a new redis-based rate-limiting approach: http://blog.jnbrymn.com/2021/03/18/estimated-average-recent-request-rate-limiter.html
It is simpler than many other approaches that I've seen in that it doesn't require you to constantly create new redis keys (e.g. instead of one per user per minute window, it's just one per user). It has some nice properties regarding "forgetfulness and forgiveness" so that, for example, abusive users can't reoffend in the next minute window. It also has a nice interpretation as the state of the rate-limiter corresponds to an estimate of the user's recent request rate.
我们发现令牌桶对于这种速率限制是更好的算法。它广泛应用于路由器/交换机中,因此我们的操作人员对这个概念更加熟悉。
We found out Token Bucket is better algorithm for this kind of rate-limiting. It's widely used in routers/switches so our operation folks are more familiar with the concept.
只是为这个问题添加一个更“现代”的答案:对于.NET WebAPI, WebApiThrottle 非常好,可能开箱即用,满足您的所有需求。
它还在 NuGet 上提供。
实施只需一分钟左右,并且高度可定制:
Just to add a more 'modern' answer to this problem: For .NET WebAPI, WebApiThrottle is excellent and probably does everything you want out of the box.
It's also available on NuGet.
Implementation takes only a minute or so and it's highly customisable:
使用基于内存的快速哈希表,例如 memcached。密钥将是您要限制的目标(例如IP),每个存储值的到期时间应该是最大限制时间。
为每个键存储的值将包含他们在执行操作时所做的最后 N 次尝试的序列化列表,以及每次尝试的时间。
Use a fast memory-based hashtable like memcached. The keys will be the target you are limiting (e.g. an IP) and the expiration of each stored value should be the maximum limitation time.
The values stored for each key will contain a serialized list of the last N attempts they made at performing the action, along with the time for each attempt.
您发现此页面很有趣:
http://www.codeproject.com/ KB/aspnet/10ASPNetPerformance.aspx
需要注意的部分如下所示:
编辑:这里有类似的问题:
实现请求限制的最佳方法在 ASP.NET MVC 中?
You find this page to be an interesting read:
http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx
The section to look out for starts as follows:
EDIT: Similar question here:
Best way to implement request throttling in ASP.NET MVC?