Nested condition matching method and system based on logic syntax tree and storage medium

By parsing the logical syntax tree from the Sigma rule file and building a pre-order traversal result stack, converting nested conditions into nested expressions, the problem of low matching efficiency between nested conditions and log data is solved, and efficient nested conditions matching is achieved .

CN119025494APending Publication Date: 2024-11-26HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 0 Cites 5 Cited by

Patent Information

Application Number
CN202410939789.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-12
Publication Date
2024-11-26

AI Technical Summary

Technical Problem

In the prior art, when processing Sigma rules in YAML format, nested conditions cannot be directly matched with log data, resulting in low matching efficiency and the inability to use the logical relationship between atomic matching conditions for matching pruning.

Method used

By parsing the logical syntax tree from the rule file, obtaining the result stack in the pre-order traversal based on the logical syntax tree, determining the nested expression, converting the nested conditions into a form that can directly match the log data, and fusing the atomic matching conditions and logical relationships .

Benefits of technology

The matching efficiency between nested conditions and log data is improved, and the word-by-word matching of atomic conditions is avoided, which enhances the efficiency of the matching process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119025494A_ABST
    Figure CN119025494A_ABST
Patent Text Reader

Abstract

The invention provides a nesting condition matching method and system based on a logic syntax tree and a storage medium, and is characterized in that the logic syntax tree is obtained according to a preset rule file, the rule file comprises at least one nesting condition, and the logic syntax tree comprises a plurality of logic nodes and a plurality of logic operation nodes; the plurality of logic nodes and the plurality of logic operation nodes set rule files in a hierarchical nesting manner; according to the logic syntax tree, obtaining a pre-order traversal result stack in which a plurality of logic nodes and a plurality of logic operation nodes are sequentially merged; determining a nested expression corresponding to at least one nested condition according to the preorder traversal result stack; and matching the to-be-matched traffic logs according to the nested expression. According to the method, the nesting condition in the rule file is converted, so that the nesting expression obtained after conversion can be directly matched with the log data.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of log data matching, and in particular to a nested condition matching method, system and storage medium based on a logical syntax tree. Background Art

[0002] In the process of matching log data, the Sigma rules in YAML format need to be converted into different formats. The most important part is the processing of nested conditions. Since the nested conditions are presented in the original rule file in the form of atomic conditions and logical relationships between atomic conditions are separated, it is impossible to directly match the nested conditions with the log data.

[0003] In view of this, the present application parses the logical syntax tree from the rule file, obtains the pre-order traversal result stack based on the logical syntax tree, determines the nested expression according to the order in the pre-order traversal result stack, and transforms the nested conditions in the rule file, which is conducive to the nested expression obtained after the transformation to be directly matched with the log data. Summary of the invention

[0004] The embodiments of the present application provide a nested condition matching method, system and storage medium based on a logical syntax tree.

[0005] In a first aspect, a logical syntax tree is obtained according to a preset rule file, wherein the rule file includes at least one nested condition, and the logical syntax tree includes a plurality of logical nodes and a plurality of logical operation nodes, wherein the plurality of logical nodes and the plurality of logical operation nodes are hierarchically nested to set the rule file;

[0006] According to the logical syntax tree, a pre-order traversal result stack is obtained in which the plurality of logical nodes and the plurality of logical operation nodes are sequentially incorporated;

[0007] Determine a nested expression corresponding to the at least one nested condition according to the pre-order traversal result stack;

[0008] The traffic log to be matched is matched according to the nested expression.

[0009] Optionally, obtaining a logical syntax tree according to a preset rule file includes:

[0010] Obtaining a logical expression according to the atomic condition name in the rule file and the logical operators between the atomic conditions;

[0011] The logical expression is parsed to obtain the logical syntax tree.

[0012] Optionally, obtaining, according to the logical syntax tree, a pre-order traversal result stack in which the plurality of logical nodes and the plurality of logical operation nodes are sequentially incorporated comprises:

[0013] The plurality of logic nodes and the plurality of logic operation nodes are merged into an intermediate stack according to the hierarchical order, wherein the intermediate stack includes a plurality of empty stack cells, to obtain the pre-order traversal result stack.

[0014] Optionally, the step of merging the plurality of logic nodes and the plurality of logic operation nodes into an intermediate stack in hierarchical order includes:

[0015] Starting from the leftmost node at the first level of the logical syntax tree, traverse rightwards to the rightmost node at the first level;

[0016] Starting from the leftmost node of the current level pointed to by the leftmost node of the previous level, traverse rightward to the rightmost node of the current level in sequence, until the nodes of the current level pointed to by the nodes of the previous level are traversed in sequence, traversing all nodes in the logical syntax tree.

[0017] Optionally, the merging of the plurality of logic nodes and the plurality of logic operation nodes into an intermediate stack in hierarchical order, wherein the node value includes an atomic condition name, a logic operator and null, comprises:

