Results 1 to 7 of 7

Thread: Managing Firewall Rules via INetFwPolicy2

  1. #1

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    671

    Managing Firewall Rules via INetFwPolicy2

    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 Attached Files
    Malware analyst, VirusNet developer, HiJackThis Fork author || my CodeBank works

  2. #2
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,650

    Re: Managing Firewall Rules via INetFwPolicy2

    HOW TO LIST ALL Firewall EXE LIST?OR NAME LIST?
    EXE1 NAME1
    EXE2 NAME2

  3. #3

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    671

    Re: Managing Firewall Rules via INetFwPolicy2

    Code:
    
    Private Sub Form_Load()
        'Lists all firewall rules, excepting disabled 
        Firewall_ListRules
    End Sub
    
    Public Sub Firewall_ListRules()
    
        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
            If pFwRule.Enabled Then
                Debug.Print _
                    IIf(pFwRule.Action = NET_FW_ACTION_ALLOW, "[Allow]", "[Block]") & " " & _
                    IIf(pFwRule.Direction = NET_FW_RULE_DIR_IN, "[in] ", "[out]") & " " & _
                    pFwRule.ApplicationName & " (" & pFwRule.Name & ")"
            End If
        Next
    
        Set pFwNetFwPolicy2 = Nothing
        Exit Sub
    ErrH:
        Debug.Print "ERROR: in " & "Firewall_ListRules" & ". Err # " & Err.Number & " (" & Err.LastDllError & ") - " & Err.Description
    End Sub
    
    Be carefull, system rule names are localized.
    Malware analyst, VirusNet developer, HiJackThis Fork author || my CodeBank works

  4. #4
    Frenzied Member
    Join Date
    Jan 2020
    Posts
    1,650

    Re: Managing Firewall Rules via INetFwPolicy2

    thank you very much

  5. #5
    New Member
    Join Date
    Jun 2017
    Posts
    7

    Re: Managing Firewall Rules via INetFwPolicy2

    I would like to list the actual port and protocol exceptions for my program that are allowed in the firewall.

    Can this be done?

  6. #6

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    671

    Re: Managing Firewall Rules via INetFwPolicy2

    cidtek, use the code in the post #3.

    And filter what you need by checking .ApplicationName property.

    You can find other properties via IntelliSense or F2 of NetFwRule object.

    Port and protocol are saved in properties:
    - pFwRule.Protocol
    - pFwRule.RemotePorts
    - pFwRule.LocalPorts
    Malware analyst, VirusNet developer, HiJackThis Fork author || my CodeBank works

  7. #7
    New Member
    Join Date
    Jun 2017
    Posts
    7

    Re: Managing Firewall Rules via INetFwPolicy2

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width