Nasser Heidari

2009-08-26

PF – Packet Filter (Part 1)

Filed under: freebsd — Nasser Heidari @ 08:22

From: http://www.openbsd.org/faq/pf

# echo 'pf=YES' >> /etc/rc.conf.local

# pfctl -e    -> activate PF
# pfctl -d    -> deactivate PF 

Note:Note that this just enables or disables PF, it doesn’t actually load a ruleset. The ruleset must be loaded separately, either before or after PF is enabled.

# pfctl -f /etc/pf.confLoad the pf.conf file
# pfctl -nf /etc/pf.conf    Parse the file, but don't load it
# pfctl -Nf /etc/pf.conf    Load only the NAT rules from the file
# pfctl -Rf /etc/pf.conf    Load only the filter rules from the file

# pfctl -sn   		    Show the current NAT rules 
# pfctl -sr                 Show the current filter rules
# pfctl -ss                 Show the current state table
# pfctl -si                 Show filter stats and counters
# pfctl -sa                 Show EVERYTHING it can show

Lists:

Lists are defined by specifying items within { } brackets:

block out on fxp0 from { 192.168.0.1, 10.5.32.6 } to any
block out on fxp0 proto { tcp udp } from { 192.168.0.1, 10.5.32.6 } to any port { ssh telnet }

trusted = “{ 192.168.1.2 192.168.5.36 }”
pass in inet proto tcp from { 10.10.0.0/24 $trusted } to port 22

Note:The commas between list items are optional.

Tables:

table { 192.0.2.0/24 }
table const { 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8 }
table persist file “/etc/spammers”

block in on fxp0 from { , } to any
pass in on fxp0 from to any


Table Address Matching

An address lookup against a table will return the most narrowly matching entry. This allows for the creation of tables such as:

table { 172.16.0.0/16, !172.16.1.0/24, 172.16.1.100 }

block in on dc0 all
pass in on dc0 from to any

Any packet coming in through dc0 will have its source address matched against the table :
• 172.16.50.5 – narrowest match is 172.16.0.0/16; packet matches the table and will be passed
• 172.16.1.25 – narrowest match is !172.16.1.0/24; packet matches an entry in the table but that entry is negated (uses the “!” modifier); packet does not match the table and will be blocked
• 172.16.1.100 – exactly matches 172.16.1.100; packet matches the table and will be passed
• 10.1.4.55 – does not match the table and will be blocked

Manipulating with pfctl

Tables can be manipulated on the fly by using pfctl(8). For instance, to add entries to the table created above:

# pfctl -t spammers -T add 218.70.0.0/16 

This will also create the table if it doesn’t already exist. To list the addresses in a table:

# pfctl -t spammers -T show 

The -v argument can also be used with -Tshow to display statistics for each table entry. To remove addresses from a table:

# pfctl -t spammers -T delete 218.70.0.0/16 
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: