Skip to content

Commit 2e198ae

Browse files
committed
Land rapid7#7721, better smtp connection error messages
2 parents f74fd9e + 62d8cc7 commit 2e198ae

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

lib/msf/core/exploit/smtp_deliver.rb

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ def connect_login(global = true)
107107
# Have to double the username. SMTP auth is weird
108108
user = "#{datastore["USERNAME"]}\0" * 2
109109
auth = Rex::Text.encode_base64("#{user}#{datastore["PASSWORD"]}")
110-
raw_send_recv("AUTH PLAIN #{auth}\r\n", nsock)
110+
res = raw_send_recv("AUTH PLAIN #{auth}\r\n", nsock)
111+
unless res[0..2] == '235'
112+
print_error("Authentication failed, quitting")
113+
disconnect(nsock)
114+
raise RuntimeError.new 'Could not authenticate to SMTP server'
115+
end
111116
else
112117
print_status("Server requested auth and no creds given, trying to continue anyway")
113118
end
@@ -117,7 +122,12 @@ def connect_login(global = true)
117122
auth = Rex::Text.encode_base64("#{datastore["PASSWORD"]}")
118123
raw_send_recv("AUTH LOGIN\r\n",nsock)
119124
raw_send_recv("#{user}\r\n",nsock)
120-
raw_send_recv("#{auth}\r\n",nsock)
125+
res = raw_send_recv("#{auth}\r\n",nsock)
126+
unless res[0..2] == '235'
127+
print_error("Authentication failed, quitting")
128+
disconnect(nsock)
129+
raise RuntimeError.new 'Could not authenticate to SMTP server'
130+
end
121131
else
122132
print_status("Server requested auth and no creds given, trying to continue anyway")
123133
end
@@ -157,34 +167,37 @@ def send_message(data)
157167
end
158168

159169
raw_send_recv("MAIL FROM: <#{datastore['MAILFROM']}>\r\n", nsock)
160-
raw_send_recv("RCPT TO: <#{datastore['MAILTO']}>\r\n", nsock)
161-
162-
resp = raw_send_recv("DATA\r\n", nsock)
163-
164-
# If the user supplied a Date field, use that, else use the current
165-
# DateTime in the proper RFC2822 format.
166-
if datastore['DATE'].present?
167-
date = "Date: #{datastore['DATE']}\r\n"
168-
else
169-
date = "Date: #{DateTime.now.rfc2822}\r\n"
170-
end
170+
res = raw_send_recv("RCPT TO: <#{datastore['MAILTO']}>\r\n", nsock)
171+
if res[0..2] == '250'
172+
resp = raw_send_recv("DATA\r\n", nsock)
173+
174+
# If the user supplied a Date field, use that, else use the current
175+
# DateTime in the proper RFC2822 format.
176+
if datastore['DATE'].present?
177+
date = "Date: #{datastore['DATE']}\r\n"
178+
else
179+
date = "Date: #{DateTime.now.rfc2822}\r\n"
180+
end
171181

172-
# If the user supplied a Subject field, use that
173-
subject = nil
174-
if datastore['SUBJECT'].present?
175-
subject = "Subject: #{datastore['SUBJECT']}\r\n"
176-
end
182+
# If the user supplied a Subject field, use that
183+
subject = nil
184+
if datastore['SUBJECT'].present?
185+
subject = "Subject: #{datastore['SUBJECT']}\r\n"
186+
end
177187

178-
# Avoid sending tons of data and killing the connection if the server
179-
# didn't like us.
180-
if not resp or not resp[0,3] == '354'
181-
print_error("Server refused our mail")
188+
# Avoid sending tons of data and killing the connection if the server
189+
# didn't like us.
190+
if not resp or not resp[0,3] == '354'
191+
print_error("Server refused our mail")
192+
else
193+
full_msg = ''
194+
full_msg << date unless data =~ /date: /i
195+
full_msg << subject unless subject.nil? || data =~ /subject: /i
196+
full_msg << data
197+
send_status = raw_send_recv("#{full_msg}\r\n.\r\n", nsock)
198+
end
182199
else
183-
full_msg = ''
184-
full_msg << date unless data =~ /date: /i
185-
full_msg << subject unless subject.nil? || data =~ /subject: /i
186-
full_msg << data
187-
send_status = raw_send_recv("#{full_msg}\r\n.\r\n", nsock)
200+
print_error "Server refused to send to <#{datastore['MAILTO']}>"
188201
end
189202

190203
if not already_connected

0 commit comments

Comments
 (0)