@@ -107,7 +107,12 @@ def connect_login(global = true)
107
107
# Have to double the username. SMTP auth is weird
108
108
user = "#{ datastore [ "USERNAME" ] } \0 " * 2
109
109
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
111
116
else
112
117
print_status ( "Server requested auth and no creds given, trying to continue anyway" )
113
118
end
@@ -117,7 +122,12 @@ def connect_login(global = true)
117
122
auth = Rex ::Text . encode_base64 ( "#{ datastore [ "PASSWORD" ] } " )
118
123
raw_send_recv ( "AUTH LOGIN\r \n " , nsock )
119
124
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
121
131
else
122
132
print_status ( "Server requested auth and no creds given, trying to continue anyway" )
123
133
end
@@ -157,34 +167,37 @@ def send_message(data)
157
167
end
158
168
159
169
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
171
181
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
177
187
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
182
199
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' ] } >"
188
201
end
189
202
190
203
if not already_connected
0 commit comments