Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTLM authentication not working in some cases #582

Closed
ravenpride opened this issue Oct 27, 2017 · 60 comments
Closed

NTLM authentication not working in some cases #582

ravenpride opened this issue Oct 27, 2017 · 60 comments
Labels
question A question about how to do something

Comments

@ravenpride
Copy link

ravenpride commented Oct 27, 2017

Hello,

the issue occurs with version MailKit v1.18.1.1 (pulled via Nuget).

I've tried to get MailKit working with our Exchange Server. MailKit successfully connected to port 587 and established a secure channel using STARTTLS. After authenticating the Exchange server offered the mechanisms GSSAPI, NTLM and LOGIN.

I've removed GSSAPI and LOGIN from the authentication mechanisms MailKit may use:

client.AuthenticationMechanisms.Remove("GSSAPI");
client.AuthenticationMechanisms.Remove("LOGIN");

Only NTLM is left in the list of authentication mechanisms.

Trying to authenticate using...

client.Authenticate(CredentialCache.DefaultCredentials);

...throws the following exception

 --- Exception ---------------------------------------------------------------------------------------------
 --- Exception Type: System.NotSupportedException
 --- Message: No compatible authentication mechanisms found.
 --- Stacktrace:
    bei MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
    bei MailKit.MailService.Authenticate(ICredentials credentials, CancellationToken cancellationToken)
    ...

Here comes the log of the SMTP session:

S: 220 Elexis-Exch02.elexisad.net Microsoft ESMTP MAIL Service ready at Fri, 27 Oct 2017 13:08:11 +0200
C: EHLO [10.14.1.175]
S: 250-Elexis-Exch02.elexisad.net Hello [10.14.1.175]
S: 250-SIZE 52428800
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-AUTH GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250 CHUNKING
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [10.14.1.175]
S: 250-Elexis-Exch02.elexisad.net Hello [10.14.1.175]
S: 250-SIZE 52428800
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH GSSAPI NTLM LOGIN
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250 CHUNKING

I hope this helps you to track the issue down.

Thank you in advance!

@jstedfast jstedfast added the question A question about how to do something label Oct 27, 2017
@jstedfast
Copy link
Owner

MailKit 1.18 disabled NTLM authentication because I was getting too many bug reports about it not working and no way to diagnose the problems (because it worked just fine for me).

This is why you are getting NotSupportedException.

Just use LOGIN and it should work fine.

@PeterHagen
Copy link

Does this mean that NTLM won't be supported anymore?

@jstedfast
Copy link
Owner

It's not in the list of SASL mechanisms that are tried by default, but starting with 2.0, you can use it manually:

var ntlm = new SaslMechanismNtlm ("username", "password");
client.Authenticate (ntlm);

@PeterHagen
Copy link

Great! Thats seems to work!

@alex-jitbit
Copy link
Contributor

alex-jitbit commented Sep 9, 2020

Just switched from .NET builtin SmtpClient (which was working fine) to MailKit and several customers are reporting this problem now. How can I make MailKit to also try using NTLM among other methods?

@jstedfast
Copy link
Owner

@alex-jitbit

if (client.AuthenticationMechanisms.Contains ("NTLM")) {
    var ntlm = new SaslMechanismNtlm ("username", "password");
    client.Authenticate (ntlm);
} else {
    // use the default supported mechanisms
    client.Authenticate ("username", "password");
}

@alex-jitbit
Copy link
Contributor

@jstedfast thank you sir

@pawanbhabad
Copy link

Hello,
I have recently switched to Mailkit 2.5.0.0. Earlier I was using "System.net.mail.SmtpClient".
In case of NTLM authentication, only authentication supported by SMTP relay, client application is working with SmtpClient but with Mailkit it's not working (error: Authentication unsuccessful), even though the credentials provided are also same and same configuration.

I have tried using code mentioned above,

var ntlm = new SaslMechanismNtlm ("username", "password");
client.Authenticate (ntlm);

but same error.

i.e. username and password are windows login credentials.

Sharing Protocol logs,

Connected to smtp://pawan_dev.vxcountry.cinemax.com:25/?starttls=when-available
S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Thu, 5 Nov 2020 12:37:21 +0530
C: EHLO [10.217.79.102]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.79.102]
S: 250-AUTH NTLM
S: 250-TURN
S: 250-SIZE 2097152
S: 250-ETRN
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-8bitmime
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-VRFY
S: 250 OK
C: AUTH NTLM TlRMTVNTUAABAAAABxIAAAkACQAgAAAAAAAAACAAAABDT01NVU5JVFk=
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECMR2TRr16f68AAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAAq475QQrPWAQAAAAA=
C: TlRMTVNTUAADAAAAAAAAAG4AAAD2APYAbgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAABkAQAAAQIAAgoAWikAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZADczddHWVbSM1KTMjMJgPsWAQEAAAAAAADrAHxqcLPWAa0J73JjwXLDAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAAq475QQrPWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful

Does Mailkit supports NTLM authentication ?

Please suggest me some work-around for this issue.

Thanks!

@sauravcaptech
Copy link

sauravcaptech commented Nov 5, 2020

@pawanbhabad I am also facing similar issue
I have 3 different SMTP supporting NTLM auth mechanism . It works in one case but fails in other two
Attaching the logs . It will be good if someone can see this and make some sense

Case 1 : Successful

Connection

Connected to smtp://mail.celebritygroup.com:125/
S: 220 CFLEXHC.celebritygroup.int Microsoft ESMTP MAIL Service ready at Thu, 5 Nov 2020 17:30:43 +0530
C: EHLO [10.0.2.15]
S: 250-CFLEXHC.celebritygroup.int Hello [223.187.190.219]
S: 250-SIZE 20971520
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-X-ANONYMOUSTLS
S: 250-AUTH NTLM LOGIN
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW

Auth

