Lucene search
+L

Shodan Host Port

🗓️ 22 Dec 2020 17:41:45Reported by natto97Type 
metasploit
 metasploit
🔗 www.rapid7.com👁 35 Views

This module uses the shodan API to return all port information found on a given host I

Code
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Auxiliary
  include Msf::Exploit::Remote::HttpClient
  include Msf::Auxiliary::Report
  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'Shodan Host Port',
        'Description' => %q{
          This module uses the shodan API to return all port information found on a given host IP.
        },
        'Author' => [ 'natto97' ],
        'License' => MSF_LICENSE,
        'References' => [
          [ 'URL', 'https://honeyscore.shodan.io/']
        ],
        'Notes' => {
          'Stability' => [],
          'SideEffects' => [],
          'Reliability' => []
        }
      )
    )
    register_options(
      [
        OptString.new('SHODAN_APIKEY', [true, 'The SHODAN API key'])
      ]
    )
    deregister_options('SSL')
    deregister_options('RPORT')
    deregister_options('VHOST')
  end

  def run
    # check our API key is somewhat sane
    unless /^[a-z\d]{32}$/i.match?(datastore['SHODAN_APIKEY'])
      fail_with(Failure::BadConfig, 'Shodan API key should be 32 characters a-z,A-Z,0-9.')
    end
    key = datastore['SHODAN_APIKEY']
    # Check the length of the key (should be 32 chars)
    if key.length != 32
      fail_with(Failure::BadConfig, 'Invalid API key (Not long enough)')
    end
    cli = Rex::Proto::Http::Client.new('api.shodan.io', 443, {}, true)
    cli.connect
    req = cli.request_cgi({
      'uri' => "/shodan/host/#{rhost}?key=#{key}&minify=true",
      'method' => 'GET'
    })
    res = cli.send_recv(req)
    cli.close
    if res.nil?
      fail_with(Failure::Unreachable, 'Unable to connect to shodan')
    end
    if res.body =~ /No information available for that IP/
      print_error('The target IP address has not been scanned by Shodan!')
      return
    end
    if res.code != 200
      fail_with(Failure::UnexpectedReply, 'Shodan did not respond in an expected way. Check your api key')
    end
    json = res.get_json_document
    if !json.nil? && !json['ports'].nil? && !json['ports'].empty?
      json['ports'].each do |post|
        print_good("#{rhost}:#{post}")
        report_service(host: rhost, port: post, name: 'shodan')
      end
    else
      print_error("Shodan did not return any open ports for #{rhost}!")
    end
  end
end

Data

Build on a solid foundation with Vulners data

We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data

Api

Power your application with Vulners API

The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access

App

Assess and manage vulnerabilities with Vulners tools

Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation

03 Jan 2026 18:57Current
6.8Medium risk
Vulners AI Score6.8
35