pfを使用してmacOSで単純なポート転送を設定するには? 「ルールは順番に行う必要があります。オプション、正規化、キューイング、翻訳、フィルタリング」

pfを使用してmacOSで単純なポート転送を設定するには? 「ルールは順番に行う必要があります。オプション、正規化、キューイング、翻訳、フィルタリング」

pfポート5800のMac Aからポート5900のMac Bにトラフィックを転送しようとしています。

予想される移動経路は次のとおりです。

Client to port 5800 → Router (Yes, port forwarding is setup here) → Mac with PF → PF → 192.168.1.246 port 5900

私が使用したいルールは次のとおりです(間違っている可能性があります)。

rdr pass inet proto tcp from any to any port 5800 -> 192.168.1.246 port 5900

質問1

ルールを直接追加して/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

私の設定ファイルは次のとおりです。

#
# 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

質問2

上記と同じルールを使用すると、anchorエラーは発生しません。ただし、ポートはまだ閉じており、connection refused接続しようとすると表示されます。いくつかの調査の後、ポート5800には何も記載されておらず、拒否される可能性があることがわかりました。

  1. 何もスヌーピングしたくありません。トラフィックを別のコンピュータに転送するだけです。
  2. 聞いているにもかかわらず、nc外部と内部(localhost)で拒否され、配信されません。

ベストアンサー1

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"

from any to any省略した場合は暗示されるので読みやすくするために削除しました)

このrdrルールは、パケットフィルタにポート5800に到着するTCPパケットを処理する方法のみを通知します。通常、許可するようにpass指示するルール(たとえば、フィルタルール)が必要ですが、これはルールに追加するのに十分です。pfpassrdrrdr pass

パケットを転送するには、それを有効またはsysctl永久に設定する必要がありますsysctl.conf(参照man pfctl)。

$ sudo sysctl net.inet.ip.forwarding=1

おすすめ記事