无法阻止对 nginx 服务器的攻击

无法阻止对 nginx 服务器的攻击

我目前正在努力解决我的 Digitalocean droplet(Ubuntu 22.10)的问题,它正遭受某种攻击(可能是 DDOS)。该服务器托管一个在 nginx 上运行的容器化应用程序。

每次我访问 Droplet 控制台时,我都能看到数百行如下所示的内容:

nginx_1       | 2024/02/06 10:14:20 [error] 8#8: *1425 access forbidden by rule, client: 94.130.56.244, server: **********, request: "GET /.well-known/acme-challenge/EQAFJzQBOu70iOKYwkrcDyvxQFuf8QgkeOMFyf5_LJs HTTP/1.1", host: "www.mesoskinline24.fr"

如您所见,没有真正的安全问题,因为我已插入一条拒绝访问该特定 IP 的规则。我还禁用了访问日志以避免内存泄漏。尽管这不再是一个安全问题,但看到服务器控制台充斥着我刚刚展示的行,还是很烦人的。

迄今尝试的对策:

  • sudo ufw deny from 94.130.56.244使用 ufw 来阻止对 droplet + 的访问sudo ufw reload,不起作用。
  • 直接使用 IPTABLE 命令:iptables -A INPUT -s 94.130.56.244 -j DROP,不起作用。
  • 试图阻止 nginx 路由/.well-known/acme-challenge/登录控制台,但我并没有找到这样做的方法。

提前感谢您的帮助!

答案1

你可以在 nginx jail 中安装 fail2ban,它会自动扫描日志文件并阻止某些流量

安装 fail2ban 后,您可以在 /etc/fail2ban/jail.d/nginx.conf 中创建一个 jail(例如)

在这个文件中你把这个

[nginx-http-auth]
enabled = true
mode = normal
port    = http,https
logpath = %(nginx_error_log)s

你也可以把这个放进去,但你需要要使用“nginx-limit-req”jail,您应该拥有ngx_http_limit_req_module并定义limit_reqlimit_req_zone如 nginx 文档中所述

[nginx-limit-req]
enabled = true 
port    = http,https
logpath = %(nginx_error_log)s

在流量过大(包括 DDoS 攻击)的情况下,此功能非常有用


下面是一个 PowerShell 脚本,用于阻止 droplet 防火墙中的特定 IP

# Set your DigitalOcean API token
$token = "YOUR_DIGITALOCEAN_API_TOKEN"

# Set the ID of the firewall you want to modify
$firewallId = "YOUR_FIREWALL_ID"

# Set the IP address you want to block
$ipToBlock = "IP_ADDRESS_TO_BLOCK"

# Define the API endpoint URL
$endpoint = "https://api.digitalocean.com/v2/firewalls/$firewallId"

# Define the JSON payload to add a new inbound rule blocking the specified IP address
$jsonPayload = @{
    inbound_rules = @(
        @{
            protocol = "tcp"
            ports = ""
            sources = @{
                addresses = @($ipToBlock)
            }
        }
    )
} | ConvertTo-Json

# Make the API request to update the firewall
$response = Invoke-RestMethod -Uri $endpoint -Method Put -Headers @{Authorization = "Bearer $token"} -ContentType "application/json" -Body $jsonPayload

# Check if the request was successful
if ($response) {
    Write-Host "IP address $ipToBlock has been blocked in the firewall."
} else {
    Write-Host "Failed to block IP address $ipToBlock in the firewall."
}

如果你不想让它到达 nginx 服务器,我遇到了一个 docker-waf,你可以把它放在你的 web 服务器前面,并使用 Modsecurity 和 owasp modsecurity crs 规则一起阻止恶意流量 https://github.com/theonemule/docker-waf

OWASP Modsecurity CRS 中还有 Dos 保护。

可选的 DoS 保护,防止客户端请求过快。

#
# When a client is making more than 100 requests (excluding static files) within
# 60 seconds, this is considered a 'burst'. After two bursts, the client is
# blocked for 600 seconds.

相关内容