PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → [WD2024] - Reading in Windows Firewall Rules
[WD2024] - Reading in Windows Firewall Rules
Iniciado por Jos Pols, 03,mar. 2025 09:17 - 2 respuestas
Publicado el 03,marzo 2025 - 09:17
Hi All

I need to read in and display all the Windows Firewall Rules. I have this working as a simple Visual Foxpro 9 routine as follows:

* Create the firewall policy object
LOCAL loPolicy
loPolicy = CREATEOBJECT("HNetCfg.FwPolicy2")

* Get the collection of rules
LOCAL loRules
loRules = loPolicy.Rules

* Iterate through each rule
FOR EACH loRule IN loRules
* Display rule information
? "Rule Name: " + loRule.Name
ENDFOR

Simple as that. But I cannot seem to get this converted properly into WinDev. So far I have:

// Create the firewall policy object
FwPolicy is dynamic Automation object
FwPolicy = new object Automation "HNetCfg.FwPolicy2"

// Get the collection of rules
FwRules is dynamic Automation object
FwRules = FwPolicy>>Rules

At this point I dont know how to enumerate through the FwRules object to find each rule and its details. How do I enumerate the FwRules object and extract the name of each rule? Any suggestions?

TIA
Miembro registrado
27 mensajes
Publicado el 04,marzo 2025 - 02:15
Jos Pols wrote:
Hi All

I need to read in and display all the Windows Firewall Rules. I have this working as a simple Visual Foxpro 9 routine as follows:

* Create the firewall policy object
LOCAL loPolicy
loPolicy = CREATEOBJECT("HNetCfg.FwPolicy2")

* Get the collection of rules
LOCAL loRules
loRules = loPolicy.Rules

* Iterate through each rule
FOR EACH loRule IN loRules
* Display rule information
? "Rule Name: " + loRule.Name
ENDFOR

Simple as that. But I cannot seem to get this converted properly into WinDev. So far I have:

// Create the firewall policy object
FwPolicy is dynamic Automation object
FwPolicy = new object Automation "HNetCfg.FwPolicy2"

// Get the collection of rules
FwRules is dynamic Automation object
FwRules = FwPolicy>>Rules

At this point I dont know how to enumerate through the FwRules object to find each rule and its details. How do I enumerate the FwRules object and extract the name of each rule? Any suggestions?

TIA


Hi JP, with help from ChatGPT and the API documentation, this should do it.
I didn't work on parsing the array strings (yet).

sQuery is string = "SELECT * FROM MSFT_NetFirewallRule"
oWMI is dynamic automation object
oEnumerator is dynamic automation object
oFirewallRule is dynamic automation object
oService is dynamic automation object

// Connect to WMI
oWMI = new object automation "WbemScripting.SWbemLocator"
//oService is new object Automation
oService = oWMI>>ConnectServer(".", "root\StandardCimv2")

// Execute the Query
//oEnumerator is array of hUnicode strings
oEnumerator = oService>>ExecQuery(sQuery)

i is int

// Loop through all rules
// MSFT_NetFirewallRule API details here:
// Properties: [learn.microsoft.com]
// Parameter details: [learn.microsoft.com]

FOR i = 0 TO oEnumerator>>Count-1

oFirewallRule = oEnumerator>>ItemIndex(i)

