实施 Web 请求速率限制算法的最佳方法是什么?

发布于 2024-08-05 09:55:09 字数 651 浏览 9 评论 0原文

可能/部分重复:

我我正在寻找为 Web 应用程序实现移动时间窗口速率限制算法的最佳方法,以减少垃圾邮件或暴力攻击。

使用示例为“过去 5 分钟内给定 IP 的最大失败登录尝试次数”、“过去 N 分钟内的最大数量(帖子/投票/等...)”。

我更喜欢使用移动时间窗口算法,而不是每 X 分钟硬重置一次统计数据(如 twitter api)。

这适用于 C#/ASP.Net 应用程序。

Possible/partial duplicates:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我是男神闪亮亮 2024-08-12 09:55:10

我刚刚添加了问题的答案 如果 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.

固执像三岁 2024-08-12 09:55:10

我一直在研究一种新的基于 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.

迷鸟归林 2024-08-12 09:55:09

我们发现令牌桶对于这种速率限制是更好的算法。它广泛应用于路由器/交换机中,因此我们的操作人员对这个概念更加熟悉。

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.

咋地 2024-08-12 09:55:09

只是为这个问题添加一个更“现代”的答案:对于.NET WebAPI, WebApiThrottle 非常好,可能开箱即用,满足您的所有需求。

它还在 NuGet 上提供

实施只需一分钟左右,并且高度可定制:

config.MessageHandlers.Add(new ThrottlingHandler()
{
    Policy = new ThrottlePolicy(perSecond: 1, perMinute: 30, perHour: 500, perDay:2000)
    {
        IpThrottling = true,
        ClientThrottling = true,
        EndpointThrottling = true
    },
    Repository = new CacheRepository()
});

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:

config.MessageHandlers.Add(new ThrottlingHandler()
{
    Policy = new ThrottlePolicy(perSecond: 1, perMinute: 30, perHour: 500, perDay:2000)
    {
        IpThrottling = true,
        ClientThrottling = true,
        EndpointThrottling = true
    },
    Repository = new CacheRepository()
});
乄_柒ぐ汐 2024-08-12 09:55:09

使用基于内存的快速哈希表,例如 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.

伴梦长久 2024-08-12 09:55:09

您发现此页面很有趣:

http://www.codeproject.com/ KB/aspnet/10ASPNetPerformance.aspx

需要注意的部分如下所示:

防止拒绝服务 (DOS) 攻击

Web 服务对于黑客来说是最有吸引力的目标,因为即使是学龄前的黑客也可以通过重复调用执行昂贵工作的 Web 服务来瘫痪服务器。

编辑:这里有类似的问题:

实现请求限制的最佳方法在 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:

Prevent Denial of Service (DOS) Attack

Web services are the most attractive target for hackers because even a pre-school hacker can bring down a server by repeatedly calling a Web service which does expensive work.

EDIT: Similar question here:

Best way to implement request throttling in ASP.NET MVC?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文