NetExec/nxc/servers/smb.py

54 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import threading
from threading import enumerate
from sys import exit
from impacket import smbserver
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,
share_path="/tmp/nxc_hosted",
2023-05-02 15:17:59 +00:00
listen_address="0.0.0.0",
listen_port=445,
verbose=False,
):
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("")
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-05-08 18:39:36 +00:00
logger.error("Error starting SMB server on port 445: the port is already in use")
else:
logger.error(f"Error starting SMB server on port 445: {message}")
exit(1)
2017-03-27 21:09:36 +00:00
def addShare(self, share_name, share_path):
self.server.addShare(share_name, share_path)
def run(self):
try:
self.server.start()
except:
pass
def shutdown(self):
2023-05-02 15:17:59 +00:00
# TODO: should fine the proper way
# make sure all the threads are killed
for thread in enumerate():
if thread.is_alive():
try:
self._stop()
except:
pass