Trace("Profiles: " + oFirewallRule>>Profiles)
Trace("LooseSourceMapping: " + oFirewallRule>>LooseSourceMapping)
Trace("LocalOnlyMapping: " + oFirewallRule>>LocalOnlyMapping)
Trace("RuleGroup: " + oFirewallRule>>RuleGroup)
Trace("DisplayName: " + oFirewallRule>>DisplayName)
Trace("DisplayGroup: " + oFirewallRule>>DisplayGroup)
Trace("EdgeTraversalPolicy: " + oFirewallRule>>EdgeTraversalPolicy)
//Trace("Platforms[]: " + oFirewallRule>>Platforms[?]) // string array
Trace("Direction: " + oFirewallRule>>Direction)
Trace("Action: " + oFirewallRule>>Action)
Trace("PrimaryStatus: " + oFirewallRule>>PrimaryStatus)
Trace("StatusCode: " + oFirewallRule>>StatusCode)
Trace("Status: " + oFirewallRule>>Status)
//Trace("EnforcementStatus[]: " + oFirewallRule>>EnforcementStatus[?]) // string array
Trace("PolicyStoreSourceType: " + oFirewallRule>>PolicyStoreSourceType)
Trace("PolicyStoreSource: " + oFirewallRule>>PolicyStoreSource)
Trace("Owner: " + oFirewallRule>>Owner)
Trace("SystemCreationClassName: " + oFirewallRule>>SystemCreationClassName)
Trace("SystemName: " + oFirewallRule>>SystemName)
Trace("CreationClassName: " + oFirewallRule>>CreationClassName)
Trace("PolicyRuleName: " + oFirewallRule>>PolicyRuleName)
Trace("ConditionListType: " + oFirewallRule>>ConditionListType)
Trace("RuleUsage: " + oFirewallRule>>RuleUsage)
Trace("Priority: " + oFirewallRule>>Priority)
Trace("Mandatory: " + oFirewallRule>>Mandatory)
Trace("SequencedActions: " + oFirewallRule>>SequencedActions)
Trace("ExecutionStrategy: " + oFirewallRule>>ExecutionStrategy)
Trace("PolicyDecisionStrategy: " + oFirewallRule>>PolicyDecisionStrategy)
//Trace("PolicyRoles[]: " + oFirewallRule>>PolicyRoles[?]) // string array, ignored
Trace("Enabled: " + oFirewallRule>>Enabled)
Trace("CommonName: " + oFirewallRule>>CommonName)
//Trace("PolicyKeywords[]: " + oFirewallRule>>PolicyKeywords[?])// string array, ignored
Trace("InstanceID: " + oFirewallRule>>InstanceID)
Trace("Caption: " + oFirewallRule>>Caption)
Trace("Description: " + oFirewallRule>>Description)
Trace("ElementName: " + oFirewallRule>>ElementName)

END
Publicado el 10,marzo 2025 - 21:35
Mike James wrote:
Jos Pols wrote:
Hi All

I need to read in and display all the Windows Firewall Rules. I have this working as a simple Visual Foxpro 9 routine as follows:

* Create the firewall policy object
LOCAL loPolicy
loPolicy = CREATEOBJECT("HNetCfg.FwPolicy2")

* Get the collection of rules
LOCAL loRules
loRules = loPolicy.Rules

* Iterate through each rule
FOR EACH loRule IN loRules
* Display rule information
? "Rule Name: " + loRule.Name
ENDFOR

Simple as that. But I cannot seem to get this converted properly into WinDev. So far I have:

// Create the firewall policy object
FwPolicy is dynamic Automation object
FwPolicy = new object Automation "HNetCfg.FwPolicy2"

// Get the collection of rules
FwRules is dynamic Automation object
FwRules = FwPolicy>>Rules

At this point I dont know how to enumerate through the FwRules object to find each rule and its details. How do I enumerate the FwRules object and extract the name of each rule? Any suggestions?

TIA

Hi JP, with help from ChatGPT and the API documentation, this should do it.
I didn't work on parsing the array strings (yet).

sQuery is string = "SELECT * FROM MSFT_NetFirewallRule"
oWMI is dynamic automation object
oEnumerator is dynamic automation object
oFirewallRule is dynamic automation object
oService is dynamic automation object

// Connect to WMI
oWMI = new object automation "WbemScripting.SWbemLocator"
//oService is new object Automation
oService = oWMI>>ConnectServer(".", "root\StandardCimv2")

// Execute the Query
//oEnumerator is array of hUnicode strings
oEnumerator = oService>>ExecQuery(sQuery)

i is int

// Loop through all rules
// MSFT_NetFirewallRule API details here:
// Properties: [learn.microsoft.com]
// Parameter details: [learn.microsoft.com]

FOR i = 0 TO oEnumerator>>Count-1

oFirewallRule = oEnumerator>>ItemIndex(i)

