Firewall policy configuration method, electronic equipment, storage medium and program product
By obtaining and analyzing the detection information of Docker containers and updating the firewall policies in real-time, the problems of port change monitoring and security risks in Linux firewall settings are solved, dynamic protection and security management of Docker container ports are realized, and the security of the system is improved. efficiency.
Patent Information
- Application Number
- CN202411402461.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-09
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-10-09
AI Technical Summary
When the existing technology is used to achieve cross-network isolation and communication based on Docker containers in Linux firewall settings, the default opening of all network ports poses a major security risk, making it difficult to quickly monitor and update port changes in real time, resulting in unrestricted container access or applications. The risk of exploitation increases.
By obtaining the detection information of the target container, including the target container identification and port information, the port change information is determined using the pre-stored container mapping list, and the protection policy is updated based on the change information, and the firewall is automatically configured to monitor and manage Docker container ports in real time to achieve dynamic Protection.
It improves the security of Docker containers, can detect port abnormalities in a timely manner, automatically updates firewall policies, improves the real-time and availability of firewalls, reduces the need for manual operations, and enhances the security and efficiency of the system.
Smart Images

Figure CN119094236A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network security technology, and in particular to a configuration method, electronic device, storage medium and program product of a firewall policy. Background Art
[0002] At present, servers usually use the Linux operating system, and Docker is a common virtualization container built on Linux. As an emerging virtualization method, Docker has great advantages over traditional virtual machines, such as more efficient resource utilization, faster startup time, consistent operating environment, continuous payment and deployment, easier migration, easier maintenance and expansion, etc. The existing technology for the Linux firewall setting is based on Docker containers to achieve cross-network isolation and communication. By default, the ports exposed by the container are open to all networks. However, here all exposed ports are opened by default, which poses a great security risk to Docker, which may cause unrestricted container access or the exploitation of application vulnerabilities, etc. Therefore, it is necessary to manage these ports through firewalls. How to quickly monitor changes in Docker ports and update the firewall in real time is an urgent problem to be solved. Summary of the invention
[0003] Some embodiments of the present application aim to provide a method for configuring a firewall policy, an electronic device, a storage medium and a program product. Through the technical solution of the embodiments of the present application, by acquiring detection information of a target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier; determining preset port information corresponding to the target container identifier according to a pre-stored container mapping list; wherein the pre-stored container mapping list at least includes a correspondence between a container identifier and port information; determining port change information according to the target port information and the preset port information; determining a protection policy corresponding to the target container according to the port change information, and configuring according to the protection policy, the embodiments of the present application acquire the detection information of the target container, and determine the corresponding preset interface information according to the pre-stored container mapping list, and then analyze in real time whether the port of the target container changes. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to timely detect containers with abnormal ports and timely update the firewall.
[0004] In a first aspect, some embodiments of the present application provide a method for configuring a firewall policy, including:
[0005] Acquire detection information of the target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier;
[0006] Determine the preset port information corresponding to the target container identifier according to a pre-stored container mapping list; wherein the pre-stored container mapping list at least includes a correspondence between the container identifier and the port information;
[0007] Determining port change information according to the target port information and the preset port information;
[0008] A protection strategy corresponding to the target container is determined according to the port change information, and configuration is performed according to the protection strategy.
[0009] Some embodiments of the present application obtain detection information of the target container, and determine the corresponding preset interface information according to a pre-stored container mapping list, and then analyze in real time whether the port of the target container has changed. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to promptly detect containers with abnormal ports and update the firewall in a timely manner.
[0010] Optionally, the container mapping list is obtained in the following manner:
[0011] Obtain mapping information of a container identifier and port information, where the mapping information is used to map a log directory of the container to a specified path;
[0012] storing mapping information between the container identifier and the port information in a first database;
[0013] According to the mapping information, obtain the log directory of the container;
[0014] Determine, according to the log directory of the container, a log file corresponding to the container;
[0015] Parsing the log file to obtain keyword information, and storing the keyword information in the first database;
[0016] The container mapping list is determined according to the correspondence between the container identifier, the port information, the log file and the keyword information in the first database.
[0017] Some embodiments of the present application analyze the logs of the Docker container, record the IP addresses of abnormal access, etc., analyze the abnormal IPs on the host machine, record them after deduplication, add the recorded information to the DOCKER-USER chain, store the historical log analysis results, and obtain the container mapping list.
[0018] Optionally, the obtaining of mapping information of the container identifier and the port information, where the mapping information is used to map the log directory of the container to a specified path, includes:
[0019] Start the Docker container;
[0020] Use the -v parameter to map the container's log directory.
[0021] Some embodiments of the present application start a container through Docker, map a log directory to the container, and record the container ID and the corresponding port in a database.
[0022] Optionally, determining the log file corresponding to the container according to the log directory of the container includes:
[0023] According to the log directory of the container, multiple pieces of data from the same client source address within a preset time are obtained;
[0024] Performing deduplication processing on the multiple pieces of data to obtain processed data;
[0025] A log file corresponding to the container is obtained according to the processed data.
[0026] Some embodiments of the present application reduce the occupied storage space by retaining only the last item in each group of data among the marked multiple groups of data and deleting the remaining data with the same IP in the same group.
[0027] Optionally, parsing the log file to obtain keyword information and storing the keyword information in the first database includes:
[0028] Parsing the log file, and acquiring keyword information corresponding to the keyword identifier according to the keyword identifier, wherein the keyword identifier at least includes client information, and the client information at least includes a client identifier and a client source address;
[0029] Determine, according to the keyword information, error log information corresponding to the keyword information;
[0030] The time of the log file, the keyword information and the error log information are stored in the first database.
[0031] Some embodiments of the present application can automatically analyze Docker container logs in real time. By analyzing the special fields that appear frequently in the logs, the IP addresses of abnormal accesses and the like are extracted and added to the protection strategy of the firewall, thereby achieving the purpose of dynamically protecting Docker containers by analyzing the logs, thereby improving the security of Docker containers, and the firewall strategy does not need to be manually executed, thereby improving efficiency and real-time performance.
[0032] Optionally, determining a protection strategy corresponding to the target container according to the port change information, and configuring according to the protection strategy includes:
[0033] According to the port change information, obtaining client information in the first database;
[0034] According to the target port information and the client information, a protection policy corresponding to the target container is determined through an iptables command, and configuration is performed according to the protection policy.
[0035] Some embodiments of the present application can timely update the modified port to the firewall policy by monitoring the changes of the Docker port, thereby improving the availability of the script.
[0036] Optionally, the method further comprises:
[0037] The original protection strategy is deleted according to the port change information.
[0038] Some embodiments of the present application avoid the complexity of policies by timely deleting abandoned policies in the DOCKER-USER chain, thereby facilitating subsequent management.
[0039] In a second aspect, some embodiments of the present application provide a firewall policy configuration device, including:
[0040] An acquisition module, configured to acquire detection information of a target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier;
[0041] A search module, used to determine the preset port information corresponding to the target container identifier according to a pre-stored container mapping list; wherein the pre-stored container mapping list at least includes a correspondence between the container identifier and the port information;
[0042] A determination module, used to determine port change information according to the target port information and the preset port information;
[0043] A configuration module is used to determine a protection strategy corresponding to the target container according to the port change information, and perform configuration according to the protection strategy.
[0044] Some embodiments of the present application obtain detection information of the target container, and determine the corresponding preset interface information according to a pre-stored container mapping list, and then analyze in real time whether the port of the target container has changed. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to promptly detect containers with abnormal ports and update the firewall in a timely manner.
[0045] Optionally, the device further comprises an establishing module, wherein the establishing module is configured to:
[0046] Obtain mapping information of a container identifier and port information, where the mapping information is used to map a log directory of the container to a specified path;
[0047] storing mapping information between the container identifier and the port information in a first database;
[0048] According to the mapping information, obtain the log directory of the container;
[0049] Determine, according to the log directory of the container, a log file corresponding to the container;
[0050] Parsing the log file to obtain keyword information, and storing the keyword information in the first database;
[0051] The container mapping list is determined according to the correspondence between the container identifier, the port information, the log file and the keyword information in the first database.
[0052] Some embodiments of the present application analyze the logs of the Docker container, record the IP addresses of abnormal access, etc., analyze the abnormal IPs on the host machine, record them after deduplication, add the recorded information to the DOCKER-USER chain, store the historical log analysis results, and obtain the container mapping list.
[0053] Optionally, the establishing module is used to:
[0054] Start the Docker container;
[0055] Use the -v parameter to map the container's log directory.
[0056] Some embodiments of the present application start a container through Docker, map a log directory to the container, and record the container ID and the corresponding port in a database.
[0057] Optionally, the establishing module is used to:
[0058] According to the log directory of the container, multiple pieces of data from the same client source address within a preset time are obtained;
[0059] Performing deduplication processing on the multiple pieces of data to obtain processed data;
[0060] A log file corresponding to the container is obtained according to the processed data.
[0061] Some embodiments of the present application reduce the occupied storage space by retaining only the last item in each group of data among the marked multiple groups of data and deleting the remaining data with the same IP in the same group.
[0062] Optionally, the establishing module is used to:
[0063] Parsing the log file, and acquiring keyword information corresponding to the keyword identifier according to the keyword identifier, wherein the keyword identifier at least includes client information, and the client information at least includes a client identifier and a client source address;
[0064] Determine, according to the keyword information, error log information corresponding to the keyword information;
[0065] The time of the log file, the keyword information and the error log information are stored in the first database.
[0066] Some embodiments of the present application can automatically analyze Docker container logs in real time. By analyzing the special fields that appear frequently in the logs, the IP addresses of abnormal accesses and the like are extracted and added to the protection strategy of the firewall, thereby achieving the purpose of dynamically protecting Docker containers by analyzing the logs, thereby improving the security of Docker containers, and the firewall strategy does not need to be manually executed, thereby improving efficiency and real-time performance.
[0067] Optionally, the configuration module is used to:
[0068] According to the port change information, obtaining client information in the first database;
[0069] According to the target port information and the client information, a protection policy corresponding to the target container is determined through an iptables command, and configuration is performed according to the protection policy.
[0070] Some embodiments of the present application can timely update the modified port to the firewall policy by monitoring the changes of the Docker port, thereby improving the availability of the script.
[0071] Optionally, the configuration module is used to:
[0072] The original protection strategy is deleted according to the port change information.
[0073] Some embodiments of the present application avoid the complexity of policies by timely deleting abandoned policies in the DOCKER-USER chain, thereby facilitating subsequent management.
[0074] In a third aspect, some embodiments of the present application provide an electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein when the processor executes the program, the method for configuring the firewall policy as described in any embodiment of the first aspect can be implemented.
[0075] In a fourth aspect, some embodiments of the present application provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, can implement the method for configuring a firewall policy as described in any embodiment of the first aspect.
[0076] In a fifth aspect, some embodiments of the present application provide a computer program product, comprising a computer program, wherein the computer program, when executed by a processor, can implement the method for configuring the firewall policy as described in any embodiment of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0077] In order to more clearly illustrate the technical solutions of some embodiments of the present application, the drawings required for use in some embodiments of the present application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0078] Figure 1 A flowchart of a method for configuring a firewall policy provided in an embodiment of the present application;
[0079] Figure 2 A flowchart of another method for configuring a firewall policy provided in an embodiment of the present application;
[0080] Figure 3 A schematic diagram of the structure of a firewall policy configuration device provided in an embodiment of the present application;
[0081] Figure 4 A schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0082] The technical solutions in some embodiments of the present application will be described below in conjunction with the drawings in some embodiments of the present application.
[0083] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. At the same time, in the description of this application, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0084] At present, servers usually use the Linux operating system, and Docker is a common virtualization container built on Linux. As an emerging virtualization method, Docker has great advantages over traditional virtual machines, such as more efficient resource utilization, faster startup time, consistent operating environment, continuous payment and deployment, easier migration, easier maintenance and expansion, etc. The prior art setting of Linux firewalls is based on Docker containers to achieve cross-network isolation and communication. By default, the ports exposed by the container are open to all networks. However, all exposed ports are opened by default, which poses a great security risk to Docker, and may cause unrestricted container access or exploitation of application vulnerabilities, etc. Therefore, it is necessary to manage these ports through a firewall. How to quickly monitor changes in Docker ports and update the firewall in real time is an urgent problem to be solved. In view of this, some embodiments of the present application provide a method for configuring a firewall policy, which includes obtaining detection information of a target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier; according to a pre-stored container mapping list, determining preset port information corresponding to the target container identifier; wherein , the pre-stored container mapping list at least includes the correspondence between the container identifier and the port information; the port change information is determined according to the target port information and the preset port information; the protection strategy corresponding to the target container is determined according to the port change information, and configured according to the protection strategy. The embodiment of the present application obtains the detection information of the target container, and determines the corresponding preset interface information according to the pre-stored container mapping list, and then analyzes in real time whether the port of the target container has changed. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to promptly detect containers with abnormal ports and promptly update the firewall.
[0085] like Figure 1 As shown, an embodiment of the present application provides a method for configuring a firewall policy, the method comprising:
[0086] S101. Acquire detection information of a target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier;
[0087] Specifically, multiple Docker containers are installed on the terminal device. Docker is an open source platform for developing, delivering and running applications. It can run on Windows, macOS, and Linux computers, and package an application and its dependencies into a container. These containers can run in any environment that supports Docker. The containers are isolated from each other, but can transmit information to each other through specific channels. Docker provides a lightweight virtualization solution. Since all containers running on the same computer share the same operating system kernel, the overhead of starting and maintaining virtual machines is avoided. Therefore, compared with traditional virtual machines, Docker containers are lighter, faster, and easier to deploy. Using Docker, you can avoid the problem of inconsistent running effects on two computers. The container provides consistent behavior, so that the application has the same running effect on computers in different environments.
[0088] The terminal device obtains detection information of the target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier. Exemplarily, the terminal device obtains container 1 and port number 1 of container 1.
[0089] S102. Determine preset port information corresponding to the target container identifier according to a pre-stored container mapping list; wherein the pre-stored container mapping list at least includes a correspondence between the container identifier and the port information;
[0090] Specifically, a container mapping list is pre-stored on the terminal device, and the container mapping list includes a container identifier and port information corresponding to the container identifier. After the detection information of the target container is obtained, the preset port information corresponding to the target container identifier is found through the container mapping list.
[0091] S103, determining port change information according to the target port information and the preset port information;
[0092] The terminal device compares the target port information with the preset port information. If the target port information and the preset port information are the same, it means that the port information of the target container has not changed. If the target port information and the preset port information are different, it means that the port information of the target container has changed.
[0093] S104: Determine a protection strategy corresponding to the target container according to the port change information, and configure according to the protection strategy.
[0094] Specifically, when the port changes, the target port information and the source address information of the client related to the target container are passed into the iptables command according to the changed port information, that is, the target port information of the target container, and a rule is added to the DOCKER-USER chain to prohibit the specified source address IP from accessing the specified port, obtain the protection policy corresponding to the target container, and configure it according to the protection policy.
[0095] Some embodiments of the present application obtain detection information of the target container, and determine the corresponding preset interface information according to a pre-stored container mapping list, and then analyze in real time whether the port of the target container has changed. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to promptly detect containers with abnormal ports and update the firewall in a timely manner.
[0096] Another embodiment of the present application further supplements the configuration method of the firewall policy provided in the above embodiment.
[0097] Optionally, the container mapping list is obtained as follows:
[0098] Get the mapping information of container ID and port information. The mapping information is used to map the log directory of the container to the specified path.
[0099] storing mapping information between the container identifier and the port information in a first database;
[0100] Get the log directory of the container based on the mapping information;
[0101] According to the log directory of the container, determine the log file corresponding to the container;
[0102] Parsing the log file to obtain keyword information, and storing the keyword information in the first database;
[0103] A container mapping list is determined according to the correspondence between the container identifier, the port information, the log file, and the keyword information in the first database.
[0104] Some embodiments of the present application analyze the logs of the Docker container, record the IP addresses of abnormal access, etc., analyze the abnormal IPs on the host machine, record them after deduplication, add the recorded information to the DOCKER-USER chain, store the historical log analysis results, and obtain the container mapping list.
[0105] Optionally, obtain mapping information of the container identifier and port information, where the mapping information is used to map the log directory of the container to a specified path, including:
[0106] Start the Docker container;
[0107] Use the -v parameter to map the container's log directory.
[0108] Some embodiments of the present application start a container through Docker, map a log directory to the container, and record the container ID and the corresponding port in a database.
[0109] Optionally, according to the log directory of the container, a log file corresponding to the container is determined, including:
[0110] According to the log directory of the container, multiple data from the same client source address within a preset time are obtained;
[0111] De-duplicate multiple pieces of data to obtain processed data;
[0112] According to the processed data, a log file corresponding to the container is obtained.
[0113] Some embodiments of the present application reduce the occupied storage space by retaining only the last item in each group of data among the marked multiple groups of data and deleting the remaining data with the same IP in the same group.
[0114] Optionally, parsing the log file to obtain keyword information and storing the keyword information in the first database includes:
[0115] Parse the log file, and obtain keyword information corresponding to the keyword identifier according to the keyword identifier, wherein the keyword identifier at least includes client information, and the client information at least includes a client identifier and a client source address;
[0116] According to the keyword information, determining the error log information corresponding to the keyword information;
[0117] The time, keyword information and error log information of the log file are stored in the first database.
[0118] Some embodiments of the present application can automatically analyze Docker container logs in real time. By analyzing the special fields that appear frequently in the logs, the IP addresses of abnormal accesses and the like are extracted and added to the protection strategy of the firewall, thereby achieving the purpose of dynamically protecting Docker containers by analyzing the logs, thereby improving the security of Docker containers, and the firewall strategy does not need to be manually executed, thereby improving efficiency and real-time performance.
[0119] Optionally, according to the port change information, a protection strategy corresponding to the target container is determined, and configuration is performed according to the protection strategy, including:
[0120] According to the port change information, obtain the client information in the first database;
[0121] According to the target port information and client information, use the iptables command to determine the protection policy corresponding to the target container and configure it according to the protection policy.
[0122] Some embodiments of the present application can timely update the modified port to the firewall policy by monitoring the changes of the Docker port, thereby improving the availability of the script.
[0123] Optionally, the method further comprises:
[0124] Delete the original protection policy based on the port change information.
[0125] Some embodiments of the present application avoid the complexity of policies by timely deleting abandoned policies in the DOCKER-USER chain, thereby facilitating subsequent management.
[0126] like Figure 2 As shown, the embodiment of the present application provides a configuration method for automatically adding a firewall policy to a Docker container, including:
[0127] (1) Start the container through Docker, map the log directory to the container, and record the container ID and the corresponding port to database B (the first database);
[0128] Create a log directory in the host machine. Multiple containers can be installed on the host machine, that is, the terminal device. Each container can be associated with a client terminal. Each container has its own log file to record the access records of the client terminal, and each container is mapped to a specified path of the host machine. In this way, the host machine can find the log file of the corresponding container through the specified path in the log directory.
[0129] Specifically, the terminal device starts the container and uses the -v parameter to implement directory mapping; the corresponding relationship between the container ID (container identifier) and the port is recorded in the database B.
[0130] (2) Check whether the DockerChain service is started. If it does not exist or is not started, proceed to step (7);
[0131] Run the systemctl command to check whether the system service DockerChain is started. If it does not exist, proceed to step (3).
[0132] If the DockerChain service is started, proceed to step (7).
[0133] (3) Obtain the log file of the container, extract information based on keywords, and record it in database B;
[0134] Specifically, the host reads the log files mapped by each container at a time; locates keywords (Failed password, Access denied, Fail Login, etc.) from the log files;
[0135] Extract keyword information according to the keyword, wherein the keyword information at least includes client information, and the client information includes information such as the client's IP address and user name, and record the log time, the client's IP address, user and error log information into database B.
[0136] (4) Analyze the log files and save the deduplicated records to database B;
[0137] Check the information in database B and mark multiple pieces of data with the same source IP from the same client terminal within a certain period of time;
[0138] Among the marked multiple groups of data, only the last one in each group is retained, and the rest of the data with the same IP in the same group is deleted. You can also select any one of the multiple data as the target data.
[0139] (5) Based on the content in database B, use the iptables command to add firewall rules to the DOCKER-USER chain of docker, and record the rules (iptables commands) in database X;
[0140] According to the content in database B, the port and IP (client) are extracted as the destination port and source address parameters;
[0141] Pass the above parameters into the iptables command, add rules on the DOCKER-USER chain, and prohibit the specified source address IP from accessing the specified port;
[0142] The iptables commands executed above are recorded in database X;
[0143] (6) Write the content in database X into a shell script and register it to the system service (next time it will start automatically to start the protection function), bind it to the docker service, and when the docker service is restarted, the DockerChain service will start automatically;
[0144] Write the iptables commands in database X into a shell script and run it;
[0145] Register the shell script file to the system service DockerChain;
[0146] Bind DockerChain to the Docker service. When you restart the Docker service, the DockerChain service will start automatically.
[0147] (7) When monitoring whether the port of the container has changed, obtain the current mapping list of the docker container port (i.e., the container port and the host port mapping list), and compare it with the port in the database X (i.e., the pre-stored container mapping list, that is, the port of the container in the last executed iptables) to see if there is any change. If there is no change, execute step (3); if there is a change, execute step (8);
[0148] The command is used to obtain the mapping list of the current Docker container port, and the host obtains the detection information of the target container in the mapping list, including the target container identifier and the target port information;
[0149] According to the container ID (i.e., the target container identifier), compare the port in the database X with the port in the current mapping list to see if there is any change. If there is no change, execute step (3); if there is a change, execute step (8).
[0150] (8) According to the container ID, the content of the port in the mapping list with changes is updated to the database X, and the items before the change are written into the database TEMP (temporary database);
[0151] Update the changed port to database X;
[0152] Write the original data of the modified port in database X to database TEMP.
[0153] (9) Process the content in the database TEMP, write the processed content into a shell script, and delete the iptables port rules before the change;
[0154] The contents in the database TEMP are added after the iptables command -D to delete the specified rules on the DOCKER-USER chain;
[0155] Write the iptables commands in the database TEMP into a shell script;
[0156] Execute the shell script to delete the iptables port rules before the change.
[0157] (10) Use the iptables command to add the updated rules to the DOCKER-USER chain and execute step (3);
[0158] Write the contents of database X into a shell script and execute it; proceed to step (3);
[0159] Here, database X is the database X with updated port information after the port is changed;
[0160] Through the above steps, Docker container logs can be dynamically analyzed, container port changes can be monitored, and Docker firewall policies can be automatically added to improve efficiency and accuracy.
[0161] This example describes a configuration method and system for automatically adding firewall policies to a Docker container proposed by the present invention, which improves efficiency and accuracy by analyzing container logs in real time, detecting changes in container ports, and automatically adding firewall detection logs.
[0162] Exemplarily, the method for configuring a firewall policy provided in an embodiment of the present application includes:
[0163] (Take CentOS as an example):
[0164] 1) Start the container through Docker, map the log directory to the container, and record the container ID and the corresponding port to database B.
[0165] Use the following command to create a log directory:
[0166] mkdir logs
[0167] Start the container and use the -v parameter to implement directory mapping. The following / var / logs is the absolute path of the directory created in the previous step, / var / log is the name of the log directory in the centos7 container, and centos7 is the image name:
[0168] docker run–d–v / var / logs: / var / mysql / log centos7
[0169] Use the docker ps command to view the correspondence between the container ID and the port mapping, and record the results in database B;
[0170] Check whether the DockerChain service is started. If the service does not exist, proceed to step 3. If the service is started, proceed to step 7.
[0171] Run the systemctl status DockerChain command to check whether the DockerChain service is started. If the result contains running, it is started and you can go to step 7. If the result contains not be found, it means the service does not exist and you can go to step 3.
[0172] Get the log file, extract information based on keywords, and record it in database B.
[0173] Read the log file content on the host machine and filter the lines containing the keyword from the log file. The following example is a log sample containing the keyword Failed password.
[0174] Aug 21 02:53:12bogon sshd
[4817] :pam_unix(sshd:session):session opened for user root by(uid=0)
[0175] Aug 21 02:54:08bogon sshd
[4817] :pam_unix(sshd:session):session closed for user root
[0176] Aug 21 02:54:12bogon sshd
[4872] :Failed password for rootfrom10.65.37.21port 55167ssh2
[0177] Aug 21 02:54:13bogon sshd
[4872] :Failed password for rootfrom10.65.37.21port 55168ssh2
[0178] Aug 21 02:54:14bogon sshd
[4872] :Failed password for rootfrom10.65.37.21port 55169ssh2
[0179] Aug 21 02:54:14bogon sshd
[4872] :Failed password for rootfrom10.65.37.21port 55170ssh2
[0180] Aug 21 02:54:15bogon sshd
[4872] :Failed password for rootfrom10.65.37.21port 55171ssh2
[0181] There are 5 lines in the above sample that contain the Failed password field. The time Aug 21 02:54:12, IP address 10.65.37.21, username root, and error message Failed password forroot from 10.65.37.21port 55167ssh2 in the above log samples are extracted respectively. Several fields of the 5 pieces of information extracted above are recorded in database B. Database B is as described in Table 1:
[0182] Table 1
[0183]
[0184] 2) Analyze the log file, remove duplicates and record them in database B.
[0185] Check the information in database B and mark multiple data with the same source IP within a short period of time;
[0186] Among the marked multiple groups of data, only the last one in each group is retained, and the rest of the data with the same IP in the same group is deleted;
[0187] As shown in Table 2, for a set of data whose source IP is 10.65.37.21, only the last one is retained and the first four pieces of data are deleted. The result after deletion is shown in Table 2, the database after deduplication;
[0188] Table 2
[0189]
[0190] 3) Based on the content in database B, use the iptables command to add firewall rules on the DOCKER-USER chain and record the rules in database X.
[0191] According to database B, refer to Table 3, extract the port and IP, and pass them as parameters to the iptables command to generate the following command:
[0192] iptables-IDOCKER-USER-p tcp-s 10.65.37.21--dport 22-j DROP
[0193] The generated command is recorded in database X, as shown in Table 3:
[0194] Table 3
[0195]
[0196] 4) Write the content in database X into a shell script, register it to the system service, bind it to the docker service, and when you restart docker, the DockerChain service starts automatically.
[0197] Write the iptables commands in database X into a shell script and run it. The content of the shell script is as follows:
[0198] #! / bin / sh
[0199] iptables-IDOCKER-USER-p tcp-s 10.65.37.21--dport 22-j DROP
[0200] Save the above script as DockerChain.sh, grant execution permissions to the file, and run the script:
[0201] chmod+x DockerChain.sh
[0202] sh DockerChain.sh
[0203] Register the DockerChain service as a system service and set it to automatically run with the Docker service:
[0204] systemctl start DockerChain
[0205] systemctl enabled DockerChain
[0206] Get the mapping list of the current Docker container port and compare it with the port in database X to see if there is any change. If there is no change, execute step 3; if there is a change, execute step 8.
[0207] Get the current docker port mapping list through the command. Table 4 shows the docker container port mapping list. You can use the docker ps--format "{{.ID}}:{{.Ports}}" command to get the information:
[0208] Table 4
[0209]
[0210] Compare the corresponding container ID and the port after -> with the corresponding container ID and port in database X to check whether there is any change. The port of the container ID "b326de3af6ae" in the second row of Table 4 has changed from 22 to 8022. Since there is a change, execute step 8;
[0211] According to the container ID, the changed content of the port in the mapping list is updated to the database X, and the items before the change are written into the database TEMP.
[0212] According to the container ID, the changed data is updated to database X. Here, 8022 is updated to the 22nd position of the iptables content of database X with ID "b326de3af6ae". The content of database X after the update is shown in Table 5. Table 5 shows the result of database X after changing the port:
[0213] Table 5
[0214]
[0215] The original data of the modified port in database X is written into database TEMP. Here, the row with ID b326de3af6ae is written. The content of database TEMP is shown in Table 6:
[0216] Table 6
[0217]
[0218] Process the content in the database TEMP, write the processed content into a shell script, and delete the iptables port rules before the change.
[0219] Process the contents in the database TEMP and replace -I after iptables with -D to delete the specified rules on the DOCKER-USER chain, as shown in Table 7 below;
[0220] Table 7
[0221]
[0222] Write the iptables command of database TEMP into the shell script DelOldChain.sh. The content is as follows
[0223] #! / bin / sh
[0224] iptables -D DOCKER-USER-p tcp-s 10.65.37.21--dport 22-j DROP
[0225] Execute the shell script to delete the port rules before the change:
[0226] chmod+x DelOldChain.sh
[0227] sh DelOldChain.sh
[0228] Use the iptables command to add the updated rule to the DOCKER-USER chain, and then proceed to step 3.
[0229] Write the contents of database X into the shell script DockerChain and execute it. The contents of the script are as follows:
[0230] #! / bin / sh
[0231] iptables-IDOCKER-USER-p tcp-s 10.65.37.21--dport 8022-j DROP
[0232] Execute the DockerChain script;
[0233] sh DockerChain.sh
[0234] Go to step 3.
[0235] The embodiment of the present application analyzes the docker container log in real time, parses the abnormal access IP address, and automatically adds the firewall policy without manual execution, thereby improving efficiency and real-time performance; when the port of the docker container changes, it can be automatically updated to the firewall policy to improve the availability of the script; and the abandoned policies in the DOCKER-USER chain can be deleted in time to avoid the complexity of the policy.
[0236] It should be noted that each implementable method in this embodiment may be implemented separately, or may be implemented in combination in any combination without conflict, and this application is not limited thereto.
[0237] Another embodiment of the present application provides a firewall policy configuration device, which is used to execute the firewall policy configuration method provided in the above embodiment.
[0238] like Figure 3 , which is a schematic diagram of the structure of the firewall policy configuration device provided in the embodiment of the present application. The firewall policy configuration device includes an acquisition module 301, a search module 302, a determination module 303 and a configuration module 304, wherein:
[0239] The acquisition module 301 is used to acquire detection information of a target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier;
[0240] The search module 302 is used to determine the preset port information corresponding to the target container identifier according to the pre-stored container mapping list; wherein the pre-stored container mapping list at least includes the correspondence between the container identifier and the port information;
[0241] The determination module 303 is used to determine the port change information according to the target port information and the preset port information;
[0242] The configuration module 304 is used to determine the protection strategy corresponding to the target container according to the port change information, and perform configuration according to the protection strategy.
[0243] Regarding the device in this embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0244] Some embodiments of the present application obtain detection information of the target container, and determine the corresponding preset interface information according to a pre-stored container mapping list, and then analyze in real time whether the port of the target container has changed. When the port of the Docker container changes, the detection information of the target container can be automatically updated to the firewall policy, thereby improving the availability of the script, and being able to promptly detect containers with abnormal ports and update the firewall in a timely manner.
[0245] Another embodiment of the present application further supplements the configuration device of the firewall policy provided in the above embodiment.
[0246] Optionally, the device further comprises an establishing module, the establishing module being configured to:
[0247] Get the mapping information of container ID and port information. The mapping information is used to map the log directory of the container to the specified path.
[0248] storing mapping information between the container identifier and the port information in a first database;
[0249] Get the log directory of the container based on the mapping information;
[0250] According to the log directory of the container, determine the log file corresponding to the container;
[0251] Parsing the log file to obtain keyword information, and storing the keyword information in the first database;
[0252] A container mapping list is determined according to the correspondence between the container identifier, the port information, the log file, and the keyword information in the first database.
[0253] Some embodiments of the present application analyze the logs of the Docker container, record the IP addresses of abnormal access, etc., analyze the abnormal IPs on the host machine, record them after deduplication, add the recorded information to the DOCKER-USER chain, store the historical log analysis results, and obtain the container mapping list.
[0254] Optionally, build modules for:
[0255] Start the Docker container;
[0256] Use the -v parameter to map the container's log directory.
[0257] Some embodiments of the present application start a container through Docker, map a log directory to the container, and record the container ID and the corresponding port in a database.
[0258] Optionally, build modules for:
[0259] According to the log directory of the container, multiple data from the same client source address within a preset time are obtained;
[0260] De-duplicate multiple pieces of data to obtain processed data;
[0261] According to the processed data, a log file corresponding to the container is obtained.
[0262] Some embodiments of the present application reduce the occupied storage space by retaining only the last item in each group of data among the marked multiple groups of data and deleting the remaining data with the same IP in the same group.
[0263] Optionally, build modules for:
[0264] Parse the log file, and obtain keyword information corresponding to the keyword identifier according to the keyword identifier, wherein the keyword identifier at least includes client information, and the client information at least includes a client identifier and a client source address;
[0265] According to the keyword information, determining the error log information corresponding to the keyword information;
[0266] The time, keyword information and error log information of the log file are stored in the first database.
[0267] Some embodiments of the present application can automatically analyze Docker container logs in real time. By analyzing the special fields that appear frequently in the logs, the IP addresses of abnormal accesses and the like are extracted and added to the protection strategy of the firewall, thereby achieving the purpose of dynamically protecting Docker containers by analyzing the logs, thereby improving the security of Docker containers, and the firewall strategy does not need to be manually executed, thereby improving efficiency and real-time performance.
[0268] Optionally, a configuration module is used to:
[0269] According to the port change information, obtain the client information in the first database;
[0270] According to the target port information and client information, use the iptables command to determine the protection policy corresponding to the target container and configure it according to the protection policy.
[0271] Some embodiments of the present application can timely update the modified port to the firewall policy by monitoring the changes of the Docker port, thereby improving the availability of the script.
[0272] Optionally, the configuration module is used to:
[0273] Delete the original protection policy based on the port change information.
[0274] Some embodiments of the present application avoid the complexity of policies by timely deleting abandoned policies in the DOCKER-USER chain, thereby facilitating subsequent management.
[0275] Regarding the device in this embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0276] It should be noted that each implementable method in this embodiment may be implemented separately, or may be implemented in combination in any combination without conflict, and this application is not limited thereto.
[0277] The embodiments of the present application also provide a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the operation of the method corresponding to any embodiment of the method for configuring the firewall policy provided in the above embodiments can be implemented.
[0278] An embodiment of the present application further provides a computer program product, wherein the computer program product includes a computer program, wherein when the computer program is executed by a processor, it can implement the operations corresponding to the method of any embodiment of the firewall policy configuration method provided in the above embodiments.
[0279] like Figure 4 As shown, some embodiments of the present application provide an electronic device 400, which includes: a memory 410, a processor 420, and a computer program stored in the memory 410 and executable on the processor 420, wherein the processor 420 can implement any embodiment of the method for configuring the firewall policy as described above when reading the program from the memory 410 through a bus 430 and executing the program.
[0280] Processor 420 can process digital signals and can include various computing structures, such as complex instruction set computer structure, reduced instruction set computer structure, or a structure that implements a combination of multiple instruction sets. In some examples, processor 420 can be a microprocessor.
[0281] The memory 410 may be used to store instructions executed by the processor 420 or data related to the execution of instructions. These instructions and / or data may include codes for implementing some or all functions of one or more modules described in the embodiments of the present application. The processor 420 of the disclosed embodiment may be used to execute instructions in the memory 410 to implement the method shown above. The memory 410 includes a dynamic random access memory, a static random access memory, a flash memory, an optical memory, or other memory known to those skilled in the art.
[0282] The above are only embodiments of the present application and are not intended to limit the scope of protection of the present application. For those skilled in the art, the present application may have various changes and variations. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the scope of protection of the present application. It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings.
[0283] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0284] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
Claims
1. A method for configuring a firewall policy, characterized in that: The method comprises: Acquire detection information of the target container, wherein the detection information of the target container at least includes a target container identifier and target port information corresponding to the target container identifier; Determine the preset port information corresponding to the target container identifier according to a pre-stored container mapping list; wherein the pre-stored container mapping list at least includes a correspondence between the container identifier and the port information; Determining port change information according to the target port information and the preset port information; A protection strategy corresponding to the target container is determined according to the port change information, and configuration is performed according to the protection strategy.
2. The method for configuring a firewall policy according to claim 1, characterized in that: The container mapping list is obtained in the following way: Obtain mapping information of a container identifier and port information, where the mapping information is used to map a log directory of the container to a specified path; storing mapping information between the container identifier and the port information in a first database; According to the mapping information, obtain the log directory of the container; Determine, according to the log directory of the container, a log file corresponding to the container; Parsing the log file to obtain keyword information, and storing the keyword information in the first database; The container mapping list is determined according to the correspondence between the container identifier, the port information, the log file and the keyword information in the first database.
3. The method for configuring a firewall policy according to claim 2, characterized in that: The obtaining of mapping information of the container identifier and the port information, wherein the mapping information is used to map the log directory of the container to the specified path, includes: Start the Docker container; Use the -v parameter to map the container's log directory.
4. The method for configuring a firewall policy according to claim 2, characterized in that: The step of determining the log file corresponding to the container according to the log directory of the container includes: According to the log directory of the container, multiple pieces of data from the same client source address within a preset time are obtained; Performing deduplication processing on the multiple pieces of data to obtain processed data; A log file corresponding to the container is obtained according to the processed data.
5. The method for configuring a firewall policy according to claim 4, characterized in that: The parsing the log file to obtain keyword information and storing the keyword information in the first database includes: Parsing the log file, and acquiring keyword information corresponding to the keyword identifier according to the keyword identifier, wherein the keyword identifier at least includes client information, and the client information at least includes a client identifier and a client source address; Determine, according to the keyword information, error log information corresponding to the keyword information; The time of the log file, the keyword information and the error log information are stored in the first database.
6. The method for configuring a firewall policy according to claim 2, characterized in that: The determining, according to the port change information, a protection strategy corresponding to the target container, and configuring according to the protection strategy, includes: According to the port change information, obtaining client information in the first database; According to the target port information and the client information, a protection policy corresponding to the target container is determined through an iptables command, and configuration is performed according to the protection policy.
7. The method for configuring a firewall policy according to claim 6, characterized in that: The method further comprises: The original protection strategy is deleted according to the port change information.
8. An electronic device, characterized in that: The invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor can implement the method for configuring the firewall policy as claimed in any one of claims 1 to 7 when executing the program.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the program, when executed by a processor, can implement the method for configuring the firewall policy described in any one of claims 1 to 7.
10. A computer program product, comprising a computer program, wherein: When the computer program is executed by a processor, the method for configuring the firewall policy described in any one of claims 1 to 7 can be implemented.
Citation Information
Patent Citations
Container set abnormality processing method and device, storage medium and server
CN111176789A
Port detection method, device and equipment and storage medium
CN111200522A
Docker container security access method and device
CN111726399A
Access rule deployment method, device and equipment, medium and cloud platform
CN115801569A
IPSec connection establishment method and device, equipment and storage medium
CN116015901A
Cited By
Firewall policy configuration method and device, equipment, storage medium and product
CN119628947A