C: AUTH NTLM TlRMTVNTUAABAAAABwIAAAAAAAAgAAAAAAAAACAAAAA=
S: 334 TlRMTVNTUAACAAAAHAAcADgAAAAFAoECu0dkY4fmB3sAAAAAAAAAAMoAygBUAAAABgGxHQAAAA9DAEUATABFAEIAUgBJAFQAWQBHAFIATwBVAFAAAgAcAEMARQBMAEUAQgBSAEkAVABZAEcAUgBPAFUAUAABAA4AQwBGAEwARQBYAEgAQwAEACQAYwBlAGwAZQBiAHIAaQB0AHkAZwByAG8AdQBwAC4AaQBuAHQAAwA0AEMARgBMAEUAWABIAEMALgBjAGUAbABlAGIAcgBpAHQAeQBnAHIAbwB1AHAALgBpAG4AdAAFACQAYwBlAGwAZQBiAHIAaQB0AHkAZwByAG8AdQBwAC4AaQBuAHQABwAIAHwg/n1rs9YBAAAAAA==
C: TlRMTVNTUAADAAAAAAAAAHIAAAD2APYAcgAAABwAHABIAAAADgAOAGQAAAAAAAAAcgAAAAAAAABoAQAAAQIAAgoAWikAAAAPQwBFAEwARQBCAFIASQBUAFkARwBSAE8AVQBQAHMAdQBwAHAAbwByAHQAVP3M3RD5KXArTOqiHfvIqwEBAAAAAAAAuZjI1Zmz1gGprENbDyd/EAAAAAACABwAQwBFAEwARQBCAFIASQBUAFkARwBSAE8AVQBQAAEADgBDAEYATABFAFgASABDAAQAJABjAGUAbABlAGIAcgBpAHQAeQBnAHIAbwB1AHAALgBpAG4AdAADADQAQwBGAEwARQBYAEgAQwAuAGMAZQBsAGUAYgByAGkAdAB5AGcAcgBvAHUAcAAuAGkAbgB0AAUAJABjAGUAbABlAGIAcgBpAHQAeQBnAHIAbwB1AHAALgBpAG4AdAAHAAgAfCD+fWuz1gEAAAAA
S: 235 2.7.0 Authentication successful

Case 2 : Failed

Connection

Connected to smtp://webmail.acilogistics.net:25/
S: 220 MAIL01.acilogistics.net Microsoft ESMTP MAIL Service ready at Thu, 5 Nov 2020 18:26:25 +0600
C: EHLO [10.0.2.15]
S: 250-MAIL01.acilogistics.net Hello [223.187.190.219]
S: 250-SIZE 26214400
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-X-ANONYMOUSTLS
S: 250-AUTH NTLM
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250 XRDST

Auth

C: AUTH NTLM TlRMTVNTUAABAAAABwIAAAAAAAAgAAAAAAAAACAAAAA=
S: 334 TlRMTVNTUAACAAAAGAAYADgAAAAFAoECePSzTnORDAIAAAAAAAAAALYAtgBQAAAABgOAJQAAAA9BAEMASQBMAE8ARwBJAFMAVABJAEMAUwACABgAQQBDAEkATABPAEcASQBTAFQASQBDAFMAAQAMAE0AQQBJAEwAMAAxAAQAIABhAGMAaQBsAG8AZwBpAHMAdABpAGMAcwAuAG4AZQB0AAMALgBNAEEASQBMADAAMQAuAGEAYwBpAGwAbwBnAGkAcwB0AGkAYwBzAC4AbgBlAHQABQAgAGEAYwBpAGwAbwBnAGkAcwB0AGkAYwBzAC4AbgBlAHQABwAIAN1owiVvs9YBAAAAAA==
C: TlRMTVNTUAADAAAAAAAAAIwAAADiAOIAjAAAABgAGABIAAAALAAsAGAAAAAAAAAAjAAAAAAAAABuAQAAAQIAAgoAWikAAAAPQQBDAEkATABPAEcASQBTAFQASQBDAFMAaABlAGwAbABvAEAAYQBjAGkAbABvAGcAaQBzAHQAaQBjAHMALgBuAGUAdACanU+zhIL8LmEFAG6pwJjKAQEAAAAAAAA/LPYum7PWAXjXpkDHlwTBAAAAAAIAGABBAEMASQBMAE8ARwBJAFMAVABJAEMAUwABAAwATQBBAEkATAAwADEABAAgAGEAYwBpAGwAbwBnAGkAcwB0AGkAYwBzAC4AbgBlAHQAAwAuAE0AQQBJAEwAMAAxAC4AYQBjAGkAbABvAGcAaQBzAHQAaQBjAHMALgBuAGUAdAAFACAAYQBjAGkAbABvAGcAaQBzAHQAaQBjAHMALgBuAGUAdAAHAAgA3WjCJW+z1gEAAAAA
S: 535 5.7.3 Authentication unsuccessful

Case 3 : Failed

Connection

Connected to smtp://mail.ajlanbros.com:587/?starttls=always
S: 220 AJL-EXCH-MBX-01.ajlanbros.com Microsoft ESMTP MAIL Service ready at Thu, 5 Nov 2020 16:20:48 +0300
C: EHLO [10.0.2.15]
S: 250-AJL-EXCH-MBX-01.ajlanbros.com Hello [223.187.190.219]
S: 250-SIZE 37748736
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-AUTH GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250 CHUNKING
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [10.0.2.15]
S: 250-AJL-EXCH-MBX-01.ajlanbros.com Hello [223.187.190.219]
S: 250-SIZE 37748736
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH GSSAPI NTLM LOGIN
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250 CHUNKING

Auth

C: AUTH NTLM TlRMTVNTUAABAAAABwIAAAAAAAAgAAAAAAAAACAAAAA=
S: 334 TlRMTVNTUAACAAAAFAAUADgAAAAFAoECOJhxa2h9hIkAAAAAAAAAAMQAxABMAAAACgA5OAAAAA9BAEoATABBAE4AQgBSAE8AUwAxAAIAFABBAEoATABBAE4AQgBSAE8AUwAxAAEAHgBBAEoATAAtAEUAWABDAEgALQBNAEIAWAAtADAAMQAEABoAYQBqAGwAYQBuAGIAcgBvAHMALgBjAG8AbQADADoAQQBKAEwALQBFAFgAQwBIAC0ATQBCAFgALQAwADEALgBhAGoAbABhAG4AYgByAG8AcwAuAGMAbwBtAAUAGgBhAGoAbABhAG4AYgByAG8AcwAuAGMAbwBtAAcACADNUKindrPWAQAAAAA=
C: TlRMTVNTUAADAAAAAAAAAIgAAADwAPAAiAAAABQAFABIAAAALAAsAFwAAAAAAAAAiAAAAAAAAAB4AQAAAQIAAgoAWikAAAAPQQBKAEwAQQBOAEIAUgBPAFMAMQBzAGgAbwBwAHAAaQBuAGcAQABhAGoAbABhAG4AYgByAG8AcwAuAGMAbwBtAI/CxjSX3C49u/3j2ovZNhkBAQAAAAAAABV7VcKks9YB+IhndHHCeSoAAAAAAgAUAEEASgBMAEEATgBCAFIATwBTADEAAQAeAEEASgBMAC0ARQBYAEMASAAtAE0AQgBYAC0AMAAxAAQAGgBhAGoAbABhAG4AYgByAG8AcwAuAGMAbwBtAAMAOgBBAEoATAAtAEUAWABDAEgALQBNAEIAWAAtADAAMQAuAGEAagBsAGEAbgBiAHIAbwBzAC4AYwBvAG0ABQAaAGEAagBsAGEAbgBiAHIAbwBzAC4AYwBvAG0ABwAIAM1QqKd2s9YBAAAAAA==
S: 535 5.7.3 Authentication unsuccessful

