Class: SSHScan::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_scan/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(ip, port, timeout = 3) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/ssh_scan/client.rb', line 10

def initialize(ip, port, timeout = 3)
  @ip = ip
  @timeout = timeout

  @port = port.to_i
  @client_banner = SSHScan::Constants::DEFAULT_CLIENT_BANNER
  @server_banner = nil
  @kex_init_raw = SSHScan::Constants::DEFAULT_KEY_INIT.to_binary_s
end

Instance Method Details



28
29
30
# File 'lib/ssh_scan/client.rb', line 28

def banner
  @server_banner
end

#closeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/ssh_scan/client.rb', line 32

def close()
  begin
    unless @sock.nil?
      @sock.close
    end
  rescue
    @sock = nil
  end
  return true
end

#connectObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ssh_scan/client.rb', line 43

def connect()
  @error = nil

  begin
    Timeout::timeout(@timeout) {
      @sock = Socket.tcp(@ip, @port, connect_timeout: @timeout)
      @raw_server_banner = @sock.gets
    }
  rescue SocketError => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  rescue Errno::ETIMEDOUT,
         Timeout::Error => e
    @error = SSHScan::Error::ConnectTimeout.new(e.message)
    @sock = nil
  rescue Errno::ECONNREFUSED => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  rescue Errno::ENETUNREACH => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
   rescue Errno::ECONNRESET => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  rescue Errno::EACCES => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  rescue Errno::EHOSTUNREACH => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  rescue Errno::ENOPROTOOPT => e
    @error = SSHScan::Error::ConnectionRefused.new(e.message)
    @sock = nil
  else
    if @raw_server_banner.nil?
      @error = SSHScan::Error::NoBanner.new(
        "service did not respond with an SSH banner"
      )
      @sock = nil
    else
      @raw_server_banner = @raw_server_banner.chomp
      @server_banner = SSHScan::Banner.read(@raw_server_banner)
      @sock.puts(@client_banner.to_s)
    end
  end
end

#errorObject



94
95
96
# File 'lib/ssh_scan/client.rb', line 94

def error
  @error
end

#error?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ssh_scan/client.rb', line 90

def error?
  !@error.nil?
end

#get_kex_result(kex_init_raw = @kex_init_raw) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ssh_scan/client.rb', line 98

def get_kex_result(kex_init_raw = @kex_init_raw)
  if !@sock
    @error = "Socket is no longer valid"
    return nil
  end

  kex_exchange_init = nil

  begin
    Timeout::timeout(@timeout) {
      @sock.write(kex_init_raw)
      resp = @sock.read(4)

      if resp.nil?
        @error = SSHScan::Error::NoKexResponse.new(
          "service did not respond to our kex init request"
        )
        @sock = nil
        return nil
      end

      resp += @sock.read(resp.unpack("N").first)
      @sock.close

      kex_exchange_init = SSHScan::KeyExchangeInit.read(resp)
    }
  rescue Errno::ETIMEDOUT,
         Timeout::Error => e
    @error = SSHScan::Error::ConnectTimeout.new(e.message)
    @sock = nil
    return nil
  rescue Errno::ECONNREFUSED,
         Errno::ENETUNREACH,
         Errno::ECONNRESET,
         Errno::EACCES,
         Errno::EHOSTUNREACH
    @error = SSHScan::Error::NoKexResponse.new(
      "service did not respond to our kex init request"
    )
    @sock = nil
    return nil
  end

  return kex_exchange_init.to_hash
end

#ipObject



20
21
22
# File 'lib/ssh_scan/client.rb', line 20

def ip
  @ip
end

#portObject



24
25
26
# File 'lib/ssh_scan/client.rb', line 24

def port
  @port
end