This is a quick way to use nmap and ufw to figure out what ports are open, but not being used.
I need to clean up my UFW firewall. Here's a list of the rules, followed by an external nmap of the server. make a command to remove unusued ports.
(Pasted in a copy of the results of <code>sudo nmap example.com)</code>
(Pasted in the results of <code>sudo ufw status numbered)</code>Code language: HTML, XML (xml)
Example nmap output, taken from outside of the server:
21/tcp closed ftp
22/tcp open ssh
25/tcp closed smtp
53/tcp closed domain
80/tcp open http
110/tcp closed pop3
143/tcp closed imap
443/tcp open https
465/tcp closed smtps
873/tcp open rsync
Example uwf status numbered output, taken from inside the server:
[ 5] 22/tcp ALLOW IN Anywhere
[ 6] 443/tcp ALLOW IN Anywhere
[ 7] 80/tcp ALLOW IN Anywhere
[ 8] 143/tcp ALLOW IN Anywhere
[ 9] 993/tcp ALLOW IN Anywhere
[10] 25/tcp ALLOW IN Anywhere
[11] 465/tcp ALLOW IN Anywhere
[12] 110 ALLOW IN Anywhere
[13] 995 ALLOW IN Anywhere
The prompt matches ports to rules. If a port is “closed” but the firewall says “ALLOW IN”, it means the port is open, but it doesn’t go to any service.
A good example is port 25. I had port 25 opened, but the nmap scan says it’s closed.
The prompt will produce a command that deletes the ufw firewall rules.
for p in 21/tcp 25 53 110 143 465 993 995 1022 1723 \
3030 3031 6667 7070 8001 8008 8021 8031 8080 \
9000 9001 9002 9010 9090 9091; do
sudo ufw delete allow "$p"
doneCode language: JavaScript (javascript)