SMTP through Exchange using Integrated Windows Authentication (NTLM) using Python

后端 未结 3 1139
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 09:18

I want to use the credentials of the logged-in Windows user to authenticate an SMTP connection to an Exchange server using NTLM.

I\'m aware of the python-ntlm module

3条回答
  •  抹茶落季
    2020-12-24 09:54

    Python 2.7.x will fail on sending the NTLM Type 3 message due to the blank cmd specified:

    code, response = smtp.docmd("", ntlm_message)

    This ends up sending the correct response back to the server, however it pre-pends a space due to the nature of docmd() calling putcmd().

    smtplib.py:

    def putcmd(self, cmd, args=""):
        """Send a command to the server."""
        if args == "":
            str = '%s%s' % (cmd, CRLF)
        else:
            str = '%s %s%s' % (cmd, args, CRLF)
        self.send(str)
    
    # ...
    
    def docmd(self, cmd, args=""):
        """Send a command, and return its response code."""
        self.putcmd(cmd, args)
        return self.getreply()
    

    which as a result takes the path of the else condition, thereby sending str(' ' + ntlm_message + CRLF) which results in (501, 'Syntax error in parameters or arguments').

    As such the fix is simply to send the NTLM message as the cmd.

    code, response = smtp.docmd(ntlm_message)

    A fix to the above answer was submitted, though who knows when it will be reviewed/accepted.

提交回复
热议问题