Skip to content

Instantly share code, notes, and snippets.

@glennschler
Last active October 6, 2020 07:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save glennschler/bb83fa5ca5fbc95bf0c5 to your computer and use it in GitHub Desktop.
Save glennschler/bb83fa5ca5fbc95bf0c5 to your computer and use it in GitHub Desktop.
Firewall setting notes using PFCTL not IPFW on OS X

Referencing this discussion on how to deny loopback access, create the opposite which is to only allow access from localhost. The goal is to only allow access after the client has connected with SSH using the port tunneling option.

Add new filter rules to block VNC (port 5900) access when not connected via SSH with port forwarding. Plus add blocks to file sharing (SMB and AFP). Just in case I am running a web server block that too, or any other ports listed in this link.

# pfctl is the packet filter firewall for modern OS X. 
# For more info -> man pfctl
# flush to the default
sudo pfctl -f /etc/pf.conf

# view the current PF rules
sudo pfctl -sr 2>/dev/null

echo 'tunnelPorts = "{ 80, 443, 8080, 5900 }"' > pf_vnc.conf
echo 'doNotSharePorts = "{ 139, 445, 548 }"' >> pf_vnc.conf
echo 'block drop in proto tcp from any to any port $tunnelPorts' >> pf_vnc.conf
echo 'block drop in proto tcp from any to any port $doNotSharePorts' >> pf_vnc.conf

# allow those TCP ports on localhost, assuming client has connected with tunneling
echo 'pass in quick on lo0 proto tcp from any to any port $tunnelPorts' >> pf_vnc.conf

# Now apply all those rules
(sudo pfctl -sr 2>/dev/null; cat pf_vnc.conf) | sudo pfctl -f - 2>/dev/null

Verify the current rules

sudo pfctl -sr 2>/dev/null

# If rules look good. Enable PF if it was not already enabled
sudo pfctl -e

Reset the rules by setting back to the default configuration

sudo pfctl -f /etc/pf.conf
@lox
Copy link

lox commented May 31, 2019

Thanks, this worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment