Class: Shodan::Query

Inherits:
Object
  • Object
show all
Includes:
HasPages
Defined in:
lib/shodan/query.rb

Constant Summary collapse

SEARCH_URL =

Search URL

'http://shodan.surtri.com/'
RESULTS_PER_PAGE =

Results per page

20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasPages

#[], #each, #each_host, #each_on_page, #each_on_pages, #each_page, #first_host, #first_page, #host_at, #pages

Constructor Details

#initialize(options = {}) {|query| ... } ⇒ Query

Creates a new Query object.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :query (String)

    The query expression.

  • :countries (Array<String>)

    The Country Codes to search within.

  • :country (String)

    A Country Code to search within.

  • :hostnames (Array<String>)

    The host names to search for.

  • :hostname (String)

    A host name to search for.

  • :networks (Array<String>)

    The CIDR network blocks to search within.

  • :network (String)

    A CIDR network blocks to search within.

  • :ports (Array<Integer>)

    The ports to search for.

  • :port (Integer)

    A port to search for.

Yields:

  • (query)

    If a block is given, it will be passed the newly created Query object.

Yield Parameters:

  • query (Query)

    The newly created Query object.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/shodan/query.rb', line 96

def initialize(options={},&block)
  @agent = Shodan.web_agent
  @query = options[:query]

  @countries = []

  if options[:countries]
    @countries += options[:countries]
  elsif options[:country]
    @countries << option[:country]
  end

  @hostnames = []

  if options[:hostnames]
    @hostnames += options[:hostnames]
  elsif options[:hostname]
    @hostnames << options[:hostname]
  end

  @networks = []

  if options[:networks]
    @networks += options[:networks]
  elsif options[:network]
    @networks << options[:network]
  end

  @ports = []

  if options[:ports]
    @ports += options[:ports]
  elsif options[:port]
    @ports << options[:port]
  end

  block.call(self) if block
end

Instance Attribute Details

#countriesObject

Countries to search within



45
46
47
# File 'lib/shodan/query.rb', line 45

def countries
  @countries
end

#hostnamesObject

Hostnames to search for



48
49
50
# File 'lib/shodan/query.rb', line 48

def hostnames
  @hostnames
end

#networksObject

CIDR Network blocks to search within



51
52
53
# File 'lib/shodan/query.rb', line 51

def networks
  @networks
end

#portsObject

Ports to search for



54
55
56
# File 'lib/shodan/query.rb', line 54

def ports
  @ports
end

#queryObject

Search query



42
43
44
# File 'lib/shodan/query.rb', line 42

def query
  @query
end

Class Method Details

.from_url(url) ⇒ Query

Converts a given URL into a Query object.

Parameters:

Returns:

  • (Query)

    The Query object created from the URL.



144
145
146
147
148
149
150
# File 'lib/shodan/query.rb', line 144

def self.from_url(url)
  url = URI(url.to_s)

  return self.new(
    :query => url.query_params['q'].gsub('+',' ')
  )
end

Instance Method Details

#expressionString

The query expression from the query.

Returns:

  • (String)

    The query expression.



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/shodan/query.rb', line 170

def expression
  expr = []

  expr << @query if @query

  expr += @countries.map { |code| "country:#{code}" }
  expr += @hostnames.map { |host| "hostname:#{host}" }
  expr += @networks.map { |net| "net:#{net}" }
  expr += @ports.map { |port| "port:#{port}" }

  return expr.join(' ')
end

#page(index) ⇒ Page

Requests a page at the given index.

Parameters:

  • index (Integer)

    The index of the page.

Returns:

  • (Page)

    The page at the specified index.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/shodan/query.rb', line 224

def page(index)
  Page.new do |new_page|
    doc = @agent.get(page_url(index))

    doc.search('#search/div.result').each do |result|
      div = result.at('div')

      ip = if (a = div.at('a'))
             a.inner_text
           else
             div.children.first.inner_text
           end

      hostname = if (host_node = result.at('div/a:last'))
                   host_node.inner_text
                 end

      date = result.at('div/span').inner_text.scan(/\d+\.\d+\.\d/).first
      response = result.at('p').inner_text.strip

      new_page << Host.new(ip,date,response,hostname)
    end
  end
end

#page_url(index) ⇒ Object

The search URL for the query and given page index.

Parameters:

  • index (Integer)

    The page index to lookup.



205
206
207
208
209
210
211
212
213
# File 'lib/shodan/query.rb', line 205

def page_url(index)
  url = search_url

  unless index == 1
    url.query_params['page'] = index
  end

  return url
end

#results_per_pageInteger

The results per page.

Returns:

  • (Integer)

    The resutls per page.

See Also:



160
161
162
# File 'lib/shodan/query.rb', line 160

def results_per_page
  RESULTS_PER_PAGE
end

#search_urlURI::HTTP

The search URL for the query.

Returns:



189
190
191
192
193
194
# File 'lib/shodan/query.rb', line 189

def search_url
  url = URI(SEARCH_URL)

  url.query_params['q'] = expression
  return url
end