Delphi Developer Tip: Ultimate Way To Identify The Windows Firewall And Its Rules

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Developer Tip: Ultimate Way To Identify The Windows Firewall And Its Rules
By Anbarasan March 18, 2021

Sometimes Developers may need to identify the Firewall rules created in a windows machine from a Delphi App programmatically? Not sure how to do. Don’t worry. Для просмотра ссылки Войди или Зарегистрируйся System Information Management Suite’s helps to enumerate the available Firewall rules created for a profile, we will learn how to use the MiTeC_FW Component in this blog post.

Platforms: Windows.

Installation Steps:

You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.

  1. Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
  2. Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
  3. It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:

  • Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi27
  • Open the FW project in RAD studio 10.4.1 compile and Run the application.
  • This Demo App shows how to list down the Firewall rules created and enumerate among them.
Components used in MSIC FW Demo App:

  • TMiTeC_FW enumerates settings and rules from Windows Firewall. It also introduces methods for rules management.
  • TListView to show the Firewall settings and its rules properties.
  • TButton to save the listed Firewall settings to a .sif file and close the application.
Implementation Details:

  • An Instance is created FW of TMiTeC_FW. Loop through the FW RulesCount and add firewall rules item to the list view. List down the properties such as Name, Description, AppName, ServiceName, Protocol, LocalPorts, RemotePorts, Enabled, etc. of each TRule item to list view subitems.
  • Using AddRule, RemoveRule, EnableRule methods you can Add, Remove, Enable firewall rules respectively.
Код:
procedure TForm2.RefreshData;
var
  i: Integer;
  s: string;
begin
  Screen.Cursor:=crHourglass;
  try
    FW.RefreshData;
    cbxDomain.Checked:=FW.DomainProfile;
    cbxPublic.Checked:=FW.PublicProfile;
    cbxPrivate.Checked:=FW.PrivateProfile;
    List.Items.Clear;
    for i:=0 to FW.RuleCount-1 do
      with List.Items.Add do begin
        Caption:=FW.Rules[i].Name;
        SubItems.Add(FW.Rules[i].Description);
        SubItems.Add(FW.Rules[i].AppName);
        SubItems.Add(FW.Rules[i].ServiceName);
        case FW.Rules[i].Protocol of
          NET_FW_IP_PROTOCOL_TCP    :s:='TCP';
          NET_FW_IP_PROTOCOL_UDP    :s:='UDP';
          NET_FW_IP_PROTOCOL_ICMPv4 :s:='ICMPv4';
          NET_FW_IP_PROTOCOL_ICMPv6 :s:='ICMPv6';
          else s:=IntToStr(FW.Rules[i].Protocol);
        end;
        SubItems.Add(s);
        SubItems.Add(FW.Rules[i].LocalPorts);
        SubItems.Add(FW.Rules[i].RemotePorts);
        SubItems.Add(FW.Rules[i].LocalAddresses);
        SubItems.Add(FW.Rules[i].RemoteAddresses);
        SubItems.Add(FW.Rules[i].ICMP);
        case FW.Rules[i].Direction of
          NET_FW_RULE_DIR_IN : s:='In';
          NET_FW_RULE_DIR_OUT: s:='Out';
        end;
        SubItems.Add(s);
        SubItems.Add(BoolToStr(FW.Rules[i].Enabled,True));
        SubItems.Add(BoolToStr(FW.Rules[i].Edge,True));
        case FW.Rules[i].Action of
          NET_FW_ACTION_ALLOW: s:='Allow';
          NET_FW_ACTION_BLOCk: s:='Block';
        end;
        SubItems.Add(s);
        SubItems.Add(FW.Rules[i].Grouping);
        SubItems.Add(FW.Rules[i].IntfTypes);
      end;
  finally
    Screen.Cursor:=crDefault;
  end;
end;
  • Display the available Windows firewall rules as shown below.
1616169480316.png
MiTeC_FW Demo

It’s that simple to enumerate the windows firewall rules and list its rule properties for your application. Use this MiTeC component suite and get the job done quickly.