[0018] When the traversed element is a placeholder, the node value is empty, and the placeholder is not stored in the pre-order traversal result stack;

[0019] When the traversed element is an atomic condition name, the atomic condition name is stored in the pre-order traversal result stack;

[0020] When the traversed element is a logical operation symbol, the logical operation symbol is stored in the pre-order traversal result stack.

[0021] Optionally, determining the nested expression corresponding to the at least one nested condition according to the pre-order traversal result stack includes:

[0022] Establishing a temporary stack, wherein the temporary stack includes a top stack and a secondary stack;

[0023] If the current element is an atomic condition name, after popping the atomic condition name from the pre-order traversal result stack, the atomic condition name and its corresponding atomic condition data are stored in the temporary stack;

[0024] If the current element is a logical operator and / or, the top element and the next top element are popped from the temporary stack, and the atomic conditions with the same name corresponding to the top element and the next top element are defined respectively, and the corresponding logical expression is constructed in combination with the logical operator and / or and stored in the temporary stack;

[0025] If the current element is a logical operator negative, the top element is popped from the temporary stack, a negation operation is performed to obtain a corresponding logical expression, and the expression is stored in the temporary stack;

[0026] After all elements of the pre-order traversal result stack are popped and stored in the temporary stack, a nested expression is obtained according to all elements in the temporary stack.

[0027] Optionally, the method further includes:

[0028] By merging the same logical operators, redundant placeholders generated by multiple consecutive logical operators and / or connected logical expressions are eliminated.

[0029] In a second aspect, an embodiment of the present application provides a nested condition matching system based on a logical syntax tree, including:

[0030] A logical syntax tree parsing module is used to parse a rule file to obtain a logical syntax tree, wherein the logical syntax tree includes a plurality of nodes, each node stores an element, and the rule file is a file of a Sigma rule set in a YAML format, and the rule file includes nested conditions;

[0031] A pre-order traversal result stack module is used to establish an intermediate stack to store elements of each node in the logical syntax tree to obtain a pre-order traversal result stack;

[0032] A nested expression module, used to establish a temporary stack and combine it with the pre-order traversal result stack to obtain a nested expression;

[0033] The matching module is used to match the nested expression with the log data.

[0034] In a third aspect, an embodiment of the present application provides a terminal device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described above when executing the computer program.

[0035] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method as described above is implemented.

[0036] The present application parses a logical syntax tree from a rule file, obtains a pre-order traversal result stack based on the logical syntax tree, determines a nested expression based on the order in the pre-order traversal result stack, and transforms the nested conditions in the rule file, which is beneficial for the nested expression obtained after the transformation to be directly matched with the log data. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0038] Figure 1 It is a schematic diagram of a nested condition matching method based on a logical syntax tree provided in an embodiment of the present application;

[0039] Figure 2 It is a structural diagram of a logical syntax tree provided in an embodiment of the present application;

[0040] Figure 3 This is a schematic diagram of a pre-order traversal result stack provided in an embodiment of the present application;

[0041] Figure 4 This is a schematic diagram of a temporary stack change provided in an embodiment of the present application;

[0042] Figure 5 It is a schematic diagram of a real-time alarm rule matching process based on logical syntax tree parsing provided in an embodiment of the present application;

[0043] Figure 6 It is a structural diagram of a nested condition matching system based on a logical syntax tree provided by an embodiment of the present application;

[0044] Figure 7 It is a schematic diagram of the structure of the terminal device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0045] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.

[0046] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.

[0047] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0048] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.

[0049] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.

[0050] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0051] In the process of matching log data, the Sigma rules in YAML format need to be converted into different formats. The most important part is the processing of nested conditions. Since the nested conditions are presented in the original rule file in the form of atomic conditions and logical relationships between atomic conditions are separated, it is impossible to directly match the nested conditions with the log data.

[0052] In the traditional processing method of nested conditions of Sigma rules, the mainstream method is to match the log traffic and the atomic conditions contained in the nested conditions one by one to obtain the atomic condition matching results, and then perform logical operations on these atomic condition matching results according to the logical relationship between the atomic conditions, so as to obtain the final matching results. It can be seen that since the nested conditions of Sigma rules cannot be effectively processed, the efficiency of matching the nested conditions with log data is low, and the logical relationship between the atomic matching conditions cannot be used for matching pruning. In the prior art, there is no module specifically for parsing the logical relationship between the atomic matching conditions, and only simple nested matching conditions can be processed.

[0053] In view of this, the present application parses a logical syntax tree from a rule file, obtains a pre-order traversal result stack based on the logical syntax tree, determines a nested expression based on the order in the pre-order traversal result stack, transforms the nested conditions in the rule file, and merges the atomic matching conditions and the logical relationships between the atomic matching conditions contained in the nested matching conditions. This is beneficial for the nested expressions obtained after the transformation to be directly matched with the log data. In addition, this method does not require word-by-word matching of the atomic conditions, which is beneficial for improving the matching efficiency of the nested conditions and the log data.

