Skip to content

Commit

Permalink
Merge pull request #23 from ninoseki/update-ssh
Browse files Browse the repository at this point in the history
feat: update SSH fingerprint
  • Loading branch information
ninoseki committed Jan 3, 2020
2 parents c98cc7d + bbf17bb commit c41d10c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/apullo/fingerprints/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Apullo
module Fingerprint
class SSH < Base
DEFAULT_OPTIONS = { "timeout" => 3 }.freeze
DEFAULT_PORT = 22
DEFAULT_PORTS = [22, 2222].freeze

private

Expand All @@ -16,7 +16,7 @@ def build_results

def pluck_fingerprints
result = scan
keys = result.dig("keys") || []
keys = result.dig("keys") || {}
keys.map do |cipher, data|
raw = data.dig("raw")
fingerprints = data.dig("fingerprints") || []
Expand All @@ -30,14 +30,23 @@ def pluck_fingerprints
end.to_h
end

def scan
return {} unless target.host
def _scan(target, port: 22)
return nil unless target.host

engine = SSHScan::ScanEngine.new
dest = "#{target.host}:#{DEFAULT_PORT}"
dest = "#{target.host}:#{port}"
result = engine.scan_target(dest, DEFAULT_OPTIONS)
result.to_hash
end

def scan
[target].product(DEFAULT_PORTS).each do |target, port|
result = _scan(target, port: port)
keys = result.dig("keys") || {}
return result unless keys.empty?
end
{}
end
end
end
end
15 changes: 15 additions & 0 deletions spec/fingerprints/ssh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,20 @@
expect(results.dig(key)).to be_a(Hash)
end
end

context "when 22 port is closed" do
before do
allow(mock).to receive(:scan_target).with("1.1.1.1:22", "timeout" => 3).and_return({})
allow(mock).to receive(:scan_target).with("1.1.1.1:2222", "timeout" => 3).and_return(scan_results)
allow(SSHScan::ScanEngine).to receive(:new).and_return(mock)
end

it do
results = subject.results
%w(rsa ecdsa-sha2-nistp256 ed25519).each do |key|
expect(results.dig(key)).to be_a(Hash)
end
end
end
end
end

0 comments on commit c41d10c

Please sign in to comment.