Programmatically add an IP address for a Server 2008 firewall server

Does anyone know how to programmatically add an IP address to the Windows Server 2008 Advanced Security Firewall?

i.e. I set up a Block Action firewall rule in which there are some IP addresses listed in the "Remote IP Address" section of the area. I want to be able to programmatically add (or possibly delete) IP addresses from this list. Are there .NET objects for this?

+8
windows-server-2008 firewall
source share
4 answers

The Windows Advanced Firewall start page can be found at:

http://msdn.microsoft.com/en-us/library/ff956124(v=VS.85).aspx

In particular, it seems you need the INetFwRule interface, which is described at:

http://msdn.microsoft.com/en-us/library/aa365344(v=VS.85).aspx

Check the get_RemoteAddresses and put_RemoteAddresses parameters

+11
source share

You can also try netsh .
I used it once to change the MTU of my interface

+8
source share
+6
source share

I just did this job in vb.NET. Add the link "c: \ windows \ system32 \ firewallapi.dll"

Create a class called Firewall - for example:

Imports NetFwTypeLib Imports System.Net Public Class Firewall Implements IDisposable Private _policy As INetFwPolicy2 = Nothing Private ReadOnly Property Policy As INetFwPolicy2 Get If _policy Is Nothing Then _policy = DirectCast(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")), INetFwPolicy2) End If Return _policy End Get End Property Public Sub Add(ipAddress As IPAddress, ruleName As String) Dim firewallRule As NetFwTypeLib.INetFwRule = Policy.Rules.Item(ruleName) Dim NewAddress As String = ipAddress.ToString & "/255.255.255.255" If Not firewallRule.RemoteAddresses.Contains(NewAddress) Then firewallRule.RemoteAddresses += "," & NewAddress End If End Sub Public Sub Remove(ipAddress As IPAddress, ruleName As String) Dim firewallRule As NetFwTypeLib.INetFwRule = Policy.Rules.Item(ruleName) Dim NewAddress As String = ipAddress.ToString & "/255.255.255.255" If firewallRule.RemoteAddresses.Contains(NewAddress) Then Dim ipList As String = firewallRule.RemoteAddresses ipList = ipList.Replace(NewAddress, "") ipList = ipList.Replace(",,", ",") firewallRule.RemoteAddresses = ipList End If End Sub Public Function Exists(ipAddress As IPAddress, ruleName As String) As Boolean Dim firewallRule As NetFwTypeLib.INetFwRule = Policy.Rules.Item(ruleName) Dim NewAddress As String = ipAddress.ToString & "/255.255.255.255" If firewallRule.RemoteAddresses.Contains(NewAddress) Then Return True Else Return False End If End Function Private disposedValue As Boolean Protected Overridable Sub Dispose(disposing As Boolean) If Not Me.disposedValue Then If disposing Then End If If Not _policy Is Nothing Then _policy = Nothing End If End If Me.disposedValue = True End Sub Public Sub Dispose() Implements IDisposable.Dispose Dispose(True) GC.SuppressFinalize(Me) End Sub End Class 
+4
source share

Source: https://habr.com/ru/post/649924/


All Articles