[0054] Figure 1 Schematic diagram of a nested condition matching method based on a logical syntax tree provided in an embodiment of the present application. Figure 1 As shown, the method includes:

[0055] S100, obtaining a logical syntax tree according to a preset rule file, wherein the rule file includes at least one nested condition, and the logical syntax tree includes a plurality of logical nodes and a plurality of logical operation nodes, wherein the plurality of logical nodes and the plurality of logical operation nodes are hierarchically nested to set the rule file.

[0056] S200, obtaining, according to the logical syntax tree, a pre-order traversal result stack into which the plurality of logical nodes and the plurality of logical operation nodes are sequentially incorporated.

[0057] S300, determining a nested expression corresponding to the at least one nested condition according to the pre-order traversal result stack.

[0058] S400: Match the traffic log to be matched according to the nested expression.

[0059] In an embodiment of the present application, the rule file is a file of a Sigma rule set in YAML format. In programming, nested conditions refer to the inclusion of one or more conditional statements in a code block of a conditional statement (such as an if statement). This structure allows complex logical judgments and execution of corresponding code blocks based on multiple conditions.

[0060] Sample code 1:

[0061] selection_1:

[0062] status:Success

[0063] selection_2:

[0064] Location / re:*<Countries you Do operate out of e,g 6B,use 0R formultiple> *

[0065] condition:(not selection_1)or(not selection_2)

[0066] Among them, "selection_1:status:Success" is the atomic condition named selection_1, and "selection_2:Location / re:*<Countries you 0o operate out of e,g 6B,use 0R formultiple> *" is the atomic condition named selection_2, and "condition:(not selection_1)or(notselection_2)" is the nested condition of the atomic conditions selection_1 and selection_2. It can be seen that the nested conditions are stored in the rule file separately with the logical relationship between the atomic conditions and the atomic conditions, and cannot be directly matched with the log data.

[0067] Based on this, the present application provides a nested condition matching method based on a logical syntax tree. S100 first parses the logical syntax tree according to the nested conditions in the rule file, such as Figure 2 As shown, Figure 2 It is a structural schematic diagram of a logical syntax tree provided in an embodiment of the present application, wherein the logical syntax tree includes multiple logical nodes and multiple logical operation nodes, and the multiple logical nodes and the multiple logical operation nodes are in a hierarchical nested relationship, wherein the logical operation nodes store each logical operation symbol in the nested conditions, and the logical nodes store each element of the non-logical operation symbol in the nested conditions.

[0068] According to the logical syntax tree parsed in S100, an intermediate stack is created in S200 to obtain a pre-order traversal result stack, such as Figure 3 As shown, Figure 3 It is a schematic diagram of a pre-order traversal result stack provided by an embodiment of the present application. The elements of each logic node in the logic syntax tree and the logic operation symbols of each logic operation node are stored in the pre-order traversal result stack in sequence according to the order of pre-order traversal. It should be noted that the pre-order traversal result stack is only an intermediate stack, and its purpose is to push each element in the logic syntax tree into the stack in the order of pre-order traversal so that they can be popped out in sequence later to construct nested expressions. Therefore, the atomic conditions stored in the pre-order traversal result stack are only the atomic condition names, and there is no need to store the entire atomic condition data in the pre-order traversal result stack.

[0069] According to the pre-order traversal result stack obtained in S200, a temporary stack is created in S300 to construct a nested expression, such as Figure 4 As shown, Figure 4It is a temporary stack change schematic diagram provided in an embodiment of the present application. The elements in the pre-order traversal result stack are popped from the top of the stack one by one and pushed into the temporary stack to construct a nested expression. It should be noted that the temporary stack is used to store the intermediate results of the nested expression construction process. Finally, all the elements in the pre-order traversal result stack are popped out and pushed into the temporary stack one by one. At this time, the elements obtained in the temporary stack are the nested expressions. The atomic conditions in the nested expressions are complete atomic condition data. When the atomic condition name is popped from the pre-order traversal stack and pushed into the temporary stack, what is pushed into the temporary stack is the complete atomic condition, including the atomic condition name and the atomic condition data. For example, when selection_1 is popped from the pre-order traversal stack, the temporary stack stores the complete information of the atomic condition selection_1, that is, "selection_1:status:Success" does not only refer to the atomic condition name.

[0070] Finally, the nested expression obtained in step S300 is directly matched with the log data, wherein the nested expression and the log data are both a whole piece of code data. During the matching process, the code segment of the nested expression needs to be traversed word by word to find out whether there are corresponding characters in the log data segment that can match it.

[0071] Specifically, S100 obtains a logical syntax tree according to a preset rule file, including:

