Windows Server 2012いいですね。 最新のWindowsを触れる環境というのは幸せです。
さて、Windows Server 2012やWindows 8では、PowerShell 3.0でローカル、リモートのファイアウォール状態を取得可能です。 今回はその手法例を紹介します。
Firewall状態を取得するコマンドレット
簡単です。
Get-NetFirewallRule
取得できるメンバー
では取得できるメンバーはなんでしょうか? 見てみましょう。
Get-NetFirewallRule | Get-Member
TypeName: Microsoft.Management.Infrastructure.CimInstance#root/standardcimv2/MSFT_NetFirewallRule Name MemberType Definition ---- ---------- ---------- Group AliasProperty Group = RuleGroup ID AliasProperty ID = Name LSM AliasProperty LSM = LooseSourceMapping Name AliasProperty Name = InstanceID Platform AliasProperty Platform = Platforms Clone Method System.Object ICloneable.Clone() Dispose Method void Dispose(), void IDisposable.Dispose() Equals Method bool Equals(System.Object obj) GetCimSessionComputerName Method string GetCimSessionComputerName() GetCimSessionInstanceId Method guid GetCimSessionInstanceId() GetHashCode Method int GetHashCode() GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContex... GetType Method type GetType() ToString Method string ToString() Caption Property string Caption {get;} CommonName Property string CommonName {get;} ConditionListType Property uint16 ConditionListType {get;} CreationClassName Property string CreationClassName {get;} Description Property string Description {get;set;} DisplayGroup Property string DisplayGroup {get;} ElementName Property string ElementName {get;set;} ExecutionStrategy Property uint16 ExecutionStrategy {get;} InstanceID Property string InstanceID {get;} LocalOnlyMapping Property bool LocalOnlyMapping {get;set;} LooseSourceMapping Property bool LooseSourceMapping {get;set;} Mandatory Property bool Mandatory {get;} Owner Property string Owner {get;set;} Platforms Property string[] Platforms {get;set;} PolicyDecisionStrategy Property uint16 PolicyDecisionStrategy {get;} PolicyKeywords Property string[] PolicyKeywords {get;} PolicyRoles Property string[] PolicyRoles {get;} PolicyRuleName Property string PolicyRuleName {get;} PolicyStoreSource Property string PolicyStoreSource {get;set;} Priority Property uint16 Priority {get;} Profiles Property uint16 Profiles {get;set;} PSComputerName Property string PSComputerName {get;} RuleGroup Property string RuleGroup {get;set;} RuleUsage Property string RuleUsage {get;} SequencedActions Property uint16 SequencedActions {get;} StatusCode Property uint32 StatusCode {get;set;} SystemCreationClassName Property string SystemCreationClassName {get;} SystemName Property string SystemName {get;} Action ScriptProperty System.Object Action {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Action]($this.PSBase.CimIns... Direction ScriptProperty System.Object Direction {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Direction]($this.PSBase.... DisplayName ScriptProperty System.Object DisplayName {get=$this.PSBase.CimInstanceProperties["DisplayName"].Value;set=param($x) ; $this.PSBase.... EdgeTraversalPolicy ScriptProperty System.Object EdgeTraversalPolicy {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.EdgeTraversal]... Enabled ScriptProperty System.Object Enabled {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Enabled]($this.PSBase.CimI... EnforcementStatus ScriptProperty System.Object EnforcementStatus {get=[Microsoft.Windows.Firewall.Commands.Formatting.Formatter]::FormatEnforcementSt... PolicyStoreSourceType ScriptProperty System.Object PolicyStoreSourceType {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.PolicyStoreT... PrimaryStatus ScriptProperty System.Object PrimaryStatus {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.PrimaryStatus]($this... Profile ScriptProperty System.Object Profile {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]($this.PSBase.CimI... Status ScriptProperty System.Object Status {get=$this.PSBase.CimInstanceProperties["Status"].Value + " (" + ($this.PSBase.CimInstancePrope...
ふむふむ、良くわからない? では一レコードだけ見てみましょう。
Get-NetFirewallRule -All | Select -First 1
出力です。
Name : vm-monitoring-icmpv6 DisplayName : 仮想マシンの監視 (エコー要求 - ICMPv6 受信) Description : エコー要求のメッセージは、他のノードに対する ping 要求として送信されます。 DisplayGroup : 仮想マシンの監視 Group : @vmicres.dll,-700 Enabled : False Profile : Any Platform : {} Direction : Inbound Action : Allow EdgeTraversalPolicy : Block LooseSourceMapping : False LocalOnlyMapping : False Owner : PrimaryStatus : OK Status : 規則は、ストアから正常に解析されました。 (65536) EnforcementStatus : NotApplicable PolicyStoreSource : PersistentStore PolicyStoreSourceType : Local
ふむふむ。これなら、Display Nameがhogehogeと付くものも取得できそうですね。
取得例
例えばこれでSMBに関するDomainとPrivateの受信ルールが取得できます。
つまり、ファイル共有を受け付けるかどうかですね。
Get-NetFirewallRule -DisplayName *SMB* | where {$_.Profile -match "Domain|Private"} | where Direction -eq Inbound | sort Direction | select *
Name : FPS-SMB-In-TCP-NoScope ID : FPS-SMB-In-TCP-NoScope Group : @FirewallAPI.dll,-28502 Platform : {} LSM : False DisplayName : ファイルとプリンターの共有 (SMB 受信) Enabled : False Profile : Domain Direction : Inbound Action : Allow EdgeTraversalPolicy : Block PrimaryStatus : OK Status : 規則は、ストアから正常に解析されました。 (65536) EnforcementStatus : NotApplicable PolicyStoreSourceType : Local Caption : Description : サーバー メッセージ ブロックの名前付きパイプ経由の送受信を許可するためのファイルとプリンターの共有の受信規則です。 [TCP 445] ElementName : @FirewallAPI.dll,-28511 InstanceID : FPS-SMB-In-TCP-NoScope CommonName : PolicyKeywords : PolicyDecisionStrategy : 2 PolicyRoles : ConditionListType : 3 CreationClassName : MSFT|FW|FirewallRule|FPS-SMB-In-TCP-NoScope ExecutionStrategy : 2 Mandatory : PolicyRuleName : Priority : RuleUsage : SequencedActions : 3 SystemCreationClassName : SystemName : DisplayGroup : ファイルとプリンターの共有 LocalOnlyMapping : False LooseSourceMapping : False Owner : Platforms : {} PolicyStoreSource : PersistentStore Profiles : 1 RuleGroup : @FirewallAPI.dll,-28502 StatusCode : 65536 PSComputerName : CimClass : root/standardcimv2:MSFT_NetFirewallRule CimInstanceProperties : {Caption, Description, ElementName, InstanceID...} CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties Name : {79B75FCF-5F1B-4A67-A6BF-83B21522CEF8} ID : {79B75FCF-5F1B-4A67-A6BF-83B21522CEF8} Group : @FirewallAPI.dll,-28502 Platform : {} LSM : False DisplayName : ファイルとプリンターの共有 (SMB 受信) Enabled : True Profile : Private Direction : Inbound Action : Allow EdgeTraversalPolicy : Block PrimaryStatus : OK Status : 規則は、ストアから正常に解析されました。 (65536) EnforcementStatus : NotApplicable PolicyStoreSourceType : Local Caption : Description : サーバー メッセージ ブロックの名前付きパイプ経由の送受信を許可するためのファイルとプリンターの共有の受信規則です。 [TCP 445] ElementName : @FirewallAPI.dll,-28511 InstanceID : {79B75FCF-5F1B-4A67-A6BF-83B21522CEF8} CommonName : PolicyKeywords : PolicyDecisionStrategy : 2 PolicyRoles : ConditionListType : 3 CreationClassName : MSFT|FW|FirewallRule|{79B75FCF-5F1B-4A67-A6BF-83B21522CEF8} ExecutionStrategy : 2 Mandatory : PolicyRuleName : Priority : RuleUsage : SequencedActions : 3 SystemCreationClassName : SystemName : DisplayGroup : ファイルとプリンターの共有 LocalOnlyMapping : False LooseSourceMapping : False Owner : Platforms : {} PolicyStoreSource : PersistentStore Profiles : 2 RuleGroup : @FirewallAPI.dll,-28502 StatusCode : 65536 PSComputerName : CimClass : root/standardcimv2:MSFT_NetFirewallRule CimInstanceProperties : {Caption, Description, ElementName, InstanceID...} CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties
もう少し見やすくしてみましょう。
Get-NetFirewallRule -DisplayName *SMB* ` | where {$_.Profile -match "Domain|Private"} ` | where Direction -eq Inbound ` | sort Direction ` | select Direction, Enabled, Profile, Action, DisplayName, Description, DisplayGroup, PrimaryStatus, PolicyStoreSourceType
どうでしょうか。
Direction : Inbound Enabled : False Profile : Domain Action : Allow DisplayName : ファイルとプリンターの共有 (SMB 受信) Description : サーバー メッセージ ブロックの名前付きパイプ経由の送受信を許可するためのファイルとプリンターの共有の受信規則です。 [TCP 445] DisplayGroup : ファイルとプリンターの共有 PrimaryStatus : OK PolicyStoreSourceType : Local Direction : Inbound Enabled : True Profile : Private Action : Allow DisplayName : ファイルとプリンターの共有 (SMB 受信) Description : サーバー メッセージ ブロックの名前付きパイプ経由の送受信を許可するためのファイルとプリンターの共有の受信規則です。 [TCP 445] DisplayGroup : ファイルとプリンターの共有 PrimaryStatus : OK PolicyStoreSourceType : Local
有効なものだけ取得したい
こんな感じでどうでしょう。
Get-NetFirewallRule -Enabled True
更にInboundなどなどに絞るならこうでしょうか。 -DisplayNameパラメータと、-Directionパラメーターは共存できないので注意です。
Get-NetFirewallRule -Enabled True -Direction Inbound ` | where {$_.Profile -match "Domain|Private"} ` | select Direction, Enabled, Profile, Action, DisplayName, Description, DisplayGroup, PrimaryStatus, PolicyStoreSourceType
リモート先から取得したい
もちろん可能です。PowerShell嘗めるな的な感じで楽勝です。 要は、Get-NetFirewallRuleはCIMのオーバーロードなので、CIMsessionを利用します。
ということは、認証をGet-Credentialで取得して、cimsessionを生成し、Get-NetFirewallRuleに渡せば完了です。
$cd = Get-Credential $cimsession = New-CimSession -ComputerName "Serverhogehoge" -Credential $cd Get-NetFirewallRule -CimSession $cimsession -Enabled True -Action Allow ` | where {$_.Profile -match "Domain|Private"} ` | sort Direction ` | select Direction, Enabled, Profile, Action, DisplayName, Description, DisplayGroup, PrimaryStatus, PolicyStoreSourceType
簡単ですね。