2015-05-06 64 views
1

我想知道是否有可能限制遠程機器訪問服務器中的命名管道。 我正在對服務器進行初始化如下:是否可以限制遠程機器連接到NamedPipeServerStream?

NamedPipeServerStream pipeServer = new NamedPipeServerStream("myPipe", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); 

遠程客戶端的作用:

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(serverIP, "myPipe", PipeDirection.InOut)) 
{ 
    pipeStream.Connect(2000); 
} 

,當然它成功。 有沒有辦法限制它? 謝謝!

回答

1

找到了! 需要限制NT AUTHORITY \ NETWORK的用法:

PipeSecurity PipeSecurity = new PipeSecurity();    
     PipeAccessRule AccessRule = new PipeAccessRule(@"NT AUTHORITY\NETWORK", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Deny); 
     PipeSecurity.AddAccessRule(AccessRule); 
     PipeAccessRule AccessRule2 = new PipeAccessRule(string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName), PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow); 
     PipeSecurity.AddAccessRule(AccessRule2); 

然後將其添加到構造函數:

NamedPipeServerStream m_PipeServer = new NamedPipeServerStream("myPipe", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 0, 0, PipeSecurity); 

注意,使用管道安全性時,它不足以拒絕網絡訪問,但您需要允許應該使用該管道的當前用戶(或多個用戶)進行訪問。

string.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName) 
+0

首先:您似乎在代碼示例的第3行中有複製粘貼錯誤。 'm_AccessRule'沒有用在代碼中,可能應該是'AccessRule'。除此之外,這給了我一個關於將訪問規則添加到「PipeSecurity」對象的'IdentityNotMappedException'。這表明'NT AUTHORITY \ NETWORK'用戶在我的系統中不存在,但這是不可能的,是嗎? – aRestless

+0

@aRestless修復它。它怎麼可能不存在於你的系統中?當您嘗試共享文件夾時,請在文本框中輸入「網絡服務」,但它找不到它? – ArielB

+0

如果我們正在討論這個對話:http://puu.sh/iFLJe/1d394791a6.png(德文文本雖然)不,它沒有找到它。更加好奇的是,如果我將'AccessRule'離開,我可以遠程連接到管道,但是我需要(你的)'AccessRule2'來使管道可以被訪問。看起來像「AccessRule2」允許更多的東西比它應該。 – aRestless

相關問題