To enable the firewall on macOS, you should do the following
Open System Preferences
On your Mac, click on the Apple icon in the top left hand corner of the screen
Next, click on System Preferences
Click on Security & Privacy
Click Firewall
If the padlock at the bottom left is locked, click it to unlock it
Click on the ‘Turn On Firewall’ button
Next click on the ‘Firewall Options’ button
Next tick the box next to “Block all incoming connections”
Click OK to apply that
We’ll break this down, by turning on the Firewall we restricted the potential routes into our mac
Then by blocking all incoming connections, we ensured that any attempt to access our machine will be blocked, choosing this option also enabled ‘Stealth Mode’. Stealth Mode prevents responses to some common network communication, so other machines looking for devices on the network won’t see you, it doesn’t make you invisible however, and properly monitored networks will still be aware of you, it just gives you a bit of extra anonymity on public WiFi.