Configure firewall ports automatically by application
Mar 13, '06 06:59:00AM
Contributed by: Anonymous
With the recent security threats to OS X, I thought it would be a good idea to be able to automatically configure my firewall based on the applications I have open. If no application is using said port, it's closed. So with a little help from bash and AppleScript, I now have an application that automagically scans for open applications and configures the firewall accordingly. Enjoy!
Instructions:
- Go to the Firewall in System Preferences, and enable the firewall ports you want to activate dynamically.
- Go to the Terminal, and type sudo ipfw list. Take note of the ID numbers of the rules that concern these ports; it's the first column on the left.
- Type cd /usr/local/bin, followed by sudo pico configfw (and enter your password when prompted).
- Paste the following script:
#!/bin/bash
function psapp() {
ps -ax | grep -i "$1" | grep -i -v -q "grep.-i.$1"
}
function addrule () {
sudo ipfw -q delete "$2"
if psapp "$1"; then
sudo ipfw -q add $2 allow tcp from any to any dst-port $3 in
echo "***$1 port activated ($3)***"
else
echo "---$1 port deactivated ($3)---"
fi
}
addrule "Applicationname" "ruleid" "portnumber"
- In the addrule line, substitute Applicationname with the name of the application, ruleid with the number of the rule you noted in step two, and portnumber with the corresponding port number(s).
- Repeat adding a new addrule line for each of the applications you need to enable.
- Type Control-X, "Y", Enter.
- Open up Script Editor, and paste the following code (adding the username and password of an admin user):
do shell script "sudo /usr/local/bin/configfw" user name "yourusername"
password "xxxxxx" with administrator priveleges
- Save the script as an application, and put it where you want it.
Done! The ports will now be configured automatically for each of the applications that has an addrule line in configf every time you run the applescript application.
To confirm that it works, disable the ports in System Preferences, then run one of the applications, run the AppleScript and do sudo ipfw list in the terminal. The rule for the application should show up in the ipfw configuration. To see the script in action, with output and all, just do sudo configfw in the Terminal.
Comments (11)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20060307153931386