Trace("Profiles: " + oFirewallRule>>Profiles)
Trace("LooseSourceMapping: " + oFirewallRule>>LooseSourceMapping)
Trace("LocalOnlyMapping: " + oFirewallRule>>LocalOnlyMapping)
Trace("RuleGroup: " + oFirewallRule>>RuleGroup)
Trace("DisplayName: " + oFirewallRule>>DisplayName)
Trace("DisplayGroup: " + oFirewallRule>>DisplayGroup)
Trace("EdgeTraversalPolicy: " + oFirewallRule>>EdgeTraversalPolicy)
//Trace("Platforms[]: " + oFirewallRule>>Platforms[?]) // string array
Trace("Direction: " + oFirewallRule>>Direction)
Trace("Action: " + oFirewallRule>>Action)
Trace("PrimaryStatus: " + oFirewallRule>>PrimaryStatus)
Trace("StatusCode: " + oFirewallRule>>StatusCode)
Trace("Status: " + oFirewallRule>>Status)
//Trace("EnforcementStatus[]: " + oFirewallRule>>EnforcementStatus[?]) // string array
Trace("PolicyStoreSourceType: " + oFirewallRule>>PolicyStoreSourceType)
Trace("PolicyStoreSource: " + oFirewallRule>>PolicyStoreSource)
Trace("Owner: " + oFirewallRule>>Owner)
Trace("SystemCreationClassName: " + oFirewallRule>>SystemCreationClassName)
Trace("SystemName: " + oFirewallRule>>SystemName)
Trace("CreationClassName: " + oFirewallRule>>CreationClassName)
Trace("PolicyRuleName: " + oFirewallRule>>PolicyRuleName)
Trace("ConditionListType: " + oFirewallRule>>ConditionListType)
Trace("RuleUsage: " + oFirewallRule>>RuleUsage)
Trace("Priority: " + oFirewallRule>>Priority)
Trace("Mandatory: " + oFirewallRule>>Mandatory)
Trace("SequencedActions: " + oFirewallRule>>SequencedActions)
Trace("ExecutionStrategy: " + oFirewallRule>>ExecutionStrategy)
Trace("PolicyDecisionStrategy: " + oFirewallRule>>PolicyDecisionStrategy)
//Trace("PolicyRoles[]: " + oFirewallRule>>PolicyRoles[?]) // string array, ignored
Trace("Enabled: " + oFirewallRule>>Enabled)
Trace("CommonName: " + oFirewallRule>>CommonName)
//Trace("PolicyKeywords[]: " + oFirewallRule>>PolicyKeywords[?])// string array, ignored
Trace("InstanceID: " + oFirewallRule>>InstanceID)
Trace("Caption: " + oFirewallRule>>Caption)
Trace("Description: " + oFirewallRule>>Description)
Trace("ElementName: " + oFirewallRule>>ElementName)

END


/code

PROCEDURE GetFirewallRules()
// Initialize array to store firewall rules
arrRules is array of FirewallRule

// COM object for Windows Firewall
objFirewall is COM object = "HNetCfg.FwPolicy2"

// Check if COM object was created successfully
IF ErrorOccurred THEN
Error("Failed to create Windows Firewall COM object: " + ErrorInfo())
RETURN arrRules
END

// Get the current profile (domain, private, or public)
nCurrentProfiles is int = objFirewall.CurrentProfileTypes

// Get all firewall rules
objRules is COM Variant = objFirewall.Rules

// Get enumerator for rules collection
objEnum is COM Variant = objRules._NewEnum

// Variables for enumeration
vItem is Variant
vFlag is boolean

// Enumerate through all rules
WHILE objEnum.Next(1, &vItem, &vFlag) = 0
// Get rule object
objRule is COM Variant = vItem