[0072] S101: Obtain a logical expression according to the atomic condition names in the rule file and the logical operators between the atomic conditions.

[0073] S102: Parse the logical expression to obtain the logical syntax tree.

[0074] In an embodiment of the present application, it is necessary to define the grammar file of ANTLR before reading the rule file. ANTLR refers to an open source grammar analyzer that can automatically generate a grammar tree according to the input and display it visually, which is beneficial to automatically generate a logical grammar tree by parsing logical expressions in the present application, thereby improving parsing efficiency. The rule file is a Sigma rule set in yaml format. First, after reading the rule file in S101 to obtain the rule data stream, the rule data stream is processed by logical expression parsing, grammar tree pre-order traversal, and nested expression construction. In a possible implementation method, Flink can be used to read the rule data stream. Flink is a processing framework for a big data processing engine, which is mainly a framework and a distributed processing engine for processing streams, and is used to calculate the state of unbounded and bounded data.

[0075] In the embodiment of the present application, the regular data stream obtained by S101 (as shown in example code 1) is parsed for logical expressions and parsed into "condition: (not selection_1) or (not selection_2)", where selection_1 and selection_2 are both atomic condition names, not and or are logical operation symbols, and the logical operation symbols between selection_1 and selection_2 determine that the row of data is a logical expression.

[0076] In the embodiments of the present application, Figure 2 As shown, the logical expression (not selection_1) or (not selection_2) obtained in S101 is parsed from the first element "(", and then parsed backwards until the last element ")". The open source parser can automatically generate a logical syntax tree through the logical expression, store the logical operation symbols in the logical expression in the logical operation nodes in the logical syntax tree, and store the non-logical operation symbol elements in the logical expression in the logical nodes in the logical syntax tree, where Expr is a node in the tree, and the nodes are divided into logical nodes and logical operation nodes, where the value of the logical node can be an ID expression (for example, selection_1 or selection_2), or just a placeholder (for example, brackets or space), and the value of the logical operation node is a logical operation symbol (for example, AND / OR). There is also a possible situation that when the logical node is a non-leaf node, the value of the logical node may be a subtree.

[0077] Specifically, S200 obtains a pre-order traversal result stack in which the multiple logic nodes and the multiple logic operation nodes are sequentially merged according to the logical syntax tree, including: merging the multiple logic nodes and the multiple logic operation nodes into an intermediate stack in hierarchical order, wherein the intermediate stack includes multiple empty stack units, to obtain the pre-order traversal result stack.

[0078] In the embodiment of the present application, the intermediate stack is used to reflect the order in which the elements in the logical syntax tree are pushed into the stack in the order of pre-order traversal, so that they can be popped out in sequence and stored in a temporary stack for constructing nested expressions. Therefore, the atomic conditions stored in the pre-order traversal result stack are only the atomic condition names, and there is no need to store the entire atomic condition sequence in the pre-order traversal result stack.

[0079] Traversing a binary tree means visiting each node in the tree along a certain search path, so that each node is visited once and only once. Traversing a binary tree requires deciding the order of visiting the root node N, the left subtree L, and the right subtree. Common traversal methods for binary trees include pre-order traversal (NLR), in-order traversal (LNR), and post-order traversal (LRN). The "order" refers to when the root node is visited.

[0080] In a possible implementation, the present application adopts a pre-order traversal method according to the hierarchical order. Specifically, the merging of the plurality of logic nodes and the plurality of logic operation nodes into an intermediate stack according to the hierarchical order includes:

[0081] Starting from the leftmost node at the first level of the logical syntax tree, traverse rightwards to the rightmost node at the first level;

[0082] Starting from the leftmost node of the current level pointed to by the leftmost node of the previous level, traverse rightward to the rightmost node of the current level in sequence, until the nodes of the current level pointed to by the nodes of the previous level are traversed in sequence, traversing all nodes in the logical syntax tree.

[0083] The stacking of each element in the logical syntax tree needs to be discussed in multiple cases according to the node value of each node in the logical syntax tree. The node value includes the atomic condition name, logical operator and null, specifically including:

[0084] When the traversed element is a placeholder, the node value is empty, and the placeholder is not stored in the pre-order traversal result stack;

[0085] When the traversed element is an atomic condition name, the atomic condition name is stored in the pre-order traversal result stack;

[0086] When the traversed element is a logical operation symbol, the logical operation symbol is stored in the pre-order traversal result stack.

[0087] In an embodiment of the present application, the algorithm idea of ​​the pre-order traversal is to first visit the root node, then pre-order traverse the left subtree, and finally pre-order traversal the right subtree. If the binary tree node value is empty (for example, a placeholder bracket), the placeholder is not stored in the pre-order traversal result stack. If the binary tree node value is not empty, the case is discussed separately. One case is when the binary tree node element is an atomic condition name, that is, the node value is an ID expression (for example, selection_1 or selection_2), then the atomic condition name is stored in the pre-order traversal result stack. If the binary tree node value is not empty, another case is when the binary tree node element is a logical operation symbol, then the logical operation symbol is stored in the pre-order traversal result stack.

