Problem overview
A disabled firewall on macOS can leave endpoints vulnerable to unauthorized network access and malicious traffic, especially when devices operate outside secure corporate environments. Detecting this issue proactively helps maintain a stronger security posture and reduces the chance of breaches.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-macos-monitor-firewall
# Get the status of the firewall
FIREWALL_STATUS=$(sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate)
# Check if the firewall is enabled
if [[ "$FIREWALL_STATUS" == *"Firewall is enabled"* ]]; then
echo "SUCCESS: Firewall is enabled."
exit 0
else
echo "ALERT: Firewall is disabled."
exit 1
fi This script uses the built-in socketfilterfw command to retrieve the firewall’s status on a macOS endpoint. If it’s enabled, the script reports success, and if it’s disabled, it raises an alert for immediate attention. You can set it up as a script-based monitor in Level so that you’re notified whenever the firewall is found to be off.
You can also integrate it into a scheduled Automation in Level, running periodic checks to enforce consistent security standards across your macOS fleet, ensuring no device remains unprotected.
Use cases
- Monitoring critical workstations or servers for disabled firewalls
- Ensuring compliance with organizational security requirements
- Detecting user-initiated or accidental firewall disablement
- Proactively safeguarding remote or mobile macOS devices
Recommendations
- Test the script on a sample device to confirm firewall checks work as intended
- Use a script-based monitor in Level to receive real-time notifications
- Schedule periodic scans with a Level Automation for comprehensive coverage
- Consider automatic remediation if the firewall is disabled, such as re-enabling it via a separate script
- Keep logs of alerts to spot trends or repeated disablement