jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
316 stars 73 forks source link

Failed to authenticate with NTLM #192

Closed franky920920 closed 1 year ago

franky920920 commented 2 years ago

Hi,

I've encountered this error with following code:

    try:
        smbclient.register_session(server=host, username=username, password=password)
    except:
        print("Error establishing session")
Error establishing session
[ERROR] SMBAuthenticationError: Failed to authenticate with server: SpnegoError (1): SpnegoError (16): Operation not supported or available, Context: Retrieving NTLM store without NTLM_USER_FILE set to a filepath, Context: Unable to negotiate common mechanism
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 42, in lambda_handler
    for filename in listdir(r"\\" + host + r"\\" + destShare):
  File "/opt/python/smbclient/_os.py", line 239, in listdir
    with SMBDirectoryIO(path, mode='r', share_access='r', **kwargs) as dir_fd:
  File "/opt/python/smbclient/_io.py", line 377, in __init__
    tree, fd_path = get_smb_tree(path, **kwargs)
  File "/opt/python/smbclient/_pool.py", line 312, in get_smb_tree
    auth_protocol=auth_protocol)
  File "/opt/python/smbclient/_pool.py", line 383, in register_session
    session.connect()
  File "/opt/python/smbprotocol/session.py", line 280, in connect
    raise SMBAuthenticationError("Failed to authenticate with server: %s" % str(err.message))

SMB server is on AWS FSx for Windows Server and trying to authenticate with Windows AD credentials. Thanks. :)

adiroiban commented 2 years ago

This looks similar to #109

Just to double check... do you have a real and valid user and password there? :)

franky920920 commented 2 years ago

Yes, confirmed username and password is working. 😄

franky920920 commented 2 years ago

Wait... I've just change my code to

from smbclient import listdir, mkdir, register_session, rmdir, scandir
try:
    # smbclient.ClientConfig(username=username, password=password, auth_protocol='ntlm')
    smbclient.register_session(server=host, username=username, password=password)
except:
    print("Error establishing session")
    traceback.print_exc()

And the traceback shows

Traceback (most recent call last):
File "/var/task/lambda_function.py", line 41, in lambda_handler
smbclient.register_session(server=host, username=username, password=password)
NameError: name 'smbclient' is not defined # <- This looks wired
[ERROR] SMBAuthenticationError: Failed to authenticate with server: SpnegoError (1): SpnegoError (16): Operation not supported or available, Context: Retrieving NTLM store without NTLM_USER_FILE set to a filepath, Context: Unable to negotiate common mechanism
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 46, in lambda_handler
    for filename in listdir(r"\\" + host + r"\\" + destShare):
  File "/opt/python/smbclient/_os.py", line 239, in listdir
    with SMBDirectoryIO(path, mode='r', share_access='r', **kwargs) as dir_fd:
  File "/opt/python/smbclient/_io.py", line 377, in __init__
    tree, fd_path = get_smb_tree(path, **kwargs)
  File "/opt/python/smbclient/_pool.py", line 312, in get_smb_tree
    auth_protocol=auth_protocol)
  File "/opt/python/smbclient/_pool.py", line 383, in register_session
    session.connect()
  File "/opt/python/smbprotocol/session.py", line 280, in connect
    raise SMBAuthenticationError("Failed to authenticate with server: %s" % str(err.message))

So I change my code to

from smbclient import listdir, mkdir, register_session, rmdir, scandir
import smbclient # <- add this
try:
    # smbclient.ClientConfig(username=username, password=password, auth_protocol='ntlm')
    smbclient.register_session(server=host, username=username, password=password)
except:
    print("Error establishing session")
    traceback.print_exc()

And... It works :) My fault 😅

adiroiban commented 2 years ago

I am not sure how this works at all.

You only import register_session and not smbclient.register_session

from smbclient import listdir, mkdir, register_session, rmdir, scandir
try:
    # smbclient.ClientConfig(username=username, password=password, auth_protocol='ntlm')
    smbclient.register_session(server=host, username=username, password=password)
except:
    print("Error establishing session")
    traceback.print_exc()

A self contained example would help to understand what is going in there - http://sscce.org/

seanslma commented 11 months ago

We use smbprotocol=1.9.0 and connect to Azure File Storage via fsspec.AbstractFileSystem. Sometimes this error will pop up. Initially the username was picked up but later became None. The problem will heal by itself after a few hours. After restarting the pod in AKS the problem will disappear as well.

Part of the log:

2023-09-11 15:15:10,488 | INFO | smbprotocol.connection | __init__ | 741 | Initialising connection, guid: f2b8b354-777e-4704-803e-123456789, require_signing: True, server_name: xxxxxx.file.core.windows.net, port: 445
2023-09-11 15:15:10,489 | INFO | smbprotocol.connection | connect | 859 | Setting up transport connection
2023-09-11 15:15:10,489 | INFO | smbprotocol.transport | connect | 66 | Connecting to DirectTcp socket
2023-09-11 15:15:10,520 | INFO | smbprotocol.connection | connect | 868 | Starting negotiation with SMB server
2023-09-11 15:15:10,521 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1539 | Negotiating with SMB2 protocol with highest client dialect of: SMB_3_1_1
2023-09-11 15:15:10,523 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1602 | Sending SMB2 Negotiate message
2023-09-11 15:15:10,526 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1607 | Receiving SMB2 Negotiate response
2023-09-11 15:15:10,527 | INFO | smbprotocol.connection | connect | 881 | Negotiated dialect: (785) SMB_3_1_1
2023-09-11 15:15:10,527 | INFO | smbprotocol.connection | connect | 893 | Connection require signing: True
2023-09-11 15:15:10,527 | INFO | smbprotocol.session | __init__ | 219 | Initialising session with username: Azure\xxxxxxx
2023-09-11 15:15:10,530 | INFO | smbprotocol.session | connect | 298 | Sending SMB2_SESSION_SETUP request message
2023-09-11 15:15:10,531 | INFO | smbprotocol.session | connect | 301 | Receiving SMB2_SESSION_SETUP response message
2023-09-11 15:15:10,533 | INFO | smbprotocol.session | connect | 324 | More processing is required for SMB2_SESSION_SETUP
2023-09-11 15:15:10,534 | INFO | smbprotocol.session | connect | 298 | Sending SMB2_SESSION_SETUP request message
2023-09-11 15:15:10,535 | INFO | smbprotocol.session | connect | 301 | Receiving SMB2_SESSION_SETUP response message
2023-09-11 15:15:10,540 | INFO | smbprotocol.session | connect | 328 | Setting session id to 144678531689873509

...

2023-09-12 12:00:51,775 | INFO | smbprotocol.connection | __init__ | 741 | Initialising connection, guid: f2b8b354-777e-4704-803e-123456789, require_signing: True, server_name: xxxxxx.file.core.windows.net, port: 445
2023-09-12 12:00:51,775 | INFO | smbprotocol.connection | connect | 859 | Setting up transport connection
2023-09-12 12:00:51,775 | INFO | smbprotocol.transport | connect | 66 | Connecting to DirectTcp socket
2023-09-12 12:00:51,807 | INFO | smbprotocol.connection | connect | 868 | Starting negotiation with SMB server
2023-09-12 12:00:51,808 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1539 | Negotiating with SMB2 protocol with highest client dialect of: SMB_3_1_1
2023-09-12 12:00:51,809 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1602 | Sending SMB2 Negotiate message
2023-09-12 12:00:51,811 | INFO | smbprotocol.connection | _send_smb2_negotiate | 1607 | Receiving SMB2 Negotiate response
2023-09-12 12:00:51,812 | INFO | smbprotocol.connection | connect | 881 | Negotiated dialect: (785) SMB_3_1_1
2023-09-12 12:00:51,812 | INFO | smbprotocol.connection | connect | 893 | Connection require signing: True
2023-09-12 12:00:51,812 | INFO | smbprotocol.session | __init__ | 219 | Initialising session with username: None
2023-09-12 12:00:51,813 | INFO | smbprotocol.session | __init__ | 219 | Initialising session with username: None
2023-09-12 12:00:51,814 | INFO | smbprotocol.session | __init__ | 219 | Initialising session with username: None