Module: SSH_Fingerprint2

Defined in:
lib/audit/lib/ssh_fingerprint2.rb

Class Method Summary collapse

Class Method Details

.fingerprint(host, port = 22, timeout = 10) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/audit/lib/ssh_fingerprint2.rb', line 122

def self.fingerprint(host, port = 22, timeout = 10)
	results = {:host => host, :port => port, :timestamp => Time.now(), :status => :OK}
	logger = Logger.new(STDERR)
	logger.level = Logger::ERROR
	
	begin
		timeout(timeout) do
			begin
				ssh_transport = Net::SSH::Transport::Session.new('localhost', {:logger => logger, :port => port})
				results[:header] = ssh_transport.server_version().header();
				results[:server_version] = ssh_transport.server_version().version();
				results[:cookie] = ssh_transport.algorithms().server_data()[:raw][1 ... 17].bytes().to_a()
				results[:kex_algorithms] = ssh_transport.algorithms().server_data()[:kex]
				results[:server_host_key_algorithms] = ssh_transport.algorithms().server_data()[:host_key]
				results[:encryption_algorithms_client_to_server] = ssh_transport.algorithms().server_data()[:encryption_client]
				results[:encryption_algorithms_server_to_client] = ssh_transport.algorithms().server_data()[:encryption_server]
				results[:mac_algorithms_client_to_server] = ssh_transport.algorithms().server_data()[:hmac_client]
				results[:mac_algorithms_server_to_client] = ssh_transport.algorithms().server_data()[:hmac_server]
				results[:compression_algorithms_client_to_server] = ssh_transport.algorithms().server_data()[:compression_client]
				results[:compression_algorithms_server_to_client] = ssh_transport.algorithms().server_data()[:compression_server]
				results[:languages_client_to_server] = ssh_transport.algorithms().server_data()[:language_client]
				results[:languages_server_to_client] = ssh_transport.algorithms().server_data()[:language_server]
				
				results[:server_keys] = []
				results[:server_keys] << read_key(ssh_transport.algorithms().key_data()[:server_key])
				
				ssh_transport.algorithms().algorithms()[:host_key].reject {|x| x == ssh_transport.algorithms().key_data()[:type]}.each do|host_key_algorithm|
					ssh_transport.close()
					ssh_transport = Net::SSH::Transport::Session.new('localhost', {:logger => logger, :host_key => host_key_algorithm, :paranoid => false})
					
					results[:server_keys] << read_key(ssh_transport.algorithms().key_data()[:server_key])
				end
				
				ssh_authentication = Net::SSH::Authentication::Session.new(ssh_transport, {:logger => logger, :auth_methods => ["none"]})
				ssh_authentication.authenticate("ssh-connection", "dummy")
				
				results[:authentication_methods] = ssh_authentication.allowed_auth_methods()
			rescue Net::SSH::AuthenticationFailed => ex
			end
		end
	rescue Timeout::Error => err	
		return {:host => host, :port => port, :timestamp => Time.now(), :status => :ERROR, :type => :TIMEOUT, :error => err}
	rescue Errno::ECONNREFUSED => err
		return {:host => host, :port => port, :timestamp => Time.now(), :status => :ERROR, :type => :CONNECTION_REFUSED, :error => err}
	end

	return results
end

.read_key(key) ⇒ Object

Read all interesting properties from a key and save them in a map



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/audit/lib/ssh_fingerprint2.rb', line 95

def self.read_key(key)
	buffer = Net::SSH::Buffer.new(key.to_blob())
	type = buffer.read_string()
	
	case type
		when "ssh-rsa"
			e = buffer.read_string()
			n = buffer.read_string()
			size = (n.length() - 1) * 8
		when "ssh-dss"
			p = buffer.read_string()
			q = buffer.read_string()
			g = buffer.read_string()
			pub_key = buffer.read_string()
			size = (pub_key.length() - 1) * 8
	end
			
	
	return {#:key => key, 
			:blob => [key.to_blob()].pack("m").strip(), 
			:fingerprint => key.fingerprint(),
			:size => size,
			:type => key.ssh_type()}
end