如何使用 pf 在 macOS 上设置简单的端口转发?“规则必须有序:选项、规范化、排队、翻译、过滤”

JBi*_*Bis 6 osx port-forwarding pf

我正在尝试使用pf.

这是预期的旅行路线:

Client to port 5800 ? Router (Yes, port forwarding is setup here) ? Mac with PF ? PF ? 192.168.1.246 port 5900
Run Code Online (Sandbox Code Playgroud)

以下是我打算使用的规则(可能是错误的):

rdr pass inet proto tcp from any to any port 5800 -> 192.168.1.246 port 5900
Run Code Online (Sandbox Code Playgroud)

问题一

当我/etc/pf.conf直接添加规则并运行时,sudo pfctl -f /etc/pf.conf我得到:

$ sudo pfctl -f /etc/pf.conf
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.

No ALTQ support in kernel
ALTQ related functions disabled
/etc/pf.conf:29: Rules must be in order: options, normalization, queueing, translation, filtering
pfctl: Syntax error in config file: pf rules not loaded
Run Code Online (Sandbox Code Playgroud)

我的配置文件如下:

#
# Default PF configuration file.
#
# This file contains the main ruleset, which gets automatically loaded
# at startup.  PF will not be automatically enabled, however.  Instead,
# each component which utilizes PF is responsible for enabling and disabling
# PF via -E and -X as documented in pfctl(8).  That will ensure that PF
# is disabled only when the last enable reference is released.
#
# Care must be taken to ensure that the main ruleset does not get flushed,
# as the nested anchors rely on the anchor point defined here. In addition,
# to the anchors loaded by this file, some system services would dynamically 
# insert anchors into the main ruleset. These anchors will be added only when
# the system service is used and would removed on termination of the service.
#
# See pf.conf(5) for syntax.
#

#
# com.apple anchor point
#
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"

rdr pass inet proto tcp from any to any port 5800 -> 192.168.1.246 port 5900
Run Code Online (Sandbox Code Playgroud)

问题二

如果我使用anchor具有上述相同规则的 ,则不会出错。但是,端口仍然关闭,我connection refused在尝试连接时得到了。经过一番研究,我发现一个可能是端口5800上没有列出任何内容,因此被拒绝但

  1. 我不想听任何东西,只需将流量转发到另一台计算机
  2. 即使在nc听我仍然被外部和内部(本地主机)拒绝它不会转发

Zé *_*off 5

正如错误消息所述,您需要将您的rdr规则添加到 上的其他翻译规则旁边pf.conf。由于已经有一个rdr锚点存在,最好的办法是将你的rdr规则放在它之后:

scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr pass inet proto tcp to port 5800 -> 192.168.1.246 port 5900
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
Run Code Online (Sandbox Code Playgroud)

from any to any如果省略则隐含,因此我将其删除以提高可读性)

rdr规则只告诉包过滤器如何处理到达端口 5800 的 TCP 包。您通常需要一个pass规则(即过滤规则)来告诉pf它们被允许进入,但它足以添加passrdr规则,因此rdr pass

请注意,对于要转发的数据包,您需要sysctlsysctl.conf(参见man pfctl)中启用或永久设置它:

$ sudo sysctl net.inet.ip.forwarding=1
Run Code Online (Sandbox Code Playgroud)