大网数据广告活动
服务器运维问答

How to forbid forwarding packets to private network addresses via interface

· 52CE 我爱测

On my server, I have a default route to interface eth0 . It has public internet address and faces ISP network. I have packet forwarding enabled, and few local interfaces/bridges with private 10.x.x.x addresses. Also, all packages…

问题原文

原帖链接 · Dmitriusan · CC BY-SA 3.0

On my server, I have a default route to interface eth0. It has public internet address and faces ISP network. I have packet forwarding enabled, and few local interfaces/bridges with private 10.x.x.x addresses. Also, all packages that are outgoing to ISP network is masqueraded. The problem: if I understand correctly, any packet to non-existent 192.168.x.x address will be forwarded through public server interface (uplink) to my ISP. That is not desired.

How can I configure linux to prevent forwarding any packets with private network address destination through a public network interface? I guess just adding a single iptables rule would not be enough because there are multiple standard private network spaces (10.x.x.x, 192.16.x.x, 172.16.x.x and maybe others. Also ipv6 addresses aside). Is there any iptables plugin/system policy to accomplish that? I'd expect some system policy to mark the interface as uplink that faces internet.

Reasoning: my ISP does not like any packages with private network range destinations. I'm using a DNS server that is accessed through VPN to my work. So when VPN goes down, my ISP gets DNS requests to private network addresses and locks down the entire internet connection. They say that is done for security (just for the case if some virus overwrites my system DNS to a custom one).

已采纳解答

原帖链接 · Zip · CC BY-SA 3.0

Any packet that doesn't match a routing rule on the system will be directed to the default route, if there is any. By default, if you have an IP assigned on a given network a routing rule will be added automatically forwarding that traffic to the assigned interface. If your server is not on these networks you want to block, then it's a good idea to check whatever is generating that traffic in order to get rid of it completely, otherwise you'll still have it bothering you on your local network devices.

The IANA private network IP ranges are pretty well defined and only 4, as can be seen even on this wikipedia article. I suggest you add firewall outbound blocking rules to these network ranges, as destinations, after allowing the ranges you actually use.

Example iptables commands:

iptables -A FORWARD -d 10.0.0.0/8 -j REJECT
iptables -A FORWARD -d 172.16.0.0/12 -j REJECT
iptables -A FORWARD -d 192.168.0.0/16 -j REJECT
ip6tables -A FORWARD -d fd00::/8 -j REJECT

Note: Traffic destined to these ranges is dropped on ISPs, as they will have nowhere to forward it to. Some, however, may impose restrictions on your traffic if you leak these packages. Also if you fear traffic inspection or leakage on the ISP level this is far from the only thing you need to do to secure yourself.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。