Nytro Posted October 20, 2011 Report Posted October 20, 2011 Firewalling with OpenBSD's PF packet filterPeter N. M. Hansteen<peter _@_ bsdly.net>Copyright 2005 - 2011 Peter N. M. HansteenTable of ContentsBefore we startPF?Packet filter? Firewall?NAT?PF todayBSD vs Linux - ConfigurationSimplest possible setup (OpenBSD)Simplest possible setup (FreeBSD)Simplest possible setup (NetBSD)First rule set - single machineSlightly stricterStatistics from pfctlA simple gateway, NAT if you need itGateways and the pitfalls of in, out and onWhat is your local network, anyway?Setting upThat sad old FTP thingIf We Have To: ftp-proxy With RedirectionHistorical FTP proxies: do not useAncient FTP through NAT: ftp-proxyAncient: FTP, PF and routable addresses: ftpsesame, pftpx and ftp-proxy!ftp-proxy, slightly new styleMaking your network troubleshooting friendlyThen, do we let it all through?The easy way out: The buck stops hereLetting ping throughHelping traceroutePath MTU discoveryNetwork hygiene: Blocking, scrubbing and so onblock-policyscrubantispoofHandling non-routable addresses from elsewhereA web server and a mail server on the insideTaking care of your own - the insideTables make your life easierLoggingTaking a peek with tcpdumpOther log tools you may want to look intoBut there are limits (an anecdote)Keeping an eye on things with systatKeeping an eye on things with pftopInvisible gateway - bridgeDirecting traffic with ALTQALTQ - prioritizing by traffic typeSo why does this work?Using a match Rule for Queue AssignmentALTQ - allocation by percentageALTQ - handling unwanted trafficCARP and pfsyncWireless networks made simpleA little IEEE 802.11 backgroundWEP (Wired Equivalent Privacy)WPA (WiFi Protected Access)Setting up a simple wireless networkAn open, yet tightly guarded wireless network with authpfTurning away the brutesexpiring table entries with pfctlUsing expiretable to tidy your tablesGiving spammers a hard timeRemember, you are not alone: blacklistingList of black and grey, and the sticky tarpitSetting up spamdSome early highlights of our spamd experienceBeating'em up some more: spamdb and greytrappingEnter greytrappingYour own traplistDeleting, handling trapped entriesThe downside: some people really do not get itConclusions from our spamd experiencePF - HaikuReferencesWhere to find the tutorial on the webIf you enjoyed this: Buy OpenBSD CDs and other items, donate!Before we startThis lecture[1] will be about firewalls and related functions, starting from a little theory along with a number of examples of filtering and other network traffic directing. As in any number of other endeavors, the things I discuss can be done in more than one way. More information: The Book of PF, training, consultingMost of the topics we touch on here is covered in more detail in The Book of PF, which was written by the same author and published by No Starch Press at the end of 2007, with a revised and updated second edition published in November 2010. The book is an expanded and extensively rewritten followup to this tutorial, and covers a range of advanced topics in addition to those covered here.This tutorial is in minimal-maintainence mode, in that I'll occasionally make an effort to keep the information in it up to date, but it will not expand in scope. For more in-depth information or topics not covered here, check the book, the PF User Guide (also known as The PF FAQ) or the relevant man pages. If you buy the book via The OpenBSD Bookstore, the OpenBSD project gets a slightly larger a cut.If you need PF related consulting or training, please contact me for further details. You may want to read my Rent-a-geek writeup too.Under any circumstances I will urge you to interrupt me when you need to. That is, if you will permit me to use what I learn from your comments later, either in revised versions of this lecture or in practice at a later time.PF?What, then is PF? Let us start by looking briefly at the project's history to put things in their proper context.OpenBSD's Packet Filter subsystem, which most people refer to simply by using the abbreviated form 'PF', was originally written in an effort of extremely rapid development during the northern hemisphere summer and autumn months of 2001 by Daniel Hartmeier and a number of OpenBSD developers, and was launched as a default part of the OpenBSD 3.0 base system in December of 2001.The need for a new firewalling software subsystem for OpenBSD arose when Darren Reed announced to the world that IPFilter, which at that point had been rather intimately integrated in OpenBSD, was not after all BSD licensed. In fact quite to the contrary. The license itself was almost a word by word copy of the BSD license, omitting only the right to make changes to the code and distribute the result. The OpenBSD version of IPFilter contained quite a number of changes and customizations, which it turned out were not allowed according to the license. IPFilter was removed from the OpenBSD source tree on May 29th, 2001, and for a few weeks OpenBSD-current did not contain any firewalling software.Fortunately, in Switzerland Daniel Hartmeier was already doing some limited experiments involving kernel hacking in the networking code.His starting point was hooking a small function of his own into the networking stack, making packets pass through it, and after a while he had started thinking about filtering. Then the license crisis happened.IPFilter was pruned from the source tree on May 29th. The first commit of the PF code happened Sunday, June 24 2001 at 19:48:58 UTC.[2]A few months of rather intense activity followed, and the version of PF to be released with OpenBSD 3.0 contained a rather complete implementation of packet filtering, including network address translation.From the looks of it, Daniel Hartmeier and the other PF developers made good use of their experience with the IPFilter code. Under any circumstances Daniel presented a USENIX 2002 paper with performance tests which show that the OpenBSD 3.1 PF performed equally well as or better under stress than IPFilter on the same platform or iptables on Linux.In addition, some tests were run on the original PF from OpenBSD 3.0. These tests showed mainly that the code had gained in efficiency from version 3.0 to version 3.1. The article which provides the details is available from Daniel Hartmeier's web, see http://www.benzedrine.cx/pf-paper.html.I have not seen comparable tests performed recently, but in my own experience and that of others, the PF filtering overhead is pretty much negligible. As one data point, the machine which gateways between one of the networks where I've done a bit of work and the world is a Pentium III 450MHz with 384MB of RAM. When I've remembered to check, I've never seen the machine at less than 96 percent 'idle' according to top.It is however worth noting that various optimisations have been introduced to OpenBSD's PF code during recent releases (mainly by the current main PF developers Henning Brauer and Ryan McBride with contributions from others), making each release from 4.4 through 4.9 perform better than its predecessors..................................................................Online:http://home.nuug.no/~peter/pf/en/long-firewall.html Quote