Module: PWN::Plugins::Shodan

Defined in:
lib/pwn/plugins/shodan.rb

Overview

This plugin is used for interacting w/ Shodan’s REST API using the ‘rest’ browser type of PWN::Plugins::TransparentBrowser.

This is based on the following Shodan API Specification:

developer.shodan.io/api

Constant Summary collapse

@@logger =
PWN::Plugins::PWNLogger.create

Class Method Summary collapse

Class Method Details

.api_info(opts = {}) ⇒ Object

Supported Method Parameters

api_info = PWN::Plugins::Shodan.api_info(

api_key: 'required shodan api key'

)



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/pwn/plugins/shodan.rb', line 431

public_class_method def self.api_info(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'api-info',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.authorsObject

Author(s)

0day Inc. <[email protected]>



472
473
474
475
476
# File 'lib/pwn/plugins/shodan.rb', line 472

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.helpObject

Display Usage for this Module



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/pwn/plugins/shodan.rb', line 480

public_class_method def self.help
  puts "USAGE:
    services_by_ips = #{self}.services_by_ips(
      api_key: 'required - shodan api key',
      target_ips: 'required - comma-delimited list of ip addresses to target'
    )

    query_result_totals = PWN::Plugins::Shodan.query_result_totals(
      api_key: 'required shodan api key',
      query: 'required - shodan search query',
      facets: 'optional - comma-separated list of properties to get summary information'
    )

    search_results = #{self}.search(
      api_key: 'required shodan api key',
      query: 'required - shodan search query',
      facets: 'optional - comma-separated list of properties to get summary information'
    )

    tokens_result = #{self}.tokens(
      api_key: 'required shodan api key',
      query: 'required - shodan search query',
    )

    ports_shodan_crawls = #{self}.ports_shodan_crawls(
      api_key: 'required shodan api key'
    )

    protocols = #{self}.list_on_demand_scan_protocols(
      api_key: 'required shodan api key'
    )

    scan_network_response = #{self}.scan_network(
      api_key: 'required shodan api key',
      target_ips: 'required - comma-delimited list of ip addresses to target'
    )

    scan_internet_response = #{self}.scan_internet(
      api_key: 'required shodan api key',
      port: 'required - port to scan (see #ports_shodan_crawls for list)',
      protocol: 'required - supported shodan protocol (see #list_on_demand_scan_protocols for list)'
    )

    scan_status_result = #{self}.scan_status(
      api_key: 'required shodan api key',
      scan_id: 'required - unique ID returned by #scan_network',
    )

    services_shodan_crawls = #{self}.services_shodan_crawls(
      api_key: 'required shodan api key'
    )

    saved_search_queries_result = #{self}.saved_search_queries(
      api_key: 'required shodan api key',
      page: 'optional - page number to iterate over results (each page contains 10 items)',
      sort: 'optional - sort results by available parameters :votes|:timestamp',
      order: 'optional - sort :asc|:desc (ascending or descending)'
    )

    most_popular_tags_result = #{self}.most_popular_tags(
      api_key: 'required shodan api key',
      result_count: 'optional - number of results to return (defaults to 10)'
    )

    my_profile = #{self}.my_profile(
      api_key: 'required shodan api key'
    )

    my_pub_ip = #{self}.my_pub_ip(
      api_key: 'required shodan api key'
    )

    api_info = #{self}.api_info(
      api_key: 'required shodan api key'
    )

    honeypot_probability_scores = #{self}.honeypot_probability_scores(
      api_key: 'required shodan api key',
      target_ips: 'required - comma-delimited list of ip addresses to target'
    )

    #{self}.authors
  "
end

.honeypot_probability_scores(opts = {}) ⇒ Object

Supported Method Parameters

honeypot_probability_scores = PWN::Plugins::Shodan.honeypot_probability_scores(

api_key: 'required shodan api key',
target_ips: 'required - comma-delimited list of ip addresses to target'

)



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/pwn/plugins/shodan.rb', line 451

