Skip to content

Restore RemoteRegistry with the RemoteOperations context manager - #1547

Draft
mpgn wants to merge 2 commits into
mainfrom
fix/remoteops-context-manager
Draft

mpgn wants to merge 2 commits into
mainfrom
fix/remoteops-context-manager

Conversation

@mpgn

@mpgn mpgn commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Several registry modules leave the RemoteRegistry service started on the target (and its start type changed when it was disabled): onelogon and wcc never call RemoteOperations.finish(), and uac, reg-winlogon, wdigest, rdp, enum_interfaces and --sam/--lsa/--ntds only call it when everything succeeds.

fortra/impacket#2329 added context manager support to RemoteOperations, so every call site now uses with RemoteOperations(...) as remote_ops: and finish() always runs, even after an error. This replaces the manual finish() calls and try/finally blocks, and #1465.

The impacket lock is bumped to fortra/impacket@7aa3770d (uv.lock and poetry.lock). An error raised by finish() itself is no longer silenced: it now reaches the module's existing error handler.

The bump breaks -M remove-mic (fortra/impacket#2339), fixed separately in #1555 which should be merged first. Only other visible change: --lsa cached hash dates now print with microseconds and +00:00 (fortra/impacket#2215).

AI Assistance

Claude Code (Claude Opus 5.5) migrated the call sites and ran the before/after tests. I reviewed it and tested it on my lab.

Type of change

Insert an "x" inside the brackets for relevant items

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Deprecation of feature or functionality
  • This change requires a documentation update
  • This requires a third party update (such as Impacket, Dploot, lsassy, etc)
  • This PR was created with the assistance of AI (list what type of assistance, tool(s)/model(s) in the description)

Setup guide for the review

Tested on GOAD (CASTELBLACK, where RemoteRegistry is stopped by default, and WINTERFELL). Hide whitespace in the diff: most lines are only re-indented.

nxc smb <host> -u <user> -p <pass> -x "sc query RemoteRegistry | findstr STATE"
nxc smb <host> -u <user> -p <pass> -M wcc
nxc smb <host> -u <user> -p <pass> -x "sc query RemoteRegistry | findstr STATE"

On main RemoteRegistry is still RUNNING after wcc, with this PR it is STOPPED again. Same output as main for the other migrated modules, onelogon (DC), raisechild, --sam/--lsa (regdump and secdump) and --ntds, and the service is STOPPED after each of them.

Screenshots (if appropriate):

wcc before/after

Checklist:

Insert an "x" inside the brackets for completed and relevant items (do not delete options)

  • I have ran Ruff against my changes (poetry: poetry run ruff check ., use --fix to automatically fix what it can)
  • I have added or updated the tests/e2e_commands.txt file if necessary (new modules or features are required to be added to the e2e tests)
  • If reliant on changes of third party dependencies, such as Impacket, dploot, lsassy, etc, I have linked the relevant PRs in those projects
  • I have linked relevant sources that describes the added technique (blog posts, documentation, etc)
  • I have performed a self-review of my own code (not an AI review)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (PR here: https://github.com/Pennyw0rth/NetExec-Wiki)

mpgn added 2 commits November 25, 2026 11:07
RemoteOperations.finish() now always runs, so registry modules and
--sam/--lsa/--ntds no longer leave RemoteRegistry started on the target
(fortra/impacket#2329).
Comment thread nxc/protocols/smb.py Resolved
Comment thread nxc/modules/reg-query.py Resolved
Comment thread nxc/modules/reg-query.py Resolved
Comment thread nxc/modules/rdp.py Resolved
Comment thread nxc/modules/remote-uac.py Resolved
Comment thread nxc/modules/shadowrdp.py Resolved
Comment thread nxc/modules/wdigest.py Resolved
Comment thread nxc/modules/wdigest.py Resolved
Comment thread nxc/modules/remove-mic.py Fixed
@mpgn

mpgn commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

The bot findings are all pre-existing on main: these lines are only re-indented by the with blocks (git diff -w shows no change on them). The CodeQL MD4 alert is the gMSA NT hash in lsa(), the same code as the alert already open on main.

@NeffIsBack NeffIsBack added enhancement New feature or request bug-fix This Pull Request fixes a bug and removed enhancement New feature or request labels Nov 30, 2026

@NeffIsBack NeffIsBack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in remove-mic.py seem independent of the context manager changes. Please pull them out of this PR and create a separate one.

@mpgn mpgn mentioned this pull request Nov 30, 2026
5 of 14 tasks
@mpgn
mpgn force-pushed the fix/remoteops-context-manager branch from 68b42fc to a9c88c9 Compare November 30, 2026 08:50
@mpgn

mpgn commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Done, moved to #1555. The impacket bump here breaks remove-mic without it, so #1555 should be merged first (it works with both the current lock and impacket master).

@mpgn
mpgn requested a review from NeffIsBack November 30, 2026 08:50
@NeffIsBack NeffIsBack added the reviewed code Label for when a static code review was done label Nov 30, 2026
Comment thread nxc/protocols/smb.py
pillaged_from=host_id,
)
add_sam_hash.sam_hashes += 1
with self.remote_ops:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a DC, Dfs (DFS Namespace) depends on RemoteRegistry, so when the service was idle-stopped and we started it, the stop in finish() fails with ERROR_DEPENDENT_SERVICES_RUNNING. On main this was logged with debug, now it reaches self.logger.exception() and --sam ends with a full traceback after a successful dump (DC 2022, RemoteRegistry STOPPED before the run):

SMB  192.168.56.3  445  DC01  [+] Added 3 SAM hashes to the database
     │ ❱ 1275 │   return dce.request(request)       │
     ...
     DCERPCSessionError: SCMR SessionError: code: 0x41b - ERROR_DEPENDENT_SERVICES_RUNNING - A
     stop control has been sent to a service that other running services are dependent on.

Same for lsa() at line 2384. Since you wrote fortra/impacket#2329, I think the cleanest place is __restore(), next to the ERROR_SERVICE_NOT_ACTIVE case: the service cannot be stopped, there is nothing left to restore. Otherwise please catch it here and keep the debug line.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Fixed in __restore() as you suggested: fortra/impacket#2402. I'll bump the lock here once it's merged.

Comment thread nxc/protocols/smb.py
if secret.find("$") == -1 and secret_type == NTDSHashes.SECRET_TYPE.NTDS and "_history" not in secret:
if secret.find("\\") != -1:
domain, clean_hash = secret.split("\\")
with self.remote_ops:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue, but there is no try around this with, so the error leaves ntds() and aborts proto_flow() for the host: every option or module that comes after --ntds is skipped. On main it was a debug line.

SMB  192.168.56.3  445  DC01  [+] Dumping the NTDS, this could take a while so go grab a redbull...
[14:29:14] ERROR  Exception while calling proto_flow() on target 192.168.56.3: SCMR SessionError:
           code: 0x41b - ERROR_DEPENDENT_SERVICES_RUNNING - A stop control has been sent to a
           service that other running services are dependent on.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same fix (fortra/impacket#2402), finish() no longer raises in this case.

def on_admin_login(self, context, connection):
try:
remote_ops = RemoteOperations(connection.conn, False)
with RemoteOperations(connection.conn, False) as remote_ops:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed finally logged a finish() failure with debug, and there is no other handler in this module, so on a DC the result is now followed by Exception while calling proto_flow() (same output as the ntds() comment) and the next modules are skipped. Same for the contextlib.suppress(Exception) removed in rdp.py (115, 150) and wdigest.py (35, 63, 98): these had no "existing error handler" to fall back on.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same fix (fortra/impacket#2402). Right, the PR description was wrong about the existing handlers, I'll fix it with the bump.

@mpgn

mpgn commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

As draft waiting for fortra/impacket#2402 to be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix This Pull Request fixes a bug reviewed code Label for when a static code review was done

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants