Faster nmap scanning with the help of GNU parallel
When you give access to developers to create firewall rules, they generally open all sorts of ports to the internet. All they need to do / expected to do is get the product working. The rest doesn’t matter.
This was the same with the company I worked with. There were tonnes of firewall rules in the GCP projects which opened many ports to the public internet. One could think of deleting firewall rules straight away and check if something is breaking on production. But that’s a bad solution.
Security measures, in my opinion, should not be at the cost of production downtime (*unless there is a very critical bug to patch on prod).
So the next idea was to scan all the public IPs of VMs and K8s nodes and get a list of closed ports. This will help us get a list of firewall rules that are no longer required.
Got a CSV with GCP project name and IP address assigned with the project. The CSV file looks something like the below (headers stripped off):
|
|
After looking at the time it took to complete nmap scan on one single IP for all ports, I was sure that running scan for 1000+ IPs would take a very long time.
GNU Parallel for the win !
GNU Parallel is a command line tool that allows to execute multiple jobs in parallel. A job can be a single command or a small script that has to be run for each of the lines in the input.
For my case, I want to run nmap scan in parallel.
|
|
The above code starts 32 parallel nmap scans. You can tune the number of jobs as per your network bandwidth and the CPU limit. The greppable output of the nmap scan is put into logs/{project}/{ip}.gnmap
files once the scan is done.
The other part of the job was to grep the closed ports from the nmap output files and report it in a parsable format.
|
|