Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit 6be2cdc

Browse files
author
Jonathan Claudius
authored
Merge pull request #474 from mozilla/fix_fingerprint_storage_location
Fix up fingerprint storage capabilities
2 parents 6428df3 + 9638c0c commit 6be2cdc

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

bin/ssh_scan

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ options = {
1818
"threads" => 5,
1919
"verbosity" => nil,
2020
"logger" => Logger.new(STDERR),
21-
"fingerprint_database" => File.join(File.dirname(__FILE__),"../data/fingerprints.yml")
21+
"fingerprint_database" => ENV['HOME']+'/.ssh_scan_fingerprints.yml'
2222
}
2323

2424
# Reorder arguments before parsing
@@ -247,9 +247,9 @@ end
247247
#end
248248

249249
# Limit scope of fingerprints DB to (per scan)
250-
if options["fingerprint_database"] && File.exists?(options["fingerprint_database"])
251-
File.unlink(options["fingerprint_database"])
252-
end
250+
# if options["fingerprint_database"] && File.exists?(options["fingerprint_database"])
251+
# File.unlink(options["fingerprint_database"])
252+
# end
253253

254254
options["policy_file"] = SSHScan::Policy.from_file(options["policy"])
255255

lib/ssh_scan/scan_engine.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'socket'
22
require 'ssh_scan/client'
33
require 'ssh_scan/crypto'
4-
#require 'ssh_scan/fingerprint_database'
4+
require 'ssh_scan/fingerprint_database'
5+
require 'ssh_scan/subprocess'
56
require 'net/ssh'
67
require 'logger'
78
require 'open3'
@@ -122,17 +123,10 @@ def scan_target(socket, opts)
122123

123124
output = ""
124125

125-
begin
126-
Timeout::timeout(timeout) {
127-
stdin, stdout, stderr, wait_thr = Open3.popen3('ssh-keyscan', '-t', 'rsa,dsa', '-p', port.to_s, target)
128-
output = stdout.gets(nil) if port.nil?
129-
stdout.close
130-
output = stderr.gets(nil) if !port.nil?
131-
stderr.close
132-
exit_code = wait_thr.value
133-
}
134-
rescue Timeout::Error
135-
#nop
126+
cmd = ['ssh-keyscan', '-t', 'rsa,dsa', '-p', port.to_s, target].join(" ")
127+
128+
Utils::Subprocess.new(cmd) do |stdout, stderr, thread|
129+
output += stdout
136130
end
137131

138132
host_keys = output.split

lib/ssh_scan/subprocess.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'open3'
2+
3+
module Utils
4+
class Subprocess
5+
def initialize(cmd, &block)
6+
# see: http://stackoverflow.com/a/1162850/83386
7+
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
8+
# read each stream from a new thread
9+
{ :out => stdout, :err => stderr }.each do |key, stream|
10+
Thread.new do
11+
until (line = stream.gets).nil? do
12+
# yield the block depending on the stream
13+
if key == :out
14+
yield line, nil, thread if block_given?
15+
else
16+
yield nil, line, thread if block_given?
17+
end
18+
end
19+
end
20+
end
21+
22+
thread.join # don't exit until the external process is done
23+
end
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)