[0088] For example, the present application embodiment will be combined with Figure 2 and Figure 3 Detailed description as shown below: Figure 2 As shown, first traverse the root node "expr: 2", expr in the stack expresses the value of empty or a subtree, expr does not need to be reflected in the pre-order traversal result stack, here "expr2" represents the value of a tree, traverse the left subtree first, then the middle subtree, and finally the right subtree in hierarchical order, traverse the leftmost leaf node "expr4" in the first-level node, expr4 represents the value of the left subtree, traverse the middle leaf node "OR", OR is a logical operator, so the leaf node value is a logical operator, directly push OR to the bottom of the pre-order traversal result stack, and finally traverse the right leaf node "expr4", after traversing the branch subtree A under the root node (as shown in Figure 2 The box line A shown in the box continues to traverse the branch subtree B under the first-level node (such as Figure 2 The frame line shown is B).

[0089] The traversal process of the branch subtree B under the first-level node is to start traversing from the leftmost child node pointed to by the leftmost node "expr4" of the parent node, first traverse "(", because the brackets are placeholders, the node value is empty and does not need to be pushed into the stack, continue to traverse the middle node "expr3", expr3 represents the value of the middle subtree, continue to traverse the right node ")", because the brackets are placeholders, the node value is empty and does not need to be pushed into the stack, then traverse the child node pointed to by the rightmost node "expr4" of the parent node and start traversing, first traverse "(", because the brackets are placeholders, the node value is empty and does not need to be pushed into the stack, continue to traverse the middle node "expr3", and finally traverse the right node ")", because the brackets are placeholders, the node value is empty and does not need to be pushed into the stack, after traversing the branch subtree B under the first-level node, continue to traverse the branch subtree C under the second-level node (such as Figure 2 The frame selected as shown is C).

[0090] The traversal process of the branch subtree C under the secondary node starts from the node "expr3" of the parent node, and first traverses the leftmost leaf node "NOT". NOT is a logical operator, so NOT is directly stored in the traversal result stack, and then traverses the leaf nodes "expr5" to the right one by one. The "expr5" leaf node value points to the leaf node "ID: selection_01". selection_01 is an ID expression, so selection_01 is directly stored in the pre-order traversal result stack. After the traversal of the left subtree is completed, continue to traverse to the right one by one. When the logical operator "NOT" is traversed, NOT is directly stored in the traversal result stack. Then traverse the leaf nodes "expr5" to the right one by one. The "expr5" leaf node value points to the leaf node "ID: selection_02". selection_02 is an ID expression, so selection_02 is directly stored in the pre-order traversal result stack, and finally get the following Figure 3 It should be noted that this embodiment is supplemented by the explanation of Figure 2 The subtree selected in the middle box is only part of the subtree, and the traversal order needs to be Figure 2 All nodes in the traversal are completed.

[0091] In the embodiment of the present application, after the pre-order traversal stack obtained by step S200, a temporary stack needs to be newly created to construct a nested expression. Specifically, the temporary stack established in step S300 is combined with the pre-order traversal result stack to obtain a nested expression. The temporary stack includes the atomic condition data corresponding to the atomic condition name, including:

[0092] S301: Establish a temporary stack, wherein the temporary stack includes a top stack and a secondary stack;

[0093] S302: If the current element is an atomic condition name, after popping the atomic condition name from the pre-order traversal result stack, the atomic condition name and its corresponding atomic condition data are stored in the temporary stack;

[0094] S303: If the current element is a logical operator and / or, the top element and the next top element are popped from the temporary stack, and the atomic conditions with the same name corresponding to the top element and the next top element are defined respectively, and the corresponding logical expression is constructed in combination with the logical operator and / or, and stored in the temporary stack;

[0095] S304: If the current element is a logical operator negative, pop the top element from the temporary stack, perform a negation operation to obtain a corresponding logical expression, and store it in the temporary stack;

[0096] S305: After all elements of the pre-order traversal result stack are popped out and stored in the temporary stack, a nested expression is obtained according to all elements in the temporary stack.

[0097] In the embodiment of the present application, a temporary stack is established, including a top stack and a secondary stack. According to the pre-order traversal stack obtained in step 200, each element in the stack is popped out from the top stack in turn and stored in the temporary stack, and it is determined whether the popped element is a logical operator. If it is a non-logical operator, in one possible implementation, it is an atomic condition name, then the atomic condition data corresponding to the atomic condition name is stored in the temporary stack. If it is a logical operator symbol, it is necessary to further determine whether the logical operator symbol is and / or or no, and perform different data processing respectively. Specifically, Figure 4 As shown, the embodiment of the present application will be combined with Figure 4 The temporary stack change diagram shown in the figure details the construction process of the nested expression.

[0098] like Figure 4As shown, the first element popped out of the pre-order traversal stack is "selection_2", and selection_2 is a non-logical operator. Then, after the atomic condition name selection_2 is popped out from the pre-order traversal result stack, the atomic condition name selection_2 and the complete atomic condition data corresponding to selection_2 are stored in a temporary stack. Secondly, the second element popped out of the pre-order traversal stack is "NOT", and NOT is a logical operator. Then, the top element is popped out of the temporary stack, and the inversion operation is performed on the atomic condition selection_2 to obtain the corresponding logical expression not selection_2, that is, the complete atomic condition data selection_2 is inverted and stored in the secondary stack of the temporary stack. Then, the third element "selection_1" of the pre-order traversal stack is popped out. selection_1 is a non-logical operator. Then, after the atomic condition name selection_1 is popped out from the pre-order traversal result stack, the atomic condition name selection_1 and the complete atomic condition data corresponding to selection_1 are stored in a temporary stack. Continue to pop the fourth element "NOT" of the pre-order traversal stack. NOT is a logical operator, then pop the top element from the temporary stack, perform the negation operation on the atomic condition selection_1 to obtain the corresponding logical expression not selection_1, that is, negate the complete atomic condition data selection_1, and store it at the top of the temporary stack. Finally, pop the last element "OR" of the pre-order traversal stack. OR is a logical operator, then pop the top element and the second top element of the temporary stack, and define the atomic conditions of the same name corresponding to the top element notselection_1 and the second top element not selection_2, respectively. Combine the logical operator to construct the corresponding logical expression (not selection_1) or (not selection_2), and store it in the temporary stack. At this time, all elements in the temporary stack are the constructed nested expressions. It should be noted that, if Figure 4 In the logical expression (not selection_1) or (not selection_2) shown, selection_1 and selection_2 are both abbreviations of atomic conditions. Only the atomic condition names are written out. In fact, the complete atomic condition data corresponding to selection_1 and selection_2 are obtained in the end, and the nested expression is obtained.

[0099] The method described in the embodiment of the present application also includes: removing redundant placeholders generated by multiple consecutive logical operators and / or connected logical expressions by merging the same logical operators. For example, the expression selection_01AND selection_02AND selection_03 connected by two ANDs will be parsed into (selection_01AND selection_02)AND selection_03 according to the above process nested expression, where there are redundant brackets between the three atomic conditions. A judgment logic can be added to the process to remove the redundant brackets by merging the same logical operators.

[0100] In the embodiment of the present application, the constructed nested expression needs to be matched with the log traffic data. A possible implementation method can consume log data from kafka and parse the relevant fields to form log data. In the embodiment S400 of the present application, the nested expression and the log data are matched. For example, taking the sub-expression with the value of Status in the nested expression as an example, Success is searched in the entire log. If Success cannot be found in the log data, the match fails and the match result is no. If the inverse value is true, it means that the match result is inverted. At this time, the no of the match result is inverted, and the match result is yes, so the match result becomes successful. Similarly, the log and the sub-expression with the value of Location in the nested expression are matched successfully. Finally, the log and the rule are matched successfully and an alarm will be generated.

[0101] like Figure 5 As shown, Figure 5 It is a real-time alarm rule matching flow diagram based on logical syntax tree parsing provided by an embodiment of the present application, in which a logical expression is parsed for nested atomic conditions in an original rule file, a logical syntax tree is established for pre-order traversal, a rule data model is obtained by constructing a nested expression, a log data model is obtained according to log data parsing, and the obtained rule data model is matched with the log data model. If the match is successful, an alarm will be issued to prove that the model matches successfully. The present application obtains a pre-order traversal result stack by parsing the logical syntax tree, and then transforms the Sigma rule nested conditions by combining the pre-order traversal result stack with a temporary stack, which is conducive to the nested expression obtained after the transformation can be directly matched with the log data.

[0102] See also Figure 6 , shows a structural diagram of a nested condition matching system based on a logical syntax tree provided in an embodiment of the present application. For the sake of ease of explanation, only the parts related to the embodiment of the present application are shown.

[0103] The nested condition matching system 500 may specifically include the following modules:

[0104] A logical syntax tree parsing module 510 is used to parse a rule file to obtain a logical syntax tree, wherein the logical syntax tree includes a plurality of nodes, each node stores an element, and the rule file is a file of a Sigma rule set in a YAML format, and the rule file includes nested conditions;

[0105] A pre-order traversal result stack module 520 is used to establish an intermediate stack to store elements of each node in the logical syntax tree to obtain a pre-order traversal result stack;

[0106] A nested expression module 530, used to establish a temporary stack and combine it with the pre-order traversal result stack to obtain a nested expression;

[0107] The matching module 540 is used to match the nested expression with the log data.

[0108] The logical syntax tree parsing module 510 includes the following submodules:

[0109] A reading module 511 is used to read a rule file through a first processing framework to obtain a rule data stream;

[0110] A logic expression extraction module 512 is used to extract logic expressions according to the atomic condition names and the logical operators between the atomic conditions in the rule data flow;

[0111] The parsing module 513 is used to parse the logical expression from the first element in the logical expression in sequence, and store the elements obtained by parsing in sequence into the nodes in the logical syntax tree, where each node in the logical syntax tree is each element in the logical expression.

[0112] The pre-order traversal result stack module 520 includes the following sub-modules:

[0113] An intermediate stack building module 521, used to build an intermediate stack and traverse the logical syntax tree according to a pre-order traversal sequence;

[0114] The stacking module 522 stacks the elements obtained by traversing each node in sequence to obtain a pre-order traversal result stack.

[0115] The intermediate stack building module 521 exemplarily traverses from the leftmost node under the root node of the first level of the logical syntax tree to the rightmost node under the root node of the first level;

[0116] Starting from the leftmost node of the current level pointed to by the leftmost node of the previous level, traverse rightward to the rightmost node of the current level in sequence, until the nodes of the current level pointed to by the nodes of the previous level are traversed in sequence, traversing all nodes in the logical syntax tree.

[0117] The stacking module 522, exemplarily, when the traversed element is a placeholder, the node value is empty, and the placeholder is not stored in the pre-order traversal result stack;

[0118] When the traversed element is an atomic condition name, the atomic condition name is stored in the pre-order traversal result stack;

[0119] When the traversed element is a logical operation symbol, the logical operation symbol is stored in the pre-order traversal result stack.

[0120] The nested expression module 530 includes the following submodules:

[0121] A temporary stack establishment module 531 is used to establish a temporary stack, wherein the temporary stack includes a top stack and a secondary stack;

[0122] The judgment module 532 is specifically used for: if the current element is an atomic condition name, popping the atomic condition name from the pre-order traversal result stack, and storing the atomic condition name and its corresponding atomic condition data in the temporary stack;

[0123] If the current element is a logical operator and / or, the top element and the next top element are popped from the temporary stack, and the atomic conditions with the same name corresponding to the top element and the next top element are defined respectively, and the corresponding logical expression is constructed in combination with the logical operator and / or and stored in the temporary stack;

[0124] If the current element is a logical operator negative, the top element is popped from the temporary stack, a negation operation is performed to obtain a corresponding logical expression, and the expression is stored in the temporary stack;

[0125] The result module 532 is used to pop all the elements of the pre-order traversal result stack and store them in the temporary stack, and then obtain a nested expression according to all the elements in the temporary stack.

[0126] Figure 7 Schematic diagram of the structure of the terminal device provided in the embodiment of the present application. Figure 7 As shown, the terminal device 700 of this embodiment includes: at least one processor 710 ( Figure 7 Only one is shown in the figure), a memory 720, and a computer program 721 stored in the memory 720 and executable on the at least one processor 710, wherein the processor 710 implements the steps in the above method embodiment when executing the computer program 721.

[0127] The terminal device 700 may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal device may include, but is not limited to, a processor 710 and a memory 720. Those skilled in the art will appreciate that Figure 7 It is only an example of the terminal device 700 and does not constitute a limitation on the terminal device 700. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components, for example, it may also include input and output devices, network access devices, etc.

[0128] The processor 710 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0129] In some embodiments, the memory 720 may be an internal storage unit of the terminal device 700, such as a hard disk or memory of the terminal device 700. In other embodiments, the memory 720 may also be an external storage device of the terminal device 700, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the terminal device 700. Further, the memory 720 may also include both an internal storage unit of the terminal device 700 and an external storage device. The memory 720 is used to store an operating system, an application program, a boot loader (Boot Loader), data and other programs, such as the program code of the computer program, etc. The memory 720 may also be used to temporarily store data that has been output or is to be output.

