Skip to content

Commit

Permalink
Merge pull request #446 from Pennyw0rth/fix-windows
Browse files Browse the repository at this point in the history
Fix windows and encoding stuff
  • Loading branch information
NeffIsBack authored Oct 12, 2024
2 parents 9e61764 + 8485417 commit 485f720
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions netexec.spec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ a = Analysis(
'impacket.dcerpc.v5.lsad',
'impacket.dcerpc.v5.gkdi',
'impacket.dcerpc.v5.rprn',
'impacket.dcerpc.v5.even',
'impacket.dpapi_ng',
'impacket.tds',
'impacket.version',
Expand All @@ -48,6 +49,7 @@ a = Analysis(
'pywerview.cli.helpers',
'pylnk3',
'pypykatz',
'pyNfsClient',
'masky',
'msldap',
'msldap.connection',
Expand Down
19 changes: 11 additions & 8 deletions nxc/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ def parse_debug_args():
args, _ = debug_parser.parse_known_args()
return args


def setup_debug_logging():
debug_args = parse_debug_args()
root_logger = logging.getLogger("root")

if debug_args.verbose:
nxc_logger.logger.setLevel(logging.INFO)
root_logger.setLevel(logging.INFO)
Expand All @@ -35,7 +36,7 @@ def setup_debug_logging():
else:
nxc_logger.logger.setLevel(logging.ERROR)
root_logger.setLevel(logging.ERROR)


def create_temp_logger(caller_frame, formatted_text, args, kwargs):
"""Create a temporary logger for emitting a log where we need to override the calling file & line number, since these are obfuscated"""
Expand All @@ -47,22 +48,24 @@ def create_temp_logger(caller_frame, formatted_text, args, kwargs):

class SmartDebugRichHandler(RichHandler):
"""Custom logging handler for when we want to log normal messages to DEBUG and not double log"""

def __init__(self, formatter=None, *args, **kwargs):
super().__init__(*args, **kwargs)
if formatter is not None:
self.setFormatter(formatter)

def emit(self, record):
"""Overrides the emit method of the RichHandler class so we can set the proper pathname and lineno"""
# for some reason in RDP, the exc_text is None which leads to a KeyError in Python logging
record.exc_text = record.getMessage() if record.exc_text is None else record.exc_text

if hasattr(record, "caller_frame"):
frame_info = inspect.getframeinfo(record.caller_frame)
record.pathname = frame_info.filename
record.lineno = frame_info.lineno
super().emit(record)


def no_debug(func):
"""Stops logging non-debug messages when we are in debug mode
It creates a temporary logger and logs the message to the console and file
Expand All @@ -72,7 +75,7 @@ def no_debug(func):
def wrapper(self, msg, *args, **kwargs):
if self.logger.getEffectiveLevel() >= logging.INFO:
return func(self, msg, *args, **kwargs)
else:
else:
formatted_text = Text.from_ansi(self.format(msg, *args, **kwargs)[0])
caller_frame = inspect.currentframe().f_back
create_temp_logger(caller_frame, formatted_text, args, kwargs)
Expand All @@ -94,7 +97,7 @@ def __init__(self, extra=None):
self.logger = logging.getLogger("nxc")
self.extra = extra
self.output_file = None

logging.getLogger("impacket").disabled = True
logging.getLogger("pypykatz").disabled = True
logging.getLogger("minidump").disabled = True
Expand Down Expand Up @@ -181,7 +184,7 @@ def add_file_log(self, log_file=None):
open(output_file, "x") # noqa: SIM115
file_creation = True

file_handler = RotatingFileHandler(output_file, maxBytes=100000)
file_handler = RotatingFileHandler(output_file, maxBytes=100000, encoding="utf-8")

with file_handler._open() as f:
if file_creation:
Expand All @@ -203,7 +206,7 @@ def init_log_file():
datetime.now().strftime("%Y-%m-%d"),
f"log_{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.log",
)


class TermEscapeCodeFormatter(logging.Formatter):
"""A class to strip the escape codes for logging to files"""
Expand Down
2 changes: 1 addition & 1 deletion nxc/parsers/ldap_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def parse_result_attributes(ldap_response):
continue
attribute_map = {}
for attribute in entry["attributes"]:
val = [str(val) for val in attribute["vals"].components]
val = [str(val).encode(val.encoding).decode("utf-8") for val in attribute["vals"].components]
attribute_map[str(attribute["type"])] = val if len(val) > 1 else val[0]
parsed_response.append(attribute_map)
return parsed_response

0 comments on commit 485f720

Please sign in to comment.