Module: Msf::Auxiliary::Login

Included in:
Exploit::Remote::Telnet
Defined in:
lib/msf/core/auxiliary/login.rb

Overview

This module exposes methods that may be useful to exploits that deal with servers that require authentication via /bin/login

Constant Summary collapse

NULL =
"\000"
CR =
"\r"
LF =
"\n"
EOL =
CR + LF

Instance Method Summary collapse

Instance Method Details

#busy_message?Boolean


122
123
124
125
126
127
128
129
# File 'lib/msf/core/auxiliary/login.rb', line 122

def busy_message?
  recvn = @recvd.gsub(@busy_regex, '')
  if(recvn != @recvd)
    @recvd = recvn.strip
    return true
  end
  false
end

#command_echo?(cmd) ⇒ Boolean


104
105
106
107
108
109
110
111
# File 'lib/msf/core/auxiliary/login.rb', line 104

def command_echo?(cmd)
  recvn = @recvd.gsub(/^(\s*#{cmd}\r?\n\s*|\s*\*+\s*)/, '')
  if(recvn != @recvd)
    @recvd = recvn
    return true
  end
  false
end

#initialize(info = {}) ⇒ Object

Creates an instance of a login negoation module.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/msf/core/auxiliary/login.rb', line 21

def initialize(info = {})
  super

  # Appended to by each read and gets reset after each send.  Doing it
  # this way lets us deal with partial reads in the middle of expect
  # strings, e.g., the first recv returns "Pa" and the second returns
  # "ssword: "
  @recvd = ''
  @trace = ''

  #
  # Some of these regexes borrowed from NeXpose, others added from datasets
  #
  @login_regex = /(?:log[io]n( name|)|user(name|id|))\s*\:/i
  @password_regex = /(?:password|passwd)\s*\:/i
  @false_failure_regex = /(?:(^\s*last)\ login *\:|allows only\ .*\ Telnet\ Client\ License)/i
  @failure_regex = /(?:
      Incorrect | Unknown  | Fail      | Invalid  |
      Login     | Password | Passwd    | Username |
      Unable    | Error    | Denied    | Reject   |
      Refuse    | Close    | Closing   | %\ Bad   |
      Sorry     |
      Not\ on\ system\ console |
      Enter\ username\ and\ password |
      Auto\ Apply\ On |
      YOU\ LOGGED\ IN\ USING\ ALL\ UPPERCASE\ CHARACTERS|
      \n\*$ |
      (Login ?|User ?)(name|): |
      ^\s*\<[a-f0-9]+\>\s*$ |
      ^\s*220.*FTP|
      not\ allowed\ to\ log\ in
    )/mix

  @waiting_regex = /(?:
    .*please\ wait.* |
    .*one\ minute.*
  )/mix

  @busy_regex = /(?:
    Another\ telnet\ session\ is\ in\ progress | Disconnecting\.\.\.
  )/mix

  @success_regex = /(?:
      list\ of\ built-in     |
      sh.*[\#\$]\s*$         |
      \[\/\]\s*$             |
      or\ the\ MENU\ system  |
      Password\ is\ not\ set |
      logging\ in\ as\ visitor |
      Login\ successful
    )/mix
end

#login_failed?Boolean


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/msf/core/auxiliary/login.rb', line 139

def 
  # Naively, failure means matching the failure regex.
  #
  # However, this leads to problems with false positives in the case of
  # "login:" because unix systems commonly show "Last login: Sat Jan  3
  # 20:22:52" upon successful login, so check against a false-positive
  # regex, also.
  #

  # Empty strings should not count
  if @recvd.strip.length == 0
    return true
  end

  # If we have not seen a newline, this is likely an echo'd prompt
  if ! @recvd.index("\n")
    return true
  end

  # We do have a set of highly-accurate success patterns
  if (@recvd =~ @success_regex)
    return false
  end

  if @recvd =~ @failure_regex
    if @recvd !~ @false_failure_regex
      return true
    end
  end
  return false
end

#login_prompt?Boolean


99
100
101
102
# File 'lib/msf/core/auxiliary/login.rb', line 99

def 
  return true if @recvd =~ @login_regex
  return false
end

#login_succeeded?Boolean


171
172
173
174
175
176
# File 'lib/msf/core/auxiliary/login.rb', line 171

def 
  # Much easier to test for failure than success because a few key words
  # mean failure whereas all kinds of crap is used for success, much of
  # which also shows up in failure messages.
  return (not )
end

#password_prompt?(username = nil) ⇒ Boolean


131
132
133
134
135
136
137
# File 'lib/msf/core/auxiliary/login.rb', line 131

def password_prompt?(username=nil)
  return true if(@recvd =~ @password_regex)
  if username
    return true if( !(username.empty?) and @recvd =~ /#{username}'s/)
  end
  return false
end

#raw_send(cmd, nsock = self.sock) ⇒ Object

This method transmits a telnet command and does not wait for a response

Resets the @recvd buffer


216
217
218
219
220
# File 'lib/msf/core/auxiliary/login.rb', line 216

def raw_send(cmd, nsock = self.sock)
  @recvd = ''
  @trace << cmd
  nsock.put(cmd)
end

#recv(fd = self.sock, timeout = 10) ⇒ Object

Appends to the @recvd buffer which is used to tell us whether we're at a login prompt, a password prompt, or a working shell.


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/msf/core/auxiliary/login.rb', line 78

def recv(fd=self.sock, timeout=10)

  data = ''

  begin
    data = fd.get_once(-1, timeout)
    return nil if not data or data.length == 0

    # combine EOL into "\n"
    data.gsub!(/#{EOL}/no, "\n")

    @trace << data
    @recvd << data
    fd.flush

  rescue ::EOFError, ::Errno::EPIPE
  end

  data
end

#recv_all(nsock = self.sock, timeout = 10) ⇒ Object


206
207
208
209
# File 'lib/msf/core/auxiliary/login.rb', line 206

def recv_all(nsock = self.sock, timeout = 10)
  # Make sure we read something in
  wait_for(/./)
end

#send_pass(pass, nsock = self.sock) ⇒ Object

This method completes user authentication by sending the supplied password


192
193
194
195
196
197
198
# File 'lib/msf/core/auxiliary/login.rb', line 192

def send_pass(pass, nsock = self.sock)
  got_prompt = wait_for(@password_regex)
  if not got_prompt
    print_error("#{rhost} - Something is wrong, didn't get a password prompt")
  end
  return send_recv("#{pass}\r\n")
end

#send_recv(msg, nsock = self.sock) ⇒ Object


200
201
202
203
204
# File 'lib/msf/core/auxiliary/login.rb', line 200

def send_recv(msg, nsock = self.sock)
  raw_send(msg, nsock)
  recv_all(nsock)
  return @recvd
end

#send_user(user, nsock = self.sock) ⇒ Object

This method logs in as the supplied user by transmitting the username


181
182
183
184
185
186
187
# File 'lib/msf/core/auxiliary/login.rb', line 181

def send_user(user, nsock = self.sock)
  got_prompt = wait_for(@login_regex)
  if not got_prompt
    print_error("#{rhost} - Something is wrong, didn't get a login prompt")
  end
  return send_recv("#{user}\r\n")
end

#wait_for(expect, nsock = self.sock) ⇒ Object

Wait for the supplied string (or Regexp) to show up on the socket, or a timeout


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/msf/core/auxiliary/login.rb', line 226

def wait_for(expect, nsock = self.sock)
  if expect.kind_of? Regexp
    regx = expect
  else
    regx = /#{Regexp.quote(expect)}/i
  end
  return true if @recvd =~ regx

  resp = ''
  while (resp and not @recvd =~ regx)
    resp = recv(nsock)
  end

  return (@recvd =~ regx)
end

#waiting_message?Boolean


113
114
115
116
117
118
119
120
# File 'lib/msf/core/auxiliary/login.rb', line 113

def waiting_message?
  recvn = @recvd.gsub(@waiting_regex, '')
  if(recvn != @recvd)
    @recvd = recvn.strip
    return true
  end
  false
end