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

Attempting to Reconnect to Host Fails with #54

Open
aesrentai opened this issue Jul 27, 2022 · 0 comments
Open

Attempting to Reconnect to Host Fails with #54

aesrentai opened this issue Jul 27, 2022 · 0 comments

Comments

@aesrentai
Copy link

aesrentai commented Jul 27, 2022

Connecting to a host, disconnecting from said host, then reconnecting fails with the below error:

Traceback (most recent call last):
  File "/root/windows/reboot_test.py", line 31, in <module>
    c.connect()
  File "/usr/local/lib/python3.10/dist-packages/pypsexec/client.py", line 103, in connect
    self.connection.connect(timeout=timeout)
  File "/usr/local/lib/python3.10/dist-packages/smbprotocol/connection.py", line 799, in connect
    smb_response = self._send_smb2_negotiate(dialect, timeout, enc_algos, sign_algos)
  File "/usr/local/lib/python3.10/dist-packages/smbprotocol/connection.py", line 1497, in _send_smb2_negotiate
    response = self.receive(request, timeout=timeout)
  File "/usr/local/lib/python3.10/dist-packages/smbprotocol/connection.py", line 931, in receive
    self._check_worker_running()  # The worker may have failed while waiting for the response, check again
  File "/usr/local/lib/python3.10/dist-packages/smbprotocol/connection.py", line 1082, in _check_worker_running
    raise SMBConnectionClosed('SMB socket was closed, cannot send or receive any more data')
smbprotocol.exceptions.SMBConnectionClosed: SMB socket was closed, cannot send or receive any more data

Here is a trivial script to reproduce this error:

c = Client("IPHERE", username="Administrator", password="PASSWORDHERE")
c.connect()
try:
    # these three functions succeed
    c.create_service()
    c.remove_service()
    c.disconnect()

    # not necessary for reproduction
    time.sleep(1)
    
    # this fails with the above error
    c.connect()

    # this code is never executed
    c.create_service()

This appears to be a bug since if I reinitialize the client object after the first disconnect call, everything works fine:

c = Client("IPHERE", username="Administrator", password="PASSWORDHERE")
c.connect()
try:
    # these three functions succeed
    c.create_service()
    c.remove_service()
    c.disconnect()

    # not necessary for reproduction
    time.sleep(1)

    # added this line, which makes everything work
    c = Client("IPHERE", username="Administrator", password="PASSWORDHERE")
    
    # this now succeeds
    c.connect()

    c.create_service()

Host OS: Ubuntu 22.04 LTS, installed via pip as specified in the README
Target OS: I've tested this on Windows Server 2008 R2, Windows 7, Windows 10, Windows Server 2016, and Windows 11 and it occurred on all of them.

Based off the error message and the code, it appears that the worker is failing while waiting for a response form the client. I haven't had time to more extensively debug this, but destroying the thread on disconnect and then recreating the thread on connect may work to solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant