Module: Shodan

Defined in:
lib/shodan/host.rb,
lib/shodan/page.rb,
lib/shodan/query.rb,
lib/shodan/shodan.rb,
lib/shodan/version.rb,
lib/shodan/countries.rb,
lib/shodan/has_pages.rb

Overview

shodan-ruby - A Ruby interface to SHODAN, a computer search engine.

Copyright © 2009 Hal Brodigan (postmodern.mod3 at gmail.com)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Defined Under Namespace

Modules: Countries, HasPages Classes: Host, Page, Query

Constant Summary collapse

COMMON_PROXY_PORT =

Common proxy port.

8080
VERSION =

shodanrb version

'0.1.1'

Class Method Summary collapse

Class Method Details

.proxyHash

The proxy information used by Query.

Returns:

  • (Hash)

    The proxy information.



35
36
37
# File 'lib/shodan/shodan.rb', line 35

def Shodan.proxy
  @@shodan_proxy ||= {:host => nil, :port => COMMON_PROXY_PORT, :user => nil, :password => nil}
end

.proxy_uri(proxy_info = Shodan.proxy) ⇒ Object

Creates a HTTP URI based from the given proxy information.

Parameters:

  • proxy_info (Hash) (defaults to: Shodan.proxy)

    (Shodan.proxy) The proxy information.

Options Hash (proxy_info):

  • :host (String)

    The host of the proxy.

  • :port (Integer) — default: COMMON_PROXY_PORT

    The port of the proxy.

  • :user (String)

    The user name to authenticate as.

  • :password (String)

    The password to authenticate with.



57
58
59
60
61
62
63
64
65
66
# File 'lib/shodan/shodan.rb', line 57

def Shodan.proxy_uri(proxy_info=Shodan.proxy)
  if Shodan.proxy[:host]
    return URI::HTTP.build(
      :host => Shodan.proxy[:host],
      :port => Shodan.proxy[:port],
      :userinfo => "#{Shodan.proxy[:user]}:#{Shodan.proxy[:password]}",
      :path => '/'
    )
  end
end

.query(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.

Returns:

  • (Query)

    The new Query object.



213
214
215
# File 'lib/shodan/shodan.rb', line 213

def Shodan.query(options={},&block)
  Query.new(options,&block)
end

.user_agentString

The default Shodan User-Agent

Returns:

  • (String)

    The default User-Agent string used by Shodan.



84
85
86
# File 'lib/shodan/shodan.rb', line 84

def Shodan.user_agent
  @@shodan_user_agent ||= Shodan.user_agent_aliases['Windows IE 6']
end

.user_agent=(agent) ⇒ String

Sets the default Shodan User-Agent.

Parameters:

  • agent (String)

    The User-Agent string to use.

Returns:

  • (String)

    The new User-Agent string.



97
98
99
# File 'lib/shodan/shodan.rb', line 97

def Shodan.user_agent=(agent)
  @@shodan_user_agent = agent
end

.user_agent_alias=(name) ⇒ String

Sets the default Shodan User-Agent alias.

Parameters:

  • name (String)

    The alias of the User-Agent string to use.

Returns:

  • (String)

    The new User-Agent string.



110
111
112
# File 'lib/shodan/shodan.rb', line 110

def Shodan.user_agent_alias=(name)
  @@shodan_user_agent = Shodan.user_agent_aliases[name.to_s]
end

.user_agent_aliasesHash{String => String}

The supported Shodan User-Agent Aliases.

Returns:

  • (Hash{String => String})

    The User-Agent aliases and strings.



74
75
76
# File 'lib/shodan/shodan.rb', line 74

def Shodan.user_agent_aliases
  WWW::Mechanize::AGENT_ALIASES
end

.web_agent(options = {}, &block) ⇒ Object

Creates a new WWW::Mechanize agent.

Examples:

Creating a new Mechanize agent.

Shodan.web_agent

Creating a new Mechanize agent with a User-Agent alias.

Shodan.web_agent(:user_agent_alias => 'Linux Mozilla')

Creating a new Mechanize agent with a User-Agent string.

Shodan.web_agent(:user_agent => 'Google Bot')

Parameters:

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

    Additional options.

  • :proxy (Hash)

    a customizable set of options

Options Hash (options):

  • :user_agent_alias (String)

    The User-Agent alias to use.

  • :user_agent (String) — default: Shodan.user_agent

    The User-Agent string to use.

  • :proxy (Hash) — default: Shodan.proxy

    The proxy information to use.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/shodan/shodan.rb', line 150

def Shodan.web_agent(options={},&block)
  agent = WWW::Mechanize.new

  if options[:user_agent_alias]
    agent.user_agent_alias = options[:user_agent_alias]
  elsif options[:user_agent]
    agent.user_agent = options[:user_agent]
  elsif Shodan.user_agent
    agent.user_agent = Shodan.user_agent
  end

  proxy = (options[:proxy] || Shodan.proxy)
  if proxy[:host]
    agent.set_proxy(proxy[:host],proxy[:port],proxy[:user],proxy[:password])
  end

  block.call(agent) if block
  return agent
end