easy Firewall setup

Antonio Olivares olivares14031 at gmail.com
Thu Apr 28 02:44:00 UTC 2011


Thanks Karl for your script :) this will help tremendously

Also thanks to  Daniel & Polytropon & Krad & Warren & Fbsd for your
suggestions & help.  Sorry I could not get back since the network went
down.  I will report back as soon as I can get going again.

Regards,

Antonio

On Tue, Apr 26, 2011 at 1:48 PM, Karl Vogel <vogelke+unix at pobox.com> wrote:
>>> On Mon, 25 Apr 2011 19:43:33 -0500,
>>> Antonio Olivares <olivares14031 at gmail.com> said:
>
> A> Is there an easy firewall setup available somewhere (like the one
> A> referenced below but for FreeBSD)?
>
>   Here's a script you can use to generate a rules file for IPF.
>
> --
> Karl Vogel                      I don't speak for the USAF or my company
>
> The only freedom which deserves the name, is that of pursuing our own good
> in our own way, so long as we do not attempt to deprive others of theirs,
> or impede their efforts to obtain it.        --John Stuart Mill, "On Liberty"
>
> ---------------------------------------------------------------------------
> #!/bin/sh
> # This is a shell archive (produced by GNU sharutils 4.6).
> # To extract the files from this archive, save it to some FILE, remove
> # everything before the `!/bin/sh' line above, then type `sh FILE'.
> #
> # Existing files will *not* be overwritten unless `-c' is specified.
> # This format requires very little intelligence at unshar time.
> # "if test", "echo", "mkdir", and "sed" may be needed.
> #
> # This shar contains:
> # length mode       name
> # ------ ---------- ------------------------------------------
> #   7197 -rw-r--r-- ipf-sample-ruleset
> #     71 -rw-r--r-- ipf.break
> #    144 -rw-r--r-- ipf.header
> #     64 -rw-r--r-- ipf.whitelist
> #   4977 -rwxr-xr-x make-ipf-rules
> #
> echo=echo
> shar_tty= shar_n= shar_c='
> '
> mkdir _sh14472 || ( echo 'failed to create locking directory' '_sh14472'; exit 1 )
> # ============= ipf-sample-ruleset ==============
> if test -f 'ipf-sample-ruleset' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'ipf-sample-ruleset' '(file already exists)'
> else
>  $echo 'x -' extracting 'ipf-sample-ruleset' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'ipf-sample-ruleset' &&
> X#################################################################
> X# http://www.pc-freak.net/handbook/firewalls-ipf.html
> X# No restrictions on Inside LAN Interface for private network
> X# Not needed unless you have LAN
> X#################################################################
> X
> X#pass out quick on xl0 all
> X#pass in quick on xl0 all
> X
> X#################################################################
> X# No restrictions on Loopback Interface
> X#################################################################
> Xpass in quick on lo0 all
> Xpass out quick on lo0 all
> X
> X#################################################################
> X# Interface facing Public Internet (Outbound Section)
> X# Interrogate session start requests originating from behind the
> X# firewall on the private network
> X# or from this gateway server destine for the public Internet.
> X#################################################################
> X
> X# Allow out access to my ISP's Domain name server.
> X# xxx must be the IP address of your ISP's DNS.
> X# Dup these lines if your ISP has more than one DNS server
> X# Get the IP addresses from /etc/resolv.conf file
> Xpass out quick on dc0 proto tcp from any to xxx port = 53 flags S keep state
> Xpass out quick on dc0 proto udp from any to xxx port = 53 keep state
> X
> X# Allow out access to my ISP's DHCP server for cable or DSL networks.
> X# This rule is not needed for 'user ppp' type connection to the
> X# public Internet, so you can delete this whole group.
> X# Use the following rule and check log for IP address.
> X# Then put IP address in commented out rule & delete first rule
> Xpass out log quick on dc0 proto udp from any to any port = 67 keep state
> X#pass out quick on dc0 proto udp from any to z.z.z.z port = 67 keep state
> X
> X
> X# Allow out non-secure standard www function
> Xpass out quick on dc0 proto tcp from any to any port = 80 flags S keep state
> X
> X# Allow out secure www function https over TLS SSL
> Xpass out quick on dc0 proto tcp from any to any port = 443 flags S keep state
> X
> X# Allow out send & get email function
> Xpass out quick on dc0 proto tcp from any to any port = 110 flags S keep state
> Xpass out quick on dc0 proto tcp from any to any port = 25 flags S keep state
> X
> X# Allow out Time
> Xpass out quick on dc0 proto tcp from any to any port = 37 flags S keep state
> X
> X# Allow out nntp news
> Xpass out quick on dc0 proto tcp from any to any port = 119 flags S keep state
> X
> X# Allow out gateway & LAN users non-secure FTP ( both passive & active modes)
> X# This function uses the IPNAT built in FTP proxy function coded in
> X# the nat rules file to make this single rule function correctly.
> X# If you want to use the pkg_add command to install application packages
> X# on your gateway system you need this rule.
> Xpass out quick on dc0 proto tcp from any to any port = 21 flags S keep state
> X
> X# Allow out secure FTP, Telnet, and SCP
> X# This function is using SSH (secure shell)
> Xpass out quick on dc0 proto tcp from any to any port = 22 flags S keep state
> X
> X# Allow out non-secure Telnet
> Xpass out quick on dc0 proto tcp from any to any port = 23 flags S keep state
> X
> X# Allow out FBSD CVSUP function
> Xpass out quick on dc0 proto tcp from any to any port = 5999 flags S keep state
> X
> X# Allow out ping to public Internet
> Xpass out quick on dc0 proto icmp from any to any icmp-type 8 keep state
> X
> X# Allow out whois for LAN PC to public Internet
> Xpass out quick on dc0 proto tcp from any to any port = 43 flags S keep state
> X
> X# Block and log only the first occurrence of everything
> X# else that's trying to get out.
> X# This rule enforces the block all by default logic.
> Xblock out log first quick on dc0 all
> X
> X#################################################################
> X# Interface facing Public Internet (Inbound Section)
> X# Interrogate packets originating from the public Internet
> X# destine for this gateway server or the private network.
> X#################################################################
> X
> X# Block all inbound traffic from non-routable or reserved address spaces
> Xblock in quick on dc0 from 192.168.0.0/16 to any    #RFC 1918 private IP
> Xblock in quick on dc0 from 172.16.0.0/12 to any     #RFC 1918 private IP
> Xblock in quick on dc0 from 10.0.0.0/8 to any        #RFC 1918 private IP
> Xblock in quick on dc0 from 127.0.0.0/8 to any       #loopback
> Xblock in quick on dc0 from 0.0.0.0/8 to any         #loopback
> Xblock in quick on dc0 from 169.254.0.0/16 to any    #DHCP auto-config
> Xblock in quick on dc0 from 192.0.2.0/24 to any      #reserved for docs
> Xblock in quick on dc0 from 204.152.64.0/23 to any   #Sun cluster interconnect
> Xblock in quick on dc0 from 224.0.0.0/3 to any       #Class D & E multicast
> X
> X##### Block a bunch of different nasty things. ############
> X# That I do not want to see in the log
> X
> X# Block frags
> Xblock in quick on dc0 all with frags
> X
> X# Block short tcp packets
> Xblock in quick on dc0 proto tcp all with short
> X
> X# block source routed packets
> Xblock in quick on dc0 all with opt lsrr
> Xblock in quick on dc0 all with opt ssrr
> X
> X# Block nmap OS fingerprint attempts
> X# Log first occurrence of these so I can get their IP address
> Xblock in log first quick on dc0 proto tcp from any to any flags FUP
> X
> X# Block anything with special options
> Xblock in quick on dc0 all with ipopts
> X
> X# Block public pings
> Xblock in quick on dc0 proto icmp all icmp-type 8
> X
> X# Block ident
> Xblock in quick on dc0 proto tcp from any to any port = 113
> X
> X# Block all Netbios service. 137=name, 138=datagram, 139=session
> X# Netbios is MS/Windows sharing services.
> X# Block MS/Windows hosts2 name server requests 81
> Xblock in log first quick on dc0 proto tcp/udp from any to any port = 137
> Xblock in log first quick on dc0 proto tcp/udp from any to any port = 138
> Xblock in log first quick on dc0 proto tcp/udp from any to any port = 139
> Xblock in log first quick on dc0 proto tcp/udp from any to any port = 81
> X
> X# Allow traffic in from ISP's DHCP server. This rule must contain
> X# the IP address of your ISP's DHCP server as it's the only
> X# authorized source to send this packet type. Only necessary for
> X# cable or DSL configurations. This rule is not needed for
> X# 'user ppp' type connection to the public Internet.
> X# This is the same IP address you captured and
> X# used in the outbound section.
> Xpass in quick on dc0 proto udp from z.z.z.z to any port = 68 keep state
> X
> X# Allow in standard www function because I have apache server
> Xpass in quick on dc0 proto tcp from any to any port = 80 flags S keep state
> X
> X# Allow in non-secure Telnet session from public Internet
> X# labeled non-secure because ID/PW passed over public Internet as clear text.
> X# Delete this sample group if you do not have telnet server enabled.
> X#pass in quick on dc0 proto tcp from any to any port = 23 flags S keep state
> X
> X# Allow in secure FTP, Telnet, and SCP from public Internet
> X# This function is using SSH (secure shell)
> Xpass in quick on dc0 proto tcp from any to any port = 22 flags S keep state
> X
> X# Block and log only first occurrence of all remaining traffic
> X# coming into the firewall. The logging of only the first
> X# occurrence stops a .denial of service. attack targeted
> X# at filling up your log file space.
> X# This rule enforces the block all by default logic.
> Xblock in log first quick on dc0 all
> X################### End of rules file #####################################
> SHAR_EOF
>  : || $echo 'restore of' 'ipf-sample-ruleset' 'failed'
> fi
> # ============= ipf.break ==============
> if test -f 'ipf.break' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'ipf.break' '(file already exists)'
> else
>  $echo 'x -' extracting 'ipf.break' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'ipf.break' &&
> X# --------------------------------------------------------------------
> SHAR_EOF
>  : || $echo 'restore of' 'ipf.break' 'failed'
> fi
> # ============= ipf.header ==============
> if test -f 'ipf.header' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'ipf.header' '(file already exists)'
> else
>  $echo 'x -' extracting 'ipf.header' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'ipf.header' &&
> X#
> X# NAME:
> X#    /etc/ipf.rules
> X#
> X# DESCRIPTION:
> X#    Ruleset for IPF packet filter.
> X#
> X# AUTHOR:
> X#    Antonio Olivares <olivares14031 at gmail.com>
> X
> SHAR_EOF
>  : || $echo 'restore of' 'ipf.header' 'failed'
> fi
> # ============= ipf.whitelist ==============
> if test -f 'ipf.whitelist' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'ipf.whitelist' '(file already exists)'
> else
>  $echo 'x -' extracting 'ipf.whitelist' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'ipf.whitelist' &&
> Xfriend at some.host.com    1.2.3.4
> Xbuddy at somewhere.else    1.2.3.5
> SHAR_EOF
>  : || $echo 'restore of' 'ipf.whitelist' 'failed'
> fi
> # ============= make-ipf-rules ==============
> if test -f 'make-ipf-rules' && test "$first_param" != -c; then
>  $echo 'x -' SKIPPING 'make-ipf-rules' '(file already exists)'
> else
>  $echo 'x -' extracting 'make-ipf-rules' '(text)'
>  sed 's/^X//' << 'SHAR_EOF' > 'make-ipf-rules' &&
> X#!/bin/ksh
> X#
> X# $Date: 2011-04-26 14:22:54-04 $
> X# $Source: /home/vogelke/notebook/2011/0324/ipf/RCS/make-ipf-rules,v $
> X# $UUID: eaf3390f-4ef2-3cda-85ad-583f1413d9e0 $
> X#
> X#<make-ipf-rules: Make IPF rules using list of valid hosts.
> X
> Xexport PATH=/usr/local/bin:/bin:/usr/bin:/usr/sbin:/sbin
> Xtag=${0##*/}
> X
> Xversion () {
> X    rcs='$Revision: 1.10 $'
> X    set X $rcs
> X    echo $tag v$3
> X}
> X
> X# Network interface; set directly or run ifconfig.
> Xset X $(ifconfig -a | grep RUNNING | grep -v LOOPBACK | tr -d ':')
> Xoif=$2
> X
> X# Abbreviations and templates.
> Xks="keep state"
> Xfks="flags S keep state"
> X
> Xheader='ipf.header'
> Xtest -f "$header" || { echo $header not found; exit 1; }
> X
> Xgood='ipf.whitelist'
> Xtest -f "$good" || { echo $good not found; exit 1; }
> X
> Xsec=''
> Xtest -f "ipf.break" && sec=$(cat ipf.break)
> X
> X# --------------------------------------------------------------------
> X# This is boilerplate that should always be included.
> X
> Xecho "# Generated by $(version) at $(date)"
> Xcat $header
> X
> Xcat << PART1
> X$sec
> X# We don't care about NETBIOS broadcast crap, bootpc requests, or IGMP.
> Xblock in quick on $oif proto udp  from any to any port = 68
> Xblock in quick on $oif proto udp  from any to any port = 137
> Xblock in quick on $oif proto udp  from any to any port = 138
> Xblock in quick on $oif proto igmp from any to any
> X
> X$sec
> X# Now block everything coming down the network.
> Xblock in  log  on $oif all
> Xblock out log  on $oif all
> X
> X$sec
> X# Get rid of anything with options, as these can be used to hack.
> Xblock in  log quick     from any to any with ipopts
> X
> X$sec
> X# Get rid of short TCP/IP fragments (too small for valid comparison)
> X# as these can be used to hack.
> Xblock in  log quick proto tcp from any to any with short
> X
> X$sec
> X# Allow all traffic on loopback.
> Xpass  in  quick on lo0 all
> Xpass  out quick on lo0 all
> X
> X$sec
> X# Block all the private routable addresses, as these should never
> X# come down the network, nor should we be talking to them.
> Xblock out quick on $oif from any               to 192.168.0.0/16
> Xblock out quick on $oif from any               to 172.16.0.0/12
> Xblock out quick on $oif from any               to 127.0.0.0/8
> Xblock out quick on $oif from any               to 10.0.0.0/8
> Xblock out quick on $oif from any               to 0.0.0.0/8
> Xblock out quick on $oif from any               to 169.254.0.0/16
> Xblock out quick on $oif from any               to 192.0.2.0/24
> Xblock out quick on $oif from any               to 204.152.64.0/23
> Xblock out quick on $oif from any               to 224.0.0.0/3
> X
> Xblock in  quick on $oif from 192.168.0.0/16    to any
> Xblock in  quick on $oif from 172.16.0.0/12     to any
> Xblock in  quick on $oif from 10.0.0.0/8        to any
> Xblock in  quick on $oif from 127.0.0.0/8       to any
> Xblock in  quick on $oif from 0.0.0.0/8         to any
> Xblock in  quick on $oif from 169.254.0.0/16    to any
> Xblock in  quick on $oif from 192.0.2.0/24      to any
> Xblock in  quick on $oif from 204.152.64.0/23   to any
> Xblock in  quick on $oif from 224.0.0.0/3       to any
> X
> X$sec
> X# Block and log portmapper attempts.
> Xblock in log quick on $oif proto tcp/udp from any to any port = 111 $ks
> X
> X$sec
> X# Allow outbound state related packets.
> Xpass  out quick on $oif proto tcp from any to any $fks
> Xpass  out quick on $oif proto udp from any to any $ks
> X
> X$sec
> X# Allow ping and traceroute.  Since we're doing everything quick,
> X# we must have passes before blocks.
> Xpass  in quick on $oif proto icmp from any to any icmp-type  0 $ks
> Xpass  in quick on $oif proto icmp from any to any icmp-type  8 $ks
> Xpass  in quick on $oif proto icmp from any to any icmp-type 11 $ks
> Xpass out quick on $oif proto icmp from any to any icmp-type  0 $ks
> Xpass out quick on $oif proto icmp from any to any icmp-type  8 $ks
> Xpass out quick on $oif proto icmp from any to any icmp-type 11 $ks
> Xblock in log quick on $oif proto icmp from any to any
> X
> X$sec
> X# Allow DNS; should this be just from nameservers?
> Xpass in quick on $oif proto tcp from any to any port = 53 $fks
> Xpass in quick on $oif proto udp from any to any port = 53 $ks
> X
> X$sec
> X# Allow ssh and mail from anywhere: tcpserver filters addresses
> Xpass in quick on $oif proto tcp from any to any port = 22 $fks
> Xpass in quick on $oif proto tcp from any to any port = 25 $fks
> X
> XPART1
> X
> X# --------------------------------------------------------------------
> X# This is host-specific.  "sortcmd" sorts IP addresses correctly.
> X
> Xecho "$sec"
> Xecho '# Allow http from selected addresses.'
> Xsortcmd='/bin/sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n'
> X
> Xawk '{print $2}' $good | $sortcmd | uniq |
> Xwhile read ip; do
> X    echo "pass in quick on $oif proto tcp from $ip to any port = 80 $fks"
> Xdone
> Xecho
> X
> Xecho "$sec"
> Xecho '# Allow secure http from selected addresses.'
> Xawk '{print $2}' $good | $sortcmd | uniq |
> Xwhile read ip; do
> X    echo "pass in quick on $oif proto tcp from $ip to any port = 443 $fks"
> Xdone
> X
> X# --------------------------------------------------------------------
> X# Footer.
> X
> Xcat << FOOTER
> X
> X$sec
> X# Copyright (C) $(date +%Y) $COMPANY
> X# EOF
> XFOOTER
> X
> Xexit 0
> SHAR_EOF
>  : || $echo 'restore of' 'make-ipf-rules' 'failed'
> fi
> $echo $shar_n 'x -' 'lock directory' "\`_sh14472': " $shar_c
> if rm -fr _sh14472; then
>  $echo 'removed'
> else
>  $echo 'failed to remove'
> fi
> exit 0
>


More information about the freebsd-questions mailing list