To secure a web server running on FreeBSD, you'll want to configure the firewall to allow incoming traffic on the necessary ports while blocking all other traffic. FreeBSD uses the pf (Packet Filter) firewall by default. Here's a basic example of how to configure pf for a web server:
Step 1: Enable PF
First, ensure that pf is enabled. You can do this by editing /etc/rc.conf and adding or modifying the following line:
bash
pf_enable="YES"
Step 2: Configure PF Rules
Create or edit the /etc/pf.conf file to include rules for your web server. Here's a basic configuration that allows SSH, HTTP, and HTTPS traffic:
pf
# Macros
ext_if="em0" # Replace "em0" with your external interface name
web_server="192.168.1.100" # Replace with your web server's IP address
# Tables
table <martians> { 0.0.0.0/8 10.0.0.0/8 127.0.0.0/8 169.254.0.0/16 \
172.16.0.0/12 192.0.0.0/24 192.0.2.0/24 224.0.0.0/3 }
# Options
set skip on lo
set block-policy drop
set loginterface $ext_if
set ruleset-optimization basic
# Scrub
scrub in all
# NAT (if needed)
# nat on $ext_if from !($ext_if) to any -> ($ext_if)
# Rules
block in log all
block out log all
# Allow SSH
pass in on $ext_if proto tcp from any to $ext_if port 22
# Allow HTTP and HTTPS to the web server
pass in on $ext_if proto tcp from any to $web_server port { 80, 443 }
# Allow outgoing traffic
pass out on $ext_if proto tcp from $ext_if to any keep state
pass out on $ext_if proto udp from $ext_if to any keep state
pass out on $ext_if proto icmp from $ext_if to any keep state
Explanation:
Macros: Define your external interface ($ext_if) and the IP address of your web server ($web_server).
Tables: The <martians> table is used to block reserved and special IP addresses. You might not need to modify this.
Options: Configure pf to skip processing on the loopback interface, set the block policy to drop packets, and log packets on the external interface.
Scrub: Normalize and defragment packets.
NAT: Uncomment and adjust if you need NAT (Network Address Translation) for your setup.
Rules:
Block all incoming and outgoing traffic by default.
Allow incoming SSH connections to the firewall's IP.
Allow incoming HTTP and HTTPS traffic to your web server's IP.
Allow outgoing traffic from the firewall.
Step 3: Test and Load the Rules
Test your pf.conf for syntax errors:
bash
pfctl -n -f /etc/pf.conf
If there are no errors, load the rules:
bash
pfctl -f /etc/pf.conf
Enable pf to start at boot and start it now:
bash
service pf start
To verify that pf is running and to see the current ruleset:
bash
pfctl -sr
Step 4: Make it Persistent
To ensure your pf rules are loaded at boot, make sure pf_enable="YES" is in /etc/rc.conf. If you made any changes to /etc/rc.conf, you don't need to do anything else as the rules are loaded automatically on boot.
This configuration provides a basic level of security for a web server. Depending on your specific needs, you may need to adjust the rules (e.g., allowing more services, configuring rate limiting, etc.). Always test your firewall rules thoroughly to ensure they meet your security requirements.