Stránka 1 z 1

Podrobné nastavení Firewallu v Mac OS X - automatický skript

PříspěvekNapsal: čtv črc 08, 2010 7:37 pm
od ls8
Nevím jak vám, ale mě se aplikační firewall v Mac OS X vůbec nelíbí. Chtěl bych mít plnou vládu na tím, co a kam mám povoleno, a co zakázáno. Chtěl bych mít například na svém Macu v práci některé služby povolené pro počítače v lokální síti, jiné pro počítače zvenku, jiné pro připojení z dovova a všechno ostatní zablokované. Toto standardní firewall, který najdete v System Preferences -> Security, neumí. Naštěstí kromě tohoto obsahuje Mac OS X ještě druhý firewall 'ipfw', převzatý z BSD. Pokud tedy stejně jako já chcete mít nad nastavením firewallu plnou kontrolu, postupujte takto:

1/ Vypněte aplikační firewall v System Preferences -> Security -> Firewall.

Obrázek

2/ Následující kód uložte do souboru /etc/rc.firewall, souboru nastavte příznak spustitelnosti (sudo chmod +x /etc/rc.firewall):
Kód: Vybrat vše
#!/bin/bash

# ipfw startup script for Mac OS X 10.6
# created by (c) Lukas Svoboda, svoboda77 at gmail
# version 1.2, 2010/07/09

# ================================================= #
# CONFIGURATION STARTS HERE - PLEASE EDIT THIS PART #
# ================================================= #

# Specify trusted hosts range (leave blank for none),
# and port numbers or service names from /etc/services to be allowed for hosts in this range (leave blank for any).
TrustedHosts="147.32.27.0/22, 192.168.1.0/24, 192.168.2.0/24"
TrustedHostsPorts=""

# Specify not-so-trusted hosts range (leave blank for none),
# and port numbers or service names from /etc/services to be allowed for hosts in this range (leave blank for any).
NotSoTrustedHosts="85.207.56.211"
NotSoTrustedHostsPorts="22,5900"

# Specify public hosts range (set to 'any' for any, leave blank for none).
# and port numbers or service names from /etc/services to be allowed for hosts in this range (leave blank for any).
PublicHosts="any"
PublicHostsPorts="80,2170"

# ============================================================================= #
# CONFIGURATION ENDS HERE
# PLEASE DO NOT EDIT THE FOLLOWING PART UNLESS YOU KNOW WHAT YOU'RE DOING #
# ============================================================================= #

# Flush current firewall settings.
/sbin/ipfw -f flush

# Allow local loopback communication.
/sbin/ipfw -f add 01000 allow ip from any to any via lo*

# Prevent spoofing attacks via localhosts and source routed.
/sbin/ipfw -f add 02000 deny log ip from 127.0.0.0/8 to any in;
/sbin/ipfw -f add 02100 deny log ip from any to 127.0.0.0/8 in;
/sbin/ipfw -f add 02200 deny log ip from any to any ipoptions ssrr,lsrr in;
/sbin/ipfw -f add 02300 deny log tcp from any to any frag;

# Deny tcp multicast packets.
/sbin/ipfw -f add 02400 deny log tcp from any to 224.0.0.0/3 in

# Deny scanning for port 0 to see if the machine is there.
/sbin/ipfw -f add 02500 deny log tcp from any to any 0 in

# Create dynamic rules for all connections originating from me and allow incoming traffic for these connections.
/sbin/ipfw -f add 03000 check-state
/sbin/ipfw -f add 03100 allow tcp from me to any keep-state
/sbin/ipfw -f add 03200 allow udp from me to any keep-state

# Disable all established connections without dynamic rules.
/sbin/ipfw -f add 03300 deny log tcp from any to any established

# Allow all traffic for ${TrustedHosts}.
if [ "${TrustedHosts}" ]; then
   /sbin/ipfw -f add 04000 allow ip from ${TrustedHosts} to any ${TrustedHostsPorts} in;
fi

# Allow specific services for ${NotSoTrustedHosts}.
if [ "${NotSoTrustedHosts}" ]; then
   /sbin/ipfw -f add 05000 allow ip from ${NotSoTrustedHosts} to any ${NotSoTrustedHostsPorts} in;
fi

# Allow specific services for public hosts.
if [ "${PublicHosts}" ]; then
   /sbin/ipfw -f add 06000 allow ip from ${PublicHosts} to any ${PublicHostsPorts} in;
fi

# Allow outgoing ICMP
/sbin/ipfw -f add 07000 allow icmp from any to any out

# Deny all other connections
/sbin/ipfw -f add 65534 deny log ip from any to any
/sbin/ipfw -f add 65535 allow ip from any to any

# ================================================================= #
# TEST EXISTENCE OF STARTUP SCRIPT, CREATE ONE IF IT DOES NOT EXIST #
# ================================================================= #

if [ ! -f /Library/LaunchDaemons/localhost.firewall.plist -a -f /etc/rc.firewall ]; then
   echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>i" > /Library/LaunchDaemons/localhost.firewall.plist
   echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "<plist version=\"1.0\">" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "<dict>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <key>Label</key>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <string>localhost.firewall</string>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <key>ProgramArguments</key>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <array>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "      <string>/etc/rc.firewall</string>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   </array>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <key>RunAtLoad</key>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <true/>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <key>ServiceDescription</key>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "   <string>Firewall: load ipsw firewall rules</string>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "</dict>" >> /Library/LaunchDaemons/localhost.firewall.plist
   echo "</plist>" >> /Library/LaunchDaemons/localhost.firewall.plist;
   echo
   echo "Startup script generated and saved in /Library/LaunchDaemons/localhost.firewall.plist"
   echo
fi


3) Pokud jste soubor správně uložili do /etc/rc.firewall, vytvoří se spouštěcí skript v /Library/LaunchDaemons/localhost.firewall.plist, který zajistí automatické spuštění firewallu při startu počítače. Tento skript můžete vytvořit i ručně, jeho obsah by měl vypadat asi takto:
Kód: Vybrat vše
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>localhost.firewall</string>
   <key>ProgramArguments</key>
   <array>
      <string>/etc/rc.firewall</string>
   </array>
   <key>RunAtLoad</key>
   <true/>
   <key>ServiceDescription</key>
   <string>Firewall: load ipsw firewall rules</string>
</dict>
</plist>


4/ Restartujte počítač, ověřte, že se pravidla zavedla zadáním příkazu sudo ipfw list.

Pokud vás zajímá jakákoliv část konfigurace, rád vše vysvětlím. Strávil jsem nad sestavováním těchto pravidel několik dní, snad se mi to podařilo "vychytat" :)