iptables 规则来对抗最常见的 DoS 攻击?

alf*_*ish 7 iptables ddos

最近我收到了很多小规模的 DoS 攻击。我想知道我应该使用哪些 iptables 规则来应对最常见的 DoS 攻击,并通常保护我的 Web 服务器。

Web 服务器运行 Varnish -> nginx/php5 -> memcached -> mysql

我尝试了一些通用的收据,但它们也阻止了对我位于远程服务器上的数据库服务器的访问,所以我只是刷新了建议的规则,现在当我在 iptables 上只看到 fail2ban 时,感觉有点徒劳和勇敢。

因此,感谢您阻止最常见攻击媒介的规则。

Bar*_*Vos 20

以下是我使用的一些规则:

# Reject spoofed packets
iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -s 169.254.0.0/16 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 127.0.0.0/8 -j DROP

iptables -A INPUT -s 224.0.0.0/4 -j DROP
iptables -A INPUT -d 224.0.0.0/4 -j DROP
iptables -A INPUT -s 240.0.0.0/5 -j DROP
iptables -A INPUT -d 240.0.0.0/5 -j DROP
iptables -A INPUT -s 0.0.0.0/8 -j DROP
iptables -A INPUT -d 0.0.0.0/8 -j DROP
iptables -A INPUT -d 239.255.255.0/24 -j DROP
iptables -A INPUT -d 255.255.255.255 -j DROP

# Stop smurf attacks
iptables -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j DROP
iptables -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j DROP
iptables -A INPUT -p icmp -m icmp -j DROP

# Drop all invalid packets
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
iptables -A OUTPUT -m state --state INVALID -j DROP

# Drop excessive RST packets to avoid smurf attacks
iptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT

# Attempt to block portscans
# Anyone who tried to portscan us is locked out for an entire day.
iptables -A INPUT   -m recent --name portscan --rcheck --seconds 86400 -j DROP
iptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP

# Once the day has passed, remove them from the portscan list
iptables -A INPUT   -m recent --name portscan --remove
iptables -A FORWARD -m recent --name portscan --remove

# These rules add scanners to the portscan list, and log the attempt.
iptables -A INPUT   -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "Portscan:"
iptables -A INPUT   -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP

iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "Portscan:"
iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP
Run Code Online (Sandbox Code Playgroud)

  • 巴特,'iptables -A INPUT -s 127.0.0.0/8 -j DROP' 不会杀死 memcached 吗? (4认同)

MDM*_*rra 6

所以感谢你的防弹规则。

您应该联系您的 ISP,并在流量到达您之前将其丢弃在主干网上。如果您的防火墙必须丢弃流量,那么它已经在消耗您的可用带宽并使用您的系统资源。

这是唯一“防弹”的方法。

  • 这个答案是完全正确的,无论是谁标记了这个答案,都不知道他在说什么。 (4认同)
  • 许多常见的 DoS 攻击旨在使服务器与 Internet 的连接饱和。如果您看到一种不同类型的攻击,例如针对资源耗尽的攻击,那么您的问题应该更加具体,并且答案可能仍然不会涉及 iptables。 (3认同)