public_class_method def self.honeypot_probability_scores(opts = {})
  api_key = opts[:api_key].to_s.scrub
  target_ips = opts[:target_ips].to_s.scrub.gsub(/\s/, '').split(',')

  honeypot_probability_scores = []
  params = { key: api_key }
  target_ips.each do |target_ip|
    response = shodan_rest_call(
      api_key: api_key,
      rest_call: "labs/honeyscore/#{target_ip}",
      params: params
    )
    honeypot_probability_scores.push("#{target_ip} => #{response}")
  end
  honeypot_probability_scores
rescue StandardError => e
  raise e
end

.list_on_demand_scan_protocols(opts = {}) ⇒ Object

Supported Method Parameters

protocols = PWN::Plugins::Shodan.list_on_demand_scan_protocols(

api_key: 'required shodan api key'

)



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/pwn/plugins/shodan.rb', line 223

public_class_method def self.list_on_demand_scan_protocols(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/protocols',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end
Supported Method Parameters

most_popular_tags_result = PWN::Plugins::Shodan.most_popular_tags(

api_key: 'required shodan api key',
result_count: 'optional - number of results to return (defaults to 10)'

)



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/pwn/plugins/shodan.rb', line 366

public_class_method def self.most_popular_tags(opts = {})
  api_key = opts[:api_key].to_s.scrub
  result_count = opts[:result_count].to_i

  if result_count
    params = {
      key: api_key,
      size: result_count
    }
  else
    params = { key: api_key }
  end

  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/query/tags',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.my_profile(opts = {}) ⇒ Object

Supported Method Parameters

my_profile = PWN::Plugins::Shodan.my_profile(

api_key: 'required shodan api key'

)



394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/pwn/plugins/shodan.rb', line 394

public_class_method def self.my_profile(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'account/profile',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.my_pub_ip(opts = {}) ⇒ Object

Supported Method Parameters

my_pub_ip = PWN::Plugins::Shodan.my_pub_ip(

api_key: 'required shodan api key'

)



413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/pwn/plugins/shodan.rb', line 413

public_class_method def self.my_pub_ip(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'tools/myip',
    params: params
  )
rescue StandardError => e
  raise e
end

.ports_shodan_crawls(opts = {}) ⇒ Object

Supported Method Parameters

ports_shodan_crawls = PWN::Plugins::Shodan.ports_shodan_crawls(

api_key: 'required shodan api key'

)



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/pwn/plugins/shodan.rb', line 204

public_class_method def self.ports_shodan_crawls(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/ports',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.query_result_totals(opts = {}) ⇒ Object

Supported Method Parameters

query_result_totals = PWN::Plugins::Shodan.query_result_totals(

api_key: 'required shodan api key',
query: 'required - shodan search query',
facets: 'optional - comma-separated list of properties to get summary information'

)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pwn/plugins/shodan.rb', line 110

public_class_method def self.query_result_totals(opts = {})
  api_key = opts[:api_key].to_s.scrub
  query = opts[:query].to_s.scrub
  facets = opts[:facets].to_s.scrub

  if facets
    params = {
      key: api_key,
      query: query,
      facets: facets
    }

  else
    params = {
      key: api_key,
      query: query
    }
  end

  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/count',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.saved_search_queries(opts = {}) ⇒ Object

Supported Method Parameters

saved_search_queries_result = PWN::Plugins::Shodan.saved_search_queries(

api_key: 'required shodan api key',
page: 'optional - page number to iterate over results (each page contains 10 items)',
sort: 'optional - sort results by available parameters :votes|:timestamp',
order: 'optional - sort :asc|:desc (ascending or descending)'

)



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/pwn/plugins/shodan.rb', line 338

public_class_method def self.saved_search_queries(opts = {})
  api_key = opts[:api_key].to_s.scrub
  page = opts[:page].to_i
  sort = opts[:sort].to_sym
  order = opts[:order].to_sym

  params = {
    key: api_key,
    page: page,
    sort: sort.to_s,
    order: order.to_s
  }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/query',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.scan_internet(opts = {}) ⇒ Object

Supported Method Parameters

scan_internet_response = PWN::Plugins::Shodan.scan_internet(

api_key: 'required shodan api key',
port: 'required - port to scan (see #ports_shodan_crawls for list)',
protocol: 'required - supported shodan protocol (see #list_on_demand_scan_protocols for list)'

)



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/pwn/plugins/shodan.rb', line 268

public_class_method def self.scan_internet(opts = {})
  api_key = opts[:api_key].to_s.scrub
  port = opts[:port].to_i
  protocol = opts[:protocol].to_s.scrub

  params = { key: api_key }
  http_body = "port=#{port}&protocol=#{protocol}"
  response = shodan_rest_call(
    http_method: :post,
    api_key: api_key,
    rest_call: 'shodan/scan/internet',
    params: params,
    http_body: http_body
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.scan_network(opts = {}) ⇒ Object

Supported Method Parameters

scan__networkresponse = PWN::Plugins::Shodan.scan_network(

api_key: 'required shodan api key',
target_ips: 'required - comma-delimited list of ip addresses to target'

)



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/pwn/plugins/shodan.rb', line 243

public_class_method def self.scan_network(opts = {})
  api_key = opts[:api_key].to_s.scrub
  target_ips = opts[:target_ips].to_s.scrub.gsub(/\s/, '')

  params = { key: api_key }
  http_body = "ips=#{target_ips}"
  response = shodan_rest_call(
    http_method: :post,
    api_key: api_key,
    rest_call: 'shodan/scan',
    params: params,
    http_body: http_body
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.scan_status(opts = {}) ⇒ Object

Supported Method Parameters

scan_status_result = PWN::Plugins::Shodan.scan_status(

api_key: 'required shodan api key',
scan_id: 'required - unique ID returned by #scan_network',

)



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/pwn/plugins/shodan.rb', line 293

public_class_method def self.scan_status(opts = {})
  api_key = opts[:api_key].to_s.scrub
  scan_id = opts[:scan_id].to_s.scrub

  params = {
    key: api_key
  }

  response = shodan_rest_call(
    api_key: api_key,
    rest_call: "shodan/scan/status/#{scan_id}",
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.search(opts = {}) ⇒ Object

Supported Method Parameters

search_results = PWN::Plugins::Shodan.search(

api_key: 'required shodan api key',
query: 'required - shodan search query',
facets: 'optional - comma-separated list of properties to get summary information'

)



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pwn/plugins/shodan.rb', line 146

public_class_method def self.search(opts = {})
  api_key = opts[:api_key].to_s.scrub
  query = opts[:query].to_s.scrub
  facets = opts[:facets].to_s.scrub

  if facets
    params = {
      key: api_key,
      query: query,
      facets: facets
    }
  else
    params = {
      key: api_key,
      query: query
    }
  end

  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/search',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.services_by_ips(opts = {}) ⇒ Object

Supported Method Parameters

services_by_ips = PWN::Plugins::Shodan.services_by_ips(

api_key: 'required shodan api key',
target_ips: 'required - comma-delimited list of ip addresses to target'

)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pwn/plugins/shodan.rb', line 81

public_class_method def self.services_by_ips(opts = {})
  api_key = opts[:api_key].to_s.scrub
  target_ips = opts[:target_ips].to_s.scrub.gsub(/\s/, '').split(',')

  services_by_ips = []
  params = { key: api_key }
  target_ips.each do |target_ip|
    response = shodan_rest_call(
      api_key: api_key,
      rest_call: "shodan/host/#{target_ip}",
      params: params
    )
    services_by_ips.push(JSON.parse(response, symbolize_names: true))
  rescue StandardError => e
    services_by_ips.push(error: e.message)
    next
  end
  services_by_ips
rescue StandardError => e
  raise e
end

.services_shodan_crawls(opts = {}) ⇒ Object

Supported Method Parameters

services_shodan_crawls = PWN::Plugins::Shodan.services_shodan_crawls(

api_key: 'required shodan api key'

)



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/pwn/plugins/shodan.rb', line 316

public_class_method def self.services_shodan_crawls(opts = {})
  api_key = opts[:api_key].to_s.scrub

  params = { key: api_key }
  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/services',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.tokens(opts = {}) ⇒ Object

Supported Method Parameters

tokens_result = PWN::Plugins::Shodan.tokens(

api_key: 'required shodan api key',
query: 'required - shodan search query',

)



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pwn/plugins/shodan.rb', line 180

public_class_method def self.tokens(opts = {})
  api_key = opts[:api_key].to_s.scrub
  query = opts[:query].to_s.scrub

  params = {
    key: api_key,
    query: query
  }

  response = shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/search/tokens',
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end