@jstedfast jstedfast reopened this Nov 5, 2020
@jstedfast
Copy link
Owner

I don't suppose @sauravcaptech or @pawanbhabad can get me a log of some other SMTP client/library successfully authenticating via NTLM so that I can compare what MailKit is sending to what a successful authentication is sending?

I'm stepping thru the debugger comparing @sauravcaptech's successful vs failed NTLM exchanges and I can't tell why one works and the others don't. In all cases, the exact same NTLM flags are used by the server (and MailKit). The only differences (other than usernames and passwords) are the domain names provided by the server (and used in the final authentication request to the server). That's it.

@pawanbhabad
Copy link

@sauravcaptech As per your logs, it seems, the failure is definitely because of NTLM AUTH, i.e. case 2 & 3.
First case is successful due to LOGIN AUTH mechanism supported by server. Though there is NTLM present but not used by server.
In later cases, AUTH mechanism supported is only NTLM, so it's failing (AUTH GSSAPI also fails). So you are landing into same issue what I am facing.

@jstedfast
Copy link
Owner

No, all of @sauravcaptech's authentication examples use NTLM for authenticating (even the successful one).

As you noted, however, in the case where the NTLM authentication was successful, the server also supported the LOGIN SASL mechanism.

That should be irrelevant, though, because these authentication mechanisms do not inter-relate.

@pawanbhabad
Copy link

Hello @jstedfast
As you have asked for the working API logs for comparison. Hope, this may help!
I am using same credentials with Mailkit.

Please have a look into the following logs, using "System.net.mail.SmtpClient" API, email is sent successfully.

S: 220 PNV86SMTP01.PNE.VEN.CINEMAX.COM ESMTP Sendmail 8.14.4/8.14.4; Mon, 9 Nov 2020 14:30:43 +0530
C: EHLO Pawan_Dev.vxcountry.cinemax.com
S: 250-PNV86SMTP01.PNE.VEN.CINEMAX.COM Hello [10.217.79.102], pleased to meet you | ENHANCEDSTATUSCODES | PIPELINING | 8BITMIME | SIZE | DSN | ETRN | AUTH GSSAPI DIGEST-MD5 CRAM-MD5 | DELIVERBY | HELP
C: MAIL FROM:pawan.bhabad@cinemax.com
S: 250 2.1.0 pawan.bhabad@cinemax.com... Sender ok
C: RCPT TO:pawan.bhabad@cinemax.com
S: 250 2.1.5 pawan.bhabad@cinemax.com... Recipient ok
C: DATA
S: 354 Enter mail, end with "." on a line by itself

S: 250 2.0.0 0A990hmD059588 Message accepted for delivery
C: QUIT
S: 221 2.0.0 PNV86SMTP01.PNE.VEN.CINEMAX.COM closing connection

Logs using Mailkit;

Connected to smtp://pawan_dev.vxcountry.cinemax.com:25/?starttls=when-available
S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Thu, 5 Nov 2020 12:37:21 +0530
C: EHLO [10.217.79.102]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.79.102]
S: 250-AUTH NTLM
S: 250-TURN
S: 250-SIZE 2097152
S: 250-ETRN
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-8bitmime
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-VRFY
S: 250 OK
C: AUTH NTLM TlRMTVNTUAABAAAABxIAAAkACQAgAAAAAAAAACAAAABDT01NVU5JVFk=
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECMR2TRr16f68AAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAAqrPWAQAAAA=
C: TlRMTVNTUAADAAAAAAAAAG4AAAD2APYAbgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAABkAQAAAQIAAgoAWikAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZADczddHWVbSM1KTMjMJgPsWAQEAAAAAAADrAHxqcLPWAa0J73JjwXLDAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAAqrPWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful

@jstedfast
Copy link
Owner

jstedfast commented Nov 9, 2020

@pawanbhabad

It looks like you connected to 2 different SMTP servers.

In the case of System.Net.Mail, you used a Sendmail 8.14 server while in MailKit's case, you used a Microsoft Exchange ESMTP server. In the System.Net.Mail case, you also did not authenticate (well, the System.Net.Mail SmtpClient did not authenticate even if you gave it credentials to use).

@pawanbhabad
Copy link

@jstedfast
I am using Windows Server 2012 R2 SMTP relay, where, only NTLM AUTH is enabled (in Access control authentication) and then it is connected to Office 365 as "Anonymous access" AUTH (in Outbound security).
I am trying to send email to this SMTP relay via both the APIs (i.e. SMTPClient and Mailkit) with same authentication credentials.
Corresponding logs are generated.

Also, in context to your second point, when I provide incorrect credentials to SMTPClient API OR no credentials, then it does not work. So I think SMTPClient authenticates with an appropriate credentials.

@jstedfast
Copy link
Owner

What are you using to get the logs for System.Net.Mail's SmtpClient? Perhaps it stripped out the authentication commands? In any event, the Sendmail 8.14 server does not support NTLM, so System.Net.Mail probably used GSSAPI or CRAM-MD5 (I don't think System.Net.Mail supports DIGEST-MD5).

@pawanbhabad
Copy link

Using WireShark to get logs.
Initially, the client should connect "Pawan_Dev.vxcountry.cinemax.com", as it is a SMTP relay host then it should try for "PNV86SMTP01.PNE.VEN.CINEMAX.COM" (Office 365).
Server "PNV86SMTP01.PNE.VEN.CINEMAX.COM" does not support NTLM, as you said. Only SMTP relay "Pawan_Dev.vxcountry.cinemax.com" supports NTLM AUTH.
So I think the problem lies with authenticating "Pawan_Dev.vxcountry.cinemax.com" SMTP relay server.

