在 Linux 上使用 nftables 完成伪装 NAT 示例?

Joh*_*nst 5 networking firewall gateway nat

我正在寻找如何使用 nftables 执行此操作的完整示例。它应该是上游接口上的 DHCP 客户端,另一个接口上有一个 192.168.0.0/24 LAN,并且还充当防火墙。

在上游接口上打开一个 ssh 端口,并将端口 80 流量转发到 LAN 上的其他服务器,这是额外的功劳。

nftables wiki 留下了一些悬而未决的问题。例如,有关伪装部分没有描述如何将伪装规则附加到一个界面与另一个界面。

小智 7

这是我正在使用的,假设lan0已连接到您的内部网络和wan0您的 ISP。

我不确定“上游接口上的 DHCP 客户端”是什么意思,因为这是通过 DHCP 客户端完成的,而不是 nftables。下面的设置不限制传出流量,因此 DHCP 请求将通过。

#!/usr/bin/nft -f

flush ruleset

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;

    # allow established/related connections
    ct state {established, related} accept

    # early drop of invalid connections
    ct state invalid drop

    # allow from loopback
    iifname lo accept

    # Allow from internal network
    iifname lan0 accept

    # allow icmp
    ip protocol icmp accept

    # allow ssh
    tcp dport 22 accept comment "SSH in"

    reject
  }

  chain forward {
    type filter hook forward priority 0;

    # Allow outgoing via wan0
    oifname wan0 accept

    # Allow incoming on wan0 for related & established connections
    iifname wan0 ct state related, established accept

    # Drop any other incoming traffic on wan0
    iifname wan0 drop
  }

  chain output {
    type filter hook output priority 0;
  }

}

table ip nat {
  chain prerouting {
    type nat hook prerouting priority 0;

    # Forward traffic from wan0 to a LAN server
    iifname wan0 tcp dport 80 dnat 192.168.0.8 comment "Port forwarding to web server"
  }

  chain postrouting {
    type nat hook postrouting priority 0;

    # Masquerade outgoing traffic
    oifname wan0 masquerade
  }
}
Run Code Online (Sandbox Code Playgroud)