Firewalling with Openbsd's PF Packet Filter
Total Page:16
File Type:pdf, Size:1020Kb
Firewalling with OpenBSD’s PF packet filter Peter N. M. Hansteen [email protected] Copyright © 2005 - 2012 Peter N. M. Hansteen This document is © Copyright 2005 - 2012, Peter N. M. Hansteen. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The document is a ’work in progress’, based on a manuscript prepared for a lecture at the BLUG (see http://www.blug.linux.no/) meeting of January 27th, 2005. Along the way it has spawned several conference tutorials as well as The Book of PF (http://nostarch.com/pf2.htm) (second edition, No Starch Press November 2010), which expands on all topics mentioned in this document presents several topics that are only hinted at here. I’m interested in comments of all kinds, and you may if you wish add web or other references to html or pdf versions of the manuscript. If you do, I would like, but can not require, you to send me an email message that you’ve done it. For communication regarding this document please use the address <[email protected]> and preferably a recognizable subject line; $ whois bsdly.net provides full contact information. For full revision history, see the HTML version or the SGML source. Revision History Revision 0.09687e 02 may 2011 expiretable removed from OpenBSD ports. Thanks: Rodolfo Gouveia. Also: we’re at 4.9 Revision 0.096871e 07 may 2011 in OpenBSD 4.9, ifconfig wpa syntax is simpler Revision 0.0968711e 05 jan 2012 -stable is 5.0, bump references where appropriate, rev latest year Table of Contents Before we start ..............................................................................................1 PF? ....................................................................................................................5 Packet filter? Firewall?................................................................................7 NAT? .................................................................................................................9 PF today ........................................................................................................11 BSD vs Linux - Configuration ..................................................................13 Simplest possible setup (OpenBSD) .......................................................15 Simplest possible setup (FreeBSD).........................................................17 Simplest possible setup (NetBSD)...........................................................19 First rule set - single machine..................................................................21 Slightly stricter ...........................................................................................23 Statistics from pfctl ....................................................................................25 A simple gateway, NAT if you need it .....................................................27 Gateways and the pitfalls of in, out and on ...........................................27 What is your local network, anyway?.....................................................28 Setting up.................................................................................................29 That sad old FTP thing ..............................................................................33 If We Have To: ftp-proxy With Redirection............................................33 Historical FTP proxies: do not use .........................................................35 Ancient FTP through NAT: ftp-proxy .............................................36 Ancient: FTP, PF and routable addresses: ftpsesame, pftpx and ftp-proxy! ...................................................................................37 ftp-proxy, slightly new style ............................................................38 Making your network troubleshooting friendly..................................41 Then, do we let it all through?................................................................41 The easy way out: The buck stops here..................................................42 Letting ping through ...............................................................................42 Helping traceroute ..................................................................................43 Path MTU discovery................................................................................43 Network hygiene: Blocking, scrubbing and so on...............................45 block-policy .............................................................................................45 scrub .........................................................................................................45 antispoof ..................................................................................................46 Handling non-routable addresses from elsewhere ................................46 iii A web server and a mail server on the inside ......................................49 Taking care of your own - the inside ......................................................50 Tables make your life easier.....................................................................53 Logging..........................................................................................................55 Taking a peek with tcpdump ..................................................................55 Other log tools you may want to look into..............................................56 But there are limits (an anecdote)..........................................................57 Keeping an eye on things with systat ....................................................59 Keeping an eye on things with pftop......................................................61 Invisible gateway - bridge.........................................................................63 Directing traffic with ALTQ......................................................................65 ALTQ - prioritizing by traffic type..........................................................66 So why does this work? ....................................................................66 Using a match Rule for Queue Assignment....................................67 ALTQ - allocation by percentage ............................................................67 ALTQ - handling unwanted traffic .........................................................69 CARP and pfsync.........................................................................................71 Wireless networks made simple ..............................................................73 A little IEEE 802.11 background............................................................73 WEP (Wired Equivalent Privacy)....................................................73 WPA (WiFi Protected Access) ..........................................................74 Setting up a simple wireless network ....................................................74 An open, yet tightly guarded wireless network with authpf............79 Turning away the brutes...........................................................................83 expiring table entries with pfctl .............................................................85 Using expiretable to tidy your tables .....................................................86 Giving spammers a hard time..................................................................87 Remember, you are not alone: blacklisting ............................................87 List of black and grey, and the sticky tarpit ..........................................88 Setting up spamd.....................................................................................88 Some early highlights of our spamd experience ....................................90 Beating’em up some more: spamdb and greytrapping ..........................93 Enter greytrapping ..........................................................................94 Your own traplist..............................................................................94 Deleting, handling trapped entries .................................................95 The downside: some people really do not get it ..............................96 Conclusions from our spamd experience................................................97 iv PF - Haiku .....................................................................................................99 References ..................................................................................................101