这是 Linux 中 iptables 的一个良好起点吗?

这是 Linux 中 iptables 的一个良好起点吗?

我是 iptables 的新手,我一直在尝试组装一个防火墙,目的是保护 Web 服务器。以下是我到目前为止组装的规则,我想知道这些规则是否合理 - 以及我是否遗漏了任何重要的东西?

除了端口 80,我还需要打开端口 3306(mysql)和端口 22(ssh)以用于外部连接。

非常感谢您的任何反馈!

#!/bin/sh

# Clear all existing rules.
iptables -F

# ACCEPT connections for loopback network connection, 127.0.0.1.
iptables -A INPUT -i lo -j ACCEPT

# ALLOW established traffic
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# DROP packets that are NEW but does not have the SYN but set.
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

# DROP fragmented packets, as there is no way to tell the source and destination ports of such a packet.
iptables -A INPUT -f -j DROP

# DROP packets with all tcp flags set (XMAS packets).
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

# DROP packets with no tcp flags set (NULL packets).
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

# ALLOW ssh traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport ssh -m limit --limit 1/s  -j ACCEPT

# ALLOW http traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport http -m limit --limit 5/s -j ACCEPT

# ALLOW mysql traffic (and prevent against DoS attacks)
iptables -A INPUT -p tcp --dport mysql -m limit --limit 25/s -j ACCEPT

# DROP any other traffic.
iptables -A INPUT -j DROP

答案1

尝试使用 shorewall,它提供了一款现成的合理防火墙。启用从网络访问所需服务的功能。有针对一、二和三个接口的示例规则集。文档很好,并且维护得当。

我猜你会想要限制哪些地址可以访问 MySQL,这很容易做到。你也可以使用端口敲击来保护 SSH,除非你最近探测过另一个端口,否则端口是关闭的。

答案2

  1. 您确实想要允许 ICMP。
  2. 5/秒对于 HTTP 来说可能不够
  3. 我认为 XMAS/NULL 数据包的规则毫无意义
  4. 也没有看到制定特殊新数据包规则的理由

ETA:5。这种速率限制使得 DoS 攻击变得非常简单。我只需要每秒向您的服务器发送 1 个 SYN 数据包即可拒绝您的 ssh 访问。

答案3

我会考虑使用像 NARC 这样的东西来进行 iptables 规则配置:

http://www.knowplace.org/pages/howtos/firewalling_with_netfilter_iptables/netfilter_automatic_rule_configurator.php

这个软件包中已经有一些合理的默认设置,您应该可以信任它们。

答案4

过滤从服务器到互联网的传出通信也很重要。特别是建议只允许一台服务器使用 SMTP。

我管理 Mikrotik 防火墙,并且我习惯于执行以下操作:

  • 丢弃来自互联网、目标地址在私有范围内的传入数据包
  • 删除端口扫描(每秒检查超过 5 个端口)
  • 通过 SSH 连接到另一个端口(我知道这有点晦涩,但确实有效!:-))并限制源 IP
  • 丢弃路由器的广播
  • 丢弃 TCP RST

还有一些。我建议阅读这个:http://wiki.mikrotik.com/wiki/Dmitry_on_firewallingMikrotik 的语法很简单,并且为初学者提供了很好的指导。

相关内容