While capturing logs, the authentication commands are not captured. Shared logs, what all got generated from Wireshark.

@jstedfast
Copy link
Owner

Unfortunately, the bit of the logs that I need are the authentication commands

@jstedfast
Copy link
Owner

@pawanbhabad and @sauravcaptech if you guys can find a way to get me the logs of a successful NTLM authentication to a server where MailKit fails (via NTLM), please open a new bug.

I'm going to close this one because the original issue is resolved (and was really a question rather than a bug).

I'm definitely interested in fixing bugs in NTLM if I can, I just need info that unfortunately I don't have.

Basically, what I want to do, is to compare what MailKit is sending with what a working implementation is sending in your cases, this way I can hopefully figure out what MailKit is doing wrong.

@pawanbhabad
Copy link

Hello @jstedfast
I was trying to create logs of successful NTLM authentication to a server by using System.Net.Mail.SMTPClient, as it was not available earlier. Now here I am able to create it. Hope, this may be useful for comparing NTML authentications in both the cases, Mailkit and SMTPClient.

SMTP Relay server : "Pawan_Dev.vxcountry.cinemax.com".
SMTP Server : "PNV86SMTP01.PNE.VEN.CINEMAX.COM".

I am using same credentials in both the cases.

Below are the logs shared for both APIs;

Mailkit logs

Connected to smtp://10.217.79.102:25/?starttls=when-available
S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 23 Nov 2020 16:02:54 +0530
C: EHLO [10.217.79.102]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.79.102]
S: 250-AUTH NTLM
S: 250-TURN
S: 250-SIZE 2097152
S: 250-ETRN
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-8bitmime
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-VRFY
S: 250 OK
C: AUTH NTLM TlRMTVNTUAABAAAABwIAAAAAAAAgAAAAAAAAACAAAAA=
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECONBRvRlwC2wAAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACACfXLgGhMHWAQAAAAA=
C: TlRMTVNTUAADAAAAAAAAAG4AAAD2APYAbgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAABkAQAAAQIAAgoAWikAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZAASQHh9Lw9LhW2jR1P7QTfiAQEAAAAAAACsfnUgssHWAQ87rKz/7CSbAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACACfXLgGhMHWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful
C: MAIL FROM:pawan.bhabad@cinemax.com SIZE=367
C: RCPT TO:pawan.bhabad@cinemax.com
S: 530 5.7.3 Client was not authenticated
S: 530 5.7.3 Client was not authenticated
C: QUIT
S: 221 2.0.0 Pawan_Dev.vxcountry.cinemax.com Service closing transmission channel

SMTPClient logs

S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 23 Nov 2020 15:20:40 +0530
C: EHLO W3115570Q81SG
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.217.53] | AUTH NTLM | TURN | SIZE 2097152 | ETRN | PIPELINING | DSN | ENHANCEDSTATUSCODES | 8bitmime | BINARYMIME | CHUNKING | VRFY | OK
C: AUTH ntlm TlRMTVNTUAABAAAAB4IIogAAAAAAAAAAAAAAAAAAAAAKAO5CAAAADw==
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFgomi18THmUUjMM4AAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACABL1oYafsHWAQAAAAA=

C: TlRMTVNTUAADAAAAGAAYAIoAAAB4AXgBogAAAAAAAABYAAAAGAAYAFgAAAAaABoAcAAAAAAAAAAaAgAABYKIogoA7kIAAAAP1t31DYB66IvmtXjlDBp3jXAAYQB3AGEAbgAuAGIAaABhAGIAYQBkAFcAMwAxADEANQA1ADcAMABRADgAMQBTAEcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABo9OFnsse8fOpnPtN+EODgEBAAAAAAAAS9aGGn7B1gENlOJzBTEX/AAAAAACAA4AVgBYAEkATgBEAEkAQQABABIAUABBAFcAQQBOAF8ARABFAFYABAAmAHYAeABpAG4AZABpAGEALgB2AGUAcgBpAHQAYQBzAC4AYwBvAG0AAwA6AFAAYQB3AGEAbgBfAEQAZQB2AC4AdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQAFACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQAHAAgAS9aGGn7B1gEGAAQAAgAAAAgAMAAwAAAAAAAAAAEAAAAAIAAAQb6NkFRsPXqeapbOErEdQ9DC3EFPhyIR29iV0T05QSIKABAAAAAAAAAAAAAAAAAAAAAAAAkAKgBTAE0AVABQAFMAVgBDAC8AMQAwAC4AMgAxADcALgA3ADkALgAxADAAMgAAAAAAAAAAAA==

S: 235 2.7.0 Authentication successful
C: MAIL FROM:pawan.bhabad@cinemax.com
S: 250 2.1.0 pawan.bhabad@cinemax.com....Sender OK
C: RCPT TO:pawan.bhabad@cinemax.com
S: 250 2.1.5 pawan.bhabad@cinemax.com
C: DATA
S: 354 Start mail input; end with .
C: DATA fragment, 235 bytes
C: DATA fragment, 41 bytes
from: pawan.bhabad@cinemax.com, subject: Trial email subject, (text/html)
S: 250 2.6.0 <PAWAN_DEVFRaqbC8wSA00000027@Pawan_Dev.vxcountry.cinemax.com> Queued mail for delivery
S: 220 PNV86SMTP01.PNE.VEN.CINEMAX.COM ESMTP Sendmail 8.14.4/8.14.4; Mon, 23 Nov 2020 15:20:41 +0530
C: EHLO Pawan_Dev.vxcountry.cinemax.com
S: 250-PNV86SMTP01.PNE.VEN.CINEMAX.COM Hello [10.217.79.102], pleased to meet you | ENHANCEDSTATUSCODES | PIPELINING | 8BITMIME | SIZE | DSN | ETRN | AUTH GSSAPI DIGEST-MD5 CRAM-MD5 | DELIVERBY | HELP
C: MAIL FROM:pawan.bhabad@cinemax.com
S: 250 2.1.0 pawan.bhabad@cinemax.com... Sender ok
C: RCPT TO:pawan.bhabad@cinemax.com
S: 250 2.1.5 pawan.bhabad@cinemax.com... Recipient ok
C: DATA
S: 354 Enter mail, end with "." on a line by itself
from: pawan.bhabad@cinemax.com, subject: Trial email subject, (text/html)
S: 250 2.0.0 0AN9ofCe026795 Message accepted for delivery
C: QUIT
S: 221 2.0.0 PNV86SMTP01.PNE.VEN.CINEMAX.COM closing connection
C: QUIT
S: 221 2.0.0 Pawan_Dev.vxcountry.cinemax.com Service closing transmission channel

