Skip to content

Commit 6856d26

Browse files
feat(logging): parse debug and verbose args early, so we can properly debug log during cli_arg generation
1 parent 59297e8 commit 6856d26

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

nxc/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99
from nxc.paths import NXC_PATH
1010
from nxc.loaders.protocolloader import ProtocolLoader
1111
from nxc.helpers.logger import highlight
12-
from nxc.logger import nxc_logger
12+
from nxc.logger import nxc_logger, setup_debug_logging
1313
import importlib.metadata
1414

1515

1616
def gen_cli_args():
17+
setup_debug_logging()
18+
1719
try:
1820
VERSION, COMMIT = importlib.metadata.version("netexec").split("+")
1921
except ValueError:
2022
VERSION = importlib.metadata.version("netexec")
2123
COMMIT = ""
22-
2324
CODENAME = "nxc4u"
25+
nxc_logger.debug(f"NXC VERSION: {VERSION} - {CODENAME} - {COMMIT}")
2426

2527
parser = argparse.ArgumentParser(description=rf"""
2628
. .

nxc/logger.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,31 @@
1111
from rich.logging import RichHandler
1212
import functools
1313
import inspect
14+
import argparse
1415

1516

17+
def parse_debug_args():
18+
debug_parser = argparse.ArgumentParser(add_help=False)
19+
debug_parser.add_argument("--debug", action="store_true")
20+
debug_parser.add_argument("--verbose", action="store_true")
21+
args, _ = debug_parser.parse_known_args()
22+
return args
23+
24+
def setup_debug_logging():
25+
debug_args = parse_debug_args()
26+
root_logger = logging.getLogger("root")
27+
28+
if debug_args.verbose:
29+
nxc_logger.logger.setLevel(logging.INFO)
30+
root_logger.setLevel(logging.INFO)
31+
elif debug_args.debug:
32+
nxc_logger.logger.setLevel(logging.DEBUG)
33+
root_logger.setLevel(logging.DEBUG)
34+
else:
35+
nxc_logger.logger.setLevel(logging.ERROR)
36+
root_logger.setLevel(logging.ERROR)
37+
38+
1639
def create_temp_logger(caller_frame, formatted_text, args, kwargs):
1740
"""Create a temporary logger for emitting a log where we need to override the calling file & line number, since these are obfuscated"""
1841
temp_logger = logging.getLogger("temp")
@@ -72,6 +95,7 @@ def __init__(self, extra=None):
7295
logging.getLogger("pypykatz").disabled = True
7396
logging.getLogger("minidump").disabled = True
7497
logging.getLogger("lsassy").disabled = True
98+
logging.getLogger("neo4j").setLevel(logging.ERROR)
7599

76100
def format(self, msg, *args, **kwargs): # noqa: A003
77101
"""Format msg for output

nxc/netexec.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from os.path import exists
2222
from os.path import join as path_join
2323
from sys import exit
24-
import logging
2524
from rich.progress import Progress
2625
import platform
2726

@@ -67,29 +66,17 @@ async def start_run(protocol_obj, args, db, targets):
6766

6867
def main():
6968
first_run_setup(nxc_logger)
70-
root_logger = logging.getLogger("root")
7169
args = gen_cli_args()
7270

73-
if args.verbose:
74-
nxc_logger.logger.setLevel(logging.INFO)
75-
root_logger.setLevel(logging.INFO)
76-
elif args.debug:
77-
nxc_logger.logger.setLevel(logging.DEBUG)
78-
root_logger.setLevel(logging.DEBUG)
79-
else:
80-
nxc_logger.logger.setLevel(logging.ERROR)
81-
root_logger.setLevel(logging.ERROR)
82-
logging.getLogger("neo4j").setLevel(logging.ERROR)
83-
8471
# if these are the same, it might double log to file (two FileHandlers will be added)
8572
# but this should never happen by accident
8673
if config_log:
8774
nxc_logger.add_file_log()
8875
if hasattr(args, "log") and args.log:
8976
nxc_logger.add_file_log(args.log)
9077

91-
nxc_logger.debug("PYTHON VERSION: " + sys.version)
92-
nxc_logger.debug("RUNNING ON: " + platform.system() + " Release: " + platform.release())
78+
nxc_logger.debug(f"PYTHON VERSION: {sys.version}")
79+
nxc_logger.debug(f"RUNNING ON: {platform.system()} Release: {platform.release()}")
9380
nxc_logger.debug(f"Passed args: {args}")
9481

9582
# FROM HERE ON A PROTOCOL IS REQUIRED

0 commit comments

Comments
 (0)