Ensure ufw Default Deny Policy for Incoming Connections

Description

A default deny policy on incoming connections ensures that any unconfigured inbound network traffic will be rejected. Set DEFAULT_INPUT_POLICY to DROP or REJECT in /etc/default/ufw. Note: Any port or protocol without an explicit allow before the default deny will be blocked.

Rationale

With a default accept policy the firewall will accept any incoming packet that is not configured to be denied. It is easier to allow acceptable usage than to block unacceptable usage.

Remediation

Shell script

The following script can be run on the host to remediate the issue.

#!/bin/bash

# Remediation is applicable only in certain platforms
if dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$' && { dpkg-query --show --showformat='${db:Status-Status}' 'ufw' 2>/dev/null | grep -q '^installed$'; }; then

ufw default deny incoming

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi

Warning

Changing firewall settings while connected over network can result in being locked out of the system.