@jstedfast
Copy link
Owner

@pawanbhabad it looks like it will help, thanks.

@jstedfast jstedfast reopened this Nov 24, 2020
@jstedfast
Copy link
Owner

@pawanbhabad

I sent you an email earlier detailing the differences that I found between MailKit and System.Net.Mail.

I did a bit of reading of the NTLM docs and have spotted 2 potential problems in MailKit's code that I have "fixed", but I am not 100% confident in either of the fixes.

What I need you to do is to test the NuGet package located at https://www.myget.org/feed/mimekit/package/nuget/MailKit/2.10.0.4

Once you've switched to using the above NuGet package, what I want you to do is to test the following cases for me:

  1. According to the docs, that 24-byte array of null bytes is supposed to be sent if the server's challenge includes a TargetInfo with a timestamp. In your case, the server is sending a timestamp and as I mentioned in the email, System.Net.Mail is sending this 24-byte array of null bytes.

To enable it, do the following:

var ntlm = new SaslMechanismNtlm (username, password) {
    NtlmFixes = NtlmFixes.NTLMv2IncludeZ24
};

// The following code enables the NTLMSSP_NEGOTIATE_VERSION debugging info and
// System.Net.Mail seems to include it, so I've made it possible to include as well.
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    ntlm.OSVersion = Environment.OSVersion.Version;
  1. According to the docs, it sounds like the NTLMv2 password hashing/encryption logic is supposed to use the timestamp included in the server's TargetInfo (if it is included; otherwise it should use the current time). Currently, MailKit always uses the current time, ignoring the timestamp sent by the server.

To enable this fix, do the following:

var ntlm = new SaslMechanismNtlm (username, password) {
    NtlmFixes = NtlmFixes.NTLMv2UseTargetInfoTimestamp
};

// The following code enables the NTLMSSP_NEGOTIATE_VERSION debugging info and
// System.Net.Mail seems to include it, so I've made it possible to include as well.
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    ntlm.OSVersion = Environment.OSVersion.Version;
  1. Here's where I want you to test both fixes combined.
var ntlm = new SaslMechanismNtlm (username, password) {
    NtlmFixes = NtlmFixes.NTLMv2IncludeZ24 | NtlmFixes.NTLMv2UseTargetInfoTimestamp
};

// The following code enables the NTLMSSP_NEGOTIATE_VERSION debugging info and
// System.Net.Mail seems to include it, so I've made it possible to include as well.
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    ntlm.OSVersion = Environment.OSVersion.Version;

Let me know if any of the above solutions work.

@jstedfast
Copy link
Owner

Official docs for NTLM can be found here: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/b38c36ed-2804-4868-a9ff-8dd3182128e4

Another useful resource can be found here: http://davenport.sourceforge.net/ntlm.html

In fact, that second resource was the basis for the current NTLM implementation in MailKit (which was borrowed from Mono's NTLM code).

@pawanbhabad
Copy link

Yes sure @jstedfast
Thank you!

@pawanbhabad
Copy link

Hi @jstedfast I have tried all the above three cases but it does not seem to be working.

Following are the test case logs,

Case 1: 24 Byte array buffer case.

S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 30 Nov 2020 10:45:06 +0530
C: EHLO [10.217.216.0]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.216.0] | AUTH NTLM | TURN | SIZE 2097152 | ETRN | PIPELINING | DSN | ENHANCEDSTATUSCODES | 8bitmime | BINARYMIME | CHUNKING | VRFY | OK
C: AUTH NTLM TlRMTVNTUAABAAAABxIAAgkACQAoAAAAAAAAACgAAAAGAvAjAAAAD0NPTU1VTklUWQ==
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECnCuy2+TYfW0AAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACACdpyDE18bWAQAAAAA=
C: TlRMTVNTUAADAAAAGAAYAG4AAAD2APYAhgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAAB8AQAAAQIAAgYC8CMAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2pZmJb2q4+VplLLGJKuEJAQEAAAAAAAD+/vPdBcfWAUI7z8zYkmdHAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACACdpyDE18bWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful
C: MAIL FROM:pawan.bhabad@cinemax.com SIZE=319 | RCPT TO:pawan.bhabad@cinemax.com
S: 530 5.7.3 Client was not authenticated
S: 530 5.7.3 Client was not authenticated
C: QUIT
S: 221 2.0.0 Pawan_Dev.vxcountry.cinemax.com Service closing transmission channel

Case 2: Timestamp included in server's TargetInfo;

S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 30 Nov 2020 10:51:59 +0530
C: EHLO [10.217.216.0]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.216.0] | AUTH NTLM | TURN | SIZE 2097152 | ETRN | PIPELINING | DSN | ENHANCEDSTATUSCODES | 8bitmime | BINARYMIME | CHUNKING | VRFY | OK
C: AUTH NTLM TlRMTVNTUAABAAAABxIAAgkACQAoAAAAAAAAACgAAAAGAvAjAAAAD0NPTU1VTklUWQ==
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECUkHVuQ8pqJEAAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAArFJy62MbWAQAAAAA=
C: TlRMTVNTUAADAAAAAAAAAG4AAAD2APYAbgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAABkAQAAAQIAAgYC8CMAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZAAJ49UrdjmCx7XlF4V3As8ZAQEAAAAAAAArFJy62MbWAVbHJrGGAy8YAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACAArFJy62MbWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful
C: MAIL FROM:pawan.bhabad@cinemax.com SIZE=320 | RCPT TO:pawan.bhabad@cinemax.com
S: 530 5.7.3 Client was not authenticated
S: 530 5.7.3 Client was not authenticated
C: QUIT
S: 221 2.0.0 Pawan_Dev.vxcountry.cinemax.com Service closing transmission channel

Case 3: Both fixes combined;

