Skip to content

Commit d7d7505

Browse files
authored
Merge branch 'main' into timeroast_module
2 parents 7b7f4b2 + 45db7e6 commit d7d7505

2 files changed

Lines changed: 17 additions & 34 deletions

File tree

nxc/logger.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from logging.handlers import RotatingFileHandler
44
import os.path
55
import sys
6-
import re
76
from nxc.console import nxc_console
87
from nxc.paths import NXC_PATH
98
from termcolor import colored
@@ -43,7 +42,7 @@ def create_temp_logger(caller_frame, formatted_text, args, kwargs):
4342
temp_logger = logging.getLogger("temp")
4443
formatter = logging.Formatter("%(message)s", datefmt="[%X]")
4544
handler = SmartDebugRichHandler(formatter=formatter)
46-
handler.handle(LogRecord(temp_logger.name, logging.INFO, caller_frame.f_code.co_filename, caller_frame.f_lineno, formatted_text, args, kwargs, caller_frame=caller_frame))
45+
handler.handle(LogRecord(temp_logger.name, logging.INFO, caller_frame.f_code.co_filename, caller_frame.f_lineno, formatted_text, args, None, caller_frame=caller_frame))
4746

4847

4948
class SmartDebugRichHandler(RichHandler):
@@ -56,9 +55,6 @@ def __init__(self, formatter=None, *args, **kwargs):
5655

5756
def emit(self, record):
5857
"""Overrides the emit method of the RichHandler class so we can set the proper pathname and lineno"""
59-
# for some reason in RDP, the exc_text is None which leads to a KeyError in Python logging
60-
record.exc_text = record.getMessage() if record.exc_text is None else record.exc_text
61-
6258
if hasattr(record, "caller_frame"):
6359
frame_info = inspect.getframeinfo(record.caller_frame)
6460
record.pathname = frame_info.filename
@@ -177,7 +173,7 @@ def log_console_to_file(self, text, *args, **kwargs):
177173
self.logger.fail(f"Issue while trying to custom print handler: {e}")
178174

179175
def add_file_log(self, log_file=None):
180-
file_formatter = TermEscapeCodeFormatter("%(asctime)s | %(filename)s:%(lineno)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
176+
file_formatter = logging.Formatter("%(asctime)s | %(filename)s:%(lineno)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
181177
output_file = self.init_log_file() if log_file is None else log_file
182178
file_creation = False
183179

@@ -209,17 +205,5 @@ def init_log_file():
209205
)
210206

211207

212-
class TermEscapeCodeFormatter(logging.Formatter):
213-
"""A class to strip the escape codes for logging to files"""
214-
215-
def __init__(self, fmt=None, datefmt=None, style="%", validate=True):
216-
super().__init__(fmt, datefmt, style, validate)
217-
218-
def format(self, record): # noqa: A003
219-
escape_re = re.compile(r"\x1b\[[0-9;]*m")
220-
record.msg = re.sub(escape_re, "", str(record.msg))
221-
return super().format(record)
222-
223-
224208
# initialize the logger for all of nxc - this is imported everywhere
225209
nxc_logger = NXCAdapter()

nxc/protocols/smb.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def __init__(self, args, db, host):
159159
self.bootkey = None
160160
self.output_filename = None
161161
self.smbv1 = None
162+
self.is_timeouted = False
162163
self.signing = False
163164
self.smb_share_name = smb_share_name
164165
self.pvkbytes = None
@@ -551,8 +552,16 @@ def create_smbv1_conn(self):
551552
)
552553
self.smbv1 = True
553554
except OSError as e:
554-
if str(e).find("Connection reset by peer") != -1:
555+
if "Connection reset by peer" in str(e):
555556
self.logger.info(f"SMBv1 might be disabled on {self.host}")
557+
elif "timed out" in str(e):
558+
self.is_timeouted = True
559+
self.logger.debug(f"Timeout creating SMBv1 connection to {self.host}")
560+
else:
561+
self.logger.info(f"Error creating SMBv1 connection to {self.host}: {e}")
562+
return False
563+
except NetBIOSError:
564+
self.logger.info(f"SMBv1 disabled on {self.host}")
556565
return False
557566
except (Exception, NetBIOSTimeout) as e:
558567
self.logger.info(f"Error creating SMBv1 connection to {self.host}: {e}")
@@ -570,15 +579,7 @@ def create_smbv3_conn(self):
570579
timeout=self.args.smb_timeout,
571580
)
572581
self.smbv1 = False
573-
except OSError as e:
574-
# This should not happen anymore!!!
575-
if str(e).find("Too many open files") != -1:
576-
if not self.logger:
577-
print("DEBUG ERROR: logger not set, please open an issue on github: " + str(self) + str(self.logger))
578-
self.proto_logger()
579-
self.logger.fail(f"SMBv3 connection error on {self.host}: {e}")
580-
return False
581-
except (Exception, NetBIOSTimeout) as e:
582+
except (Exception, NetBIOSTimeout, OSError) as e:
582583
self.logger.info(f"Error creating SMBv3 connection to {self.host}: {e}")
583584
return False
584585
return True
@@ -596,7 +597,7 @@ def create_conn_obj(self, no_smbv1=False):
596597
self.smbv1 = self.create_smbv1_conn()
597598
if self.smbv1:
598599
return True
599-
else:
600+
elif not self.is_timeouted:
600601
return self.create_smbv3_conn()
601602
elif not no_smbv1 and self.smbv1:
602603
return self.create_smbv1_conn()
@@ -845,7 +846,7 @@ def shares(self):
845846
self.logger.debug(f"domain: {self.domain}")
846847
user_id = self.db.get_user(self.domain.upper(), self.username)[0][0]
847848
except IndexError as e:
848-
if self.kerberos:
849+
if self.kerberos or self.username == "":
849850
pass
850851
else:
851852
self.logger.fail(f"IndexError: {e!s}")
@@ -947,10 +948,9 @@ def shares(self):
947948
self.logger.highlight(f"{name:<15} {','.join(perms):<15} {remark}")
948949
return permissions
949950

950-
951951
def dir(self): # noqa: A003
952952
search_path = ntpath.join(self.args.dir, "*")
953-
try:
953+
try:
954954
contents = self.conn.listPath(self.args.share, search_path)
955955
except SessionError as e:
956956
error = get_error_string(e)
@@ -959,7 +959,7 @@ def dir(self): # noqa: A003
959959
color="magenta" if error in smb_error_status else "red",
960960
)
961961
return
962-
962+
963963
if not contents:
964964
return
965965

@@ -969,7 +969,6 @@ def dir(self): # noqa: A003
969969
full_path = ntpath.join(self.args.dir, content.get_longname())
970970
self.logger.highlight(f"{'d' if content.is_directory() else 'f'}{'rw-' if content.is_readonly() > 0 else 'r--':<8}{content.get_filesize():<15}{ctime(float(content.get_mtime_epoch())):<30}{full_path:<45}")
971971

972-
973972
@requires_admin
974973
def interfaces(self):
975974
"""

0 commit comments

Comments
 (0)