[0130] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0131] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0132] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in this application can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0133] In the embodiments provided in the present application, it should be understood that the disclosed devices / terminal equipment and methods can be implemented in other ways. For example, the device / terminal equipment embodiments described above are only schematic. For example, the division of the modules or units 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 interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0134] 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.

[0135] 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.

[0136] If the integrated module / 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 storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0137] The present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed through a computer program product. When the computer program product runs on a terminal device, the terminal device can implement the steps in the above-mentioned method embodiments when executing.

[0138] The above-described embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application is described in detail with reference to the above-mentioned embodiments, a person skilled in the art should understand that the technical solutions described in the above-mentioned embodiments can still be modified, or some of the technical features can be replaced by equivalents; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application.

Claims

1. A nested condition matching method based on a logical syntax tree, characterized in that: The method comprises: According to a preset rule file, a logical syntax tree is obtained, wherein the rule file includes at least one nested condition, and the logical syntax tree includes a plurality of logical nodes and a plurality of logical operation nodes, wherein the plurality of logical nodes and the plurality of logical operation nodes are hierarchically nested to set the rule file; According to the logical syntax tree, a pre-order traversal result stack is obtained in which the plurality of logical nodes and the plurality of logical operation nodes are sequentially incorporated; Determine a nested expression corresponding to the at least one nested condition according to the pre-order traversal result stack; The traffic log to be matched is matched according to the nested expression.

