Skip to content

Instantly share code, notes, and snippets.

@noize-e
Created May 29, 2019 06:48
Show Gist options
  • Save noize-e/a3b13553e89f240b769aa4a89f15b127 to your computer and use it in GitHub Desktop.
Save noize-e/a3b13553e89f240b769aa4a89f15b127 to your computer and use it in GitHub Desktop.
macOS socketfilterfw firewall decorator
#!/usr/bin/env bash
set -o errexit
set -o errtrace
usage() {
printf "\
macOS socketfilterfw decorator.
firewall [-command] [args]
-list List secured apps status
-help Display descorator help
-native Display socketfilterfw help
-app [add <path>] Block application incoming requests
[remove <path>] Whitelist application for in-reqs
-enable [1|0] Turn off the application firewall
Enables:
- setblockall
- setstealthmode
Disable:
- setallowsigned off
-status Display options:
- getglobalstate
- getallowsigned
- getstealthmode
- getloggingopt
- getloggingmode
- getblockall
"
}
execute(){
local path='/usr/libexec/ApplicationFirewall/'
if [[ -f ${path}/socketfilterfw ]] ; then
sudo ${path}/socketfilterfw $@;
else
echo "[err] 'socketfilterfw' binary not found"
return 0
fi
}
case "${1:--help}" in
-enable)
if (( ${2:?err-arg [1|0]} )) ; then
execute --setglobalstate on
execute --setblockall on
execute --setstealthmode on
execute --setallowsigned off
else
execute --setglobalstate off
fi
;;
-app)
execute ${2:?err-arg [add|remove]} "${3:?err-arg [path]}"
;;
-status)
execute --getglobalstate
execute --getstealthmode
execute --getloggingmode
execute --getloggingopt
execute --getblockall
execute --getallowsigned
;;
-list)
execute --listapps | while read line; do
echo "${line}" | awk 'NF'
done
;;
-help)
usage
;;
-native)
execute ${2:--help}
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment