Use Nmap To Automate Insecure Ports Check

While go cloud is a prevailing trend, security is something we can’t afford to ignore. People hate malicious access. Periodically check all widely open TCP Ports is one good practice to secure our system in cloud. Obviously DB ports can’t be exposed to the whole internet. Our internal REST API also need to be protected.

We should make sure firewall is properly configured. What’s more important, we need to be always on top of these security holes with minimum efforts. So let’s automate the audit process of insecure TCP Ports.

Use Nmap To Automate Insecure Ports Check



1.1 Episode 1: Identity Insecure Ports Exported In My Servers.

Here are general principles for firewall configuration.

  • Only allow public access to very few ports, like http(80), https(443), etc.
  • For sshd(22), only selective source ip can connect.
  • For DB ports, like mysql(3306), elasticsearch(9200), we don’t expose them directly. Key members can connect through ssh protocol by ssh tunnel.
  • For traffic within the cluster, the default policy is always accept. If we can configure them with more limited privilege, that’s nice but not always pratical. Developers are usually too busy to list all traffic rules correctly and precisely. Even if they do, the rules will change constantly from time to time.

Our first step is listing all TCP ports which are open to the world. If some are against above principles, we raise an alert. Here we can use Nmap, network mapper. It is an open source tool for network exploration and security auditing.

 1: # Install Nmap package
 2: sudo apt-get install nmap
 3: # Scan all TCP ports for a given host
 4: sudo nmap -sS -PN 192.168.0.164
 5: # === Run: sudo nmap -sS -PN 192.168.0.164
 6: #
 7: # Starting Nmap 6.40 ( http://nmap.org )
 8: # Nmap scan report for 192.168.0.164
 9: # Host is up (0.00051s latency).
10: # Not shown: 997 filtered ports
11: # PORT    STATE SERVICE
12: # 22/tcp  open  ssh
13: # 80/tcp  open  http
14: # 443/tcp open  https

In this post, we won’t introduce the detail usage of Nmap, which is definitely a versatile tool. We want to run the check as fast as possible. Thus we use TCP SYN(-sS) to test. And skip host discovery (-PN), assuming the sever is up.

By default Nmap scans the top 1000 most popular ports, according to the statistics generated from Internet-wide scans and large internal network scans from the summer of 2008. We may have some extra ports to scan. Here is how:

# Check certain TCP ports
sudo nmap -p T:9200-9500,8090-8100 \
    -sS 192.168.0.164

1.2 Episode 2: Automate Check Process And Get Alerts Automatically.

Now we can list all open Ports. Mostly there are a bunch of servers to check. And we need to check the output careful to detect potential issues. Definitely we don’t want to do that manually. What’s more, things will change all the time This means we will have to do it again and again. It’s way too much for human. Boring and error-prone!

To automate the process, we need to provide 3 things:

  • Sever list to check. The list might be stable or dynamic which can be retrieved from other systems.
  • Whitelist for open ports. The rule may apply to all servers or only certain servers.
  • Extra ports to scan, other than the default 1000 ports.

1.3 Put It All Together.

Here comes Jenkins job (NetworkTCPScanAuditReport), which runs daily. If it fails, we will be notified by emails or slacks!

Notice: You can find a live demo here.

github_tcp_scan_report.png

Thanks To coyee for Chinese Translation of this post.

More Reading: List The Slowest Steps In Deployment

linkedin
github
slack

PRs Welcome

Blog URL: https://www.dennyzhang.com/nmap_port_scan


One Response to Use Nmap To Automate Insecure Ports Check

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.