是否可以仅对内部 ips 应用 iptables 规则?
Is it possible to apply an iptables rule only for internal ips?
目前,我在 AWS 中有一个 NAT 实例,其中包含一些 iptables 规则,例如将来自某个端口的流量转发到其他实例。因此,如果我这样做 curl nat.address.com:8090
,流量将转发到正在侦听 8090 端口的某个其他实例,假设它是实例 A。
我想知道如何仅在原始来源位于本地网络内时应用此规则。也就是说,如果对 nat.address.com:8090
的请求来自一个 IP 为 172.31.10.10
的内部实例,则 nat 实例应将其转发给实例 A。但是,如果对 nat.address.com:8090
的请求来自某个外部实例源(例如 189.58.200.10
),则不应转发。
可能吗?
如 Documentation 所述,您可以使用 -s 选项:
-s, --source [!] address[/mask]
Source specification. Address can be either a network name, a hostname (please note that specifying any name to be resolved with a remote query such as DNS is a really bad idea), a network IP address (with /mask), or a plain IP address. The mask can be either a network mask or a plain number, specifying the number of 1's at the left side of the network mask. Thus, a mask of 24 is equivalent to 255.255.255.0. A "!" argument before the address specification inverts the sense of the address. The flag --src is an alias for this option.
例如:
iptables -t nat -A POSTROUTING -s 172.31.10.0/24 -j MASQUERADE
您可以通过以下两种方式之一执行此操作:
(1)安全组规则:
您可以在与您的 NAT 实例关联的安全组中添加适当的规则。这将阻止端口 8090 上来自任何非您的内部实例的任何源的所有数据包的入站流。
例如:如果您想转发来自 IP 为 172.31.10.10 的特定实例的数据包,您可以在 NAT 实例的安全组中添加入站规则,如下所示:
Type: Custom TCP Rule
Protocol: TCP
Port Range: 8090
Source: 172.31.10.10
并且您必须确保安全组内没有其他规则允许来自任何其他来源的 TCP 端口 8090 上的流量。
如果您想确保您的 NAT 实例转发来自 VPC 内所有内部实例的流量,那么您可以编辑源字段以包括您的 VPC 的 CIDR 范围。
仅当您了解命中 NAT 实例的网络流量时,此选项才适用,以便您可以在安全组中适当添加规则以匹配传入流量,否则您最终会阻止有意义的流量也是。
(2) IPTable规则:
此选项要求您在 NAT 实例上添加 IPTable 规则,以便您的 NAT 实例仅接受来自特定网络(即您的 VPC)且仅来自特定 TCP 端口的网络流量。例如,如果您想确保您的 NAT 实例只接受来自您的内部实例(即您的 VPC 内的实例)在端口 8090 上的流量,则将适用以下规则:
- $ iptables -A INPUT -i eth0 -p tcp -s 172.31.0.0/16 --dport 8090 -m state --state NEW,ESTABLISHED -j ACCEPT
- $ iptables -A OUTPUT -o eth0 -p tcp --sport 8090 -m state --state ESTABLISHED -j ACCEPT
目前,我在 AWS 中有一个 NAT 实例,其中包含一些 iptables 规则,例如将来自某个端口的流量转发到其他实例。因此,如果我这样做 curl nat.address.com:8090
,流量将转发到正在侦听 8090 端口的某个其他实例,假设它是实例 A。
我想知道如何仅在原始来源位于本地网络内时应用此规则。也就是说,如果对 nat.address.com:8090
的请求来自一个 IP 为 172.31.10.10
的内部实例,则 nat 实例应将其转发给实例 A。但是,如果对 nat.address.com:8090
的请求来自某个外部实例源(例如 189.58.200.10
),则不应转发。
可能吗?
如 Documentation 所述,您可以使用 -s 选项:
-s, --source [!] address[/mask] Source specification. Address can be either a network name, a hostname (please note that specifying any name to be resolved with a remote query such as DNS is a really bad idea), a network IP address (with /mask), or a plain IP address. The mask can be either a network mask or a plain number, specifying the number of 1's at the left side of the network mask. Thus, a mask of 24 is equivalent to 255.255.255.0. A "!" argument before the address specification inverts the sense of the address. The flag --src is an alias for this option.
例如:
iptables -t nat -A POSTROUTING -s 172.31.10.0/24 -j MASQUERADE
您可以通过以下两种方式之一执行此操作:
(1)安全组规则:
您可以在与您的 NAT 实例关联的安全组中添加适当的规则。这将阻止端口 8090 上来自任何非您的内部实例的任何源的所有数据包的入站流。
例如:如果您想转发来自 IP 为 172.31.10.10 的特定实例的数据包,您可以在 NAT 实例的安全组中添加入站规则,如下所示:
Type: Custom TCP Rule
Protocol: TCP
Port Range: 8090
Source: 172.31.10.10
并且您必须确保安全组内没有其他规则允许来自任何其他来源的 TCP 端口 8090 上的流量。
如果您想确保您的 NAT 实例转发来自 VPC 内所有内部实例的流量,那么您可以编辑源字段以包括您的 VPC 的 CIDR 范围。
仅当您了解命中 NAT 实例的网络流量时,此选项才适用,以便您可以在安全组中适当添加规则以匹配传入流量,否则您最终会阻止有意义的流量也是。
(2) IPTable规则:
此选项要求您在 NAT 实例上添加 IPTable 规则,以便您的 NAT 实例仅接受来自特定网络(即您的 VPC)且仅来自特定 TCP 端口的网络流量。例如,如果您想确保您的 NAT 实例只接受来自您的内部实例(即您的 VPC 内的实例)在端口 8090 上的流量,则将适用以下规则:
- $ iptables -A INPUT -i eth0 -p tcp -s 172.31.0.0/16 --dport 8090 -m state --state NEW,ESTABLISHED -j ACCEPT
- $ iptables -A OUTPUT -o eth0 -p tcp --sport 8090 -m state --state ESTABLISHED -j ACCEPT