vps_-_freebsd_setup_pf
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
vps_-_freebsd_setup_pf [2023/12/02 04:56] – [Setting up PacketFilter (pf) on FreeBSD] m( hc9 | vps_-_freebsd_setup_pf [2023/12/02 05:00] (current) – [Setting up PacketFilter (pf) on FreeBSD] m( hc9 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{example: | ||
+ | |||
+ | ====== Setting up PacketFilter (pf) on FreeBSD ====== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | //Default deny// and //default permit// are the two approaches to building a firewall. The default deny approach blocks all traffic and permits only traffic specified by a rule. The default permit does the opposite. It allows all traffic and blocks only traffic specified by a rule. This tutorial uses the default deny approach. | ||
+ | |||
+ | Pf rulesets are stored in a configuration file at ''/ | ||
+ | |||
+ | This tutorial adds rules to control SSH traffic. Before starting ensure that you can access your SDF VPS console. It is recommended to connect to your VPS through the console for this tutorial to avoid inadvertently locking yourself out via SSH. | ||
+ | |||
+ | Begin by taking note of the external interface name of your VPS. This can be found using '' | ||
+ | |||
+ | < | ||
+ | |||
+ | xn0: flags=8843< | ||
+ | options=3< | ||
+ | ether aa: | ||
+ | inet EXTERNAL_IP netmask 0xffffff00 broadcast 205.166.94.255 | ||
+ | media: Ethernet manual | ||
+ | status: active | ||
+ | nd6 options=29< | ||
+ | |||
+ | </ | ||
+ | |||
+ | Edit ''/ | ||
+ | |||
+ | < | ||
+ | |||
+ | ext_if=" | ||
+ | ssh_in=" | ||
+ | svc_out=" | ||
+ | icmp_types=" | ||
+ | |||
+ | </ | ||
+ | |||
+ | These macros will be expanded where referenced in the rules. If you have set any services to run on non-standard ports make sure that the port numbers are in the appropriate macro. The services defined in '' | ||
+ | |||
+ | Next, add some tables to the config. Tables are similar to macros but are designed to hold groups of IP addresses. The first table is ''< | ||
+ | |||
+ | < | ||
+ | |||
+ | table < | ||
+ | 169.254.0.0/ | ||
+ | 192.0.0.0/ | ||
+ | 192.168.0.0/ | ||
+ | 240.0.0.0/4 255.255.255.255/ | ||
+ | table < | ||
+ | |||
+ | </ | ||
+ | |||
+ | Set the default return policy and logging. | ||
+ | |||
+ | < | ||
+ | |||
+ | set block-policy return | ||
+ | set loginterface $ext_if | ||
+ | set skip on lo0 # Don't apply any filtering on the loopback interface | ||
+ | scrub in all fragment reassemble max-mss 1440 # Reassemble all fragmented packets before processing with a max packet size of 1440 | ||
+ | |||
+ | </ | ||
+ | |||
+ | Add the rules as the last part of the config. | ||
+ | |||
+ | |||
+ | < | ||
+ | |||
+ | # Apply the antispoof directive on $ext_if. The automatic | ||
+ | # antispoofing blocks all traffic from the network of that | ||
+ | # interface unless it originates from that same interface. | ||
+ | antispoof quick for $ext_if | ||
+ | |||
+ | # The quick keyword executes a rule immediately without | ||
+ | # considering the rest of the ruleset. The egress keyword | ||
+ | # automatically finds the default route(s) on a given | ||
+ | # interface. | ||
+ | block in quick on egress from < | ||
+ | block return out quick on egress to < | ||
+ | |||
+ | # The default deny policy | ||
+ | block all | ||
+ | |||
+ | # The inbound SSH rule. This rule allows traffic on | ||
+ | # $ext_if to the $ssh_in port, limiting connections | ||
+ | # to 15 per-host at a rate of 3 connections per-second | ||
+ | # while adding hosts breaking those rules to the | ||
+ | # < | ||
+ | pass in on $ext_if proto tcp to port $ssh_in \ | ||
+ | keep state (max-src-conn 15, max-src-conn-rate 3/1, \ | ||
+ | overload < | ||
+ | |||
+ | # Allow all TCP and UDP traffic on the $svc_out ports. | ||
+ | # This permits communication to the defined services. | ||
+ | pass out proto { tcp udp } to port $svc_out | ||
+ | |||
+ | # Allow the defined ICMP types | ||
+ | pass out inet proto icmp icmp-type $icmp_types | ||
+ | |||
+ | </ | ||
+ | |||
+ | Save the ruleset at ''/ | ||
+ | |||
+ | < | ||
+ | |||
+ | # Enable the services via rc.conf | ||
+ | sysrc pf_enable=" | ||
+ | sysrc pflog_enable=" | ||
+ | |||
+ | # Start the services | ||
+ | service pf start | ||
+ | service pflog start | ||
+ | |||
+ | # Load the rules | ||
+ | pfctl -f / | ||
+ | |||
+ | </ | ||
+ | |||
+ | Pf is now started and the ruleset is enabled. For good measure reboot the system as well. | ||
+ | |||
+ | After rebooting, test the firewall by attempting to ping or connect to outside hosts and connect to your VPS via SSH. To see pf stats, run '' | ||
+ | |||
+ | Over time the ''< | ||
+ | |||
+ | ---- | ||
+ | |||
+ | $Id: VPS_FreeBSD_Setup_PF.html, | ||