A method for automatically adding firewall policies to a program
By creating firewall policy management objects and utilizing robotic process automation, the problem of automatically adding firewall policies by programs in the Windows operating system is solved, automated configuration is achieved, the operation process is simplified, and efficiency and convenience are improved.
Patent Information
- Application Number
- CN202310724464.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-16
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2043-06-16
AI Technical Summary
Existing programs cannot automatically add firewall policies in the Windows operating system, causing users to manually configure them, which increases the complexity and time-consuming operation. Especially in program development and network management work, it affects the promotion and deployment of software.
By creating firewall policy management objects, traverse existing firewall programs, determine and configure firewall policies that meet permission requirements, and realize automated configuration, including creating, deleting, and modifying firewall rules, and using robotic process automation and system assemblies for operations.
It realizes that there is no need for users to manually configure firewall policies in a firewall-protected system environment. The program can run normally, saving time and effort. It is easy to deploy and promote, and supports batch configuration and management.
Smart Images

Figure CN116846753B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of firewall configuration, and in particular to a method for automatically adding firewall policies to a program. Background Art
[0002] With the rapid development of integrated circuits, computers and servers are becoming more and more popular in work and life. As the main operating system for computers, Windows has become the most common operating system for people. However, along with the upgrade of Windows system, its integrated Defender firewall has become the main protection software for computers and server devices. Windows Defender Firewall with Advanced Security provides host-based two-way traffic filtering to block unauthorized traffic in and out of local devices.
[0003] In daily use, after installing a new application, the user needs to manually configure the firewall policy of the application to allow the application to communicate over the network, which increases the complexity and inconvenience of user operations. In program development work or network room management work that requires a large number of high-frequency program installations and management, a large amount of firewall configuration work is extremely time-consuming and labor-intensive. In the promotion and application of software, the additional operations of firewall configuration will also have an adverse effect on the software's installation volume. However, existing applications do not have a good method for automatically adding firewall policies. Summary of the invention
[0004] In order to overcome the problem that existing programs cannot automatically add firewall policies on Windows operating systems, the present invention provides a method for automatically adding firewall policies to a program.
[0005] The present invention provides a method for automatically adding firewall policies to a program, which is used to automatically configure firewall policies for programs with firewall permission requirements. The specific implementation steps include:
[0006] When the program is started, a firewall policy management object is created and started;
[0007] Check existing firewall programs to see if they contain the firewall policy of the program;
[0008] If not, create a new firewall policy for the program and add it to the firewall program;
[0009] If so, determine whether the existing firewall policy of the program meets the permission requirements of the program;
[0010] If not, delete the existing firewall policy of the program, create a new firewall policy for the program based on the permission requirements, and add it to the firewall program;
[0011] If so, execute the procedure.
[0012] Preferably, when the program is started, a firewall policy management object is created and started, specifically:
[0013] When the program is started, the system environment in which the program is running is detected, and the corresponding system program set is selected according to the system environment;
[0014] Create and start the firewall policy management object by calling the system assembly.
[0015] Preferably, the traversing existing firewall programs to determine whether the program is included is specifically:
[0016] Identify whether the firewall policy management object is running with administrator privileges;
[0017] If not, restart the firewall policy management object with administrator authority;
[0018] If so, check whether the existing firewall programs include the program.
[0019] Preferably, the newly created firewall policy of the program and adding it to the firewall program is specifically:
[0020] Through the robotic process automation program, the creation and saving of the firewall policy of the program are completed.
[0021] Preferably, the creation and saving of the firewall policy of the program are completed by a robotic process automation program, and the specific implementation steps are:
[0022] Open the firewall settings option;
[0023] Configure firewall rules for said program;
[0024] Configure the network that is allowed to be accessed according to the permission requirements of the program;
[0025] Save the firewall rules.
[0026] The beneficial effects of the present invention are:
[0027] By creating a firewall policy management object, you can automatically add Windows firewall policies, allowing the program to run normally in a firewall-protected system environment without the need for users to manually configure firewall policies, saving time and effort and facilitating deployment and promotion. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] The present invention will be further described below in conjunction with the accompanying drawings, wherein:
[0029] Figure 1 The present invention is a flowchart of a method according to one embodiment of the present invention. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0031] See also Figure 1 As one of the embodiments of the present invention, a method for automatically adding a firewall policy to a program is disclosed, which is used to configure the firewall permissions of the software in the Windows Defender firewall software when installing the software (hereinafter referred to as the program) in the Windows 10 system, and the implementation steps include:
[0032] S1. When the program is started, a firewall policy management object is created and started; wherein the firewall policy management object is used to execute the following steps S2 to S5;
[0033] S2, traversing the existing firewall programs to see whether they contain the firewall policy of the program;
[0034] S3. If not, create a new firewall policy for the program and add it to the firewall program, and execute step S6;
[0035] S4. If yes, determine whether the existing firewall policy of the program meets the permission requirements of the program;
[0036] S5. If not, delete the existing firewall policy of the program, create a new firewall policy for the program based on the permission requirements and add it to the firewall program, and execute step S6;
[0037] S6. If yes, execute the program.
[0038] Among them, the traversal of step S2 mainly traverses the two lists of "inbound rules" and "outbound rules" in the Windows Defender firewall.
[0039] This embodiment automatically adds Windows firewall policies by creating a firewall policy management object, so that the program can run normally in a system environment protected by a firewall without the need for users to manually configure firewall policies, saving time and effort and facilitating deployment and promotion.
[0040] In some embodiments, the batch configuration and management of the firewall policies of multiple devices on a local area network or a network can also be achieved by using a GPO (Group Policy Object) tool.
[0041] As another specific embodiment of the above embodiment, some specific procedures and program annotations of this embodiment are as follows:
[0042] Step S1:
[0043] / / Project start
[0044] protected override void OnStartup(StartupEventArgs e)
[0045] {
[0046] / / Start the program to determine whether to restart
[0047] System.Threading.Mutex mutex=new System.Threading.Mutex(true,Assembly.GetExecutingAssembly().GetName().Name);
[0048] If(!mutey waitOne(0,false))
[0049] / / Run firewall policy
[0050] RunFireWallTactics();
[0051] base.OnStartup(e);
[0052] / / Application.Current.MainWindow.Activate();
[0053] / / Application.Current.MainWindow.Show();
[0054] }
[0055] Step S2 of this embodiment is implemented as follows:
[0056] / / / <summary>
[0057] / / / Firewall policy
[0058] / / / < / summary>
[0059] private void RunFireWallTactics()
[0060] {
[0061] / / Thread
[0062] Task.Factory.StartNew(()=>
[0063] {
[0064] / / Firewall enabled status
[0065] if(Firewall.FirewallManager.FirewallEnabled)
[0066] {
[0067] try
[0068] {
[0069] / / Find the firewall rules for a specific program
[0070] var appRule=Firewall.FirewallManager.GetFindFirewallRule(System.Windows.Forms.Application.ExecutablePath);
[0071] / / Find the firewall rules for a specific program
[0072] if(appRule==null||appRule.Length==0)
[0073] {
[0074] if(!IsAdministrator)
[0075] / / Run the administrator firewall tool
[0076] AppAdminRunFirewallTool();
[0077] }
[0078] else
[0079] {
[0080] bool isRunTool = true;
[0081] / / Loop through the policies in the Windows Firewall Management object to determine whether there is a firewall policy with the name of this program
[0082] foreach(var item in appRule)
[0083] {
[0084] if(item.Profiles==(int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL
[0085] &&item.ApplicationName.Leneth==System.Windows.Forms.Application.ExecutablePath.Length)
[0086] {
[0087] isRunTool = false;
[0088] break;
[0089] }
[0090] }
[0091] If(isRunTool)
[0092] AppAdminRunFirewallTool();
[0093] }
[0094] }
[0095] catch(Exception ex)
[0096] {
[0097] LogHelper.Error("There was a problem running the firewall policy", ex);
[0098] }
[0099] }
[0100] });
[0101] }
[0102] In step S2 of this embodiment, the specific implementation steps are as follows:
[0103] S21, identifying whether the firewall policy management object is running with administrator privileges;
[0104] S22. If not, restart the firewall policy management object with administrator authority;
[0105] S23: If yes, check whether the program is included in the existing firewall program.
[0106] The implementation procedure of step S21 is as follows:
[0107] / / / <summary>
[0108] / / / Run the administrator firewall tool
[0109] / / / < / summary>
[0110] private void AppAdminRunFirewallTool()
[0111] {
[0112] ProcessStartInfo startInfo=new ProcessStartInfo();
[0113] startInfo.FileName=Path.Combine(System.I0.Directory.GetCurrentDirectory(),"DAP.Firewall.exe");
[0114] startInfo.Verb = "runas";
[0115] try
[0116] {
[0117] Process.Start(startInfo);
[0118] }
[0119] catch(Exception ex)
[0120] {
[0121] LogHelper.Error("Administrator operation problem",ex);
[0122] }
[0123] }
[0124] The implementation procedure of step S22 is as follows:
[0125] Protected override void OnStartup(StartupEventArgs e)
[0126] {
[0127] string path=System.I0.Path.Combine(System.I0.Directory.GetCurrentDirect ory(),"DAP.Client.exe");
[0128] / / Remove program through firewall
[0129] FirewallManager.RemoveAppUseFirewall(path);
[0130] / / Allow the program to pass through the firewall
[0131] FirewallManager.AppUseFirewall(path,"Allow programs to pass rules");
[0132] base.OnStartup(e);
[0133] System.Environment.Exit(0);
[0134] }
[0135] In step S5, the existing firewall policy of the program is deleted, and the specific implementation procedure is as follows:
[0136] / / / <summary>
[0137] / / / Remove the application through the firewall
[0138] / / / < / summary>
[0139] / / / <param name="appPath"> The absolute path to the application
[0140] public static void RemoveAppUseFirewall(string appPath)
[0141] {
[0142] / / The parameter is the absolute path of the program
[0143] string name=DesignAppRuleName(appPath);
[0144] INetFwRule rule=FirewallPolicy.Rules.OfType <inetfwrule>().FirstOrDefault(r=>r.Name==name);
[0145] if (rule!= null)
[0146] {
[0147] FirewallPolicy.Rules.Remove(rule.Name);
[0148] }
[0149] }
[0150] In step S5, according to the permission requirements, a new firewall policy for the program is created and added to the firewall program. The program of this embodiment needs to obtain all firewall communication authorization permissions. The specific implementation procedure is as follows:
[0151] / / / <summary>
[0152] / / / Allow the application to pass through the firewall
[0153] / / / < / summary>
[0154] / / / <param name="appPath"> The absolute path to the application
[0155] / / / exception cref="FileNotFoundException">Program file not found < / exception)
[0156] public static void AppUseFirewall(string appPath,string Description)
[0157] {
[0158] If(System.I0.File.Exists(appPath)==false)
[0159] {
[0160] throw new System.I0.FileNotFoundException("Program file not found");
[0161] }
[0162] INetFwRule firewallRule=FwRule;
[0163] firewallRule.Action=NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
[0164] / / Allow incoming traffic
[0165] firewallRule.ApplicationName=appPath; / / Application path and file name
[0166] firewallRule.Description=Description; / / description of the rule
[0167] firewallRule.Enabled = true; / / Enable the rule
[0168] firewallRule.InterfaceTypes = "All"; / / Allow all types of network interfaces
[0169] firewallRule.Name = DesignAppRuleName (appPath); / / Rule name
[0170] firewallRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL; / / Restriction rules
[0171] firewallRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW-IP_PROTOCOL_ANY; / / Allow all protocols
[0172] firewallRule.RemoteAddresses = "*"; / / Allow all remote addresses
[0173] INetFwPolicy2 firewallPolicy=FirewallPolicy;
[0174] firewallPolicy.Rules.Add(firewallRule);
[0175] }
[0176] This embodiment calls the system assembly, uses the Process class to create a new process and sets the FileName of the process to netsh.exe, and calls the Windows firewall type library (NetFwTypeLib) through C# to achieve the function of automatically adding firewall policies.
[0177] The above program is only the main program part for implementing this embodiment, which is used to illustrate the execution logic of this embodiment, and is not the entire program language.
[0178] As another embodiment of the present solution, in a system environment where it is difficult to call some system program sets, or where a bug that is difficult to resolve is found during the calling process, the present embodiment simulates manual operations through robotic process automation (RPA) to complete batch configuration of firewall protocols. The implementation steps are as follows:
[0179] A1. Open the firewall settings option: In Windows, open the firewall settings through the "Windows Defender Firewall" option in the Control Panel; or search and open the "Windows Security Center" option in the Windows start menu, and select the "Firewall and Network Protection" option to open the firewall settings;
[0180] A2. Configure firewall rules for programs: In the firewall settings, select the "Advanced Settings" option. In the pop-up "Advanced Security" window, select the "Inbound Rules" or "Outbound Rules" option according to the type of firewall rules to be created, and then select the "New Rules" option. Follow the prompts to select the type of rule to be configured, including "Program", "Port", "Predefined" and other options. In the "Program" rule, you need to specify the program path to be allowed to access the network;
[0181] A3. Configure the network that is allowed to be accessed according to the permission requirements of the program: In the firewall rules, you need to specify the network that is allowed to be accessed, including three types of networks: "domain", "private" and "public". Select the type of network that needs to be allowed to be accessed according to the actual situation;
[0182] A4. Save firewall rules: In the pop-up "New Rule" dialog box, fill in the rule name, confirm the rule settings, and select the "Finish" button to save the rule settings.
[0183] In some other embodiments, robotic process automation can also be used to complete the operations of any step from step S1 to step S5 of this solution, achieving the same configuration effect as calling a system assembly to complete the configuration of the firewall policy.< / inetfwrule>
Claims
1. A method for automatically adding firewall policies to a program, which is used to automatically configure firewall policies for programs that have firewall permission requirements. It is characterized in that include: When the program is started, the system environment in which the program is running is detected, and a corresponding system program set is selected according to the system environment; Create and start the firewall policy management object by calling the system assembly; The firewall policy management object performs the following steps after startup: Check whether existing firewall programs contain the firewall policy of the program; If not, create a new firewall policy for the program and add it to the firewall program so that the program can run normally in the system environment protected by the firewall; If so, determine whether the existing firewall policy of the program meets the permission requirements of the program; If not, delete the existing firewall policy of the program, create a new firewall policy for the program based on the permission requirements and add it to the firewall program, so that the program can run normally in the system environment protected by the firewall; If so, execute the procedure.
2. A method for automatically adding a firewall policy to a program according to claim 1, It is characterized in that The traversal of the existing firewall program to determine whether it contains the firewall policy of the program is specifically: Identify whether the firewall policy management object is running with administrator privileges; If not, restart the firewall policy management object with administrator authority; If so, check whether the existing firewall programs include the firewall policy of the program.
3. A method for automatically adding a firewall policy to a program according to claim 1, It is characterized in that The newly created firewall policy of the program and added to the firewall program is specifically as follows: Through the robotic process automation program, the creation and saving of the firewall policy of the program are completed.
4. A method for automatically adding a firewall policy to a program according to claim 3, It is characterized in that The steps of completing the creation and saving of the firewall policy of the program through the robot process automation program are as follows: Open the firewall settings option; Configure firewall rules for said program; Configure the network that is allowed to be accessed according to the permission requirements of the program; Save the firewall rules.
