Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

Managing Firewall Rules via INetFwPolicy2

$
0
0
Below provided several examples how to work with INetFwPolicy2 interface to setup Windows Firewall for your application.

You'll have to connect type library via Project - References - NetFW.TLB

Code:


Option Explicit

Private Sub Form_Load()
    'Adding program to list of allowed applications (inbound & outbound)
    Firewall_AddAllowedProgram "app", "c:\path\app.exe"

    'Checking if program is allowed
    Debug.Print Firewall_IsAllowedProgram("c:\path\app.exe")

    'Disabling firewall rule by program path
    Firewall_DisableProgram "c:\path\app.exe"

    'Removing firewall rule by name
    Firewall_RemoveRuleName "app"
End Sub

Public Function Firewall_AddAllowedProgram(RuleName As String, ProgramPath As String) As Boolean

    On Error GoTo ErrH

    Dim pFwNetFwPolicy2 As New NetFwPolicy2
    Dim pFwRules As INetFwRules
    Dim pFwRule As NetFwRule

    Set pFwRules = pFwNetFwPolicy2.Rules

    Firewall_RemoveRuleName RuleName
    Firewall_DisableProgram ProgramPath

    Set pFwRule = New NetFwRule
    With pFwRule
        .Action = NET_FW_ACTION_ALLOW
        .ApplicationName = ProgramPath
        .Direction = NET_FW_RULE_DIR_OUT
        .Enabled = True
        .InterfaceTypes = "All"
        .LocalAddresses = "*"
        .Name = RuleName
        .Profiles = NET_FW_PROFILE2_ALL
        .Protocol = NET_FW_IP_PROTOCOL_ANY
        .RemoteAddresses = "*"
    End With

    pFwRules.Add pFwRule

    Set pFwRule = New NetFwRule
    With pFwRule
        .Action = NET_FW_ACTION_ALLOW
        .ApplicationName = ProgramPath
        .Direction = NET_FW_RULE_DIR_IN
        .Enabled = True
        .InterfaceTypes = "All"
        .LocalAddresses = "*"
        .Name = RuleName
        .Profiles = NET_FW_PROFILE2_ALL
        .Protocol = NET_FW_IP_PROTOCOL_ANY
        .RemoteAddresses = "*"
    End With

    pFwRules.Add pFwRule
    Set pFwNetFwPolicy2 = Nothing

    Firewall_AddAllowedProgram = True
    Exit Function
ErrH:
    Debug.Print "ERROR: in " & "Firewall_AddAllowedProgram" & ". Err # " & Err.Number & " (" & Err.LastDllError & ") - " & Err.Description
End Function

Public Function Firewall_RemoveRuleName(RuleName As String) As Boolean

    On Error GoTo ErrH

    Dim pFwNetFwPolicy2 As New NetFwPolicy2
    Dim pFwRules As INetFwRules

    Set pFwRules = pFwNetFwPolicy2.Rules
    pFwRules.Remove RuleName
    pFwRules.Remove RuleName

    Set pFwNetFwPolicy2 = Nothing

    Firewall_RemoveRuleName = True
    Exit Function
ErrH:
    Debug.Print "ERROR: in " & "Firewall_RemoveRuleName" & ". Err # " & Err.Number & " (" & Err.LastDllError & ") - " & Err.Description
End Function

Public Function Firewall_DisableProgram(sPath As String) As Boolean

    On Error GoTo ErrH

    Dim pFwNetFwPolicy2 As New NetFwPolicy2
    Dim pFwRules As INetFwRules
    Dim pFwRule As NetFwRule

    Set pFwRules = pFwNetFwPolicy2.Rules
    For Each pFwRule In pFwRules
        With pFwRule
            If StrComp(.ApplicationName, sPath, 1) = 0 Then
                .Enabled = False
            End If
        End With
    Next

    Set pFwNetFwPolicy2 = Nothing

    Firewall_DisableProgram = True
    Exit Function
ErrH:
    Debug.Print "ERROR: in " & "Firewall_DisableProgram" & ". Err # " & Err.Number & " (" & Err.LastDllError & ") - " & Err.Description
End Function

Public Function Firewall_IsAllowedProgram(sPath As String) As Boolean

    On Error GoTo ErrH

    Dim pFwNetFwPolicy2 As New NetFwPolicy2
    Dim pFwRules As INetFwRules
    Dim pFwRule As NetFwRule

    Set pFwRules = pFwNetFwPolicy2.Rules
    For Each pFwRule In pFwRules
        With pFwRule
            If StrComp(.ApplicationName, sPath, 1) = 0 Then
                If .Enabled And .Action = NET_FW_ACTION_ALLOW Then
                    Firewall_IsAllowedProgram = True
                    Exit For
                End If
            End If
        End With
    Next

    Set pFwNetFwPolicy2 = Nothing
    Exit Function
ErrH:
    Debug.Print "ERROR: in " & "Firewall_IsAllowedProgram" & ". Err # " & Err.Number & " (" & Err.LastDllError & ") - " & Err.Description
End Function

Attached Files

Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>