shell_strike 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +12 -0
- data/appveyor.yml +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/shell_strike.rb +120 -0
- data/lib/shell_strike/event_bus.rb +21 -0
- data/lib/shell_strike/exceptions.rb +6 -0
- data/lib/shell_strike/host.rb +25 -0
- data/lib/shell_strike/result.rb +32 -0
- data/lib/shell_strike/ssh.rb +100 -0
- data/lib/shell_strike/ssh/command_result.rb +9 -0
- data/lib/shell_strike/ssh/errors.rb +7 -0
- data/lib/shell_strike/version.rb +3 -0
- data/shell_strike.gemspec +32 -0
- metadata +164 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0394b2f374817abfe1e11a0b82623004e4a4f77
|
4
|
+
data.tar.gz: 4baa11bd003fafbe05dfb6a5626e6c9279b5a5ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 64cc475de8ee0c701c80317a4c471524068f91004edeff464afebc3a60eb178df95c8e3742b40cb4a182e43883d37c55c2380f5d0983010220df337528e20760
|
7
|
+
data.tar.gz: 19fa396853389ff94c01c2652da7f58d1f001d47bd1d9b4da7d879d40e99b3622393507b7cf5126feac9e72c4678048debf9580a68bd440e5a6141e18c27397d
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
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
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at andrew.walter@burnet.edu.au. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Andrew Walter
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# ShellStrike
|
2
|
+
[![Build Status](https://travis-ci.org/xtrasimplicity/shell-strike.svg?branch=master)](https://travis-ci.org/xtrasimplicity/shell-strike)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/github/xtrasimplicity/shell-strike/badge.svg?branch=master)](https://coveralls.io/github/xtrasimplicity/shell-strike?branch=master)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/f66cd28d67eb40caf879/maintainability)](https://codeclimate.com/github/xtrasimplicity/shell-strike/maintainability)
|
5
|
+
[![Read the Docs](https://img.shields.io/readthedocs/pip.svg)](http://www.rubydoc.info/github/xtrasimplicity/shell-strike/master)
|
6
|
+
|
7
|
+
A simple ruby gem to automatically identify valid SSH credentials for a server using custom username and password dictionaries.
|
8
|
+
|
9
|
+
|
10
|
+
This gem is intended to be used for educational purposes, and was written to assist with quickly identifying credential combinations for poorly documented internal servers. This gem is intended to be used against servers that you are authorised to 'attack' and the developer takes no responsibility for any issues which may arise due to misuse of this gem.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'shell_strike'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install shell_strike
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
Create a new instance of ShellStrike, passing in an array of `ShellStrike::Host` objects, an array of usernames and an array of passwords. i.e.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
hosts = [
|
33
|
+
ShellStrike::Host.new('192.168.1.100'),
|
34
|
+
ShellStrike::Host.new('192.168.1.101')
|
35
|
+
]
|
36
|
+
|
37
|
+
usernames = ['admin', 'root']
|
38
|
+
passwords = ['password', 'letmein']
|
39
|
+
|
40
|
+
shell_strike = ShellStrike.new(hosts, usernames, passwords)
|
41
|
+
|
42
|
+
shell_strike.identify_credentials!
|
43
|
+
```
|
44
|
+
|
45
|
+
Access identified credentials using `shell_strike.identified_credentials`.
|
46
|
+
|
47
|
+
## Events
|
48
|
+
ShellStrike includes numerous events which users can consume on each instance of ShellStrike, like this:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
shell_strike = ShellStrike.new(...)
|
52
|
+
shell_strike.identify_credentials!
|
53
|
+
|
54
|
+
shell_strike.on(:credentials_identified) do |host, username, password|
|
55
|
+
puts "Success! #{username} / #{password} can be used to login to #{host}."
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
The following events are available:
|
60
|
+
* `:credentials_identified` => Triggered when valid credentials have been identified. Yields blocks supplied by listeners with three arguments: (1) The host object, (2) the valid username, and (3) the valid password.
|
61
|
+
* `:credentials_failed` => Triggered when credential validation fails. Yields blocks supplied by listeners with three arguments: (1) The host object, (2) the invalid username, and (3) the invalid password.
|
62
|
+
|
63
|
+
## TODO
|
64
|
+
- Add support for user-defineable SSH arguments such as KexAlgorithms and key-based auth.
|
65
|
+
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
70
|
+
|
71
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/xtrasimplicity/shell_strike. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
80
|
+
|
81
|
+
## Code of Conduct
|
82
|
+
|
83
|
+
Everyone interacting in the ShellStrike project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/xtrasimplicity/shell_strike/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'cucumber'
|
4
|
+
require 'cucumber/rake/task'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'coveralls/rake/task'
|
7
|
+
|
8
|
+
Cucumber::Rake::Task.new(:features)
|
9
|
+
RSpec::Core::RakeTask.new(:spec)
|
10
|
+
Coveralls::RakeTask.new
|
11
|
+
|
12
|
+
task :default => [:spec, :features, 'coveralls:push']
|
data/appveyor.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
cache:
|
2
|
+
- vendor/bundle
|
3
|
+
|
4
|
+
environment:
|
5
|
+
matrix:
|
6
|
+
- RUBY_VERSION: 25
|
7
|
+
- RUBY_VERSION: 24
|
8
|
+
- RUBY_VERSION: 23
|
9
|
+
- RUBY_VERSION: 22
|
10
|
+
- RUBY_VERSION: 21
|
11
|
+
- RUBY_VERSION: 200
|
12
|
+
|
13
|
+
install:
|
14
|
+
- set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH%
|
15
|
+
- bundle config --local path vendor/bundle
|
16
|
+
- bundle install
|
17
|
+
|
18
|
+
build: off
|
19
|
+
|
20
|
+
before_test:
|
21
|
+
- ruby -v
|
22
|
+
- gem -v
|
23
|
+
- bundle -v
|
24
|
+
|
25
|
+
test_script:
|
26
|
+
- bundle exec rake
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "shell_strike"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/shell_strike.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
require "shell_strike/version"
|
2
|
+
require "shell_strike/event_bus"
|
3
|
+
require "shell_strike/ssh"
|
4
|
+
require "shell_strike/exceptions"
|
5
|
+
require "shell_strike/result"
|
6
|
+
require "shell_strike/host"
|
7
|
+
|
8
|
+
class ShellStrike
|
9
|
+
attr_reader :hosts, :usernames, :passwords, :global_actions
|
10
|
+
|
11
|
+
# Initialises a new ShellStrike instance
|
12
|
+
# @param hosts [Array<Host>] an array of Hosts
|
13
|
+
# @param usernames [Array<String>] an array of usernames to test; a username dictionary.
|
14
|
+
# @param passwords [Array<String>] an array of passwords to test; a password dictionary.
|
15
|
+
# @param global_actions [Array<String>] an array of shell commands to execute against every host. Interactive shell commands are NOT supported.
|
16
|
+
def initialize(hosts, usernames, passwords, global_actions = [])
|
17
|
+
raise HostsNotDefined if hosts.nil? || hosts.empty?
|
18
|
+
raise UsernamesNotDefined if usernames.nil? || usernames.empty?
|
19
|
+
raise PasswordsNotDefined if passwords.nil? || passwords.empty?
|
20
|
+
|
21
|
+
@hosts = hosts
|
22
|
+
@usernames = usernames
|
23
|
+
@passwords = passwords
|
24
|
+
@global_actions = global_actions
|
25
|
+
end
|
26
|
+
|
27
|
+
# Identifies valid credentials for each host and populates the `identified_credentials`, `failed_hosts` and `unreachable_hosts` arrays.
|
28
|
+
def identify_credentials!
|
29
|
+
@hosts.each do |host|
|
30
|
+
is_reachable, explanation = Ssh.check_host_reachable(host)
|
31
|
+
|
32
|
+
unless is_reachable
|
33
|
+
store_unreachable_host(host, explanation)
|
34
|
+
next
|
35
|
+
end
|
36
|
+
|
37
|
+
credential_failure_count = 0
|
38
|
+
|
39
|
+
username_password_combinations.each do |username, password|
|
40
|
+
if Ssh.valid_credentials?(host, username, password)
|
41
|
+
store_valid_credentials(host, username, password)
|
42
|
+
else
|
43
|
+
credential_failure_count += 1
|
44
|
+
event_bus.emit(:credentials_failed, host, username, password)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
store_failed_host(host) if credential_failure_count == username_password_combinations.length
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
# A hash of hosts and their valid credentials.
|
55
|
+
# @return A hash of Host URIs and their valid credentials.
|
56
|
+
# @example
|
57
|
+
# { '192.168.1.100:22' => ['admin', 'password'] }
|
58
|
+
def identified_credentials
|
59
|
+
@identified_credentials ||= {}
|
60
|
+
end
|
61
|
+
|
62
|
+
# A hash of hosts which were unreachable.
|
63
|
+
# @return A hash of Host objects and their error messages.
|
64
|
+
# @example
|
65
|
+
# #<ShellStrike::Host:*> => 'Unable to connect to *. No route to host'
|
66
|
+
def unreachable_hosts
|
67
|
+
@unreachable_hosts ||= {}
|
68
|
+
end
|
69
|
+
|
70
|
+
# An array of hosts for which valid credentials were not able to be identified.
|
71
|
+
# @return An array of Host objects
|
72
|
+
def failed_hosts
|
73
|
+
@failed_hosts ||= []
|
74
|
+
end
|
75
|
+
|
76
|
+
# Subscribe to an event
|
77
|
+
# @param event_name [Symbol] The event to subscribe to.
|
78
|
+
# @yieldparam block The block to execute
|
79
|
+
def on(event_name, &block)
|
80
|
+
event_bus.on(event_name, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# Creates an array of username and password combinations, using the previously supplied usernames and passwords.
|
86
|
+
# @return An array of (yet to be validated!) username and password combinations
|
87
|
+
# @example
|
88
|
+
# [ ['root', 'letmein'], ['admin', 'password'] ]
|
89
|
+
def username_password_combinations
|
90
|
+
@usernames.product(@passwords)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Stores valid credentials into the #identified_credentials array
|
94
|
+
# @param host [Host] The host object for which to store the valid credentials
|
95
|
+
# @param username [String] The valid username for this host
|
96
|
+
# @param password [String] The valid password for this host
|
97
|
+
def store_valid_credentials(host, username, password)
|
98
|
+
identified_credentials[host.to_uri] = [] unless identified_credentials.has_key? host.to_uri
|
99
|
+
identified_credentials[host.to_uri] << [username, password]
|
100
|
+
|
101
|
+
event_bus.emit(:credentials_identified, host, username, password)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Stores the unreachable host into the unreachable hosts array
|
105
|
+
# @param host [Host] The unreachable host.
|
106
|
+
# @param message [String] A message with further information about the unreachability of the host.
|
107
|
+
def store_unreachable_host(host, message)
|
108
|
+
unreachable_hosts[host.to_uri] = message
|
109
|
+
end
|
110
|
+
|
111
|
+
# Stores the host (for which no valid credentials could be identified) into the failed hosts array.
|
112
|
+
# @param host [Host] the host for which no valid credentials could be identified.
|
113
|
+
def store_failed_host(host)
|
114
|
+
failed_hosts << host
|
115
|
+
end
|
116
|
+
|
117
|
+
def event_bus
|
118
|
+
@event_bus ||= EventBus.new
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class ShellStrike::EventBus
|
2
|
+
def on(event_name, &block)
|
3
|
+
raise ShellStrike::InvalidEvent unless event_name.is_a?(Symbol)
|
4
|
+
|
5
|
+
listeners[event_name] ||= []
|
6
|
+
listeners[event_name] << block
|
7
|
+
end
|
8
|
+
|
9
|
+
def emit(event_name, *args)
|
10
|
+
raise ShellStrike::InvalidEvent unless event_name.is_a?(Symbol)
|
11
|
+
return unless listeners[event_name].respond_to? :each
|
12
|
+
|
13
|
+
listeners[event_name].each { |e| e.call(*args) }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def listeners
|
19
|
+
@listeners ||= {}
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class ShellStrike::Host
|
2
|
+
attr_reader :host, :port, :connection_timeout, :actions
|
3
|
+
|
4
|
+
# Initialises a new Host object.
|
5
|
+
# @param host [String] the hostname or IP address of the host.
|
6
|
+
# @param port [Number] the port number to use for the connection.
|
7
|
+
# @param connection_timeout [Number] how long to wait before timing out connection attempts (in seconds).
|
8
|
+
# @param actions [Array<String>] Shell commands to execute against the server, upon successful connection. Interactive commands are NOT supported.
|
9
|
+
def initialize(host, port = 22, connection_timeout = 30, actions = [])
|
10
|
+
@host = host
|
11
|
+
@port = port
|
12
|
+
@connection_timeout = connection_timeout
|
13
|
+
@actions = actions
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the current host's address in URI form.
|
17
|
+
# @return [String] the current host's address in URI form. (host:port)
|
18
|
+
# @example
|
19
|
+
# 192.168.1.200:22
|
20
|
+
# 172.20.16.20:200
|
21
|
+
# example.com:22
|
22
|
+
def to_uri
|
23
|
+
"#{self.host}:#{self.port}"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class ShellStrike::Result
|
2
|
+
# Initialises a new Result object, used to represent whether a task was successful.
|
3
|
+
# @param success_value [Boolean] whether the action was successful.
|
4
|
+
# @param message [String] a message explaining the result.
|
5
|
+
# @param error_type [Symbol, nil] a symbol representing the type of failure.
|
6
|
+
def initialize(success_value, message = '', error_type = nil)
|
7
|
+
@success_value = success_value
|
8
|
+
@message = message
|
9
|
+
@error_type = error_type
|
10
|
+
end
|
11
|
+
|
12
|
+
# Whether the success_value is set to true
|
13
|
+
def success?
|
14
|
+
@success_value
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?
|
18
|
+
success?
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [String] a message explaining the result.
|
22
|
+
def message
|
23
|
+
@message
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [Symbol, nil] a symbol representing the type of error that occurred; or `nil` if #success? is true
|
27
|
+
def error_type
|
28
|
+
return nil if success?
|
29
|
+
|
30
|
+
@error_type
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
module ShellStrike::Ssh
|
5
|
+
class << self
|
6
|
+
def check_host_reachable(host)
|
7
|
+
reachable = false
|
8
|
+
explanation = nil
|
9
|
+
|
10
|
+
begin
|
11
|
+
establish_connection(host, 'fake_username', 'fake_password')
|
12
|
+
reachable = true
|
13
|
+
rescue Net::SSH::AuthenticationFailed
|
14
|
+
reachable = true
|
15
|
+
rescue Net::SSH::ConnectionTimeout
|
16
|
+
explanation = :timeout
|
17
|
+
rescue Errno::EHOSTUNREACH
|
18
|
+
explanation = :unreachable
|
19
|
+
rescue Net::SSH::Exception
|
20
|
+
explanation = :unexpected_error
|
21
|
+
end
|
22
|
+
|
23
|
+
[reachable, explanation]
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid_credentials?(host, username, password)
|
27
|
+
return false unless check_host_reachable(host)[0]
|
28
|
+
|
29
|
+
begin
|
30
|
+
establish_connection(host, username, password)
|
31
|
+
true
|
32
|
+
rescue Net::SSH::Exception, Errno::EHOSTUNREACH
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Executes the specified command against the specified host.
|
38
|
+
# @param host [ShellStrike::Host] The host to execute the command against.
|
39
|
+
# @param username [String] The username to use to establish the connection.
|
40
|
+
# @param password [String] The password to use to establish the connection.
|
41
|
+
# @param command [String] The command to run against the remote host.
|
42
|
+
# @raise [HostUnreachableError] If a connection to the host could not be established.
|
43
|
+
# @raise [InvalidCredentialsError] If the credentials supplied are invalid.
|
44
|
+
# @return [CommandResult] The result of the command's execution.
|
45
|
+
def execute_command(host, username, password, command)
|
46
|
+
raise HostUnreachableError unless check_host_reachable(host)[0]
|
47
|
+
raise InvalidCredentialsError unless valid_credentials?(host, username, password)
|
48
|
+
|
49
|
+
exit_code = 1
|
50
|
+
stdout_text = ''
|
51
|
+
stderr_text = ''
|
52
|
+
|
53
|
+
|
54
|
+
establish_connection(host, username, password) do |ssh|
|
55
|
+
ssh.open_channel do |channel|
|
56
|
+
channel.exec(command) do |ch, success|
|
57
|
+
raise CommandExecutionFailureError unless success
|
58
|
+
|
59
|
+
# Process the stdout stream
|
60
|
+
ch.on_data do |_, stdout_buf|
|
61
|
+
stdout_text << stdout_buf.read
|
62
|
+
end
|
63
|
+
|
64
|
+
# Process the stderr stream
|
65
|
+
ch.on_extended_data do |_, stderr_buf|
|
66
|
+
stderr_text << stderr_buf.read
|
67
|
+
end
|
68
|
+
|
69
|
+
ch.on_request('exit-status') do |_, status|
|
70
|
+
exit_code = status.read_long
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
channel.wait # Make sure we don't close the channel until the command has completed
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Split the output streams at each linefeed
|
79
|
+
stdout_arr = stdout_text.split("\n")
|
80
|
+
stderr_arr = stderr_text.split("\n")
|
81
|
+
|
82
|
+
CommandResult.new(command: command, exit_code: exit_code, stdout: stdout_arr, stderr: stderr_arr)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
# Establishes a connection to the specified host.
|
89
|
+
# @param host [ShellStrike::Host] The host to connect to.
|
90
|
+
# @param username [String] The username to connect using.
|
91
|
+
# @param password [String] The password to connect using.
|
92
|
+
# @yieldparam [Net::SSH::Connection::Session] block The block to execute against the established SSH connection.
|
93
|
+
def self.establish_connection(host, username, password, &block)
|
94
|
+
Net::SSH::start(host.host, username, password: password, port: host.port, non_interactive: true, timeout: host.connection_timeout, &block)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
require_relative 'ssh/command_result'
|
100
|
+
require_relative 'ssh/errors'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "shell_strike/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "shell_strike"
|
8
|
+
spec.version = ShellStrike::VERSION
|
9
|
+
spec.authors = ["Andrew Walter"]
|
10
|
+
spec.email = ["andrew@xtrasimplicity.com"]
|
11
|
+
|
12
|
+
spec.summary = "A simple ruby gem to automatically identify valid SSH credentials for a server using custom username and password dictionaries."
|
13
|
+
spec.homepage = "https://github.com/xtrasimplicity/shell-strike"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "net-ssh"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "cucumber"
|
29
|
+
spec.add_development_dependency "rspec"
|
30
|
+
spec.add_development_dependency "coveralls"
|
31
|
+
spec.add_development_dependency "yard"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shell_strike
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Walter
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ssh
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cucumber
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: yard
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- andrew@xtrasimplicity.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- appveyor.yml
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- lib/shell_strike.rb
|
130
|
+
- lib/shell_strike/event_bus.rb
|
131
|
+
- lib/shell_strike/exceptions.rb
|
132
|
+
- lib/shell_strike/host.rb
|
133
|
+
- lib/shell_strike/result.rb
|
134
|
+
- lib/shell_strike/ssh.rb
|
135
|
+
- lib/shell_strike/ssh/command_result.rb
|
136
|
+
- lib/shell_strike/ssh/errors.rb
|
137
|
+
- lib/shell_strike/version.rb
|
138
|
+
- shell_strike.gemspec
|
139
|
+
homepage: https://github.com/xtrasimplicity/shell-strike
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.6.13
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: A simple ruby gem to automatically identify valid SSH credentials for a server
|
163
|
+
using custom username and password dictionaries.
|
164
|
+
test_files: []
|