Showing posts with label Packet Filter. Show all posts
Showing posts with label Packet Filter. Show all posts

Saturday, December 09, 2006

Know Your Docs

Over the past couple of days I have written and tested a couple of different pf(4) rulesets and I am really happy with the results. I was surprised how easy I found writing these rulesets.
When I learned FreeBSD's ipfw(8) I struggled a lot with the finer points of configuration. I believe that the difference I have made in learning pf(4) is becoming so familiar with the documentation before moving on to the implementation step. With ipfw(8) I tried to "learn by doing". Even though I think that learning by doing is an effective method, I believe for myself I need to read and know the documentation first, then put what I have learned into practice.
I am really having fun with pf, it is a wonderful and very complete piece of software, and I will be using it much more from now on.

Todays mantra:
  1. Locate the Documentation
  2. Read the Documentation
  3. Follow the Documentation
  4. BECOME the Documentation

Thursday, December 07, 2006

Reading and Writing

I finally answered all my e-mail today, I haven't had a chance to go through it for a couple of days. I got an e-mail from my little brother he is arriving back in Australia today, he and his girlfriend have been traveling around Laos and Cambodia. It will be really good to see him. Although I will have to wait a couple of weeks, as he lives a few hours away from me.

Also I have been reading a few articles from: OpenBSD Support, Kernel-Panic.it and INETDAEMON.com

Tonight I started to write my first pf.conf file for my firewall. I think that it should go well. I will have to finish it tomorrow though because I'm exhausted. Goodnight all...

Tuesday, December 05, 2006

Packet Filter

Today I have been going over all of the documents on Packet Filter (pf). The following are the notes which I made while reading (The notes are not complete, just my reference):

Activation from boot:
Edit /etc/rc.conf.local adding:
pf=YES

Activation using pfctl(8):
The pfctl program allows us to activate pf using:
# pfctl -e
And deactivate pf using:
# pfctl -d

*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.
Configuration
At boot time pf reads /etc/pf.conf for it's configuration. The file has several parts:
  • Macros: User-defined variables that can hold IP addresses, interface names, etc.
  • Tables: A structure used to hold lists of IP addresses.
  • Options: Various options to control how PF works.
  • Scrub: Reprocessing packets to normalize and defragment them.
  • Queuing: Provides bandwidth control and packet prioritization.
  • Translation: Controls Network Address Translation and packet redirection.
  • Filter Rules: Allows the selective filtering or blocking of packets as they pass through any of the interfaces.
Lists
Lists allow one rule to contain multiple items, e.g. multiple IP addresses, port numbers etc. Lists are defined by specifying items within { } brackets. e.g:
block out on rl0 from ( 192.168.0.1, 10.0.0.1 } to any

When loading a ruleset and a list is encountered by the pfctl(8) program multiple rules are created. e.g. If pfctl found the above rule it would expand that to:
block out on rl0 from 192.168.0.1 to any
block out on rl0 from 10.0.0.1 to any

*Note that the commas between list items are optional.

Macros
Macros are user-defined variables. They can hold port numbers, IP addresses etc.
Macro names must start with a letter and may contain letters, digits, and underscores. e.g.:
int_if = "sis0"
pass in on $int_if from any to any
Macros can also contain lists.

Tables
Tables hold a groups of IP addresses. Tables are different from lists in that the lookups use less memory and processor and therefore are very fast.
Tables can be used in the following ways:
  • source and/or destination address in filter, scrub, NAT, and redirection rules.
  • translation address in NAT rules.
  • redirection address in redirection rules.
  • destination address in route-to, reply-to, and dup-to filter rule options.
To create a table in pf.conf the table directive is used. There are two attributes that can be specified for each table:
  • const - Once the table has been created the contents can not be changed. If this attribute is not specified; address can be added or removed using pfctl.
  • persist - Keep table in memory even if no rules are referring to it.
For Example:
table <MyNet> { 172.16.2.0/16, !172.16.2.100 }
table <rfc1918> const { 192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8 }
table <spammers> persist file "/etc/spammers"

block in on vr0 from { <rfc1918>, <spammers> } to any
pass in on vr0 from <MyNet> to any

