2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-09-12 06:52:50 +00:00
|
|
|
import threading
|
|
|
|
import logging
|
2017-05-03 00:52:16 +00:00
|
|
|
from sys import exit
|
2016-09-12 06:52:50 +00:00
|
|
|
from impacket import smbserver
|
|
|
|
|
|
|
|
class CMESMBServer(threading.Thread):
|
|
|
|
|
2017-03-27 21:09:36 +00:00
|
|
|
def __init__(self, logger, share_name, share_path='/tmp/cme_hosted', listen_address='0.0.0.0', listen_port=445, verbose=False):
|
2016-09-12 06:52:50 +00:00
|
|
|
try:
|
|
|
|
threading.Thread.__init__(self)
|
2017-03-27 21:09:36 +00:00
|
|
|
self.server = smbserver.SimpleSMBServer(listen_address, listen_port)
|
|
|
|
self.server.addShare(share_name.upper(), share_path)
|
2016-09-12 06:52:50 +00:00
|
|
|
if verbose: self.server.setLogFile('')
|
2019-11-10 21:42:04 +00:00
|
|
|
self.server.setSMB2Support(True)
|
2016-09-12 06:52:50 +00:00
|
|
|
self.server.setSMBChallenge('')
|
|
|
|
except Exception as e:
|
|
|
|
errno, message = e.args
|
|
|
|
if errno == 98 and message == 'Address already in use':
|
2017-03-27 21:09:36 +00:00
|
|
|
logger.error('Error starting SMB server on port 445: the port is already in use')
|
2016-09-12 06:52:50 +00:00
|
|
|
else:
|
2017-03-27 21:09:36 +00:00
|
|
|
logger.error('Error starting SMB server on port 445: {}'.format(message))
|
2017-05-03 00:52:16 +00:00
|
|
|
exit(1)
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2017-03-27 21:09:36 +00:00
|
|
|
def addShare(self, share_name, share_path):
|
|
|
|
self.server.addShare(share_name, share_path)
|
2016-09-12 06:52:50 +00:00
|
|
|
|
|
|
|
def run(self):
|
2016-12-15 07:28:00 +00:00
|
|
|
try:
|
2016-09-12 06:52:50 +00:00
|
|
|
self.server.start()
|
2016-12-15 07:28:00 +00:00
|
|
|
except:
|
2016-09-12 06:52:50 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def shutdown(self):
|
2020-04-27 20:37:01 +00:00
|
|
|
# TODO: should fine the proper way
|
2016-09-12 06:52:50 +00:00
|
|
|
# make sure all the threads are killed
|
|
|
|
for thread in threading.enumerate():
|
2021-01-29 23:15:03 +00:00
|
|
|
if thread.is_alive():
|
2016-09-12 06:52:50 +00:00
|
|
|
try:
|
2020-04-27 20:37:01 +00:00
|
|
|
self._stop()
|
2016-09-12 06:52:50 +00:00
|
|
|
except:
|
|
|
|
pass
|