83
83
from six import PY2 , StringIO , BytesIO , b , assertRaisesRegex , assertCountEqual
84
84
85
85
from impacket .smb import SMB_DIALECT
86
- from impacket .smbserver import normalize_path , isInFileJail , SimpleSMBServer
86
+ from impacket .smbserver import normalize_path , isInFileJail , SimpleSMBServer , SMBSERVER
87
87
from impacket .smbconnection import SMBConnection , SessionError , compute_lmhash , compute_nthash
88
+ from threading import Thread
88
89
90
+ import select
91
+ import socket
92
+
93
+ class StoppableMixin ():
94
+ def serve_forever (self ):
95
+ self .must_serve = True
96
+ self .timeout = 0.1
97
+
98
+ while self .must_serve :
99
+ self .handle_request ()
100
+
101
+ def close_request (self ,request ):
102
+ if self .must_serve :
103
+ request .close ()
104
+
105
+ def get_request (self ):
106
+ timeout = 0.1
107
+ while self .must_serve :
108
+ _read ,_ ,_ = select .select ([self .socket ],[],[],timeout )
109
+
110
+ if _read and self .must_serve :
111
+ return self .socket .accept ()
112
+ raise socket .error
113
+
114
+ class SMBSERVERForTests (StoppableMixin ,SMBSERVER ):
115
+ pass
89
116
90
117
class SMBServerUnitTests (unittest .TestCase ):
91
118
"""Unit tests for the SMBServer
@@ -214,7 +241,10 @@ def tearDown(self):
214
241
self .stop_smbserver ()
215
242
216
243
def get_smbserver (self , add_credential = True , add_share = True ):
217
- smbserver = SimpleSMBServer (listenAddress = self .address , listenPort = int (self .port ))
244
+ #smbserver = SimpleSMBServerForTests(listenAddress=self.address, listenPort=int(self.port))
245
+ # smbserver should be run in a host thread and also be able to be terminated in order to run several times
246
+ # different configurations.
247
+ smbserver = SimpleSMBServer (listenAddress = self .address , listenPort = int (self .port ),smbserverclass = SMBSERVERForTests )
218
248
if add_credential :
219
249
smbserver .addCredential (self .username , 0 , self .lmhash , self .nthash )
220
250
if add_share :
@@ -232,18 +262,25 @@ def start_smbserver(self, server):
232
262
"""Starts the SimpleSMBServer process.
233
263
"""
234
264
self .server = server
235
- self .server_process = Process (target = server .start )
265
+ #self.server_process = Process(target=server.start)
266
+ # avoid using Process beacuse of bug in python3.13 https://github.com/python/cpython/issues/134381
267
+ # TODO: remove these changes once a bugfix gets backported.
268
+ self .server_process = Thread (target = server .start )
269
+ self .server_process .daemon = True
236
270
self .server_process .start ()
237
271
238
272
def stop_smbserver (self ):
239
273
"""Stops the SimpleSMBServer process and wait for insider threads to join.
240
274
"""
241
275
if self .server :
242
276
self .server .stop ()
277
+ self .server .getServer ().must_serve = False
243
278
self .server = None
244
279
if self .server_process :
245
- self .server_process .terminate ()
280
+ #self.server_process.terminate()
281
+ #self.server_process._stop()
246
282
sleep (0.1 )
283
+ self .server_process .join ()
247
284
self .server_process = None
248
285
249
286
def test_smbserver_login_valid (self ):
0 commit comments