The file /etc/spammers would contain a list of IP addresses and/or CIDR network blocks, one per line. Any line beginning with # is treated as a comment and ignored.
Tables can also be manipulated with pfctl, see: pfctl(8).

Packet Filter
A highly simplified syntax for filter rules is:
action [direction] [log] [quick] [on interface] [af] [proto protocol] \
[from src_addr [port src_port]] [to dst_addr [port dst_port]] \
[flags tcp_flags] [state]


Default Deny
In a default deny filter policy, the first filter rules are:
block in all
block out all

Now traffic has to passed by the firewall other wise it will be dropped.

quick
If a packet matches a rule which is using the quick keyword, then no other processing is needed, and the specified action is taken.

Keeping State
Keeping state or stateful inspection allows pf to keep track of network connection. Information is stored about each connection in a state table, and pf then determines if a passing packet belongs to an established connection, if it does the packet is passed.
When a rule has the keep state option, the first packet matching the rule creates a "state" between the sender and receiver. Now, not only do packets going from the sender to receiver match the state entry and bypass ruleset evaluation, but so do the reply packets from receiver to sender. For example:
pass out on fxp0 proto tcp from any to any keep state
Stateful filtering has a number of options:
  • max number: The max number of state entries the rule can create.
  • source-track: Track number of states created per IP.
  • max-src-nodes number: limit the number of source IP addresses that can simultaneously create state.
  • max-src-states number: When the source-track option is used, max-src-states will limit the number of simultaneous state entries that can be created per source IP address.
If a connection has completed the 3-way handshake, then other restrictions can apply to stateful connections:
  • max-src-conn number: The maximum number of simultaneous TCP connections which a single host can make.
  • max-src-conn-ratenumber / interval: Limit the rate of new connections to a certain amount per time interval.
  • overload <table>: Put an offending host's IP address into the named table.
  • flush [global]: Kill any other states that match this rule and that were created by this source IP. When global is specified, kill all states matching this source IP, regardless of which rule created the state.
TCP Flags
  • F : FIN - Finish; end of session
  • S : SYN - Synchronize; indicates request to start session
  • R : RST - Reset; drop a connection
  • P : PUSH - Push; packet is sent immediately
  • A : ACK - Acknowledgement
  • U : URG - Urgent
  • E : ECE - Explicit Congestion Notification Echo
  • W : CWR - Congestion Window Reduced
In a rule flags are specified using the following syntax: flags check/mask
The mask tells pf to only inspect the specified flags.
The check specified which flag must be "on" in the header for a match

TCP SYN Proxy
Proxy the handshake; pf will complete a client handshake, initiate a server handshake, then pass the packets between the two. e.g.:
pass in on $ext_if proto tcp from any to $web_server port www flags S/SA synproxy state
synproxy state also includes the same functionality as keep state and modulate state

Blocking Spoofed Packets
pf uses the antispoof keyword to protect against spoofing.:
antispoof [log] [quick] for interface [af]

Unicast Reverse Path Forwarding
An uRPF check compares the source IP of a packet with the routing table, to see if the outbound interface in the routing table is the same as the packet.
This check is performed using the urpf-failed keywords in filter rules:
block in quick from urpf-failed label uRPF

OS finger Printing
Using the os keyword in a rule, can detect the operating system of a remote host.

IP Options
To allow packets with IP options set which are block by default you need to use the allow-opts keyword.

NAT - Network Address Translation
A highly simplified syntax for a NAT rule is:
nat [pass [log]] on interface [af] from src_addr [port src_port] to dst_addr [port dst_port] -> ext_addr [pool_type] [static-port]

Example: (tl0 is external, dc0 internal):
nat on tl0 from dc0:network to any -> (tl0)
This rule says to perform NAT on the tl0 interface for any packets coming from the dc0 interfaces network and to replace the source IP with the current address of the tl0 interface.

Bidirectional Mapping (1:1 mapping)
A bidirectional mapping can be established by using the binat rule. A binat rule establishes a one to one mapping between an internal IP address and an external address.

Rule Exceptions
Exceptions can be made to translation rules by using the no keyword. e.g.:
no nat on tl0 from 192.168.1.208 to any
nat on tl0 from 192.168.1.0/24 to any -> 24.2.74.79

The entire 192.168.1.0/24 network would have its packets translated to the external address 24.2.74.79 except for 192.168.1.208.
The no keyword can be used with nat, binat and rdr rules.

