Skip to content

Wait for RemoteRegistry to be running instead of sleeping 1 second - #2336

Merged
alexisbalbachan merged 2 commits into
fortra:masterfrom
mpgn:remoteregistry-wait-running
Nov 4, 2026
Merged

alexisbalbachan merged 2 commits into
fortra:masterfrom
mpgn:remoteregistry-wait-running

Conversation

@mpgn

@mpgn mpgn commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Hello guys, another one that will help :)

When RemoteRegistry is stopped or disabled, RemoteOperations.__checkServiceStatus() in secretsdump.py and regsecrets.py starts it, then sleeps for a fixed second before connecting to \pipe\winreg. That second is spent every time the service is not already running. This is the usual case: Windows 10/11 ship it disabled, and on the Windows Server 2019 test host it was stopped (auto start with a winreg named-pipe trigger). On that host the service already reports SERVICE_RUNNING on the first status query after hRStartServiceW() returns (12-30 ms), so the rest of the second is wasted.

This change polls hRQueryServiceStatus() every 0.1 s until the service reports SERVICE_RUNNING, for at most 5 s of elapsed time, and then connects to winreg as before. When the service is already running, nothing changes.

The wait is still needed: StartService can return while the service is still SERVICE_START_PENDING, and __connectWinReg() does not retry. Opening \winreg while nothing serves it fails with STATUS_OBJECT_NAME_NOT_FOUND, which I checked on the same host with the service stopped and disabled. If the cap is reached, a warning is logged and the outcome is the same as before (the winreg connect fails), only 4 s later.

Measured with NetExec against Windows Server 2019 (build 17763). The table shows the median time spent in enableRegistry(), with the median wall time of the whole run in brackets. Base and patched runs were interleaved: 4 runs per cell, 3 when the service was already running. --sam and --lsa go through regsecrets.RemoteOperations, --sam secdump through secretsdump.RemoteOperations:

RemoteRegistry before the run command before after gain in enableRegistry()
stopped, auto start --sam 1.154 s (3.65 s) 0.167 s (2.75 s) −0.99 s
stopped, auto start --sam secdump 1.155 s (18.67 s) 0.154 s (18.21 s) −1.00 s
stopped, auto start --lsa 1.147 s (4.39 s) 0.149 s (3.29 s) −1.00 s
stopped, disabled --sam 1.172 s (3.56 s) 0.168 s (2.77 s) −1.00 s
stopped, disabled --sam secdump 1.230 s (20.11 s) 0.151 s (17.78 s) −1.08 s
stopped, disabled --lsa 1.159 s (4.42 s) 0.190 s (3.48 s) −0.97 s
running --sam / --sam secdump / --lsa 0.091 / 0.090 / 0.084 s 0.087 / 0.089 / 0.086 s none (nothing to wait for)

In all 66 runs the dumped SAM hashes and LSA secrets were identical. After each run the service was back in the state it was in before (stopped, or stopped and disabled, or running). With the patch, the first poll returned SERVICE_RUNNING every time, so the 0.1 s sleep was never taken, and the winreg bind succeeded on the first try. The secdump wall times vary by a few seconds from run to run because the hive save and download dominate them.

Tests:

  • pytest tests -m "not remote": 701 passed, 6 skipped
  • flake8 --select=E9,F63,F7,F82 on both files: clean
  • Offline, with a fake SCM: a service that reaches RUNNING after 1.5 s now connects (before, the connect failed after the 1 s sleep). A service that never reaches RUNNING fails with the same exception as before, after about 5 s. The disabled → demand → disabled restore sequence is unchanged.

Not changed here: when the service is found in a pending state, the Unknown service state exception reads ans['CurrentState'], which raises KeyError: 'CurrentState'. This is on a different branch of the same function and needs its own fix.

#2329 changes __restore() and finish() in the same two files; this change is in __checkServiceStatus() only, and the patch applies cleanly on top of #2329 (one hunk shifts by one line).

After starting a stopped or disabled RemoteRegistry service,
RemoteOperations.__checkServiceStatus() in secretsdump.py and
regsecrets.py slept a fixed second before connecting to winreg. Poll
hRQueryServiceStatus() every 0.1 s until the service reports
SERVICE_RUNNING instead, for at most 50 tries (about 5 s). After that,
the winreg connect is attempted as before.

The wait is still needed because StartService can return while the
service is START_PENDING, and __connectWinReg() does not retry. On
Windows Server 2019 the first poll already returns SERVICE_RUNNING, so
enableRegistry() drops from about 1.15 s to 0.15 s.

@alexisbalbachan alexisbalbachan left a comment •

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.

Hi, this PR goes in the right direction by querying the service status instead of blindly waiting 1 second. There are 2 minimal issues:

  • We're expecting a max waiting time of 5 seconds, however, each call to hRQueryServiceStatus will have its own delay (for example, if it takes 200ms to complete we'll be waiting (200ms+0.1s)*50 => 15 seconds). It should check the elapsed time between each query and stop if it exceeds the 5 second limit.
  • If the waiting limit is reached we should at least issue a warning log about it.

@alexisbalbachan alexisbalbachan added waiting for response Further information is needed from people who opened the issue or pull request and removed in review This issue or pull request is being analyzed labels Oct 28, 2026
The 50-try cap did not count the time each hRQueryServiceStatus() call
takes, so a slow link could stretch the wait well past 5 seconds (15 s
with a 200 ms round trip). Stop once 5 seconds have elapsed, and log a
warning when the service is still not running before connecting to
winreg.
@mpgn

mpgn commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Good points, thanks! Fixed in 0158142: the wait now stops once 5 s have elapsed (checked after each query), and a warning is logged if the service is still not running at that point.

With a fake SCM that answers each query in 200 ms and never reaches SERVICE_RUNNING, it now gives up after 5.2 s instead of 15.2 s.

@alexisbalbachan

Copy link
Copy Markdown
Collaborator

Looks perfect now, merging it now, thank you!

@alexisbalbachan
alexisbalbachan merged commit c96563e into fortra:master Nov 4, 2026
8 checks passed
godylockz pushed a commit to godylockz/impacket that referenced this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for response Further information is needed from people who opened the issue or pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants