Scanning without Scanning with NMAP (APIs FTW)
A year ago I wrote up using Shodan's API to collect info on open ports and services without actually scanning for them (Shodan's API for the (Recon) Win!). This past week I was trolling through the NMAP scripts directory, and imagine my surprise when I stumbled on shodan-api.nse.
So the network scanner we all use daily can be used to scan without actually scanning? Apparently yes!
First the syntax:
nmap <target> --script shodan-api --script-args 'shodan-api.apikey=SHODANAPIKEY'
(note: use double quotes for script-args if you are doing this in Windows)
This still does a basic scan of the target host though. To do this without scanning, without even sending any packets to your host, add:
-sn do a ping scan (ie we're not doing a port scan)
-Pn Don't ping the host, just assume that it's online
Neat trick there eh? This essentially tells nmap to do nothing for each host in the target list, but don't forget that script we asked you to run!
This also has the advantage of doing the "scan" even if the host is down (or doesn't return on a ping)
Plus, just to be complete:
-n Don't even do DNS resolution
This way NMAP isn't sending anything to the host or even to hosts under the client's control (for instance if they happen to host their own DNS).
If you're doing a whole subnet, or the output is large enough to scroll past your buffer, or if you want much (much) more useful output, add this to your script-args clause:
shodan-api.outfile=outputfile.csv
Let's put this all together:
nmap -sn -Pn -n www.cisco.com --script shodan-api --script-args "shodan-api.outfile=out.csv,shodan-api.apikey=<my-api-key-not-yours>"
Starting Nmap 7.92 ( https://nmap.org ) at 2024-05-17 09:53 Eastern Daylight Time
Nmap scan report for www.cisco.com (184.26.152.97)
Host is up.
Host script results:
| shodan-api: Report for 184.26.152.97 (www.static-cisco.com, www.cisco.com, www.mediafiles-cisco.com, www-cloud-cdn.cisco.com, a184-26-152-97.deploy.static.akamaitechnologies.com)
| PORT PROTO PRODUCT VERSION
| 80 tcp AkamaiGHost
|_443 tcp AkamaiGHost
Post-scan script results:
| shodan-api: Shodan done: 1 hosts up.
|_Wrote Shodan output to: out.csv
Nmap done: 1 IP address (1 host up) scanned in 1.20 seconds
Neat eh? It collects the product and version info (when it can get it). The CSV file looks like this:
IP,Port,Proto,Product,Version
184.26.152.97,80,tcp,AkamaiGHost,
184.26.152.97,443,tcp,AkamaiGHost,
This file format is a direct import into a usable format in powershell, python or just about any tool you might desire, even Excel :-)
Looking at a more "challenging" scan target:
nmap -sn -Pn -n isc.sans.edu --script shodan-api --script-args "shodan-api.outfile=out.csv,shodan-api.apikey=<my-api-key-not-yours>"
IP,Port,Proto,Product,Version
45.60.103.34,25,tcp,,
45.60.103.34,43,tcp,,
45.60.103.34,53,tcp,,
45.60.103.34,53,udp,,
.. and so on.
Look at line 4! If you've ever done a UDP scan, you know that it can take for-e-ver! Since this is just an api call, it collects both tcp and udp info from Shodan.
How many ports are in the output?
type out.csv | wc -l
160
159 ports, that's how many! (subtract one for the header line) This would have taken a while with a regular port scan, but with a shodan query it finishes in how long?
Post-scan script results:
| shodan-api: Shodan done: 1 hosts up.
|_Wrote Shodan output to: out.csv
Nmap done: 1 IP address (1 host up) scanned in 1.20 seconds
Yup, 1.2 seconds!
This script is a great addition to nmap, it allows you to do a quick and dirty scan for what ports and services have been available recently, with a bit of rudimentary info attached.
Did you catch that last hint? If you're doing a pentest, it's well worth digging into that word "recently". Looking at ports that are in the shodan list, but aren't in a real portscan (that you'd get from nmap -sT or -sU) can be very interesting. These are services that the client has recently disabled, maybe just for the duration of the pentest. For instance, that FTP server or totally vulnerable web or application server that they have open "only when they need it" (translation: always, except for during the annual pentest). If you can pull a diff report between what's in the shodan output and what's actually there now, that's well worth looking into, say for instance using archive.org. If you do find something good, my bet is that it falls into your scope! If not, you should update your scope to "services found during the test in the target IP ranges or DNS scopes" or similar. You don't want something like this excluded simply because it's (kinda) not there during the actual assessment :-)
Got another API you'd like to see used in NMAP? Please use our comment form. Stay tuned I have a list, but if you've got one I haven't thought of I'm happy to add anohter one!
===============
Rob VandenBrink
rob<at>coherentsecurity.com
Comments