NAT Status
To view the active NAT translations pfctl(8) is used with the -s state option.

Redirection (Port Forwarding)
Redirection allows
incoming traffic to be sent to a machine behind the NAT gateway.e.g.:
rdr on tl0 proto tcp from any to any port 80 -> 192.168.1.20
This line redirects TCP port 80 (web server) traffic to a machine inside the network at 192.168.1.20. So, even though 192.168.1.20 is behind your gateway and inside your network, the outside world can access it.

Friday, November 24, 2006

Back to Ubuntu

I am back to Ubuntu for a while. I have some work to do, and I need to focus. I had trouble finding gnome tools for OpenBSD, and I am thinking of going back to FreeBSD as my desktop.
I used to have FreeBSD a while back, it was a wonderful desktop, then I started having troubles after updating to 6.1 so I decided to try something different for a while. I think I will enjoy going back to it though. I would just be better off making my own system from a FreeBSD base, that way I'll have everything I want when I want it. It should be fun.

But first I need to finish my firewall. I wrote my notes, diagrams and configurations for my network today. Now I just need to write my firewall & NAT rules, and then install everything, including the proxies, overall it shouldn't take to long. If I get off my but and do it. I have been a little preoccupied lately. I need to get my head back in to it.

At the moment I am sitting and reading the pf users guide, which is a really excellent reference.
If anyone knows of any good links they are willing to share please let me know :) Thank you. Have a good one.

Sunday, November 12, 2006

Learning Networking, OpenBSD style

I have only been learning OpenBSD for the past couple of weeks, so I am still eagerly learning how the Networking side of things works.
Until today I have never looked much into pf. I have used IPFW2 and IPF many times, and iptables a bit, but never pf. I started out with the information in the OpenBSD FAQ. I always find that this is a good place to start. Then the man pages available on the subject

Reading: I started out with the OpenBSD FAQ Networking section:
http://openbsd.org/faq/faq6.html
As well as investigating the following pages:
lo(4);
The loop interface is a software loopback mechanism which may be used for performance analysis, software testing, and/or local communication.
pflog(4);
The pflog interface is a pseudo-device which makes visible all packets logged by the packet filter, pf(4). Logged packets can easily be monitored in real time by invoking tcpdump(8) on the pflog interface, or stored to disk using pflogd(8).
sl(4);
The sl interface allows serial lines to be used as network interfaces using the slip protocol
ppp(4);
The ppp interface allows serial lines to be used as network interfaces using the Point-to-Point Protocol (PPP).
tun(4);
The tun driver provides a network interface pseudo-device. Packets sent to this interface can be read by a userland process and processed as desired.
enc(4);
The enc interface is a software loopback mechanism that allows hosts or firewalls to filter ipsec(4) traffic using pf(4).
bridge(4);
The bridge device creates a logical link between two or more Ethernet interfaces or encapsulation interfaces
vlan(4);
The vlan Ethernet interface allows construction of virtual LANs when used in conjunction with IEEE 802.1Q-compliant Ethernet devices.
gre(4);
The gre network interface allows tunnel construction using the Cisco GRE or the Mobile-IP (RFC 2004) encapsulation protocols.
gif(4);
The gif interface is a generic tunnelling pseudo-device for IPv4 and IPv6.
carp(4);
The carp interface is a pseudo-device which implements and controls the CARP protocol. carp allows multiple hosts on the same local network to share a set of IP addresses.
tcpdump(8);
tcpdump prints out the headers of packets on a network interface that match the boolean expression.
pflogd(8);
pflogd is a background daemon which reads packets logged by pf(4) to a pflog(4) interface, normally pflog0, and writes the packets to a logfile (normally /var/log/pflog) in tcpdump(8) binary format.
pf(4);
Packet filtering takes place in the kernel. A pseudo-device, /dev/pf, allows userland processes to control the behaviour of the packet filter through an ioctl(2) interface.
ioctl(2);
The ioctl() function manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g., terminals) may be controlled with ioctl() requests.
pf.conf(5);
The pf(4) packet filter modifies, drops or passes packets according to rules or definitions specified in pf.conf.
altp(9);
altq - kernel interfaces for manipulating output queues on network interfaces