2. The method according to claim 1, characterized in that: The logical syntax tree is obtained according to the preset rule file, including: Obtaining a logical expression according to the atomic condition name in the rule file and the logical operators between the atomic conditions; The logical expression is parsed to obtain the logical syntax tree.

3. The method according to claim 1, characterized in that: The step of obtaining, according to the logical syntax tree, a pre-order traversal result stack into which the plurality of logical nodes and the plurality of logical operation nodes are sequentially incorporated comprises: The plurality of logic nodes and the plurality of logic operation nodes are merged into an intermediate stack according to the hierarchical order, wherein the intermediate stack includes a plurality of empty stack cells, to obtain the pre-order traversal result stack.

4. The method according to claim 3, characterized in that: The step of merging the plurality of logic nodes and the plurality of logic operation nodes into an intermediate stack in hierarchical order includes: Starting from the leftmost node at the first level of the logical syntax tree, traverse rightwards to the rightmost node at the first level; Starting from the leftmost node of the current level pointed to by the leftmost node of the previous level, traverse rightward to the rightmost node of the current level in sequence, until the nodes of the current level pointed to by the nodes of the previous level are traversed in sequence, traversing all nodes in the logical syntax tree.

5. The method according to claim 3, characterized in that: The step of merging the plurality of logic nodes and the plurality of logic operation nodes into an intermediate stack in hierarchical order, wherein the node value includes an atomic condition name, a logic operator and null, comprises: When the traversed element is a placeholder, the node value is empty, and the placeholder is not stored in the pre-order traversal result stack; When the traversed element is an atomic condition name, the atomic condition name is stored in the pre-order traversal result stack; When the traversed element is a logical operation symbol, the logical operation symbol is stored in the pre-order traversal result stack.