S: 220 Pawan_Dev.vxcountry.cinemax.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 30 Nov 2020 10:19:18 +0530
C: EHLO [10.217.216.0]
S: 250-Pawan_Dev.vxcountry.cinemax.com Hello [10.217.216.0] | AUTH NTLM | TURN | SIZE 2097152 | ETRN | PIPELINING | DSN | ENHANCEDSTATUSCODES | 8bitmime | BINARYMIME | CHUNKING | VRFY | OK
C: AUTH NTLM TlRMTVNTUAABAAAABxIAAgkACQAoAAAAAAAAACgAAAAGAvAjAAAAD0NPTU1VTklUWQ==
S: 334 TlRMTVNTUAACAAAADgAOADgAAAAFAoECX0mzDfWJGtMAAAAAAAAAAMoAygBGAAAABgOAJQAAAA9WAFgASQBOAEQASQBBAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACADDOM4p1MbWAQAAAAA=
C: TlRMTVNTUAADAAAAGAAYAG4AAAD2APYAhgAAAA4ADgBIAAAAGAAYAFYAAAAAAAAAbgAAAAAAAAB8AQAAAQIAAgYC8CMAAAAPVgBYAEkATgBEAEkAQQBwAGEAdwBhAG4ALgBiAGgAYQBiAGEAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACxl6cmbx9v3GgM0EFxkEfjAQEAAAAAAADDOM4p1MbWAWaJNx3w3+yyAAAAAAIADgBWAFgASQBOAEQASQBBAAEAEgBQAEEAVwBBAE4AXwBEAEUAVgAEACYAdgB4AGkAbgBkAGkAYQAuAHYAZQByAGkAdABhAHMALgBjAG8AbQADADoAUABhAHcAYQBuAF8ARABlAHYALgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAUAJgB2AHgAaQBuAGQAaQBhAC4AdgBlAHIAaQB0AGEAcwAuAGMAbwBtAAcACADDOM4p1MbWAQAAAAA=
S: 535 5.7.3 Authentication unsuccessful
C: MAIL FROM:pawan.bhabad@cinemax.com SIZE=320 | RCPT TO:pawan.bhabad@cinemax.com
S: 530 5.7.3 Client was not authenticated
S: 530 5.7.3 Client was not authenticated
C: QUIT
S: 221 2.0.0 Pawan_Dev.vxcountry.cinemax.com Service closing transmission channel

@ukashanoor
Copy link

Hi @jstedfast ,

I am also facing the same issue pointed by @pawanbhabad. Firstly, when I tried with "smtpClient.Authenticate(smtp.Login, smtp.Password);" to authenticate it gave me the error that " No compatible authentication mechanisms found." and the smtp server is using NTLM as Authentication mechanism.
So then I used this "var ntlm = new SaslMechanismNtlm ("username", "password");" for authentication then it gave me error that " Authentication unsuccessful".
Can u help me with a work around or let me know when this issue will be fixed.
@pawanbhabad did u find any work around for this.

@kundankrishna
Copy link

Hi @jstedfast , I am also facing the same issue on .NetCore 3.1 with Mailkit library. Can you please help with some workaround?

@jstedfast
Copy link
Owner

@ukashanoor @kundankrishna the workaround right now is to use the SaslMechanismNtlmIntegrated class in #332 (comment) instead of MailKit's built-in SaslMechanismNtlm class.

I would replace MailKit's SaslMechanismNtlm with that other class, but unfortunately my goal is to make MailKit cross platform and that class won't work on Linux or Mac.

@kundankrishna
Copy link

@jstedfast Thank you for your response. Since our target runtime is Linux, this workaround is not applicable. Just wondering what could be the right solution for this and when that can be made available to all.

@jstedfast
Copy link
Owner

