我试图阻止从同一个 ip 到端点的连接,我正在为 100 个连接执行此操作,但这行为很奇怪
当我第一次尝试从邮递员到达我的端点 {{URL}}/connect/token 时,我能够尽可能多地连接到端点(例如甚至 300 次),第二次时我到达了相同的端点,得到了 403,这是正确的,不知道是什么导致了这种奇怪的行为
非常感谢任何帮助
resource "aws_wafv2_regex_pattern_set" "url_pattern" {
name = "url_pattern-regex"
description = "A regex that matches Account api Token and Authorize endpoints"
scope = "REGIONAL"
regular_expression {
regex_string = "/connect/token"
}
regular_expression {
regex_string = "/connect/authorize"
}
}
locals {
name = "${var.environment}-${var.stack}-acl"
}
resource "aws_wafv2_web_acl" "x-account-acl" {
name = local.name
description = "rate based statement."
scope = "REGIONAL"
default_action {
allow {}
}
rule {
name = "rule-1"
priority = 1
action {
count {}
}
statement {
rate_based_statement {
aggregate_key_type = "IP" //Count number of calls from IP
limit = 100
scope_down_statement {
regex_pattern_set_reference_statement {
arn = aws_wafv2_regex_pattern_set.url_pattern.arn
field_to_match {
uri_path {}
}
text_transformation {
priority = 1
type = "NONE"
}
}
}
}
}
visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "friendly-rule-metric-name"
sampled_requests_enabled = false
}
}
visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "friendly-metric-name"
sampled_requests_enabled = false
}
}
resource "aws_wafv2_web_acl_association" "web_acl_association_loadbalancer" {
resource_arn = module.alb.alb_arn
web_acl_arn = aws_wafv2_web_acl.x-account-acl.arn
}
我的邮递员代码:
const postRequest = {
url: pm.environment.get("URL") + '/connect/token',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
mode: 'urlencoded',
urlencoded:
[
{key: "client_id",value: pm.environment.get("Client_Id_userApi")},
{key: "grant_type",value: "client_credentials"},
{key: "client_secret",value: pm.environment.get("Client_Secret_userApi")},
]
}
};
for (let i = 0; i < 300; i++) {
pm.sendRequest(postRequest, (error, response) => {
if (error) {
console.log(error,i);
}
});
}
对我来说,代码或配置似乎没问题,但这似乎是一种奇怪的行为,
AWS WAF 速率限制通过可配置的 5 分钟滑动窗口运行,评估期每隔一段时间一次。根据 AWS 文档,应该在 30 秒左右
我们来假设一个场景
AWS WAF 速率限制每 30 秒进行一次评估,具有 5 分钟滚动窗口和 100 个请求速率限制配置
时间过去了 | 请求发送聚合 | 行动 |
---|---|---|
0 | 0 | 允许 |
10 | 1000 | 允许 |
20 | 5000 | 允许 |
29 | 10000 | 允许 |
30 | 10001 | 阻止 |
60 | 15000 | 阻止 |
120 | 15000 | 阻止 |
300 | 15000 | 阻止 |
330 | 15000 | 阻止 |
360 | 150001 | 阻止 |
361 | 150002 | 允许 |
只要您在过去5分钟内的请求不低于阈值,您将不被允许重新进入,并且每30秒会有一次评估延迟
将其想象为与此实现类似的东西 https://aws.amazon.com/solutions/implementations/security-automations-for-aws-waf/
您有一个 Lambda + Athena 每 30 秒执行一次查询,回顾过去 5 分钟的数据,然后更新要阻止的 ip 上的 ipset
理论上,您可以在 30 秒窗口结束之前发送一万亿个请求,然后在接下来的 5 分钟内被阻止
参考资料:
每次 AWS WAF 估计请求率时,AWS WAF 都会回顾配置的评估时段内传入的请求数。由于这一因素以及传播延迟等其他因素,在 AWS WAF 检测到请求并对其进行速率限制之前,请求可能会在长达几分钟内以过高的速率传入。相似地。在 AWS WAF 检测到下降并停止速率限制操作之前,请求速率可能会低于限制一段时间。 通常,此延迟低于 30 秒。
当 5 分钟滑动窗口中收到的请求数量超过您定义的阈值时,AWS WAF 会自动阻止不良参与者的 IP 地址