2016-07-11 48 views
1

我想檢查一個端口是否在Windows防火牆中打開。如何檢查Windows防火牆中的規則?

我發現使用Netsh是這樣的:

netsh advfirewall firewall show rule name="My rule" 

如果規則存在與否將返回...

但是,根據Windows語言,這將返回不同的消息。我正試圖以更好的方式解決這個問題。我想得到結果YesNo,TrueFalse,而不是本地化的字符串。

您有任何提示嗎?

+0

如果有第三方顯示器會怎麼樣? 也「advfirewall」AFAIR是在Vista或Win7中引入的,在2000/XP中沒有這樣的命令AFAIR –

+0

有一個api。你有沒有搜索過它? –

+0

試試這個[使用具有高級安全性的Windows防火牆腳本API和Delphi](https://theroadtodelphi.com/2013/11/21/using-the-windows-firewall-with-advanced-security-scripting-api-and -delphi /) – RRUZ

回答

3

AS:在Windows Vista中引入了「advfirewall」命令和底層服務。 Windows 2000/XP沒有它並支持它,你應該使用不同的接口。

也是一樣安裝第三方,非Microsoft防火牆的計算機(如殺毒套裝的一部分。例如)。

一般在Vista +你應該獲得INetFwRules COM對象,然後枚舉它所有的規則,並檢查每一條規則,如果它涵蓋了你對這個端口。

跟隨例如使用靜態綁定,而不是OleVariant應該更快,更可靠的獲取和枚舉的規則 https://theroadtodelphi.com/2013/11/21/using-the-windows-firewall-with-advanced-security-scripting-api-and-delphi/#Enumerating防火牆規則

var 
CurrentProfiles : Integer; 
fwPolicy2  : OleVariant; 
RulesObject  : OleVariant; 
rule   : OleVariant; 
oEnum   : IEnumvariant; 
iValue   : LongWord; 

    fwPolicy2 := CreateOleObject('HNetCfg.FwPolicy2'); 
    RulesObject := fwPolicy2.Rules; 
    CurrentProfiles := fwPolicy2.CurrentProfileTypes; 

    ..... 

    Writeln('Rules:'); 

    oEnum   := IUnknown(Rulesobject._NewEnum) as IEnumVariant; 
    while oEnum.Next(1, rule, iValue) = 0 do 
    begin 
    if (rule.Profiles And CurrentProfiles)<>0 then 
    begin 
     Writeln(' Rule Name:   ' + rule.Name); 
     Writeln(' ----------------------------------------------'); 
     Writeln(' Description:  ' + rule.Description); 
     Writeln(' Application Name: ' + rule.ApplicationName); 
     Writeln(' Service Name:  ' + rule.ServiceName); 

     if (rule.Protocol = NET_FW_IP_PROTOCOL_TCP) or (rule.Protocol = NET_FW_IP_PROTOCOL_UDP) then 
     begin 
      Writeln(' Local Ports:  ' + rule.LocalPorts); 
      Writeln(' Remote Ports:  ' + rule.RemotePorts); 
      Writeln(' LocalAddresses:  ' + rule.LocalAddresses); 
      Writeln(' RemoteAddresses: ' + rule.RemoteAddresses); 
     end; 

    ..... 

    end; 

OTOH,檢查https://github.com/yypbd/yypbd-Delphi-HeaderPorting/tree/master/example/FirewallExample