Class: Shodan::WebAPI
- Inherits:
-
Object
- Object
- Shodan::WebAPI
- Defined in:
- lib/shodan/api.rb
Overview
The WebAPI class interfaces with the shodanhq.com/api It currently supports 2 methods:
-
search (query)
-
host (ip)
- Author
-
achillean (jmath at surtri.com)
:title:Shodan::WebAPI
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#dataloss ⇒ Object
Returns the value of attribute dataloss.
-
#exploitdb ⇒ Object
Returns the value of attribute exploitdb.
-
#msf ⇒ Object
Returns the value of attribute msf.
Instance Method Summary collapse
-
#count(query) ⇒ Object
Find how many results there are for a search term.
-
#host(ip) ⇒ Object
Get all available information on an IP.
-
#info(query) ⇒ Object
Returns information about the current API key.
-
#initialize(api_key) ⇒ WebAPI
constructor
A new instance of WebAPI.
-
#locations(query) ⇒ Object
Get a greater list of all cities and countries where the devices are located.
-
#request(func, args) ⇒ Object
Internal method that sends out the HTTP request.
-
#search(query, params = {}) ⇒ Object
Perform a search on Shodan.
Constructor Details
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
17 18 19 |
# File 'lib/shodan/api.rb', line 17 def api_key @api_key end |
#base_url ⇒ Object
Returns the value of attribute base_url.
18 19 20 |
# File 'lib/shodan/api.rb', line 18 def base_url @base_url end |
#dataloss ⇒ Object
Returns the value of attribute dataloss.
19 20 21 |
# File 'lib/shodan/api.rb', line 19 def dataloss @dataloss end |
#exploitdb ⇒ Object
Returns the value of attribute exploitdb.
20 21 22 |
# File 'lib/shodan/api.rb', line 20 def exploitdb @exploitdb end |
#msf ⇒ Object
Returns the value of attribute msf.
21 22 23 |
# File 'lib/shodan/api.rb', line 21 def msf @msf end |
Instance Method Details
#count(query) ⇒ Object
Find how many results there are for a search term.
Arguments: query - search query; same format as the website (string)
Returns a hash containing the total number of search results
81 82 83 |
# File 'lib/shodan/api.rb', line 81 def count(query) return request('count', {:q => query}) end |
#host(ip) ⇒ Object
Get all available information on an IP.
Arguments: ip - host IP (string)
Returns a hash containing the host information
60 61 62 |
# File 'lib/shodan/api.rb', line 60 def host(ip) return request('host', {:ip => ip}) end |
#info(query) ⇒ Object
Returns information about the current API key.
97 98 99 |
# File 'lib/shodan/api.rb', line 97 def info(query) return request('info', {}) end |
#locations(query) ⇒ Object
Get a greater list of all cities and countries where the devices are located.
Arguments: query - search query; same format as the website (string)
Returns a hash containing the search results
92 93 94 |
# File 'lib/shodan/api.rb', line 92 def locations(query) return request('locations', {:q => query}) end |
#request(func, args) ⇒ Object
Internal method that sends out the HTTP request. Expects a webservice function (ex. ‘search’) name and a hash of arguments.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/shodan/api.rb', line 33 def request(func, args) # Convert the argument hash into a string args_string = args.map{|k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&") # Craft the final request URL url = "#{@base_url}#{func}?key=#{@api_key}&#{args_string}" # Send the request response = Net::HTTP.get_response(URI.parse(url)) # Convert the JSON data into a native Ruby hash data = JSON.parse(response.body) # Raise an error if something went wrong if data.has_key? 'error' raise data['error'] end return data end |
#search(query, params = {}) ⇒ Object
Perform a search on Shodan.
Arguments: query - search query; same format as the website (string)
Returns a hash containing the search results
70 71 72 73 |
# File 'lib/shodan/api.rb', line 70 def search(query, params={}) params[:q] = query return request('search', params) end |