jborean93 / smbprotocol

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

smbclient produces 'STATUS_ACCESS_DENIED' error on windows 2016 server #206

Closed johnmantios closed 12 months ago

johnmantios commented 1 year ago

I want to write some files to a remote windows file system. When I do it from my local workstation (windows 10) the code is executed correctly without any errors. However, when I try to do the same thing from a windows 2016 server I get a 'STATUS_ACCESS_DENIED' error for that share. For what it's worth, I can map that specific share to the server so I have excluded the possibility of this being a networking issue. I am wondering whether this is a specific smbprotocol version error and if yes how can I change the used protocol version since I use the high-level smbclient interface. If this is not version related, are there any ideas about what might be wrong?

smbprotocol==1.6.2

The code I use for the file writing transaction:

class SMBClient:
    def __init__(self, host: str) -> None:
        self.host = host
        self.base_path = fr"\\{host}"

        self._create_session()

    def _create_session(self) -> None:
        cred_args = {
            "username": os.environ.get("nsa_user"),
            "password": os.environ.get("nsa_pass"),
        }

        smbclient.register_session(self.host, **cred_args)

        del cred_args

    @staticmethod
    def close() -> None:
        smbclient.reset_connection_cache()

    def write_file(
        self,
        path: str,
        file_buff: _io.BufferedReader,
        fmode: str = "ab",
        timestamp: bool = False,
    ) -> None:

        # append timestamp to filename if requested
        if timestamp:
            path_obj = Path(path)
            current_ts = datetime.utcnow().strftime("%Y%m%d")

            new_file = f"{path_obj.stem}_{current_ts}{path_obj.suffix}"
            path = os.path.join(str(path_obj.parent), new_file)

        full_path = os.path.join(self.host, path)
        with smbclient.open_file(full_path, mode=fmode) as fd:
            fd.write(file_buff.read())

smb_client = SMBClient(os.environ.get("ingestion_host"))
smb_client.write_file(
f"{os.environ[config[destination][filename]]}\\{instance_name}{suffix}",
buffer,
"ab",
 False,
 )
smb_client.close()
jborean93 commented 1 year ago

I would recommend you install the latest version. But here are some things I recommend you try:

You can always try to hardcode the credentials to rule out any environment differences in your tests.

jborean93 commented 12 months ago

Closing as there was no response to the last message.