pypsexec
pypsexec copied to clipboard
Attempting to Reconnect to Host Fails with
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.