Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Last active February 25, 2023 22:42
Show Gist options
  • Select an option

  • Save allaniftrue/ee30d40d4f696da9fcc3 to your computer and use it in GitHub Desktop.

Select an option

Save allaniftrue/ee30d40d4f696da9fcc3 to your computer and use it in GitHub Desktop.
/etc/ipfw.rules
#!/bin/sh
# Flush out the list before we begin.
ipfw -q -f flush
# Set rules command prefix
cmd="ipfw -q add"
pif="vtnet0" # interface name of NIC attached to Internet
$cmd 00005 allow all from any to any via vtnet0
# No restrictions on Loopback Interface
$cmd 00010 allow all from any to any via lo0
$cmd 00101 check-state
# Allow access to ISP's DHCP server for cable/DSL configurations.
$cmd 00120 allow log udp from any to any 67 out via $pif keep-state
# Allow DNS out
$cmd 00130 allow tcp from me to any dst-port 53 out via $pif setup keep-state
$cmd 00131 allow udp from me to any dst-port 53 out via $pif keep-state
# Allow outbound HTTP and HTTPS connections
$cmd 00200 allow tcp from any to any 80 out via $pif setup keep-state
$cmd 00220 allow tcp from any to any 443 out via $pif setup keep-state
# Allow outbound email connections
$cmd 00230 allow tcp from any to any 25 out via $pif setup keep-state
#$cmd 00231 allow tcp from any to any 110 out via $pif setup keep-state
#$cmd 00232 allow tcp from any to any 465 out via $pif setup keep-state
#$cmd 00231 allow tcp from any to any 587 out via $pif setup keep-state
# Allow outbound ping
$cmd 00250 allow icmp from any to any out via $pif keep-state
# Allow outbound NTP
$cmd 00260 allow tcp from any to any 37 out via $pif setup keep-state
# deny and log all other outbound connections
$cmd 00299 deny log all from any to any out via $pif
# Deny all inbound traffic from non-routable reserved address spaces
$cmd 00300 deny all from 192.168.0.0/16 to any in via $pif #RFC 1918 private IP
$cmd 00301 deny all from 172.16.0.0/12 to any in via $pif #RFC 1918 private IP
$cmd 00302 deny all from 10.0.0.0/8 to any in via $pif #RFC 1918 private IP
$cmd 00303 deny all from 127.0.0.0/8 to any in via $pif #loopback
$cmd 00304 deny all from 0.0.0.0/8 to any in via $pif #loopback
$cmd 00305 deny all from 169.254.0.0/16 to any in via $pif #DHCP auto-config
$cmd 00306 deny all from 192.0.2.0/24 to any in via $pif #reserved for docs
$cmd 00307 deny all from 204.152.64.0/23 to any in via $pif #Sun cluster interconnect
$cmd 00308 deny all from 224.0.0.0/3 to any in via $pif #Class D & E multicast
# Deny public pings
$cmd 00310 deny icmp from any to any in via $pif
# Deny ident
$cmd 00315 deny tcp from any to any 113 in via $pif
# Deny all Netbios services.
$cmd 00320 deny tcp from any to any 137 in via $pif
$cmd 00321 deny tcp from any to any 138 in via $pif
$cmd 00322 deny tcp from any to any 139 in via $pif
$cmd 00323 deny tcp from any to any 81 in via $pif
# Deny fragments
$cmd 00330 deny all from any to any frag in via $pif
# Deny ACK packets that did not match the dynamic rule table
$cmd 00332 deny tcp from any to any established in via $pif
# Allow HTTP connections to internal web server
$cmd 00400 allow tcp from any to me 80 in via $pif
# Allow inbound SSH connections
$cmd 00410 allow tcp from any to me 22 in via $pif setup limit src-addr 2
# SSHguard puts offender addresses in table 22. Set up the table rule
# Please note the '\(22\)' syntax, necessary since it's run as shell command
$cmd 01000 deny ip from table\(22\) to any
# Allow inbound DNS connections
$cmd 00430 allow udp from any 53 to me in via $pif
$cmd 00431 allow tcp from any 53 to me in via $pif
# Reject and log all other incoming connections
$cmd 00499 deny log all from any to any in via $pif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment