Method and related equipment for testing security defense capability of regular expressions
By dividing the regular expression into multiple matching segments and evaluating its security, the problem of inaccurate regular expression security defense capability testing in the prior art is solved, and a more efficient and accurate security assessment is achieved.
Patent Information
- Application Number
- CN202311252054.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-26
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2043-09-26
AI Technical Summary
Existing methods for testing the security defense capabilities of regular expressions rely on simulating a large amount of tainted data, resulting in inaccurate inspections, incorrect passes or missed passes, and the inability to fully evaluate the security defense capabilities of regular expressions.
Divide the regular expression into multiple matching segments, conduct security detection respectively, and evaluate the security defense capabilities of the regular expression based on the ratio of safe matching segments to dangerous matching segments, without the need for external taint data support.
It improves the accuracy of regular expression security defense capabilities, reduces loopholes in the verification process, reduces the amount of calculation, and enhances the accuracy of regular expression security detection.
Smart Images

Figure CN117290853B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of security detection, and specifically to a method and related equipment for testing the security defense capability of regular expressions. Background Art
[0002] With the development of science and technology, the Internet has become an indispensable part of people's lives. While the network transmits a large amount of information and data, some lawless elements or hackers may mix viruses or offensive commands into it, which may pose a threat and damage to our information security and personal property.
[0003] Regular expressions are text patterns with retrieval and screening functions, and are often used in network security defense systems to verify the legitimacy of API parameters of web applications. Different regular expressions have different screening capabilities for tainted data, so it is necessary to test the security defense capabilities of regular expressions.
[0004] Currently, a commonly used method to test the security defense capability of regular expressions is to simulate a large amount of tainted data to pass the regular expression, and to test the security defense capability of the regular expression based on the tainted data that passes the regular expression and the tainted data that is rejected. However, the types of simulated tainted data are not comprehensive enough, and there are cases where the regular expression mistakenly passes or misses tainted data. This method results in the security defense capability of the regular expression not being accurate enough. Summary of the invention
[0005] The present application provides a method for testing the security defense capability of a regular expression. The method divides a regular expression into a plurality of matching segments, detects the security of each matching segment one by one, and finally tests the security defense capability of the regular expression according to the number of secure matching segments and dangerous matching segments. The security defense capability of the regular expression is tested by performing segmented analysis on the regular expression itself. The method does not require the aid of external tainted data, and can accurately test the security defense capability of the regular expression.
[0006] In a first aspect, the present application provides a method for verifying the security defense capability of a regular expression, the method comprising:
[0007] The regular expression is divided into several matching segments based on the matching rules of the regular expression;
[0008] Dividing the plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segment includes at least one;
[0009] The quantity ratio of the safe matching segments to the dangerous matching segments is determined, and the security defense capability of the regular expression is tested based on the quantity ratio.
[0010] Through the above technical solution, the regular expression is divided into several matching segments, and its security is tested with the matching segments as the minimum test units. It is divided into safe matching segments and dangerous matching segments in a corresponding manner. Compared with the existing method for testing the security defense capabilities of regular expressions, the method provided by the present application does not require the use of external tainted data, can reduce omissions in the testing process, and improve the accuracy of the regular expression security defense capability test.
[0011] Optionally, the regular expression-based matching rule divides the regular expression into a plurality of matching segments, including:
[0012] Dividing the regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on a grammar rule and a matching rule of the regular expression;
[0013] The dividing the plurality of matching segments into safe matching segments and dangerous matching segments includes:
[0014] Based on whether the first number of ASCII matching segments contain offensive characters, the ASCII matching segments are divided into safe matching segments and dangerous matching segments;
[0015] The Unicode is divided into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments.
[0016] By adopting the above technical solution, the matching segments are divided into ASCII type matching segments and Unicode type matching segments according to grammatical rules, and the safety and danger of the corresponding matching segment types are judged in detail according to the safety of the corresponding matching segment types. Not only can the corresponding recognition methods be used to process different types of matching segments to improve the inspection speed and inspection efficiency, but also the inspection accuracy of the matching segments can be further improved.
[0017] Optionally, the step of distinguishing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters includes:
[0018] Determining whether the first number of ASCII matching segments contains the offensive character;
[0019] If the offensive character is included in the first number of ASCII matching segments, define the ASCII matching segments as the dangerous matching segments, record the third number of the ASCII matching segments and output the third number of the ASCII matching segments;
[0020] If the ASCII matching segment does not include the offensive character, the ASCII matching segment is defined as the safe matching segment, and a fourth number of the ASCII matching segments is recorded.
[0021] Through the above technical scheme, the process of making security judgment on the first number of ASCII matching segments is elaborated in detail, the first number of ASCII matching segments are divided into a third number of safe ASCII matching segments and a fourth number of dangerous ASCII matching segments, the first number is the sum of the third number and the fourth number, and the fourth number of dangerous ASCII matching segments are visualized, the security defense capability of the regular expression for ASCII code can be understood through the number of divided ASCII matching segments, and data support can be provided for the subsequent security defense capability of the overall regular expression.
[0022] Optionally, before distinguishing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters, the method further includes:
[0023] Get the offensive characters entered by the user;
[0024] or,
[0025] Obtain offensive characters from a secure database on a cloud server.
[0026] Through the above technical solution, the ASCII matching segment is verified by checking whether there are offensive characters in the regular expression. To ensure the security of system data, users are required to enter the corresponding offensive characters or obtain them from the cloud security database to avoid security risks caused by offensive characters obtained from unknown paths.
[0027] Optionally, after dividing the regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on the grammar rule and the matching rule of the regular expression, the method further includes:
[0028] Obtaining an application scenario of the regular expression input by a user;
[0029] The type of the offensive character is selected based on an application scenario of the regular expression.
[0030] Through the above technical solution, the type of corresponding offensive characters is selected according to the application scenario of the regular expression to obtain a more accurate inspection effect in this application scenario.
[0031] Optionally, the step of distinguishing Unicode into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments includes:
[0032] Determine whether the character length of the second number of Unicode matching segments is greater than a first threshold;
[0033] If the character length of the second number of Unicode matching segments is greater than the first threshold, defining the Unicode matching segments as the dangerous matching segments, recording the fifth number of the Unicode matching segments and outputting the fifth number of the Unicode matching segments;
[0034] If the character length of the Unicode matching segment is not greater than the first threshold, the Unicode matching segment is defined as the safe matching segment, and a sixth number of the Unicode matching segments is recorded.
[0035] Through the above technical scheme, the security evaluation process of Unicode matching segments is described in detail. Taking the character length of the Unicode matching segments as the evaluation criterion, the second number of Unicode matching segments is divided into a fifth number of dangerous Unicode matching segments and a sixth number of safe Unicode matching segments. The Unicode type matching segments can be tested and distinguished, providing data support for the subsequent overall analysis of the security defense capabilities of this regular expression.
[0036] Optionally, before determining whether the character length of the second number of Unicode matching segments is greater than a first threshold, the step further includes:
[0037] Obtaining an application scenario of the regular expression input by a user;
[0038] The value of the first threshold is selected based on the application scenario of the regular expression.
[0039] Through the above technical solution, different security defense requirements of regular expressions are adjusted accordingly according to different application scenarios to achieve accurate division of safe matching segments and dangerous matching segments of Unicode matching segments.
[0040] In a second aspect, the present application provides a device for testing the security defense capability of a regular expression, the device comprising:
[0041] A regular expression segmentation module is used to segment a regular expression into a plurality of matching segments based on a matching rule of the regular expression;
[0042] A matching segment distinguishing module, which divides the plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segment includes at least one;
[0043] The defense capability testing module is used to determine the quantity ratio of the safe matching segments to the dangerous matching segments, and to test the security defense capability of the regular expression based on the quantity ratio.
[0044] Through the above-mentioned device, a hardware foundation is provided for testing the security defense capability of regular expressions, so that modules with related functions can be implemented in the device, the modules are functionally divided, and each module can independently process and execute corresponding method steps, and can jointly complete the accurate detection of the security defense capability of regular expressions through data transmission.
[0045] In a third aspect of the present application, an electronic device is provided, comprising a processor, a memory and a transceiver, wherein the memory is used to store instructions, the transceiver is used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device performs the above method steps.
[0046] In a fourth aspect of the present application, a computer storage medium is provided, wherein the computer storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor and executing the above method steps.
[0047] The beneficial effects brought by the technical solution provided by the embodiment of the present application include at least:
[0048] In an embodiment of the present application, a regular expression is divided into detailed matching segments, with a single matching segment as the minimum detection unit, and security detection is performed on all matching segments contained in the regular expression. Corresponding security detection methods and detection standards are selected according to the types of different matching segments. Compared with the existing method of testing the overall regular expression, the security defense capability of the regular expression can be tested without the help of external tainted data, and the amount of calculation can be reduced to a certain extent while improving the accuracy of detecting the security defense capability of the regular expression. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 It is a flowchart of a method for testing the security defense capability of a regular expression according to an embodiment of the present application;
[0050] Figure 2 It is a flowchart of another method for testing the security defense capability of regular expressions according to an embodiment of the present application;
[0051] Figure 3 It is a flowchart of another method for testing the security defense capability of a regular expression in an embodiment of the present application;
[0052] Figure 4 It is a structural schematic diagram of a device for testing the security defense capability of a regular expression according to an embodiment of the present application;
[0053] Figure 5 It is a structural schematic diagram of another device for testing the security defense capability of regular expressions according to an embodiment of the present application;
[0054] Figure 6It is a structural schematic diagram of an electronic device according to an embodiment of the present application.
[0055] Explanation of the accompanying drawings: 10. Regular expression division module; 20. Matching segment distinction module; 30. Defense capability inspection module; 11. Type division module; 21. ASCII matching segment distinction module; 22. Unicode matching segment distinction module; 23. ASCII matching segment judgment module; 24. Offensive character acquisition module; 25. First scene selection module; 26. Unicode matching segment judgment module; 27. Second scene selection module; 1000. Electronic device; 1001. Processor; 1002. Communication bus; 1003. User interface; 1004. Network interface; 1005. Memory. DETAILED DESCRIPTION
[0056] In order to enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this application, not all of the embodiments.
[0057] In the description of the embodiments of the present application, words such as "illustrative", "for example" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "illustrative", "for example" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "illustrative", "for example" or "for example" is intended to present related concepts in a specific way.
[0058] In the description of the embodiments of the present application, the meaning of the term "multiple" refers to two or more. For example, multiple systems refer to two or more systems, and multiple screen terminals refer to two or more screen terminals. In addition, the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the indicated technical features. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. The terms "include", "comprise", "have" and their variations all mean "including but not limited to", unless otherwise specifically emphasized.
[0059] The present application relates to the field of network data security detection, which is a subdivision of the field of security detection. First, a brief introduction is given to some special terms in this field.
[0060] A regular expression is a logical formula for string operations. It is composed of some predefined specific characters and combinations of these specific characters to form a regular string, which is used to express a filtering logic for the string. A regular expression is also a text pattern, consisting of ordinary characters and metacharacters. It matches a series of strings with certain syntactic rules through a single string, and is often used to retrieve and replace certain matching strings. Regular expressions are highly flexible, logical, and functional, and can easily and quickly copy and control strings. Therefore, they are widely used in various text editors and underlying network data security protection scenarios. In a text editor, the purpose of a regular expression is to obtain the specific part we want from a string, such as the search function in a text editor. In the network data security protection scenario, the purpose of a regular expression is to determine whether a string meets the filtering logic of the regular expression to achieve security protection.
[0061] ASCII code is a computer coding system based on the Latin alphabet. It uses a specified 7-bit or 8-bit binary number combination to represent 128 or 256 possible characters. The standard ASCII code only uses 7 bits to represent the 128 binary number combinations of actual data. The extended ASCII code enables the remaining binary number, making it possible to represent 256 possible characters.
[0062] Unicode is a character encoding used on computers. Unicode sets a unified and unique binary encoding for each character in each language to meet the requirements of cross-language and cross-platform text conversion and processing. The two character encoding methods use different grammatical rules to obtain different strings, and regular expressions can also be divided according to the different grammatical rules of the two character encoding methods.
[0063] See also Figure 1 , Figure 1 A flow chart of a method for verifying the security defense capability of a regular expression provided in an embodiment of the present application is provided to reflect the main implementation process of the method for verifying the security defense capability of a regular expression.
[0064] S101, dividing a regular expression into a plurality of matching segments based on a matching rule of the regular expression.
[0065] A matching segment is the most basic element of a regular expression. It is a group of characters that describe the characteristics of a string and can match a string. The matching segments are divided based on the matching rules of the regular expression. For example, the symbol "^" indicates the beginning of a matching segment, and the symbol "$" is used to match a string ending with a given character. Correspondingly, the regular expression can be divided into several matching segments using escape sequences.
[0066] S102, dividing a plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segment includes at least one
[0067] Safe matching segments and dangerous matching segments are relative. Matching segments are screened according to their character features. Matching segments that meet the safety feature indicators are defined as safe matching segments, and matching segments whose character features meet the dangerous feature indicators are defined as dangerous matching segments. Safety feature indicators are determined based on the character features of different matching segments. For example, some matching segments contain certain system operation instructions, that is, data set characters that can change system settings can pass through this matching segment of the regular expression, thereby posing a threat to system security. Character features refer to the data range, character length and other features that the matching segment can pass through.
[0068] A security check is performed on each divided matching segment, and the matching segments are divided into safe matching segments and dangerous matching segments according to the results of the security check. The number of dangerous matching segments is related to the security defense capability of the regular expression. At the same time, there may be no dangerous matching segments, that is, the security defense capability of the regular expression is very strong, and there may also be one or more dangerous matching segments.
[0069] S103, determining the quantity ratio of the safe matching segments to the dangerous matching segments, and testing the security defense capability of the regular expression based on the quantity ratio.
[0070] After the security of each matching segment is determined as above, the quantitative relationship between the safe matching segments and the dangerous matching segments is obtained. The sum of the number of safe matching segments and the dangerous matching segments is the number of all matching segments of the regular expression. The security defense capability of this regular expression is tested according to the ratio of the number of safe matching segments to the number of dangerous matching segments. For example, if the number of safe matching segments accounts for 98% of the total number of matching segments, it means that the security defense capability of this regular expression is strong.
[0071] In one embodiment of the present application, in order to test the security defense capability of a regular expression, the regular expression is divided into multiple matching segments, the security defense capabilities of the matching segments are tested one by one, the matching segments are divided into safe matching segments and dangerous matching segments according to the test standard, and the security defense capability of the regular expression is judged according to the relationship between the safe matching segments, the dangerous matching segments and the total number of matching segments. The existing method of testing the security defense capability of regular expressions is to simulate tainted data on a large amount of character data to test the regular expression. When the amount of data is small, the test is not accurate enough, and when the amount of data is large, the amount of calculation is huge, and it is difficult to find a suitable amount of test data. The matching segments are used as the test object. Compared with the traditional method of directly applying the regular expression to the tainted data to test its screening and processing capabilities for the tainted data, the method provided by the present application is to test the matching segments of the regular expression without the need to introduce additional tainted data, which can improve the test accuracy and reduce the calculation steps to a certain extent.
[0072] See also Figure 2 , Figure 2 A flow chart of another method for verifying the security defense capability of regular expressions provided for this application elaborates in detail the specific process and implementation steps of the method for verifying the security defense capability of regular expressions.
[0073] S201 : Divide a regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on a grammatical rule and a matching rule of the regular expression.
[0074] First, the regular expression is divided into a number of matching segments according to the matching rules of the regular expression, and then the types of the matching segments are divided according to the types of metacharacters in the matching segments, and divided into ASCII matching segments in the range of [0~255] and Unicode matching segments. The Unicode matching segment is an octal character whose metacharacter is " / u" and exceeds the range of [0~255]. The first number and the second number represent the number of divided ASCII matching segments and the number of Unicode matching segments respectively, and the sum of the first number and the second number is the total number of matching segments of this regular expression.
[0075] S202: Differentiate the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters.
[0076] Offensive characters are strings containing certain instructions that can perform offensive operations on the database, thereby threatening and damaging data security. For example, allowing the "'%0a" character to pass will lead to SQL injection offensive instructions. If any offensive character exists in the ASCII matching segment, it will be classified as a dangerous matching segment.
[0077] Optionally, offensive characters input by a user are obtained; or offensive characters are obtained from a security database on a cloud server.
[0078] Offensive characters are used to detect the security of ASCII matching segments. However, as the character data to be tested, the source of the character data itself also needs to be secure. To ensure the security of the data, some offensive characters can be manually input by the user, or obtained from the secure database of the cloud server segment for further screening of the ASCII matching segments.
[0079] Optionally, an application scenario of a regular expression input by a user is obtained; and a type of offensive characters is selected based on the application scenario of the regular expression.
[0080] Regular expressions use character data as the detection object to implement functions such as search and filtering. Therefore, they can be used in a variety of scenarios such as text search, character replacement, and network security defense. Even in the field of network security defense, the requirements and standards for the security defense capabilities of regular expressions are different in different security defense scenarios. For example, in the scenario of web security, users need to have a good browsing experience. At this time, if the security protection performance of regular expressions is strong, the information on the web page interface will not be displayed in a timely manner or even cannot be displayed. When checking the security of a file, the main purpose is to check whether it contains threatening data, and the time spent on the test is secondary. In this scenario, the security protection performance requirements of regular expressions are relatively high. Therefore, it is necessary to select the security performance evaluation criteria of regular expressions based on the application scenario input by the user.
[0081] Optionally, determine whether the first number of ASCII matching segments contain offensive characters; if the first number of ASCII matching segments contain offensive characters, define the ASCII matching segments as dangerous matching segments, record a third number of ASCII matching segments and output the third number of ASCII matching segments; if the ASCII matching segments do not contain offensive characters, define the ASCII matching segments as safe matching segments, and record a fourth number of ASCII matching segments.
[0082] For the first number of ASCII matching segments, a single ASCII matching segment can be matched with a plurality of offensive characters one by one, and then the next ASCII matching segment can be matched with offensive characters until the matching of the first number of ASCII matching segments is completed; a single offensive character can be matched with the first number of ASCII matching segments one by one, and then the next offensive character can be matched with the first number of ASCII matching segments; or the first number of ASCII matching segments can be matched with a plurality of offensive characters at the same time. According to the matching results, safe matching segments and dangerous matching segments in the ASCII matching segments are defined, and the numbers of the two corresponding matching segments are recorded, and the third number of dangerous matching segments in the ASCII matching segments are outputted at the same time, so as to find out the root cause of the problem in the ASCII matching segment.
[0083] S203: Differentiate Unicode into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments.
[0084] According to the division of grammatical rules, Unicode matching segments are often divided into three character encoding schemes: UTF-32, UTF-16 and UTF-8. UTF-8 is a superset of ASCII, and ASCII code is a special case of Unicode encoding. In the embodiments of the present application, ASCII matching segments are more commonly used in regular expressions, and the number of Unicode matching segments is relatively small compared to ASCII matching segments. Therefore, Unicode matching segments refer to other matching segments in regular expressions except ASCII matching segments. In addition, Unicode matching segments can be reliably identified by a simple algorithm, but as the length of the string increases, the constructible range of the passed data will also increase, thereby weakening the Unicode matching segment's ability to screen the passed data.
[0085] Optionally, determine whether the character length of the second number of Unicode matching segments is greater than the first threshold; if the character length of the second number of Unicode matching segments is greater than the first threshold, define the Unicode matching segment as a dangerous matching segment, record the fifth number of Unicode matching segments and output the fifth number of Unicode matching segments; if the character length of the Unicode matching segment is not greater than the first threshold, define the Unicode matching segment as a safe matching segment, and record the sixth number of Unicode matching segments.
[0086] The first threshold is a benchmark threshold determined according to the amount of data that can be passed by the character length, and a certain Unicode matching segment is defined as a safe matching segment or a dangerous matching segment according to the relationship between the character length of the Unicode matching segment and the first threshold. Similarly, the character length of the second number of Unicode matching segments can be judged one by one, or the first threshold can be set to a certain standard, and the character length of all the second number of Unicode matching segments can be judged to directly screen out dangerous matching segments and safe matching segments, and the number of the two matching segments is recorded at the same time and the dangerous matching segments are output, so as to facilitate the tracing of the dangerous matching segments.
[0087] Optionally, an application scenario of a regular expression input by a user is obtained; and a value of the first threshold is selected based on the application scenario of the regular expression.
[0088] When selecting application scenarios for offensive characters mentioned above, the types and quantities of offensive characters used by regular expressions in different application scenarios are different. Similarly, the value of the first threshold of regular expressions in different application scenarios is also different. Therefore, it is necessary to screen the Unicode matching segments of regular expressions to different degrees according to the actual scenarios.
[0089] S204, determining the quantity ratio of the safe matching segments to the dangerous matching segments, and testing the security defense capability of the regular expression based on the quantity ratio.
[0090] The security defense capability of a regular expression is based on the entire regular expression. After defining the security of all matching segments of the regular expression, all safe matching segments and dangerous matching segments are counted. Safe matching segments include ASCII-type safe matching segments and Unicode-type safe matching segments, and dangerous matching segments include ASCII-type dangerous matching segments and Unicode-type dangerous matching segments. It should be noted that dangerous matching segments may not exist, that is, the number of dangerous matching segments is zero. Calculate their share in the total matching segments. For example, if the ratio of safe matching segments to all matching segments is 95%, it can be verified that the security defense capability of this regular expression is strong.
[0091] See also Figure 3 , which is a flow chart of a method for verifying the security defense capability of a regular expression provided in an embodiment of the present application, which explains in detail the processing and judgment division process of the regular expression.
[0092] In one embodiment of the present application, a regular expression is divided into several ASCII matching segments and several Unicode matching segments according to matching rules and grammatical rules, and different evaluation criteria are used to perform security checks on the two types of matching segments, and safe matching segments and dangerous matching segments are defined. The overall security defense capability of the regular expression is tested based on the quantitative relationship between the two matching segments. Different definition methods are used for different types of matching segments to improve the accuracy of the test. At the same time, the evaluation criteria for defining a matching segment as a safe matching segment or a dangerous matching segment can be adjusted according to the actual application scenario of the regular expression to meet the different security defense requirements of regular expressions in different application scenarios.
[0093] See also Figure 4 , which shows a schematic diagram of the structure of a device for checking the security defense capability of a regular expression provided by an exemplary embodiment of the present application. The binding device can be implemented as all or part of the device through software, hardware or a combination of both. The device includes a regular expression partitioning module 10, a matching segment distinguishing module 20, and a defense capability checking module 30.
[0094] The regular expression division module 10 is used to divide the regular expression into a plurality of matches based on the matching rule of the regular expression.
[0095] The matching segment distinguishing module 20 divides a plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segment includes at least one.
[0096] The defense capability testing module 30 is used to determine the ratio of the number of safe matching segments to the number of dangerous matching segments, and to test the security defense capability of the regular expression based on the ratio.
[0097] See also Figure 5 , Figure 5 A schematic diagram of the structure of a device for verifying the security defense capability of regular expressions provided in an embodiment of the present application.
[0098] Optional, such as Figure 5 As shown, the device also includes:
[0099] The type classification module 11 is used to divide the regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on the grammatical rules and matching rules of the regular expression.
[0100] Optional, such as Figure 5 As shown, the device also includes:
[0101] The ASCII matching segment distinguishing module 21 is used to distinguish the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters.
[0102] Optional, such as Figure 5 As shown, the device also includes:
[0103] The Unicode matching segment distinguishing module 22 is used to distinguish Unicode into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments.
[0104] Optional, such as Figure 5 As shown, the device also includes:
[0105] The ASCII matching segment judgment module 23 is used to judge whether the first number of ASCII matching segments contain offensive characters; if the first number of ASCII matching segments contain offensive characters, the ASCII matching segments are defined as dangerous matching segments, a third number of ASCII matching segments are recorded and the third number of ASCII matching segments are output; if the ASCII matching segments do not contain offensive characters, the ASCII matching segments are defined as safe matching segments, and a fourth number of ASCII matching segments are recorded.
[0106] Optional, such as Figure 5 As shown, the device also includes:
[0107] The offensive character acquisition module 24 is used to acquire offensive characters input by a user; or acquire offensive characters from a security database on a cloud server.
[0108] Optional, such as Figure 5 As shown, the device also includes:
[0109] The first scenario selection module 25 is used to obtain the application scenario of the regular expression input by the user; and select the type of offensive characters based on the application scenario of the regular expression.
[0110] Optional, such as Figure 5 As shown, the device also includes:
[0111] The Unicode matching segment judgment module 26 is used to judge whether the character length of the second number of Unicode matching segments is greater than the first threshold; if the character length of the second number of Unicode matching segments is greater than the first threshold, the Unicode matching segment is defined as a dangerous matching segment, and the fifth number of Unicode matching segments are recorded and output; if the character length of the Unicode matching segment is not greater than the first threshold, the Unicode matching segment is defined as a safe matching segment, and the sixth number of Unicode matching segments are recorded.
[0112] Optional, such as Figure 5 As shown, the device also includes:
[0113] The second scenario selection module 27 is used to obtain the application scenario of the regular expression input by the user; and select the value of the first threshold based on the application scenario of the regular expression.
[0114] The present application also provides a computer storage medium that can store multiple instructions, which are suitable for being loaded and executed by a processor as described above. Figure 1~Figure 5 The method for verifying the security defense capability of regular expressions in the illustrated embodiment, the specific execution process can be found in Figure 1~Figure 5 The specific description of the illustrated embodiment will not be repeated here.
[0115] See also Figure 6 , is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 6 As shown, the electronic device 1000 may include: at least one processor 1001 , at least one network interface 1004 , a user interface 1003 , a memory 1005 , and at least one communication bus 1002 .
[0116] The communication bus 1002 is used to realize the connection and communication between these components.
[0117] The user interface 1003 may include a display screen (Display) and a camera (Camera). Optionally, the user interface 1003 may also include a standard wired interface and a wireless interface.
[0118] The network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a WI-FI interface).
[0119] Among them, the processor 1001 may include one or more processing cores. The processor 1001 uses various interfaces and lines to connect various parts in the entire server, and executes various functions of the server and processes data by running or executing instructions, programs, code sets or instruction sets stored in the memory 1005, and calling data stored in the memory 1005. Optionally, the processor 1001 can be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field programmable gate array (Field-Programmable Gate Array, FPGA), and programmable logic array (Programmable Logic Array, PLA). The processor 1001 can integrate one or a combination of a central processing unit (Central Processing Unit, CPU), a graphics processing unit (Graphics Processing Unit, GPU) and a modem. Among them, the CPU mainly processes the operating system, user interface and application programs; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 1001, and it can be implemented separately through a chip.
[0120] Among them, the memory 1005 may include a random access memory (Random Access Memory, RAM) and may also include a read-only memory (Read-Only Memory). Optionally, the memory 1005 includes a non-transitory computer-readable storage medium. The memory 1005 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 1005 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 1005 may also be optionally at least one storage device located away from the aforementioned processor 1001. As Figure 6 As shown, the memory 1005 as a computer storage medium may include an operating system, a network communication module, a user interface module, and an application program for a method of checking the security defense capability of a regular expression.
[0121] exist Figure 6In the electronic device 1000 shown, the user interface 1003 is mainly used to provide an input interface for the user and obtain data input by the user; and the processor 1001 can be used to call an application stored in the memory 1005 for a method for verifying the security defense capability of a regular expression. When executed by one or more processors, the electronic device executes one or more methods in the above-mentioned embodiments.
[0122] An electronic device readable storage medium stores instructions, which, when executed by one or more processors, enable the electronic device to execute one or more methods in the above embodiments.
[0123] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required for the present application.
[0124] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0125] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0126] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0127] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0128] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a memory and includes several instructions for a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the various embodiments of the present application. The aforementioned memory includes: various media that can store program codes, such as USB flash drives, mobile hard drives, magnetic disks or optical disks.
[0129] The above are only exemplary embodiments of the present disclosure and cannot be used to limit the scope of the present disclosure. That is, any equivalent changes and modifications made according to the teachings of the present disclosure are still within the scope of the present disclosure. After considering the disclosure of the specification and the truth of practice, it will be easy for those skilled in the art to think of other embodiments of the present disclosure. This application is intended to cover any modification, use or adaptation of the present disclosure, which follows the general principles of the present disclosure and includes common knowledge or customary technical means in the technical field that are not recorded in the present disclosure. The description and examples are regarded as exemplary only, and the scope and spirit of the present disclosure are defined by the claims.
Claims
1. A method for testing the security defense capability of regular expressions, characterized in that: The method comprises: The regular expression is divided into several matching segments based on the matching rules of the regular expression; The matching rule based on the regular expression divides the regular expression into a plurality of matching segments, including: dividing the regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on the grammar rule and matching rule of the regular expression; dividing the plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segment includes at least one; The dividing the plurality of matching segments into safe matching segments and dangerous matching segments includes: dividing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters; dividing the Unicode matching segments into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments; The quantity ratio of the safe matching segments to the dangerous matching segments is determined, and the security defense capability of the regular expression is tested based on the quantity ratio.
2. The method according to claim 1, characterized in that: The step of distinguishing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters comprises: Determining whether the first number of ASCII matching segments contains the offensive character; If there are any ASCII matching segments containing the offensive characters in the first number of ASCII matching segments, define the ASCII matching segments as the dangerous matching segments, record a third number of ASCII matching segments and output the third number of ASCII matching segments, wherein the third number of ASCII matching segments indicates the third number of ASCII matching segments containing the offensive characters; If the ASCII matching segment does not include the offensive character, the ASCII matching segment is defined as the safe matching segment, and a fourth number of ASCII matching segments is recorded.
3. The method according to claim 1, characterized in that Before distinguishing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters, the method further includes: Get the offensive characters entered by the user; or, Obtain offensive characters from a secure database on a cloud server.
4. The method according to claim 1, characterized in that: After the regular expression is divided into a first number of ASCII matching segments and a second number of Unicode matching segments based on the grammar rule and the matching rule of the regular expression, the method further includes: Obtaining an application scenario of the regular expression input by a user; The type of the offensive character is selected based on an application scenario of the regular expression.
5. The method according to claim 1, characterized in that The step of distinguishing Unicode into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments includes: Determine whether the character length of the second number of Unicode matching segments is greater than a first threshold; If the character length of the second number of Unicode matching segments is greater than the first threshold, the Unicode matching segment is defined as the dangerous matching segment, a fifth number of Unicode matching segments is recorded and output; the fifth number of Unicode matching segments represents the number of Unicode matching segments whose character length is greater than the first threshold; If the character length of the Unicode matching segment is not greater than the first threshold, the Unicode matching segment is defined as the safe matching segment, and a sixth number of Unicode matching segments is recorded.
6. The method according to claim 5, characterized in that Before determining whether the character length of the second number of Unicode matching segments is greater than the first threshold, the method further includes: Obtaining an application scenario of the regular expression input by a user; The value of the first threshold is selected based on the application scenario of the regular expression.
7. A device for testing the security defense capability of regular expressions, characterized in that: The device comprises: A regular expression division module, used to divide the regular expression into a plurality of matching segments based on the matching rule of the regular expression; said dividing the regular expression into a plurality of matching segments based on the matching rule of the regular expression comprises: dividing the regular expression into a first number of ASCII matching segments and a second number of Unicode matching segments based on the grammatical rule and the matching rule of the regular expression; A matching segment distinguishing module is configured to divide the plurality of matching segments into safe matching segments and dangerous matching segments, wherein the safe matching segments include at least one; the dividing the plurality of matching segments into safe matching segments and dangerous matching segments includes: distinguishing the ASCII matching segments into safe matching segments and dangerous matching segments based on whether the first number of ASCII matching segments contain offensive characters; distinguishing the Unicode matching segments into safe matching segments and dangerous matching segments based on the character lengths of the second number of Unicode matching segments; The defense capability testing module is used to determine the quantity ratio of the safe matching segments to the dangerous matching segments, and to test the security defense capability of the regular expression based on the quantity ratio.
8. An electronic device, characterized in that: It includes a processor, a memory, a user interface and a network interface, the memory is used to store instructions, the user interface and the network interface are used to communicate with other devices, and the processor is used to execute the instructions stored in the memory so that the electronic device performs the method steps as described in any one of claims 1 to 6.
9. A computer storage medium, characterized in that The computer storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the method steps as claimed in any one of claims 1 to 6.
