Step 1:
Backup existing firewall rules
sudo cp /etc/ufw/before.rules /etc/ufw/before.rules_backup
Step 2 :
Actual task is to replace Accept with Drop in firewall rules as below
Step 3:
Run below sed command to find and replace
sudo sed -i '/ufw-before-input.*icmp/s/ACCEPT/DROP/g' /etc/ufw/before.rules
If firewall already enabled then use reload command . If not enabled then use enable command as follows
sudo ufw enable or sudo ufw reload