Add named pipe (RPC/NP) transport support to SamServer#223
Merged
MichaelGrafnetter merged 3 commits intomasterfrom Mar 26, 2026
Merged
Add named pipe (RPC/NP) transport support to SamServer#223MichaelGrafnetter merged 3 commits intomasterfrom
MichaelGrafnetter merged 3 commits intomasterfrom
Conversation
When SamConnectWithCreds is used, the RPC connection defaults to TCP, which blocks password change operations. Add a useNamedPipes parameter to SamServer that establishes an authenticated SMB session (IPC$) before connecting, forcing RPC over named pipes (ncacn_np). Restores WNet P/Invoke methods (WNetAddConnection2, WNetCancelConnection2) using CsWin32-generated types (NETRESOURCEW, NET_RESOURCE_SCOPE, etc.) instead of the previously deleted manual enum/struct files. Closes #218 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-in (currently default-on) path to force SAMR RPC to use named pipes by pre-establishing an authenticated SMB IPC$ session, addressing “Access is denied” failures during password hash reset when connecting via SamConnectWithCreds.
Changes:
- Extend
SamServerwith auseNamedPipesoption and lifecycle management for an SMB IPC$ session. - Add mpr.dll interop (
WNetAddConnection2W,WNetCancelConnection2W) and supporting CsWin32 types/enums. - Update PowerShell SAM cmdlet base class to instantiate
SamServerwith named pipes enabled.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| Src/DSInternals.SAM/Wrappers/SamServer.cs | Adds named-pipe transport support by creating/disposing an IPC$ SMB session around SAM connect. |
| Src/DSInternals.SAM/NativeMethods.txt | Extends CsWin32 generation inputs with NETRESOURCE/WNet-related types/enums. |
| Src/DSInternals.SAM/Interop/NativeMethods.Mpr.cs | Introduces mpr.dll P/Invokes and a helper to connect to IPC$ with credentials. |
| Src/DSInternals.SAM/Interop/NamedPipeConnection.cs | New helper to manage IPC$ SMB connection lifetime used to force ncacn_np. |
| Src/DSInternals.SAM/Interop/Enums/NetCancelOptions.cs | Adds flags wrapper for WNetCancelConnection2 options. |
| Src/DSInternals.PowerShell/Commands/Base/SamCommandBase.cs | Forces named pipes on for SAM cmdlets when creating SamServer. |
Comments suppressed due to low confidence (1)
Src/DSInternals.SAM/Wrappers/SamServer.cs:66
- If
SamConnect*/Validator.AssertSuccessthrows after_namedPipeConnectionis created, the constructor exits early and the SMB session is never disposed, leaving an authenticated\\server\IPC$connection behind. Consider creating theNamedPipeConnectionin a local variable and disposing it in acatch/finallywhen the SAM connect fails, or only assigning it to the field after the SAM handle is successfully opened.
if (useNamedPipes && credential != null)
{
// Establish an authenticated SMB session to force RPC over named pipes
_namedPipeConnection = new NamedPipeConnection(serverName, credential);
}
NtStatus result = (credential != null) ?
NativeMethods.SamConnectWithCreds(serverName, out SafeSamHandle serverHandle, accessMask, credential) :
NativeMethods.SamConnect(serverName, out serverHandle, accessMask);
Validator.AssertSuccess(result);
this.Handle = serverHandle;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
When SamConnectWithCreds is used, the RPC connection defaults to TCP, which blocks password change operations. Add a useNamedPipes parameter to SamServer that establishes an authenticated SMB session (IPC$) before connecting, forcing RPC over named pipes (ncacn_np).
Restores WNet P/Invoke methods (WNetAddConnection2, WNetCancelConnection2) using CsWin32-generated types (NETRESOURCEW, NET_RESOURCE_SCOPE, etc.) instead of the previously deleted manual enum/struct files.
Closes #150