Nmap shodan-api NSE Script


This page contains detailed information about how to use the shodan-api NSE script. For list of all NSE scripts, visit the Nmap NSE Library.

Select:
Overview
Error Messages

Script Overview


Script source code: https://github.com/nmap/nmap/tree/master/scripts/shodan-api.nse
Script categories: discovery, safe, external
Target service / protocol: -
Target network port(s): -
List of CVEs: -

Script Description


The shodan-api.nse script queries Shodan API for given targets and produces similar output to a -sV nmap scan. The ShodanAPI key can be set with the 'apikey' script argument, or hardcoded in the .nse file itself. You can get a free key from

N.B if you want this script to run completely passively make sure to include the -sn -Pn -n flags.

Shodan-api NSE Script Arguments


This is a full list of arguments supported by the shodan-api.nse script:

shodan-api.apikey

Specify the ShodanAPI key. This can also be hardcoded in the nse file.

shodan-api.outfile

Write the results to the specified CSV file

shodan-api.target

Specify a single target to be scanned.

smbdomain

The domain to log in with. If you aren't in a domain environment, then anything will (should?) be accepted by the server.

smbhash

A password hash to use when logging in. This is given as a single hex string (32 characters) or a pair of hex strings (both 32 characters, optionally separated by a single character). These hashes are the LanMan or NTLM hash of the user's password, and are stored on disk or in memory. They can be retrieved from memory using the fgdump or pwdump tools.

smbnoguest

Use to disable usage of the 'guest' account.

smbpassword

The password to connect with. Be cautious with this, since some servers will lock accounts if the incorrect password is given. Although it's rare that the Administrator account can be locked out, in the off chance that it can, you could get yourself in trouble. To use a blank password, leave this parameter off altogether.

smbtype

The type of SMB authentication to use. These are the possible options:

  • v1: Sends LMv1 and NTLMv1.
  • LMv1: Sends LMv1 only.
  • NTLMv1: Sends NTLMv1 only (default).
  • v2: Sends LMv2 and NTLMv2.
  • LMv2: Sends LMv2 only.
  • NTLMv2: Doesn't exist; the protocol doesn't support NTLMv2 alone. The default, NTLMv1, is a pretty decent compromise between security and compatibility. If you are paranoid, you might want to use v2 or lmv2 for this. (Actually, if you're paranoid, you should be avoiding this protocol altogether!). If you're using an extremely old system, you might need to set this to v1 or lm, which are less secure but more compatible. For information, see smbauth.lua.

smbusername

The SMB username to log in with. The forms "DOMAIN\username" and "username@DOMAIN" are not understood. To set a domain, use the smbdomain argument.

slaxml.debug

Debug level at which default callbacks will print detailed parsing info. Default: 3

http.host

The value to use in the Host header of all requests unless otherwise set. By default, the Host header uses the output of stdnse.get_hostname().

http.max-body-size

Limit the received body to specific number of bytes. An oversized body results in an error unless script argument http.truncated-ok or request option truncated_ok is set to true. The default is 2097152 (2MB). Use value -1 to disable the limit altogether. This argument can be overridden case-by-case with request option max_body_size.

http.max-cache-size

The maximum memory size (in bytes) of the cache.

http.max-pipeline

If set, it represents the number of outstanding HTTP requests that should be sent together in a single burst. Defaults to http.pipeline (if set), or to what function get_pipeline_limit returns.

http.pipeline

If set, it represents the number of HTTP requests that'll be sent on one connection. This can be set low to make debugging easier, or it can be set high to test how a server reacts (its chosen max is ignored).

http.truncated-ok

Do not treat oversized body as error. (Use response object flag truncated to check if the returned body has been truncated.) This argument can be overridden case-by-case with request option truncated_ok.

http.useragent

The value of the User-Agent header field sent with requests. By default it is "Mozilla/5.0 (compatible; Nmap Scripting Engine; https://nmap.org/book/nse.html)". A value of the empty string disables sending the User-Agent header field.

- - -
To use these script arguments, add them to the Nmap command line using the --script-args arg1=value,[arg2=value,..] syntax. For example:

nmap --script=shodan-api --script-args shodan-api.apikey=value,shodan-api.outfile=value <target>

Shodan-api NSE Script Example Usage


Here's an example of how to use the shodan-api.nse script:

nmap --script shodan-api x.y.z.0/24 -sn -Pn -n --script-args 'shodan-api.outfile=potato.csv,shodan-api.apikey=SHODANAPIKEY'

nmap --script shodan-api --script-args 'shodan-api.target=x.y.z.a,shodan-api.apikey=SHODANAPIKEY'

Shodan-api NSE Script Example Output


Here's a sample output from the shodan-api.nse script:

| shodan-api: Report for 2600:3c01::f03c:91ff:fe18:bb2f (scanme.nmap.org)
| PORT	PROTO	PRODUCT      VERSION
| 80   tcp   Apache httpd
| 3306 tcp   MySQL        5.5.40-0+wheezy1
| 22   tcp   OpenSSH      6.0p1 Debian 4+deb7u2
|_443  tcp

Shodan-api NSE Script Example XML Output


Here's a sample XML output from the shodan-api.nse script produced by providing the -oX <file> Nmap option:

 <table key="hostnames">
   <elem>scanme.nmap.org</elem>
 </table>
 <table key="ports">
   <table>
     <elem key="protocol">tcp</elem>
     <elem key="number">22</elem>
   </table>
   <table>
     <elem key="version">2.4.7</elem>
     <elem key="product">Apache httpd</elem>
     <elem key="protocol">tcp</elem>
     <elem key="number">80</elem>
   </table>
 </table>

Author


References


See Also


Visit Nmap NSE Library for more scripts.

The shodan-api.nse script may fail with the following error messages. Check for the possible causes by using the code snippets highlighted below found in the script source code. This can often times help in identifying the root cause of the problem.

Error parsing Shodan response: %s


Here is a relevant code snippet related to the "Error parsing Shodan response: %s" error message:

90:	    return nil
91:	  end
92:	
93:	  local stat, resp = json.parse(response.body)
94:	  if not stat then
95:	    stdnse.debug1("Error parsing Shodan response: %s", resp)
96:	    return nil
97:	  end
98:	
99:	  return resp
100:	end

Unable to query data


Here is a relevant code snippet related to the "Unable to query data" error message:

120:	      }
121:	      tab.addrow(tab_out, e.port, e.transport, e.product or "", e.version or "")
122:	    end
123:	    return out, tab.dump(tab_out)
124:	  else
125:	    return "Unable to query data"
126:	  end
127:	end
128:	
129:	prerule = function ()
130:	  if (outFile ~= nil) then

