I am a newbie to python programming. I am writing small smtp client for testing the POSTFIX smtp server implementation. I have used python smtplib for the same. Here is the code snippet.
import smtplib
def smtpTest(serverStr, mode=1):
servername = serverStr
server = smtplib.SMTP(servername, 25)
try:
server.set_debuglevel(True)
server.ehlo()
# If we can encrypt this session, do it
if server.has_extn('STARTTLS'):
server.docmd('STARTTLS\r\n RSET')
finally:
print "Closing the smtp session ...."
server.quit()
if __name__ == "__main__":
print "Closing the session now ..."
smtpTest(<ip address of smtp server>, 1)`
I am using the docmd() which should for the smtp server response. I am trying to check the smtp server behavior for the command 'STARTTLS\r\n RSET'.
Response details:- My expectation is that the smtp server, if vulnerable, should give 2 response codes in sequence. One for the STARTTLS and other for RSET commands. My observation is that the server gets ready with tls tunnel and then the code enters in the finally block to close the session. So my conclusion is that the server is not vulnerable to STARTTLS.
My query:- If I reverse the command string as 'RSET\r\n STARTTLS', I get the 2 response codes. With the reverse string of resetting and then starting tls, the quit function call ends the session and the command display for starttls appears after the client sends the quit command. I wanted to synchronize the call of quit only after I get the complete response from the server. In this case, the first being for RSET and then for STARTTLS. Any pointers will be of great help.