Unfortunately reverse engineering NTLM is a difficult process :(

@jstedfast jstedfast changed the title NTLM authentication not working NTLM authentication not working in some cases Jul 8, 2021
@jstedfast
Copy link
Owner

@kundankrishna @pawanbhabad @sauravcaptech

For those of you who are having problems with MailKit's NTLM implementation:

  1. Can you check https://docs.microsoft.com/en-us/troubleshoot/windows-server/windows-security/authentication-fails-non-windows-ntlm-kerberos-server - I'd like to know if your server requires ChannelBinding
  2. Does anyone have non-ASCII server/domain names?
  3. Has anyone tried setting the SaslMechanismNtlm.Workstation property to Environment.MachineName ?

@jstedfast
Copy link
Owner

jstedfast commented Aug 15, 2021

Also, can anyone check this key in their registry?

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\LMCompatibilityLevel

@jstedfast
Copy link
Owner

I've completely rewritten the NTLM support in the https://github.com/jstedfast/MailKit/tree/ntlmv2 branch

If any of you guys could test that for me, that would be amazing.

@kundankrishna
Copy link

kundankrishna commented Aug 19, 2021

@jstedfast While we have no such local SMTP server with NTLM support to play around with configurations, but still we can test it using the above branch with few SMTP servers we were having issues already. Will let you know how it goes by early next week.

@jstedfast
Copy link
Owner

That would be awesome, thanks!

@jstedfast
Copy link
Owner

@kundankrishna any update?

@kundankrishna
Copy link

Here is the code with which we tried

private static SmtpClient CreateSMTPClient()
{
SmtpClient smtpClient = new SmtpClient
{
ServerCertificateValidationCallback = (s, c, h, e) => true,
Timeout = 25 * 1000 //milli-seconds
};
if (smtp.EnableSSL)
smtpClient.Connect(smtp.Server, smtp.Port, SecureSocketOptions.StartTls);
else
smtpClient.Connect(smtp.Server, smtp.Port, SecureSocketOptions.None);
//smtpClient.Authenticate(smtp.Login, smtp.Password);
if (smtpClient.AuthenticationMechanisms.Contains("NTLM"))
{
Console.WriteLine("NTLM");
var ntlm = new SaslMechanismNtlm(smtp.Login, smtp.Password);
ntlm.Workstation = Environment.MachineName;
smtpClient.Authenticate(ntlm);
}
else
{
// use the default supported mechanisms
smtpClient.Authenticate(smtp.Login, smtp.Password);
}
return smtpClient;
}

MimeMessage mimeMessage = new MimeMessage();
BodyBuilder bodyBuilder = new BodyBuilder();
mimeMessage.From.Add(new MailboxAddress(smtp.FromName, smtp.FromAddress));
mimeMessage.To.Add(new MailboxAddress("ukashanoor@gmail.com"));
mimeMessage.Subject = "Hi";
bodyBuilder.TextBody = "Bye!";
bodyBuilder.HtmlBody = "Bye!";
mimeMessage.Body = bodyBuilder.ToMessageBody();
using SmtpClient smtpClient = CreateSMTPClient();
smtpClient.Send(mimeMessage);
smtpClient.Disconnect(true);

Complete Exception:
MailKit.Security.AuthenticationException: 535: 5.7.3 Authentication unsuccessful
at MailKit.Net.Smtp.SmtpClient.AuthenticateAsync(SaslMechanism mechanism, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.Authenticate(SaslMechanism mechanism, CancellationToken cancellationToken)
at SMTPTest.Program.CreateSMTPClient() in C:\Users\ukasha\Downloads\WxmIntegration-main\New folder\SMTPTest\SMTPTest\Program.cs:line 124
at SMTPTest.Program.Main(String[] args) in C:\Users\ukasha\Downloads\WxmIntegration-main\New folder\SMTPTest\SMTPTest\Program.cs:line 91

Error message:
Authentication unsuccessful

@jstedfast
Copy link
Owner

Any chance you could provide the protocol logs?

@jstedfast
Copy link
Owner

This should work now.

@ccnani
Copy link

ccnani commented Sep 21, 2021

Hi @jstedfast ,

This is Nani from CISCO team. The above provided inputs and code haven't resolved NTML issue for Linux. @kundankrishna has already shared code snippet which we are used.

Could you please assist on this, as it is a critical issue and impacting the deliverables.

@jstedfast
Copy link
Owner

Which build is confirmed not to work? Have you guys tried the very latest CI builds? Can you get me an account on any of these mail servers so that I can test my code against them?

@jstedfast
Copy link
Owner

@ccnani I've made more fixes since your comment. Any updates as to whether those fixes have helped?

@shav
Copy link

shav commented Dec 21, 2021

Hi @jstedfast !
I'm also moving from System.Net.SmtpClient to MailKit, and as other developers I faced the similar problem.
I tried the code that had been recommended above for NTLM-authentication:

var ntlm = new SaslMechanismNtlm(login, password, domain);
smtpClient.Authenticate(ntlm);

  • When I send mail messages from a client machine that is located at the same local network where smtp-server is located, then I get well known error:
    MailKit.Security.AuthenticationException: 535: 5.7.3 Authentication unsuccessful
  • But if the client machine that I send mail messages from is located in the internet, I get other error:
    MailKit.Security.AuthenticationException: 504: 5.7.4 Unrecognized authentication type
    although client.AuthenticationMechanisms contains "NTLM".

Now I'm using version 2.13.0 of MailKit. I tried to use the version 3.0.0 but I got the same problems.

Debugging the MailKit code I found out that the reason of problem "5.7.3 Authentication unsuccessful" in my case
is that SaslMechanismNtlm in the challenge phase of NTLM-authentication uses domain that smtp-server responded in the negotiate phase of NTLM-authentication:

public Type3Message (Type2Message type2, ...)
{
      ...
     // type2 is the message that smtp-server responded in negotiate phase of authentication
     Domain = type2.TargetName;
     ...
}

I tried to rollback MailKit to previous versions and found out that version 2.0.7 is the last version working in my case.
Because version 2.0.7 is the last version that uses domain provided in client credentials for the challenge phase of NTLM-authentication:

static MessageBase GetChallengeResponse (string userName, string password, string domain, byte[] token, int startIndex, int length)
{
	var type2 = new Type2Message (token, startIndex, length);
	var type3 = new Type3Message (type2, userName, string.Empty);
	type3.Password = password;

	if (!string.IsNullOrEmpty (domain))
		type3.Domain = domain;

	return type3;
}

In my case the domain from client credentials is different from the domain that smtp-server responded
(is that normal case or smtp-server has wrong settings when the smtp-server cannot authenticate by the domain that it sent to the client?).

I tried to patch the version 2.13.0 of MailKit with this code and, lo and behold, that worked for me!
But unfortunately this patch didn't work for the version 3.0.0 where NTLM-authentication was completely rewrote (or I'm doing something wrong?).

Dear @jstedfast,

  • How can I apply domain provided in the client credentials for SaslMechanismNtlm in the new version 3.0.0 of MailKit?
  • Is there any chance that this fix will be added to standard version of MailKit in the future?
    (for example, setting of SaslMechanismNtlm that indicates which domain should be used for authentication - from client or from smtp-server)
    Or should I fix this problem in my local sourcecode / fork of MailKit?
  • Do you have any idea what can be reason of the other error "5.7.4 Unrecognized authentication type" (when I send mail messages from the internet)?
    Is it problem of MailKit or the smtp-server might have wrong settings?

@jstedfast
Copy link
Owner

Hi @shav,

  • It looks like there isn't a way to do that, but if this is what is causing NTLM to fail for you, then it seems like I should change the code to use the provided domain (at least in some cases). The question is when should it use the domain in the NetworkCredentials vs when should it use the TargetDomain provided by the server? For that, I may need your help in stepping thru the code to narrow this down.
  • Yes, if you can help me figure out when it is appropriate to use, then I would prefer to have that fix in the mainline version of MailKit.
  • "5.7.4 Unrecognized authentication type" is an error code from the SMTP server. I would need to see the protocol logs to have a better idea of what is going on. If you say that NTLM is listed in the AuthenticationMechanisms, then it sounds like there is a bug in the server. It's possible, however, that you just need to upgrade to an SSL/TLS connection before using NTLM (technically this is still a bug in the server, since it should not advertise NTLM until the client has upgraded to the SSL/TLS connection).

I'm looking at the NTLM logic right now and my first question to you (since you can debug this), is what code-path does it take in https://github.com/jstedfast/MailKit/blob/master/MailKit/Security/Ntlm/NtlmAuthenticateMessage.cs#L59 in your case?

The code branch I'm looking at is this:

if ((challenge.Flags & NtlmFlags.TargetTypeDomain) != 0) {
    // The server is domain-joined, so the TargetName will be the domain.
    Domain = challenge.TargetName;
} else {
    // The server is not domain-joined, so the TargetName will be the machine name of the server.
    Domain = challenge.TargetInfo?.DomainName;

    // TODO: throw if TargetInfo is null?
}

I think the approach that I will take is to pass the domain variable calculated in SaslMechanismNtlm.Challenge() to the GetChallengeResponse() method and from there to the NtlmAuthenticateMessage constructor.

What we may find eventually is that we should always use the supplied domain name, if non-empty, but I would prefer to start off with narrowly fixing this in your particular case so that we don't we don't break this for anyone else if I'm wrong in my assumption.

So to sum up what I want to know is does challenge.Flags contain the NtlmFlags.TargetTypeDomain flag? Or not?

If you can get me that info for your case, I'll make the necessary changes (I'm already working on them locally) and I'll make the needed adjustment and push a fix.