Error: Please specify your ShodanAPI key with the %s.apikey argument


Here is a relevant code snippet related to the "Error: Please specify your ShodanAPI key with the %s.apikey argument" error message:

136:	  if registry.apiKey == "" then
137:	    registry.apiKey = nil
138:	  end
139:	
140:	  if not registry.apiKey then
141:	    stdnse.verbose1("Error: Please specify your ShodanAPI key with the %s.apikey argument", SCRIPT_NAME)
142:	    return false
143:	  end
144:	
145:	  local response = http.get("api.shodan.io", 443, "/api-info?key=" .. registry.apiKey, {any_af=true})
146:	  if (response.status ~= 200) then

Error: Your ShodanAPI key (%s) is invalid


Here is a relevant code snippet related to the "Error: Your ShodanAPI key (%s) is invalid" error message:

142:	    return false
143:	  end
144:	
145:	  local response = http.get("api.shodan.io", 443, "/api-info?key=" .. registry.apiKey, {any_af=true})
146:	  if (response.status ~= 200) then
147:	    stdnse.verbose1("Error: Your ShodanAPI key (%s) is invalid", registry.apiKey)
148:	    -- Prevent further stages from running
149:	    registry.apiKey = nil
150:	    return false
151:	  end
152:	

Error: %s.target must be an IP address


Here is a relevant code snippet related to the "Error: %s.target must be an IP address" error message:

151:	  end
152:	
153:	  if arg_target then
154:	    local is_ip, err = ipOps.expand_ip(arg_target)
155:	    if not is_ip then
156:	      stdnse.verbose1("Error: %s.target must be an IP address", SCRIPT_NAME)
157:	      return false
158:	    end
159:	    return true
160:	  end
161:	end

Version


This page has been created based on Nmap version 7.92.

Go back to menu.