# Custom ShodanAPI Class :) # The pre-built option is broken and doesn't work in several places.... # So we re-wrote it! class ShodanAPI # Initialize ShodanAPI via passed API Key def initialize(apikey) @url="http://www.shodanhq.com/api/" if shodan_connect(apikey) @key=apikey end end # Check API Key against API Info Query # Return True on success, False on Error or Failure def shodan_connect(apikey) url = @url + "info?key=#{apikey}" begin c = Curl::Easy.perform(url) if c.body_str =~ /"unlocked_left": \d+, "telnet": .+, "plan": ".+", "https": .+, "unlocked": .+/i results = JSON.parse(c.body_str) @plan = results['plan'] @unlocked = results['unlocked'] @unlocks = results['unlocked_left'] @https = results['https'] @telnet = results['telnet'] return true elsif c.body_str =~ /"error": "API access denied"/i puts "Access Denied using API Key '#{apikey}'".light_red + "!".white puts "Check Key & Try Again".light_red + "....".white return false else puts "Unknown Problem with Connection to Shodan API".light_green + "!".white return false end rescue => e puts "Problem with Connection to Shodan API".light_red + "!".white puts "\t=> #{e}" return false end end # Just checks our key is working (re-using shodan_connect so updates @unlocks) # Returns True or False def connected? if shodan_connect(@key) return true else return false end end # Return the number of unlocks remaining def unlocks if shodan_connect(@key) return @unlocks.to_i else return nil end end # Check if HTTPS is Enabled def https? if shodan_connect(@key) if @https return true else return false end else return false end end # Check if Telnet is Enabled def telnet? if shodan_connect(@key) if @telnet return true else return false end else return false end end # Actually display Basic Info for current API Key def info url = @url + 'info?key=' + @key begin c = Curl::Easy.perform(url) results = JSON.parse(c.body_str) puts puts "Shodan API Key Confirmed".light_green + "!".white puts "API Key".light_green + ": #{@key}".white puts "Plan Type".light_green + ": #{results['plan']}".white puts "Unlocked".light_green + ": #{results['unlocked']}".white puts "Unlocks Remaining".light_green + ": #{results['unlocked_left']}".white puts "HTTPS Enabled".light_green + ": #{results['https']}".white puts "Telnet Enabled".light_green + ": #{results['telnet']}".white return true rescue => e puts "Problem with Connection to Shodan API".light_red + "!".white puts "\t=> #{e}".white return false end end # Lookup all available information for a specific IP address # Returns results hash or nil def host(ip) url = @url + 'host?ip=' + ip + '&key=' + @key begin c = Curl::Easy.perform(url) results = JSON.parse(c.body_str) return results rescue => e puts "Problem running Host Search".light_red + "!".white puts "\t=> #{e}".white return nil end end # Returns the number of devices that a search query found # Unrestricted usage of all advanced filters # Return results count or nil on failure def count(string) url = @url + 'count?q=' + string + '&key=' + @key begin c = Curl::Easy.perform(url) results = JSON.parse(c.body_str) return results['total'] rescue => e puts "Problem grabbing results count".light_red + "!".white puts "\t=> #{e}".white return nil end end # Search Shodan for devices using a search query # Returns results hash or nil def search(string, filters={}) prem_filters = [ 'city', 'country', 'geo', 'net', 'before', 'after', 'org', 'isp', 'title', 'html' ] cheap_filters = [ 'hostname', 'os', 'port' ] url = @url + 'search?q=' + string if not filters.empty? filters.each do |k, v| if cheap_filters.include?(k) url += ' ' + k + ":\"#{v}\"" end if prem_filters.include?(k) if @unlocks.to_i > 1 url += ' ' + k + ":\"#{v}\"" @unlocks = @unlocks.to_i - 1 # Remove an unlock for use of filter else puts "Not Enough Unlocks Left to run Premium Filter Search".light_red + "!".white puts "Try removing '#{k}' filter and trying again".light_red + "....".white return nil end end end end url += '&key=' + @key begin c = Curl::Easy.perform(url) results = JSON.parse(c.body_str) return results rescue => e puts "Problem running Shodan Search".light_red + "!".white puts "\t=> #{e}".white return nil end end # Quick Search Shodan for devices using a search query # Results are limited to only the IP addresses # Returns results array or nil def quick_search(string, filters={}) prem_filters = [ 'city', 'country', 'geo', 'net', 'before', 'after', 'org', 'isp', 'title', 'html' ] cheap_filters = [ 'hostname', 'os', 'port' ] url = @url + 'search?q=' + string if not filters.empty? filters.each do |k, v| if cheap_filters.include?(k) url += ' ' + k + ":\"#{v}\"" end if prem_filters.include?(k) if @unlocks.to_i > 1 url += ' ' + k + ":\"#{v}\"" @unlocks = @unlocks.to_i - 1 else puts "Not Enough Unlocks Left to run Premium Filter Search".light_red + "!".white puts "Try removing '#{k}' filter and trying again".light_red + "....".white return nil end end end end url += '&key=' + @key begin ips=[] c = Curl::Easy.perform(url) results = JSON.parse(c.body_str) results['matches'].each do |host| ips << host['ip'] end return ips rescue => e puts "Problem running Shodan Quick Search".light_red + "!".white puts "\t=> #{e}".white return nil end end # Perform Shodan Exploit Search as done on Web # Provide Search String and source # Source can be: metasploit, exploitdb, or cve # Returns results hash array on success: { downloadID => { link => description } } # Returns nil on failure def sploit_search(string, source) sources = [ "metasploit", "exploitdb", "cve" ] if sources.include?(source.downcase) sploits = 'https://exploits.shodan.io/?q=' + string + ' source:"' + source.downcase + '"' begin results={} c = Curl::Easy.perform(sploits) page = Nokogiri::HTML(c.body_str) # Parsable doc object now # Enumerate target section, parse out link & description page.css('div[class="search-result well"]').each do |linematch| if linematch.to_s =~ /