BTW, thanks for doing the research/debugging that you've already done, it is very exciting to feel like we've almost got NTLM working correctly in MailKit. It's been a long journey!

@jstedfast
Copy link
Owner

I've committed what I think may be the correct fix and a new build will be uploaded to https://www.myget.org/feed/mimekit/package/nuget/MailKit in the next hour (or less).

That said, I'd still be interested in knowing the answer to my question in my last comment just in case my fix breaks things for someone else (I made it always prefer the supplied domain name if non-empty).

@shav
Copy link

shav commented Dec 22, 2021

Hi, @jstedfast !
I've tried your recent fix where you authenticate to the smtp-server using domain supplied in client credentials. And I'm happy to report you that it works in one of my cases when smtp-client is located on windows machine in local network where smtp-server is located. Thanks, I think that's a good progress in my case!

But unfortunately this fix doesn't work in case when the smtp-client is on a linux machine (I tested it on Ubuntu 20.04). In this case smtp-server still responds "535: 5.7.3 Authentication unsuccessful". To say the truth, I have some troubles with server-side certificate (smtp-client thinks that certificate is invalid) and I have to connect to the smtp-server without SSL/TLS (hack ServerCertificateValidationCallback = (s,c,h,e) => true doesn't help me). But regardless that fact, your fix with local credentials domain applied to version 2.13.0 of MailKIt and original version 2.0.7 without fixes works successfully on the same my client-side code (include client credentials), the same linux machine and the same smtp-server.

I have also debugged https://github.com/jstedfast/MailKit/blob/master/MailKit/Security/Ntlm/NtlmAuthenticateMessage.cs#L59 as you requested and found out that in my case code goes by path

if ((challenge.Flags & NtlmFlags.TargetTypeDomain) != 0) {
    // The server is domain-joined, so the TargetName will be the domain.
    Domain = challenge.TargetName;
} 

In my case challenge.Flags has value Negotiate56 | NegotiateUnicode | RequestTarget | NegotiateNtlm | NegotiateAlwaysSign | TargetTypeDomain | NegotiateExtendedSessionSecurity | NegotiateTargetInfo | NegotiateVersion | Negotiate128.
But in fact challenge.TargetName and challenge.TargetInfo?.DomainName are equal.

@jstedfast
Copy link
Owner

@shav can you submit a new bug report for the case that is still failing? I'd prefer to have separate it from all the noise in this bug report.

@shav
Copy link

shav commented Jan 2, 2022

@jstedfast I'm sorry, that was wrong alarm about not working SmtpClient on Windows from internet. I wrote about my troubles to the administrator of my smtp-server and he informed me that a connection to smtp-server from internet was denied in server settings. He allowed connection to the smtp-server from my client machine via internet, and my SmtpClient established the connection successfully.
So, now I have troubles only connecting to the smtp-server from Linux client machine via NTLM-authentication using 3.0.0. version of MailKit (with your fix at version 3.0.0.271). At the same environment NTLM-authentication using 2.17.0 version of MailKit or other kinds of authentication (I tested LOGIN-authentication) using 3.0.0. version of MailKit works successfully.

@jstedfast
Copy link
Owner

@shav great news! Thanks for the update!

Elanis added a commit to Dysnomia-Studio/dysnomia-website that referenced this issue Jul 14, 2023
Bumps [MailKit](https://github.com/jstedfast/MailKit) from 3.0.0 to 3.1.1.
<details>
<summary>Changelog</summary>

*Sourced from [MailKit's changelog](https://github.com/jstedfast/MailKit/blob/master/ReleaseNotes.md).*

> ### MailKit 3.1.1 (2022-01-30)
>
> * Reduced string allocations in Pop3Engine's capability parser.
> * Updated GMail and Outlook.com SSL certificates.
> * Modified SmtpClient to try and use the system hostname in EHLO/HELO commands.
>   (issue [#1314](jstedfast/MailKit#1314))
>
> ### MailKit 3.1.0 (2022-01-14)
>
> * Fixed NTLM to always prefer the supplied domain over the TargetName or TargetInfo.DomainName.
>   (issue [#582](jstedfast/MailKit#582))
> * Updated GMail and Outlook.com SSL certificate info.
> * Added a new SslCipherSuite property to each client that allows developers to get information
>   about the SSL/TLS cipher suite that was negotiated with the server.
>   (pull [#1312](jstedfast/MailKit#1312))
> * Reduced string allocations in SmtpClient's EHLO capability parsing logic.
> * Default ProtocolLogger.RedactSecrets to true for added added security.
> * Added work-around for parsing malformed GMail ENVELOPE responses that reverse the name and address components
>   of the Sender address. (pull [#1319](jstedfast/MailKit#1319))
> * Added net6.0 to the list of TargetFrameworks.
</details>
<details>
<summary>Commits</summary>

- [`7f3affd`](jstedfast/MailKit@7f3affd) Bumped version to 3.1.1
- [`f6f4a4e`](jstedfast/MailKit@f6f4a4e) Updated README.md
- [`839ec61`](jstedfast/MailKit@839ec61) Updated README.md
- [`8c50d12`](jstedfast/MailKit@8c50d12) Updated GettingStarted.md
- [`a7db8cf`](jstedfast/MailKit@a7db8cf) Bump NUnit3TestAdapter from 4.2.0 to 4.2.1 ([#1323](jstedfast/MailKit#1323))
- [`a5637f9`](jstedfast/MailKit@a5637f9) minor update to previous unit test
- [`163fa9b`](jstedfast/MailKit@163fa9b) Added unit tests for SmtpClient.OnNoRecipientsAccepted()
- [`dfafce8`](jstedfast/MailKit@dfafce8) Added more SmtpClient unit tests
- [`5c5e11d`](jstedfast/MailKit@5c5e11d) Reduce string allocations in Pop3Engine's capability parser
- [`a818ada`](jstedfast/MailKit@a818ada) Updated GMail SSL certificates
- Additional commits viewable in [compare view](jstedfast/MailKit@3.0.0...3.1.1)
</details>

<br />

Co-authored-by: Elanis <elanis@hotmail.com>
Reviewed-on: https://gitea.dysnomia.studio/elanis/dysnomia-website/pulls/23
Co-authored-by: elanis <elanis@noreply.example.org>
Co-committed-by: elanis <elanis@noreply.example.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question about how to do something
Projects
None yet
Development

No branches or pull requests

10 participants