It can be done with Firewall interfaces
For example (must be Admin (requireAdministrator in Manifest) to update a rule) :
Dim hr As HRESULT = HRESULT.S_OK
Dim pNetFwRule As INetFwRule = CType(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")), INetFwRule)
hr = pNetFwRule.put_Action(NET_FW_ACTION.NET_FW_ACTION_ALLOW)
hr = pNetFwRule.put_Description("blah description")
hr = pNetFwRule.put_ApplicationName("C:\Program Files\blah\App.exe")
hr = pNetFwRule.put_Direction(NET_FW_RULE_DIRECTION.NET_FW_RULE_DIR_IN)
hr = pNetFwRule.put_Enabled(True)
hr = pNetFwRule.put_Name("blah")
Dim pNetFwPolicy2 As INetFwPolicy2 = CType(Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")), INetFwPolicy2)
Dim pNetFwRules As INetFwRules = Nothing
hr = pNetFwPolicy2.get_Rules(pNetFwRules)
Try
hr = pNetFwRules.Add(pNetFwRule)
Catch ex As Exception
MessageBox.Show(String.Format("Error : {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
with declarations :
Public Enum HRESULT As Integer
S_OK = 0
S_FALSE = 1
E_NOINTERFACE = &H80004002
E_NOTIMPL = &H80004001
E_FAIL = &H80004005
End Enum
Public Enum NET_FW_PROFILE_TYPE2 As Integer
NET_FW_PROFILE2_DOMAIN = &H1
NET_FW_PROFILE2_PRIVATE = &H2
NET_FW_PROFILE2_PUBLIC = &H4
NET_FW_PROFILE2_ALL = &H7FFFFFFF
End Enum
Public Enum NET_FW_IP_PROTOCOL As Integer
NET_FW_IP_PROTOCOL_TCP = 6
NET_FW_IP_PROTOCOL_UDP = 17
NET_FW_IP_PROTOCOL_ANY = 256
End Enum
Public Enum NET_FW_RULE_DIRECTION
NET_FW_RULE_DIR_IN = 1
NET_FW_RULE_DIR_OUT = NET_FW_RULE_DIR_IN + 1
NET_FW_RULE_DIR_MAX = NET_FW_RULE_DIR_OUT + 1
End Enum
Public Enum NET_FW_ACTION
NET_FW_ACTION_BLOCK = 0
NET_FW_ACTION_ALLOW = NET_FW_ACTION_BLOCK + 1
NET_FW_ACTION_MAX = NET_FW_ACTION_ALLOW + 1
End Enum
Public Enum NET_FW_MODIFY_STATE
NET_FW_MODIFY_STATE_OK = 0
NET_FW_MODIFY_STATE_GP_OVERRIDE = NET_FW_MODIFY_STATE_OK + 1
NET_FW_MODIFY_STATE_INBOUND_BLOCKED = NET_FW_MODIFY_STATE_GP_OVERRIDE + 1
End Enum
<ComImport> <Guid("9C4C6277-5027-441E-AFAE-CA1F542DA009")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface INetFwRules
#Region "<IDispatch>"
Function GetTypeInfoCount(<[Out]()> ByRef pctinfo As UInteger) As HRESULT
Function GetTypeInfo(iTInfo As UInteger, lcid As UInteger, <[Out]()> ByRef ppTInfo As IntPtr) As HRESULT
Function GetIDsOfNames(ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.LPArray)> rgszNames As String(),
<[In], MarshalAs(UnmanagedType.U4)> cNames As Integer,
<[In], MarshalAs(UnmanagedType.U4)> lcid As UInteger,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef rgDispId As Integer()) As HRESULT
Function Invoke(dispIdMember As Integer, ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.U4)> lcid As Integer,
<[In], MarshalAs(UnmanagedType.U4)> dwFlags As Integer,
<Out, [In]> ByRef pDispParams As System.Runtime.InteropServices.ComTypes.DISPPARAMS,
<Out> ByRef pVarResult As Object, <Out, [In]> ByRef pExcepInfo As System.Runtime.InteropServices.ComTypes.EXCEPINFO,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef pArgErr As IntPtr()) As HRESULT
#End Region
Function get_Count(<Out> ByRef count As Integer) As HRESULT
Function Add(rule As INetFwRule) As HRESULT
Function Remove(name As System.Text.StringBuilder) As HRESULT
Function Item(name As String, <Out> ByRef rule As INetFwRule) As HRESULT
'HRESULT get__NewEnum([MarshalAs(UnmanagedType.IUnknown)] out object newEnum);
Function get__NewEnum(<Out> ByRef newEnum As IntPtr) As HRESULT
End Interface
<ComImport> <Guid("8267BBE3-F890-491C-B7B6-2DB1EF0E5D2B")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface INetFwServiceRestriction
#Region "<IDispatch>"
Function GetTypeInfoCount(<[Out]()> ByRef pctinfo As UInteger) As HRESULT
Function GetTypeInfo(iTInfo As UInteger, lcid As UInteger, <[Out]()> ByRef ppTInfo As IntPtr) As HRESULT
Function GetIDsOfNames(ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.LPArray)> rgszNames As String(),
<[In], MarshalAs(UnmanagedType.U4)> cNames As Integer,
<[In], MarshalAs(UnmanagedType.U4)> lcid As UInteger,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef rgDispId As Integer()) As HRESULT
Function Invoke(dispIdMember As Integer, ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.U4)> lcid As Integer,
<[In], MarshalAs(UnmanagedType.U4)> dwFlags As Integer,
<Out, [In]> ByRef pDispParams As System.Runtime.InteropServices.ComTypes.DISPPARAMS,
<Out> ByRef pVarResult As Object, <Out, [In]> ByRef pExcepInfo As System.Runtime.InteropServices.ComTypes.EXCEPINFO,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef pArgErr As IntPtr()) As HRESULT
#End Region
Function RestrictService(serviceName As String, appName As String, <MarshalAs(UnmanagedType.VariantBool)> rs As Boolean, <MarshalAs(UnmanagedType.VariantBool)> serviceSidRestricted As Boolean) As HRESULT
Function ServiceRestricted(serviceName As String, appName As String, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef rs As Boolean) As HRESULT
Function get_Rules(<Out> ByRef rules As INetFwRules) As HRESULT
End Interface
<ComImport> <Guid("AF230D27-BABA-4E42-ACED-F524F22CFCE2")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface INetFwRule
#Region "<IDispatch>"
Function GetTypeInfoCount(<[Out]()> ByRef pctinfo As UInteger) As HRESULT
Function GetTypeInfo(iTInfo As UInteger, lcid As UInteger, <[Out]()> ByRef ppTInfo As IntPtr) As HRESULT
Function GetIDsOfNames(ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.LPArray)> rgszNames As String(),
<[In], MarshalAs(UnmanagedType.U4)> cNames As Integer,
<[In], MarshalAs(UnmanagedType.U4)> lcid As UInteger,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef rgDispId As Integer()) As HRESULT
Function Invoke(dispIdMember As Integer, ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.U4)> lcid As Integer,
<[In], MarshalAs(UnmanagedType.U4)> dwFlags As Integer,
<Out, [In]> ByRef pDispParams As System.Runtime.InteropServices.ComTypes.DISPPARAMS,
<Out> ByRef pVarResult As Object, <Out, [In]> ByRef pExcepInfo As System.Runtime.InteropServices.ComTypes.EXCEPINFO,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef pArgErr As IntPtr()) As HRESULT
#End Region
Function get_Name(<Out> ByRef name As String) As HRESULT
Function put_Name(name As String) As HRESULT
Function get_Description(<Out> ByRef desc As String) As HRESULT
Function put_Description(desc As String) As HRESULT
Function get_ApplicationName(<Out> ByRef imageFileName As String) As HRESULT
Function put_ApplicationName(imageFileName As String) As HRESULT
Function get_ServiceName(<Out> ByRef serviceName As String) As HRESULT
Function put_ServiceName(serviceName As String) As HRESULT
Function get_Protocol(<Out> ByRef protocol As NET_FW_IP_PROTOCOL) As HRESULT
Function put_Protocol(protocol As NET_FW_IP_PROTOCOL) As HRESULT
Function get_LocalPorts(<Out> ByRef portNumbers As String) As HRESULT
Function put_LocalPorts(portNumbers As String) As HRESULT
Function get_RemotePorts(<Out> ByRef portNumbers As String) As HRESULT
Function put_RemotePorts(portNumbers As String) As HRESULT
Function get_LocalAddresses(<Out> ByRef localAddrs As String) As HRESULT
Function put_LocalAddresses(localAddrs As String) As HRESULT
Function get_RemoteAddresses(<Out> ByRef remoteAddrs As String) As HRESULT
Function put_RemoteAddresses(remoteAddrs As String) As HRESULT
Function get_IcmpTypesAndCodes(<Out> ByRef icmpTypesAndCodes As String) As HRESULT
Function put_IcmpTypesAndCodes(icmpTypesAndCodes As String) As HRESULT
Function get_Direction(<Out> ByRef remoteAddrs As NET_FW_RULE_DIRECTION) As HRESULT
Function put_Direction(remoteAddrs As NET_FW_RULE_DIRECTION) As HRESULT
' VARIANT
Function get_Interfaces(<Out> ByRef interfaces As IntPtr) As HRESULT
Function put_Interfaces(interfaces As IntPtr) As HRESULT
Function get_InterfaceTypes(<Out> ByRef interfaceTypes As String) As HRESULT
Function put_InterfaceTypes(interfaceTypes As String) As HRESULT
Function get_Enabled(<Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef enabled As Boolean) As HRESULT
Function put_Enabled(<MarshalAs(UnmanagedType.VariantBool)> enabled As Boolean) As HRESULT
Function get_Grouping(<Out> ByRef context As String) As HRESULT
Function put_Grouping(context As String) As HRESULT
'HRESULT get_Grouping(StringBuilder context);
'HRESULT put_Grouping(StringBuilder context);
Function get_Profiles(<Out> ByRef profileTypesBitmask As NET_FW_PROFILE_TYPE2) As HRESULT
Function put_Profiles(profileTypesBitmask As NET_FW_PROFILE_TYPE2) As HRESULT
Function get_EdgeTraversal(<Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef enabled As Boolean) As HRESULT
Function put_EdgeTraversal(<MarshalAs(UnmanagedType.VariantBool)> enabled As Boolean) As HRESULT
Function get_Action(<Out> ByRef action As NET_FW_ACTION) As HRESULT
Function put_Action(action As NET_FW_ACTION) As HRESULT
End Interface
<ComImport> <Guid("98325047-C671-4174-8D81-DEFCD3F03186")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface INetFwPolicy2
#Region "<IDispatch>"
Function GetTypeInfoCount(<[Out]()> ByRef pctinfo As UInteger) As HRESULT
Function GetTypeInfo(iTInfo As UInteger, lcid As UInteger, <[Out]()> ByRef ppTInfo As IntPtr) As HRESULT
Function GetIDsOfNames(ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.LPArray)> rgszNames As String(),
<[In], MarshalAs(UnmanagedType.U4)> cNames As Integer,
<[In], MarshalAs(UnmanagedType.U4)> lcid As UInteger,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef rgDispId As Integer()) As HRESULT
Function Invoke(dispIdMember As Integer, ByRef riid As Guid, <[In], MarshalAs(UnmanagedType.U4)> lcid As Integer,
<[In], MarshalAs(UnmanagedType.U4)> dwFlags As Integer,
<Out, [In]> ByRef pDispParams As System.Runtime.InteropServices.ComTypes.DISPPARAMS,
<Out> ByRef pVarResult As Object, <Out, [In]> ByRef pExcepInfo As System.Runtime.InteropServices.ComTypes.EXCEPINFO,
<Out, MarshalAs(UnmanagedType.LPArray)> ByRef pArgErr As IntPtr()) As HRESULT
#End Region
Function get_CurrentProfileTypes(<Out> ByRef profileTypesBitmask As Integer) As HRESULT
Function get_FirewallEnabled(profileType As NET_FW_PROFILE_TYPE2, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef enabled As Boolean) As HRESULT
Function put_FirewallEnabled(profileType As NET_FW_PROFILE_TYPE2, <MarshalAs(UnmanagedType.VariantBool)> enabled As Boolean) As HRESULT
Function get_ExcludedInterfaces(profileType As NET_FW_PROFILE_TYPE2, <Out> ByRef interfaces As IntPtr) As HRESULT
Function put_ExcludedInterfaces(profileType As NET_FW_PROFILE_TYPE2, interfaces As IntPtr) As HRESULT
Function get_BlockAllInboundTraffic(profileType As NET_FW_PROFILE_TYPE2, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef Block As Boolean) As HRESULT
Function put_BlockAllInboundTraffic(profileType As NET_FW_PROFILE_TYPE2, <MarshalAs(UnmanagedType.VariantBool)> Block As Boolean) As HRESULT
Function get_NotificationsDisabled(profileType As NET_FW_PROFILE_TYPE2, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef disabled As Boolean) As HRESULT
Function put_NotificationsDisabled(profileType As NET_FW_PROFILE_TYPE2, <MarshalAs(UnmanagedType.VariantBool)> disabled As Boolean) As HRESULT
Function get_UnicastResponsesToMulticastBroadcastDisabled(profileType As NET_FW_PROFILE_TYPE2, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef disabled As Boolean) As HRESULT
Function put_UnicastResponsesToMulticastBroadcastDisabled(profileType As NET_FW_PROFILE_TYPE2, <MarshalAs(UnmanagedType.VariantBool)> disabled As Boolean) As HRESULT
Function get_Rules(<Out> ByRef rules As INetFwRules) As HRESULT
Function get_ServiceRestriction(<Out> ByRef ServiceRestriction As INetFwServiceRestriction) As HRESULT
Function EnableRuleGroup(profileTypesBitmask As Integer, group As String, <MarshalAs(UnmanagedType.VariantBool)> enable As Boolean) As HRESULT
Function IsRuleGroupEnabled(profileTypesBitmask As Integer, group As String, <Out> <MarshalAs(UnmanagedType.VariantBool)> ByRef enabled As Boolean) As HRESULT
Function RestoreLocalFirewallDefaults() As HRESULT
Function get_DefaultInboundAction(profileType As NET_FW_PROFILE_TYPE2, <Out> ByRef action As NET_FW_ACTION) As HRESULT
Function put_DefaultInboundAction(profileType As NET_FW_PROFILE_TYPE2, action As NET_FW_ACTION) As HRESULT
Function get_DefaultOutboundAction(profileType As NET_FW_PROFILE_TYPE2, <Out> ByRef action As NET_FW_ACTION) As HRESULT
Function put_DefaultOutboundAction(profileType As NET_FW_PROFILE_TYPE2, action As NET_FW_ACTION) As HRESULT
Function get_IsRuleGroupCurrentlyEnabled(group As String, <MarshalAs(UnmanagedType.VariantBool)> enabled As Boolean) As HRESULT
Function get_LocalPolicyModifyState(<Out> ByRef modifyState As NET_FW_MODIFY_STATE) As HRESULT
End Interface