shodany 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cafa0b4f89aa817dded9a45eee898da17ef63db904f6944417c5a4d9f9a2a3a8
4
+ data.tar.gz: 65f76c492c70b9c5625a74d80391fd1c58e14fe5b0966cefc2174a2378622b2b
5
+ SHA512:
6
+ metadata.gz: 429fa90cd1c8871872fda4ceb60bab33194ee344e1b838805b51bee75495a95423d0ddacfdf14fe8521954eb137a2b38a19346226a7edde46d9348469393feab
7
+ data.tar.gz: 50962a1dbde2aa5a55b1631b2d741546390b2a127ba615415539f68eb7d57e2b0ddc9a0bf39d11b0e591cac52fab98856be12cfa5d137f5c13ccc4f661f17999
data/.gitignore ADDED
@@ -0,0 +1,40 @@
1
+ # Ignore Ruby and Rails specific files
2
+ *.rbc
3
+ *.log
4
+ *.gem
5
+ *.rdoc
6
+ db/*.sqlite3
7
+ db/*.sqlite3-journal
8
+ tmp/
9
+ coverage/
10
+
11
+ # Ignore all logfiles and tempfiles.
12
+ /log/*
13
+ /tmp/*
14
+ !/log/.keep
15
+ !/tmp/.keep
16
+
17
+ # Ignore files related to version control systems
18
+ .git/
19
+ .svn/
20
+ .hg/
21
+
22
+ # Ignore bundler config and installed gems
23
+ /.bundle
24
+ /vendor/bundle
25
+
26
+ # Ignore specific environment configuration files
27
+ .env
28
+ .secret
29
+
30
+ # Ignore IDE-specific files (e.g., for VSCode, RubyMine, SublimeText)
31
+ .idea/
32
+ *.swp
33
+ *.swo
34
+ *.sublime-workspace
35
+ *.sublime-project
36
+ .vscode/
37
+
38
+ # Other common ignore patterns
39
+ .DS_Store
40
+ Thumbs.db
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --format documentation
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ All notable changes to "shodany" project will be documented in this file.
4
+
5
+ ## [Unreleased]
6
+
7
+ - Changes developed but not yet released.
8
+
9
+ ## [1.0.1] - 2024-01-20
10
+
11
+ ### Added
12
+ - Added `some_info` method to enhance IP information retrieval.
13
+ - New parsing options for API responses.
14
+
15
+ ### Fixed
16
+ - Fixed issue with JSON parsing in the `all_info` method.
17
+ - Improved error handling in the network requests.
18
+
19
+ ## [1.0.0] - 2024-01-10
20
+
21
+ - Initial release of Shodany.
22
+ - Features include `all_info`, `vuln`, `computer`, and `search` methods.
23
+ - Basic API integration with Shodan.
@@ -0,0 +1,55 @@
1
+ Creating a `CODE_OF_CONDUCT.md` file is an important step in establishing a positive and inclusive community around your project. A good code of conduct should outline the expectations for behavior of contributors and participants, as well as the consequences for unacceptable behavior. It should also provide information on how to report incidents.
2
+
3
+ Here's a basic structure for your `CODE_OF_CONDUCT.md` file, based on common practices within open-source communities:
4
+
5
+ ---
6
+
7
+ # Code of Conduct for Shodany
8
+
9
+ ## Our Pledge
10
+
11
+ In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
12
+
13
+ ## Our Standards
14
+
15
+ Examples of behavior that contributes to creating a positive environment include:
16
+
17
+ - Using welcoming and inclusive language
18
+ - Being respectful of differing viewpoints and experiences
19
+ - Gracefully accepting constructive criticism
20
+ - Focusing on what is best for the community
21
+ - Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ - The use of sexualized language or imagery and unwelcome sexual attention or advances
26
+ - Trolling, insulting/derogatory comments, and personal or political attacks
27
+ - Public or private harassment
28
+ - Publishing others' private information, such as a physical or electronic address, without explicit permission
29
+ - Other conduct which could reasonably be considered inappropriate in a professional setting
30
+
31
+ ## Our Responsibilities
32
+
33
+ Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
34
+
35
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
36
+
37
+ ## Scope
38
+
39
+ This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
40
+
41
+ ## Enforcement
42
+
43
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at abdullahsaleh010@gmail.com . All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident.
44
+
45
+ Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
46
+
47
+ ## Attribution
48
+
49
+ This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 1.4, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct.html](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html).
50
+
51
+ [homepage]: http://contributor-covenant.org
52
+ [version]: http://contributor-covenant.org/version/1/4/
53
+
54
+ ---
55
+
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+ # Specify your gem's dependencies in shodany.gemspec
5
+ gemspec
6
+
7
+ # Development dependencies
8
+ group :development, :test do
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'webmock'
11
+
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shodany (1.0)
5
+ json (~> 2.0)
6
+ net-http (~> 0.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.6)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ crack (0.4.5)
14
+ rexml
15
+ diff-lcs (1.5.0)
16
+ hashdiff (1.1.0)
17
+ json (2.7.1)
18
+ net-http (0.4.1)
19
+ uri
20
+ public_suffix (5.0.4)
21
+ rake (13.1.0)
22
+ rexml (3.2.6)
23
+ rspec (3.12.0)
24
+ rspec-core (~> 3.12.0)
25
+ rspec-expectations (~> 3.12.0)
26
+ rspec-mocks (~> 3.12.0)
27
+ rspec-core (3.12.2)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-expectations (3.12.3)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-mocks (3.12.6)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-support (3.12.1)
36
+ uri (0.13.0)
37
+ webmock (3.19.1)
38
+ addressable (>= 2.8.0)
39
+ crack (>= 0.3.2)
40
+ hashdiff (>= 0.4.0, < 2.0.0)
41
+
42
+ PLATFORMS
43
+ arm64-darwin-22
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 2.0)
48
+ rake (~> 13.0)
49
+ rspec (~> 3.0)
50
+ shodany!
51
+ webmock
52
+
53
+ BUNDLED WITH
54
+ 2.5.4
data/LICENSE ADDED
@@ -0,0 +1,37 @@
1
+ <<<<<<< HEAD
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Abo5
5
+
6
+ =======
7
+ The MIT License (MIT)
8
+
9
+ Copyright (c) 2024 Abdullah Maven
10
+ >>>>>>> c3d5001 (Initial Commit)
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ <<<<<<< HEAD
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+ =======
22
+ The above copyright notice and this permission notice shall be included in
23
+ all copies or substantial portions of the Software.
24
+ >>>>>>> c3d5001 (Initial Commit)
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ <<<<<<< HEAD
32
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33
+ SOFTWARE.
34
+ =======
35
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36
+ THE SOFTWARE.
37
+ >>>>>>> c3d5001 (Initial Commit)
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ <<<<<<< HEAD
2
+ # shodany
3
+ Ruby library for interacting with the Shodan API
4
+ =======
5
+
6
+ ---
7
+
8
+ # Shodany
9
+
10
+ Shodany is a versatile Ruby library for interacting with the Shodan API. It simplifies the process of searching for internet-connected devices, retrieving specific IP information, exploring vulnerabilities, and more.
11
+
12
+ ## Table of Contents
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [Setting Up](#setting-up)
16
+ - [Retrieving Host Information](#retrieving-host-information)
17
+ - [Searching for Vulnerabilities](#searching-for-vulnerabilities)
18
+ - [General Shodan Search](#general-shodan-search)
19
+ - [Listing All Ports](#listing-all-ports)
20
+ - [Requesting a Shodan Scan](#requesting-a-shodan-scan)
21
+ - [Getting Domain Information](#getting-domain-information)
22
+ - [Examples](#examples)
23
+ - [Development](#development)
24
+ - [Contributing](#contributing)
25
+ - [License](#license)
26
+ - [Code of Conduct](#code-of-conduct)
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'shodany'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ ```bash
39
+ $ bundle install
40
+ ```
41
+
42
+ Or install it yourself as:
43
+
44
+ ```bash
45
+ $ gem install shodany
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ ### Setting Up
51
+
52
+ ```ruby
53
+ require 'shodany'
54
+
55
+ client = Shodany.new("YOUR_SHODAN_API_KEY")
56
+ ```
57
+
58
+ ### [Other Usage Instructions]
59
+
60
+ ## Examples
61
+
62
+ Refer to the `examples` directory for detailed examples of using Shodany in various scenarios.
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/Abo5/shodany](https://github.com/Abo5/shodany). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Abo5/shodany/blob/master/CODE_OF_CONDUCT.md).
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
75
+
76
+ ## Code of Conduct
77
+
78
+ Everyone interacting in the Shodany project's codebase, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](https://github.com/Abo5/shodany/blob/master/CODE_OF_CONDUCT.md).
79
+
80
+ ---
81
+ >>>>>>> c3d5001 (Initial Commit)
82
+ # shodany
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ # Creates a Rake task for running RSpec tests.
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ # Sets the default task for Rake to :spec,
7
+ # which means running 'rake' without arguments will run RSpec tests.
8
+ task default: :spec
@@ -0,0 +1,20 @@
1
+ # get_domain_info_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+ # Specify the domain for which you want information
8
+ domain = "example.com"
9
+
10
+ # Begin exception handling
11
+ begin
12
+ # Get domain information from Shodan
13
+ domain_info = client.get_domain_info(domain)
14
+ # Print the domain information
15
+ puts domain_info
16
+ # Rescue block to catch and handle any errors
17
+ rescue Shodany::Error => e
18
+ # Output the error message
19
+ puts "An error occurred: #{e.message}"
20
+ end
@@ -0,0 +1,20 @@
1
+ # get_host_info_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+ # Specify the IP address you want information about
8
+ ip = "8.8.8.8"
9
+
10
+ # Begin exception handling
11
+ begin
12
+ # Retrieve host information for the specified IP
13
+ info = client.get_host_info(ip)
14
+ # Print the retrieved information
15
+ puts info
16
+ # Rescue block to catch and handle any errors
17
+ rescue Shodany::Error => e
18
+ # Output the error message
19
+ puts "An error occurred: #{e.message}"
20
+ end
@@ -0,0 +1,18 @@
1
+ # list_ports_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+
8
+ # Begin exception handling
9
+ begin
10
+ # List all ports that Shodan is crawling
11
+ ports = client.list_ports
12
+ # Print the list of ports
13
+ puts ports
14
+ # Rescue block to catch and handle any errors
15
+ rescue Shodany::Error => e
16
+ # Output the error message
17
+ puts "An error occurred: #{e.message}"
18
+ end
@@ -0,0 +1,20 @@
1
+ # request_scan_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+ # Specify the IPs to be scanned
8
+ ips = "192.168.1.1"
9
+
10
+ # Begin exception handling
11
+ begin
12
+ # Request Shodan to scan the specified IPs
13
+ scan_result = client.request_scan(ips)
14
+ # Print the results of the scan
15
+ puts scan_result
16
+ # Rescue block to catch and handle any errors
17
+ rescue Shodany::Error => e
18
+ # Output the error message
19
+ puts "An error occurred: #{e.message}"
20
+ end
@@ -0,0 +1,20 @@
1
+ # search_exploits_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+ # Define your search query
8
+ query = "apache"
9
+
10
+ # Begin exception handling
11
+ begin
12
+ # Search for exploits related to the query
13
+ results = client.search_exploits(query)
14
+ # Print the results
15
+ puts results
16
+ # Rescue block to catch and handle any errors
17
+ rescue Shodany::Error => e
18
+ # Output the error message
19
+ puts "An error occurred: #{e.message}"
20
+ end
@@ -0,0 +1,20 @@
1
+ # search_shodan_example.rb
2
+ # Include the Shodany library
3
+ require 'shodany'
4
+
5
+ # Initialize a new Shodany client with your API key
6
+ client = Shodany.new("YOUR_API_KEY")
7
+ # Define your search query
8
+ query = "server:apache country:DE"
9
+
10
+ # Begin exception handling
11
+ begin
12
+ # Perform a general search on Shodan with the query
13
+ results = client.search_shodan(query)
14
+ # Print the search results
15
+ puts results
16
+ # Rescue block to catch and handle any errors
17
+ rescue Shodany::Error => e
18
+ # Output the error message
19
+ puts "An error occurred: #{e.message}"
20
+ end
@@ -0,0 +1,130 @@
1
+ # lib/shodany/api.rb
2
+ require_relative 'error.rb'
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ class ShodanyAPI # Defines the ShodanyAPI class for interacting with the Shodan API.
7
+ BASE_URL = 'https://api.shodan.io/' # Base URLs for the Shodan API and the Exploits API.
8
+ EXPLOITS_BASE_URL = 'https://exploits.shodan.io/api'
9
+
10
+
11
+ def initialize(api_key) # Initialize the API with the user's API key.
12
+ raise Shodany::MissingApiKeyError, 'API key is missing or empty' if api_key.to_s.empty? # Raise an error if the API key is missing or empty.
13
+ @api_key = api_key
14
+ end
15
+
16
+ # Search for exploits in the Shodan Exploits database.
17
+ # Search for exploits
18
+ # Accepts various parameters for filtering the search.
19
+ def search_exploits(query, author: nil, bid: nil, code: nil, cve: nil, date: nil, description: nil, msb: nil, osvdb: nil, platform: nil, port: nil, title: nil, type: nil, facets: nil, page: 1)
20
+ params = { # Base parameters for the search query.
21
+ query: query,
22
+ key: @api_key,
23
+ page: page
24
+ }
25
+
26
+ additional_params = { author: author, bid: bid, code: code, cve: cve, date: date, description: description, msb: msb, osvdb: osvdb, platform: platform, port: port, title: title, type: type, facets: facets } # Additional filtering parameters.
27
+ additional_params.each { |key, value| params[key] = value if value } # Add non-nil additional parameters to the base parameters.
28
+
29
+ uri = URI(EXPLOITS_BASE_URL + "/search?#{URI.encode_www_form(params)}") # Construct the request URI with the query parameters.
30
+
31
+ begin # Perform the HTTP GET request and handle exceptions.
32
+ response = Net::HTTP.get(uri)
33
+ result = JSON.parse(response)
34
+ raise Shodany::InvalidResponseError, result['error'] if result.key?('error') # Raise an error if the response contains an error message.
35
+ result
36
+ rescue JSON::ParserError
37
+ raise Shodany::InvalidResponseError, 'Invalid response format' # Raise an error for invalid response format.
38
+ end
39
+ end
40
+
41
+
42
+
43
+ # Retrieve host information
44
+ # ip means: host you want to get info about it e.g ("8.8.8.8")
45
+ # history means:
46
+ def get_host_info(ip, history: false, minify: false)
47
+ uri = URI("#{BASE_URL}/shodan/host/#{ip}?key=#{@api_key}&history=#{history}&minify=#{minify}")
48
+
49
+ begin
50
+ response = Net::HTTP.get(uri)
51
+ result = JSON.parse(response)
52
+ raise Shodany::NotFoundError, result['error'] if result.key?('error')
53
+ result
54
+ rescue JSON::ParserError
55
+ raise Shodany::InvalidResponseError, 'Invalid response format'
56
+ end
57
+ end
58
+
59
+
60
+ # Search Shodan for anything
61
+ # query means: [String] Shodan search query. The provided string is used to search the database of banners in Shodan, with the additional option to provide filters inside the search query using a "filter:value" format. For example, the following search query would find Apache Web servers located in Germany: "apache country:DE"
62
+ # facets means: [string] "US" or "SA"
63
+ # page means: number of page e.g 1 or 2...
64
+ # minify means: [Boolean] True or False; whether or not to truncate some of the larger fields (default: True)
65
+ def search_shodan(query, facets: nil, page: 1, minify: true)
66
+ params = {
67
+ key: @api_key,
68
+ query: query,
69
+ page: page,
70
+ minify: minify
71
+ }
72
+ params[:facets] = facets if facets
73
+
74
+ uri = URI("#{BASE_URL}/shodan/host/search?#{URI.encode_www_form(params)}")
75
+
76
+ begin
77
+ response = Net::HTTP.get(uri)
78
+ result = JSON.parse(response)
79
+ raise Shodany::InvalidResponseError, result['error'] if result.key?('error')
80
+ result
81
+ rescue JSON::ParserError
82
+ raise Shodany::InvalidResponseError, 'Invalid response format'
83
+ end
84
+ end
85
+
86
+
87
+
88
+ # List all ports .. This method returns a list of port numbers that the crawlers are looking for.
89
+ def list_ports
90
+ uri = URI("#{BASE_URL}/shodan/ports?key=#{@api_key}")
91
+
92
+ begin
93
+ response = Net::HTTP.get(uri)
94
+ JSON.parse(response)
95
+ rescue JSON::ParserError
96
+ raise Shodany::InvalidResponseError, 'Invalid response format'
97
+ end
98
+ end
99
+
100
+
101
+ # Request Shodan to crawl an IP/ netblock
102
+ def request_scan(ips, services: nil)
103
+ uri = URI("#{BASE_URL}/shodan/scan?key=#{@api_key}")
104
+ data = { ips: ips, services: services }.to_json
105
+
106
+ begin
107
+ response = Net::HTTP.post(uri, data, "Content-Type" => "application/json")
108
+ result = JSON.parse(response.body)
109
+ raise Shodany::InvalidResponseError, result['error'] if result.key?('error')
110
+ result
111
+ rescue JSON::ParserError
112
+ raise Shodany::InvalidResponseError, 'Invalid response format'
113
+ end
114
+ end
115
+
116
+
117
+ # Get domain information
118
+ def get_domain_info(domain, history: false, type: nil, page: 1)
119
+ uri = URI("#{BASE_URL}/dns/domain/#{domain}?key=#{@api_key}&history=#{history}&type=#{type}&page=#{page}")
120
+
121
+ begin
122
+ response = Net::HTTP.get(uri)
123
+ result = JSON.parse(response)
124
+ raise Shodany::NotFoundError, result['error'] if result.key?('error')
125
+ result
126
+ rescue JSON::ParserError
127
+ raise Shodany::InvalidResponseError, 'Invalid response format'
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'api.rb'
2
+
3
+ class ShodanyClient
4
+ def initialize(api_key)
5
+ raise Shodany::MissingApiKeyError, 'API key is missing or empty' if api_key.to_s.empty?
6
+ @api = ShodanyAPI.new(api_key)
7
+ end
8
+
9
+ def search_exploits(query, **options)
10
+ begin
11
+ results = @api.search_exploits(query, **options)
12
+ display_results(results)
13
+ rescue Shodany::Error => e
14
+ puts "An error occurred: #{e.message}"
15
+ end
16
+ end
17
+
18
+ def get_host_info(ip, history: false, minify: false)
19
+ info = @api.get_host_info(ip, history: history, minify: minify)
20
+ display_results(info)
21
+ end
22
+
23
+ def search_shodan(query, facets = nil, page = 1, minify = true)
24
+ results = @api.search_shodan(query, facets: facets, page: page, minify: minify)
25
+ display_results(results)
26
+ end
27
+
28
+
29
+ def list_ports
30
+ ports = @api.list_ports
31
+ display_results(ports)
32
+ end
33
+
34
+ def request_scan(ips, services: nil)
35
+ scan_result = @api.request_scan(ips, services: services)
36
+ display_results(scan_result)
37
+ end
38
+
39
+ def get_domain_info(domain, history: false, type: nil, page: 1)
40
+ domain_info = @api.get_domain_info(domain, history: history, type: type, page: page)
41
+ display_results(domain_info)
42
+ end
43
+
44
+ private
45
+
46
+ def display_results(results)
47
+ # Flexible display function
48
+ # Developers can modify this to change how results are displayed/formatted
49
+ if results.is_a?(Hash) && results['error']
50
+ puts "Error: #{results['error']}"
51
+ else
52
+ return JSON.pretty_generate(results)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ # lib/shodany/error.rb
2
+
3
+ module Shodany
4
+ # General error for all exceptions in Shodany
5
+ class Error < StandardError; end
6
+
7
+ # Error raised when there is a network or connection problem
8
+ class NetworkError < Error; end
9
+
10
+ # Error raised when the response from the API is unexpected or invalid
11
+ class InvalidResponseError < Error; end
12
+
13
+ # Error raised when the requested resource is not found
14
+ class NotFoundError < Error; end
15
+
16
+ # api token missing
17
+ class MissingApiKeyError < Error; end
18
+ end
19
+
@@ -0,0 +1,5 @@
1
+ # version.rb
2
+ # Defines the version of the Shodany library.
3
+ module Shodany
4
+ VERSION = "1.0"
5
+ end
data/lib/shodany.rb ADDED
@@ -0,0 +1,13 @@
1
+ # lib/shodany.rb
2
+ # Main file for the Shodany library, requires necessary components.
3
+ require_relative 'shodany/version'
4
+ require_relative 'shodany/api'
5
+ require_relative 'shodany/client'
6
+
7
+ # Defines the Shodany module to provide a simple interface for users.
8
+
9
+ module Shodany
10
+ def self.new(api_key)
11
+ ShodanyClient.new(api_key) # Method to create a new instance of ShodanyClient with the provided API key.
12
+ end
13
+ end
data/shodany.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ require File.expand_path('lib/shodany/version', __dir__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'shodany'
5
+ spec.version = Shodany::VERSION
6
+ spec.authors = ['Maven', 'A88', 'Nazi']
7
+ spec.email = ['abdullahsaleh010@gmail.com']
8
+ spec.summary = 'A Ruby library for interacting with the Shodan API.'
9
+ spec.description = 'Shodany provides an easy-to-use interface for the Shodan API to search internet-connected devices, retrieve IP information, and search for vulnerabilities.'
10
+ spec.homepage = 'https://github.com/Abo5/shodany'
11
+ spec.license = 'MIT'
12
+ spec.platform = Gem::Platform::RUBY
13
+ spec.required_ruby_version = '>= 2.5.0'
14
+
15
+ # Include all files from git tracking, excluding test, spec, and features
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+
18
+ # Ensure all necessary files are included in the gem
19
+ spec.files += Dir['README.md', 'LICENSE', 'CHANGELOG.md', 'lib/**/*.rb', 'lib/**/*.rake', 'shodany.gemspec', '.github/*.md', 'Gemfile', 'Rakefile', 'examples/*']
20
+
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ # Define runtime and development dependencies
26
+ spec.add_runtime_dependency 'json', '~> 2.0'
27
+ spec.add_runtime_dependency 'net-http', '~> 0.1'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 2.0'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shodany
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Maven
8
+ - A88
9
+ - Nazi
10
+ autorequire:
11
+ bindir: exe
12
+ cert_chain: []
13
+ date: 2024-01-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: net-http
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.1'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.1'
43
+ - !ruby/object:Gem::Dependency
44
+ name: bundler
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '2.0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '13.0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '13.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '3.0'
85
+ description: Shodany provides an easy-to-use interface for the Shodan API to search
86
+ internet-connected devices, retrieve IP information, and search for vulnerabilities.
87
+ email:
88
+ - abdullahsaleh010@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - CHANGELOG.md
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE
100
+ - README.md
101
+ - Rakefile
102
+ - examples/get_domain_info_example.rb
103
+ - examples/get_host_info_example.rb
104
+ - examples/list_ports_example.rb
105
+ - examples/request_scan_example.rb
106
+ - examples/search_exploits_example.rb
107
+ - examples/search_shodan_example.rb
108
+ - lib/shodany.rb
109
+ - lib/shodany/api.rb
110
+ - lib/shodany/client.rb
111
+ - lib/shodany/error.rb
112
+ - lib/shodany/version.rb
113
+ - shodany.gemspec
114
+ homepage: https://github.com/Abo5/shodany
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 2.5.0
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.5.4
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: A Ruby library for interacting with the Shodan API.
137
+ test_files: []