shodan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2010 John Matherly <jmath@surtri.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ Except as contained in this notice, the name(s) of the above
16
+ copyright holders shall not be used in advertising or otherwise
17
+ to promote the sale, use or other dealings in this Software
18
+ without prior written authorization.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ ## Installation
2
+
3
+ To install the library use rubygems:
4
+
5
+ gem install shodan
6
+
7
+ ## Usage
8
+
9
+ Before you can use the API, you need to have an API key.
10
+
11
+ [Get your API key here](http://www.shodanhq.com/api_doc)
12
+
13
+ Setup the SHODAN WebAPI:
14
+
15
+ require 'shodan'
16
+
17
+ api = Shodan::WebAPI.new(MY_API_KEY)
18
+
19
+ Print a list of cisco-ios devices:
20
+
21
+ result = api.search("cisco-ios")
22
+ result['matches'].each{ |host|
23
+ puts host['ip']
24
+ }
25
+
26
+ Get all the information SHODAN has on the IP 217.140.75.46:
27
+
28
+ host = api.host('217.140.75.46')
29
+ puts host.to_s
30
+
31
+ To properly handle potential errors, you should wrap all requests in a try/except block:
32
+
33
+ begin
34
+ api.search("cisco-ios")
35
+ rescue Exception => e
36
+ puts "Error: #{e.to_s}"
data/lib/shodan.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Import all the SHODAN classes
2
+ require 'shodan/api'
3
+ require 'shodan/version'
data/lib/shodan/api.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'rubygems'
2
+ require 'cgi'
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+ module Shodan
7
+
8
+ # The WebAPI class interfaces with the shodanhq.com/api
9
+ # It currently supports 2 methods:
10
+ # 1. search (query)
11
+ # 2. host (ip)
12
+ #
13
+ # Author:: achillean (mailto:jmath at surtri.com)
14
+ #
15
+ # :title:Shodan::WebAPI
16
+ class WebAPI
17
+ attr_accessor :api_key
18
+ attr_accessor :base_url
19
+
20
+ def initialize(api_key)
21
+ @api_key = api_key
22
+ @base_url = "http://www.shodanhq.com/api/"
23
+ end
24
+
25
+ # Internal method that sends out the HTTP request.
26
+ # Expects a webservice function (ex. 'search') name and a hash of arguments.
27
+ def request(func, args)
28
+ # Convert the argument hash into a string
29
+ args_string = args.map{|k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"}.join("&")
30
+
31
+ # Craft the final request URL
32
+ url = "#{@base_url}#{func}?key=#{@api_key}&#{args_string}"
33
+
34
+ # Send the request
35
+ response = Net::HTTP.get_response(URI.parse(url))
36
+
37
+ # Convert the JSON data into a native Ruby hash
38
+ data = JSON.parse(response.body)
39
+
40
+ # Raise an error if something went wrong
41
+ if data.has_key? 'error'
42
+ raise data['error']
43
+ end
44
+
45
+ return data
46
+ end
47
+ private :request
48
+
49
+ # Get all available information on an IP.
50
+ #
51
+ # Arguments:
52
+ # ip - host IP (string)
53
+ #
54
+ # Returns a hash containing the host information
55
+ def host(ip)
56
+ return request('host', {:ip => ip})
57
+ end
58
+
59
+ # Perform a search on Shodan.
60
+ #
61
+ # Arguments:
62
+ # query - search query; same format as the website (string)
63
+ #
64
+ # Returns a hash containing the search results
65
+ def search(query)
66
+ return request('search', {:q => query})
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ module Shodan
2
+ Version = VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shodan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Matherly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-09-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.4.6
24
+ version:
25
+ description: " A Ruby library to interact with the SHODAN API.\n"
26
+ email: jmath@surtri.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - README.md
36
+ - LICENSE
37
+ - HISTORY.md
38
+ - lib/shodan.rb
39
+ - lib/shodan/version.rb
40
+ - lib/shodan/api.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/achillean/shodan-ruby
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A Ruby library to interact with the SHODAN API.
69
+ test_files: []
70
+