Class: Shodan::WebAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/shodan/api.rb

Overview

The WebAPI class interfaces with the shodanhq.com/api It currently supports 2 methods:

  1. search (query)

  2. host (ip)

Author

achillean (jmath at surtri.com)

:title:Shodan::WebAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ WebAPI

Returns a new instance of WebAPI.



23
24
25
26
27
28
29
# File 'lib/shodan/api.rb', line 23

def initialize(api_key)
  @api_key = api_key
  @base_url = "http://www.shodanhq.com/api/"
  @dataloss = DatalossDB.new(self)
  @exploitdb = ExploitDB.new(self)
  @msf = Msf.new(self)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



17
18
19
# File 'lib/shodan/api.rb', line 17

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



18
19
20
# File 'lib/shodan/api.rb', line 18

def base_url
  @base_url
end

#datalossObject

Returns the value of attribute dataloss.



19
20
21
# File 'lib/shodan/api.rb', line 19

def dataloss
  @dataloss
end

#exploitdbObject

Returns the value of attribute exploitdb.



20
21
22
# File 'lib/shodan/api.rb', line 20

def exploitdb
  @exploitdb
end

#msfObject

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