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'

)



474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/pwn/plugins/shodan.rb', line 474

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

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'api-info',
    params: params
  )
rescue StandardError => e
  raise e
end

.authorsObject

Author(s)

0day Inc. <[email protected]>



531
532
533
534
535
# File 'lib/pwn/plugins/shodan.rb', line 531

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

.get_uris(opts = {}) ⇒ Object

Supported Method Parameters

uri_arr = PWN::Plugins::Shodan.get_uris(

search_results: 'required - search_results object returned from #search method'

)



517
518
519
520
521
522
523
524
525
526
527
# File 'lib/pwn/plugins/shodan.rb', line 517

public_class_method def self.get_uris(opts = {})
  search_results = opts[:search_results]

  search_results.map do |search_results_hash|
    extract_uris(
      search_results_hash: search_results_hash
    )
  end.flatten
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/pwn/plugins/shodan.rb', line 539

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'
    )

    uri_arr = #{self}.get_uris(
      search_results: 'required - search_results object returned from #search method'
    )

    #{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'

)



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/pwn/plugins/shodan.rb', line 493

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'

)



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

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

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/protocols',
    params: params
  )
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)'

)



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/pwn/plugins/shodan.rb', line 411

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

  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/query/tags',
    params: params
  )
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'

)



438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/pwn/plugins/shodan.rb', line 438

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

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'account/profile',
    params: params
  )
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'

)



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

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'

)



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/pwn/plugins/shodan.rb', line 255

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

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/ports',
    params: params
  )
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'

)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pwn/plugins/shodan.rb', line 164

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

  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/count',
    params: params
  )
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)'

)



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/pwn/plugins/shodan.rb', line 383

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
  }

  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/query',
    params: params
  )
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)'

)



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

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}"
  shodan_rest_call(
    http_method: :post,
    api_key: api_key,
    rest_call: 'shodan/scan/internet',
    params: params,
    http_body: http_body
  )
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'

)



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

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}"
  shodan_rest_call(
    http_method: :post,
    api_key: api_key,
    rest_call: 'shodan/scan',
    params: params,
    http_body: http_body
  )
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',

)



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

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
  }

  shodan_rest_call(
    api_key: api_key,
    rest_call: "shodan/scan/status/#{scan_id}",
    params: params
  )
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'

)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/pwn/plugins/shodan.rb', line 199

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

  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/search',
    params: params
  )
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'

)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pwn/plugins/shodan.rb', line 135

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(response)
  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'

)



362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/pwn/plugins/shodan.rb', line 362

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

  params = { key: api_key }
  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/services',
    params: params
  )
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',

)



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/pwn/plugins/shodan.rb', line 232

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
  }

  shodan_rest_call(
    api_key: api_key,
    rest_call: 'shodan/host/search/tokens',
    params: params
  )
rescue StandardError => e
  raise e
end