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
|
2023-05-03 20:31:54 +00:00
|
|
|
from threading import enumerate
|
2017-05-03 00:52:16 +00:00
|
|
|
from sys import exit
|
2016-09-12 06:52:50 +00:00
|
|
|
from impacket import smbserver
|
2023-09-22 14:51:38 +00:00
|
|
|
from nxc.logger import nxc_logger
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2023-03-30 20:36:58 +00:00
|
|
|
|
2023-09-15 18:46:48 +00:00
|
|
|
class NXCSMBServer(threading.Thread):
|
2023-05-02 15:17:59 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
logger,
|
|
|
|
share_name,
|
2023-09-14 21:07:15 +00:00
|
|
|
share_path="/tmp/nxc_hosted",
|
2023-05-02 15:17:59 +00:00
|
|
|
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)
|
2023-05-02 15:17:59 +00:00
|
|
|
if verbose:
|
|
|
|
self.server.setLogFile("")
|
2019-11-10 21:42:04 +00:00
|
|
|
self.server.setSMB2Support(True)
|
2023-05-02 15:17:59 +00:00
|
|
|
self.server.setSMBChallenge("")
|
2016-09-12 06:52:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
errno, message = e.args
|
2023-05-02 15:17:59 +00:00
|
|
|
if errno == 98 and message == "Address already in use":
|
2023-09-20 16:02:37 +00:00
|
|
|
nxc_logger.error("Error starting SMB server on port 445: the port is already in use")
|
2016-09-12 06:52:50 +00:00
|
|
|
else:
|
2023-09-20 16:02:37 +00:00
|
|
|
nxc_logger.error(f"Error starting SMB server on port 445: {message}")
|
2017-05-03 00:52:16 +00:00
|
|
|
exit(1)
|
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()
|
2023-09-20 16:02:37 +00:00
|
|
|
except Exception as e:
|
|
|
|
nxc_logger.debug(f"Error starting SMB server: {e}")
|
2016-09-12 06:52:50 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def shutdown(self):
|
2023-05-02 15:17:59 +00:00
|
|
|
# TODO: should fine the proper way
|
2016-09-12 06:52:50 +00:00
|
|
|
# make sure all the threads are killed
|
2023-05-03 20:31:54 +00:00
|
|
|
for thread in 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()
|
2023-09-20 16:02:37 +00:00
|
|
|
except Exception as e:
|
|
|
|
nxc_logger.debug(f"Error stopping SMB server: {e}")
|
2016-09-12 06:52:50 +00:00
|
|
|
pass
|