Repository navigation
Wait for RemoteRegistry to be running instead of sleeping 1 second - #2336
Merged
Merged
Conversation
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.
14 of 17 tasks
36 of 44 tasks
alexisbalbachan
requested changes
Oct 28, 2026
Collaborator
There was a problem hiding this comment.
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.
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.
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 |
Collaborator
|
Looks perfect now, merging it now, thank you! |
godylockz
pushed a commit
to godylockz/impacket
that referenced
this pull request
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello guys, another one that will help :)
When RemoteRegistry is stopped or disabled,
RemoteOperations.__checkServiceStatus()insecretsdump.pyandregsecrets.pystarts 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 awinregnamed-pipe trigger). On that host the service already reportsSERVICE_RUNNINGon the first status query afterhRStartServiceW()returns (12-30 ms), so the rest of the second is wasted.This change polls
hRQueryServiceStatus()every 0.1 s until the service reportsSERVICE_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:
StartServicecan return while the service is stillSERVICE_START_PENDING, and__connectWinReg()does not retry. Opening\winregwhile nothing serves it fails withSTATUS_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.--samand--lsago throughregsecrets.RemoteOperations,--sam secdumpthroughsecretsdump.RemoteOperations:enableRegistry()--sam--sam secdump--lsa--sam--sam secdump--lsa--sam/--sam secdump/--lsaIn 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_RUNNINGevery time, so the 0.1 s sleep was never taken, and the winreg bind succeeded on the first try. Thesecdumpwall 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 skippedflake8 --select=E9,F63,F7,F82on both files: cleanNot changed here: when the service is found in a pending state, the
Unknown service stateexception readsans['CurrentState'], which raisesKeyError: 'CurrentState'. This is on a different branch of the same function and needs its own fix.#2329 changes
__restore()andfinish()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).