jborean93 / smbprotocol

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

KeyError: 1 when trying to connect to share hosted by ksmbd #215

Closed erlingen closed 1 year ago

erlingen commented 1 year ago

Hi,

I'm running into issues when using v1.1.0 or later of smbprotocol towards a SMB server that is using ksmbd. The issue seems to be that ksmbd is using session id "1" for the first session which will cause conflicts in the preauth_session_table of the Connection class since items are inserted both by session id (1) and message id (1 for the first session setup request).

I'm using the file-management.py example and get the follwing error:

Traceback (most recent call last): File "/home/erik/smbprotocol/examples/low-level/file-management.py", line 47, in session.connect()
File "/home/erik/.local/lib/python3.9/site-packages/smbprotocol/session.py", line 299, in connect
request = self.connection.send(session_setup, sid=self.session_id, credit_request=64) File "/home/erik/.local/lib/python3.9/site-packages/smbprotocol/connection.py", line 965, in send return self._send( File "/home/erik/.local/lib/python3.9/site-packages/smbprotocol/connection.py", line 1281, in _send self.preauth_session_table[session_id].preauth_integrity_hash_value.append(b_header) KeyError: 1

jborean93 commented 1 year ago

Do you have an easy way to setup a server that runs ksmbd, I'm having trouble seeing examples online to try and reproduce this. I'll continue trying but if you can help with that it would be great.

erlingen commented 1 year ago

I'll try to find a way to build a kernel that could be run with qemu for testing this.

jborean93 commented 1 year ago

How are you currently running it?

adiroiban commented 1 year ago

I have a standard Ubuntu 22.04 and I can see the ksmbd is available and there is ksmbd-tools - cifsd kernel server userspace utilities

$ sudo modprobe ksmb

so I guess that this is a question of providing guidance for generic configuration and steps to reproduce it.

jborean93 commented 1 year ago

Yea I'm happy to run a VM of Ubuntu 22.04, it's just the configuration/setting up that is a bit unclear for me. I mostly only tried Fedora/Rocky Linux earlier and found it hard to get ksmbd-tools running.

erlingen commented 1 year ago

Hi again, sorry for the late reply. We use ksmbd to host smb shares on a NAS product at work.

I managed to reproduce it using ubuntu 22.04 in virtualbox as well. Here are some instructions for setting up the share using ksmbd-tools (I used the version 3.4.4-1 available to install with apt):

// Add local user sudo adduser --no-create-home smbuser

// Add the following to configure share in /etc/ksmbd/smb.conf [share] path = /share_test/ read only = no valid user = smbuser guest ok = no

// Create share folder and let smbuser own it sudo mkdir /share_test/ sudo chown -R smbuser /share_test/

// Add user to ksmbd user database sudo ksmbd.adduser -a smbuser

// Load kernel module sudo modprobe ksmbd

// Start ksmbd sudo ksmbd.mountd

I tried both with the ksmbd module available in ubuntu and by building the latest version from github.I had to disable encryption when the Session is created in the file-management.py example by adding False as the fourth argument to Session().

I can get past the KeyError failure by changing the code to use a separate list to insert by message_id here and then look up the value in the new list here

These changes fixes my problems when I'm using it with our NAS but I run into more problems in Ubuntu.

jborean93 commented 1 year ago

Thank you very much for sharing that info as well as sharing a way to fix it. The PR https://github.com/jborean93/smbprotocol/pull/224 works for me locally with a ksmbd server. If you have time it wuold be great if you could test it out and confirm it works for your NAS.

erlingen commented 1 year ago

Tested PR 224 and it works towards our NAS as well! Thanks!