Skip to content

Commit 80f1173

Browse files
committed
Style and scanner usability cleanup for ssh_version
1 parent 8d640a0 commit 80f1173

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

modules/auxiliary/scanner/ssh/ssh_version.rb

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,74 @@
55

66
require 'msf/core'
77

8-
98
class Metasploit3 < Msf::Auxiliary
10-
119
include Msf::Exploit::Remote::Tcp
1210
include Msf::Auxiliary::Scanner
1311
include Msf::Auxiliary::Report
1412

13+
# the default timeout (in seconds) to wait, in total, for both a successful
14+
# connection to a given endpoint and for the initial protocol response
15+
# from the supposed SSH endpoint to be returned
16+
DEFAULT_TIMEOUT = 30
17+
1518
def initialize
1619
super(
1720
'Name' => 'SSH Version Scanner',
1821
'Description' => 'Detect SSH Version.',
1922
'References' =>
2023
[
21-
[ 'URL', 'http://en.wikipedia.org/wiki/SecureShell' ],
24+
[ 'URL', 'http://en.wikipedia.org/wiki/SecureShell' ]
2225
],
2326
'Author' => [ 'Daniel van Eeden <metasploit[at]myname.nl>' ],
2427
'License' => MSF_LICENSE
2528
)
2629

2730
register_options(
28-
[
29-
Opt::RPORT(22),
30-
OptInt.new('TIMEOUT', [true, 'Timeout for the SSH probe', 30])
31-
], self.class)
31+
[
32+
Opt::RPORT(22),
33+
OptInt.new('TIMEOUT', [true, 'Timeout for the SSH probe', DEFAULT_TIMEOUT])
34+
],
35+
self.class
36+
)
3237
end
3338

34-
def to
35-
return 30 if datastore['TIMEOUT'].to_i.zero?
36-
datastore['TIMEOUT'].to_i
39+
def timeout
40+
datastore['TIMEOUT'] <= 0 ? DEFAULT_TIMEOUT : datastore['TIMEOUT']
3741
end
3842

3943
def run_host(target_host)
4044
begin
41-
::Timeout.timeout(to) do
42-
45+
::Timeout.timeout(timeout) do
4346
connect
4447

45-
resp = sock.get_once(-1, 5)
48+
resp = sock.get_once(-1, timeout)
4649

47-
if (resp and resp =~ /SSH/)
48-
ver,msg = (resp.split(/[\r\n]+/))
49-
# Check to see if this is Kippo, which sends a premature
50-
# key init exchange right on top of the SSH version without
51-
# waiting for the required client identification string.
52-
if msg and msg.size >= 5
53-
extra = msg.unpack("NCCA*") # sz, pad_sz, code, data
54-
if (extra.last.size+2 == extra[0]) and extra[2] == 20
55-
ver << " (Kippo Honeypot)"
50+
if resp
51+
if resp =~ /^SSH/
52+
ver, msg = resp.split(/[\r\n]+/)
53+
# Check to see if this is Kippo, which sends a premature
54+
# key init exchange right on top of the SSH version without
55+
# waiting for the required client identification string.
56+
if msg && msg.size >= 5
57+
extra = msg.unpack("NCCA*") # sz, pad_sz, code, data
58+
if (extra.last.size + 2 == extra[0]) && extra[2] == 20
59+
ver << " (Kippo Honeypot)"
60+
end
5661
end
62+
print_status("#{target_host}:#{rport}, SSH server version: #{ver}")
63+
report_service(host: rhost, port: rport, name: 'ssh', proto: 'tcp', info: ver)
64+
else
65+
vprint_warning("#{target_host}:#{rport} was not SSH --" \
66+
" #{resp.size} bytes beginning with #{resp[0, 12]}")
5767
end
58-
print_status("#{target_host}:#{rport}, SSH server version: #{ver}")
59-
report_service(:host => rhost, :port => rport, :name => "ssh", :proto => "tcp", :info => ver)
6068
else
61-
print_error("#{target_host}:#{rport}, SSH server version detection failed!")
69+
vprint_warning("#{target_host}:#{rport} no response")
6270
end
63-
64-
disconnect
6571
end
66-
6772
rescue Timeout::Error
68-
print_error("#{target_host}:#{rport}, Server timed out after #{to} seconds. Skipping.")
73+
vprint_warning("#{target_host}:#{rport} timed out after #{timeout} seconds. Skipping.")
74+
ensure
75+
disconnect
6976
end
7077
end
7178
end

0 commit comments

Comments
 (0)