// Create a structure to hold rule information
stRule is FirewallRule
stRule.Name = objRule.Name
stRule.Description = objRule.Description
stRule.ApplicationName = objRule.ApplicationName
stRule.ServiceName = objRule.ServiceName
stRule.Protocol = objRule.Protocol
stRule.LocalPorts = objRule.LocalPorts
stRule.RemotePorts = objRule.RemotePorts
stRule.LocalAddresses = objRule.LocalAddresses
stRule.RemoteAddresses = objRule.RemoteAddresses
stRule.Direction = (objRule.Direction = 1) ? "Inbound" ELSE "Outbound"
stRule.Action = (objRule.Action = 1) ? "Allow" ELSE "Block"
stRule.Enabled = objRule.Enabled
stRule.Profiles = GetProfileNames(objRule.Profiles)

// Add rule to array
Add(arrRules, stRule)
END

// Return the array of rules
RETURN arrRules
END

// Structure for firewall rule information
STRUCTURE FirewallRule
Name is string
Description is string
ApplicationName is string
ServiceName is string
Protocol is int
LocalPorts is string
RemotePorts is string
LocalAddresses is string
RemoteAddresses is string
Direction is string
Action is string
Enabled is boolean
Profiles is string
END

// Function to convert profile bitmask to readable names
FUNCTION GetProfileNames(nProfiles is int)
sProfiles is string = ""

IF (nProfiles & 1) <> 0 THEN
sProfiles += "Domain, "
END

IF (nProfiles & 2) <> 0 THEN
sProfiles += "Private, "
END

IF (nProfiles & 4) <> 0 THEN
sProfiles += "Public, "
END

// Remove trailing comma and space
IF Length(sProfiles) > 0 THEN
sProfiles = Left(sProfiles, Length(sProfiles) - 2)
END

RETURN sProfiles
END

// Convert protocol number to protocol name
FUNCTION GetProtocolName(nProtocol is int)
SWITCH nProtocol
CASE 1 : RETURN "ICMPv4"
CASE 6 : RETURN "TCP"
CASE 17 : RETURN "UDP"
CASE 58 : RETURN "ICMPv6"
OTHER CASE : RETURN nProtocol
END
END

// Display firewall rules in a table
PROCEDURE DisplayFirewallRules()
// Get the firewall rules
arrRules is array of FirewallRule = GetFirewallRules()

// Clear the table
TableDeleteAll(TABLE_Rules)

// Populate the table with rules
FOR EACH stRule OF arrRules
// Add a row to the table
TableAddLine(TABLE_Rules,
stRule.Name,
stRule.Enabled,
stRule.Direction,
stRule.Action,
GetProtocolName(stRule.Protocol),
stRule.LocalPorts,
stRule.RemotePorts,
stRule.ApplicationName,
stRule.Profiles,
stRule.Description)
END
END

// Example of creating a window to display firewall rules
PROCEDURE CreateFirewallRulesWindow()
// Create a new window
Window is Window
Window..Title = "Windows Firewall Rules"
Window..Width = 1200
Window..Height = 800

// Create a table in the window
TABLE_Rules is Table(Window)
TABLE_Rules..X = 10
TABLE_Rules..Y = 10
TABLE_Rules..Width = 1180
TABLE_Rules..Height = 700

// Define table columns
TableAddColumn(TABLE_Rules, "Name", 150)
TableAddColumn(TABLE_Rules, "Enabled", 60)
TableAddColumn(TABLE_Rules, "Direction", 80)
TableAddColumn(TABLE_Rules, "Action", 60)
TableAddColumn(TABLE_Rules, "Protocol", 70)
TableAddColumn(TABLE_Rules, "Local Ports", 100)
TableAddColumn(TABLE_Rules, "Remote Ports", 100)
TableAddColumn(TABLE_Rules, "Application", 200)
TableAddColumn(TABLE_Rules, "Profiles", 100)
TableAddColumn(TABLE_Rules, "Description", 250)

// Create a refresh button
BTN_Refresh is Button(Window)
BTN_Refresh..X = 550
BTN_Refresh..Y = 720
BTN_Refresh..Caption = "Refresh Rules"
BTN_Refresh..Width = 100
BTN_Refresh..Height = 30

// Set button click event
PROCEDURE BTN_Refresh.Click()
DisplayFirewallRules()
END

// Load the rules when the window opens
PROCEDURE Window.Load()
DisplayFirewallRules()
END

// Show the window
Window.Open()
END

/code