6. The method according to claim 1, characterized in that: The determining, according to the pre-order traversal result stack, a nested expression corresponding to the at least one nested condition comprises: Establishing a temporary stack, wherein the temporary stack includes a top stack and a secondary stack; If the current element is an atomic condition name, after popping the atomic condition name from the pre-order traversal result stack, the atomic condition name and its corresponding atomic condition data are stored in the temporary stack; If the current element is a logical operator and / or, the top element and the next top element are popped from the temporary stack, and the atomic conditions with the same name corresponding to the top element and the next top element are defined respectively, and the corresponding logical expression is constructed in combination with the logical operator and / or and stored in the temporary stack; If the current element is a logical operator negative, the top element is popped from the temporary stack, a negation operation is performed to obtain a corresponding logical expression, and the expression is stored in the temporary stack; After all elements of the pre-order traversal result stack are popped and stored in the temporary stack, a nested expression is obtained according to all elements in the temporary stack.

7. The method according to claim 6, characterized in that: The method further comprises: By merging the same logical operators, redundant placeholders generated by multiple consecutive logical operators and / or connected logical expressions are eliminated.

8. A nested condition matching system based on a logical syntax tree, characterized in that: The nested condition matching system comprises: A logical syntax tree parsing module, used to obtain a logical syntax tree according to a preset rule file, wherein the rule file includes at least one nested condition, and the logical syntax tree includes a plurality of logical nodes and a plurality of logical operation nodes, wherein the plurality of logical nodes and the plurality of logical operation nodes are hierarchically nested to set the rule file; A pre-order traversal result stack module is used to obtain a pre-order traversal result stack in which the plurality of logic nodes and the plurality of logic operation nodes are sequentially incorporated according to the logic syntax tree; A nested expression module, used to determine a nested expression corresponding to the at least one nested condition according to the pre-order traversal result stack; The matching module is used to match the traffic log to be matched according to the nested expression.

9. A terminal device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Cited By

  • Condition analysis execution method and device based on dynamic configuration and computer equipment

    CN120723814A

  • Configuration solving method and device based on multi-normal-form analysis and electronic equipment

    CN121560324A

  • Intelligent agent tool dynamic optimization and hot update method, device, equipment and system based on runtime feedback

    CN122285047A

  • Agent tool dynamic optimization and hot updating method, device, equipment and system based on runtime feedback

    CN122285047B

  • Text parsing method, system, and related device

    WO2026145477A1