A network traffic restoration method based on custom protocol description language
By customizing the protocol description language and protocol parser, automatic analysis and dynamic expansion of network traffic are realized, which solves the problems of low development efficiency and insufficient real-time analysis in the existing technology, and is suitable for application scenarios with high real-time performance and security. .
Patent Information
- Application Number
- CN202211548448.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-05
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2042-12-05
AI Technical Summary
In the existing technology, the development efficiency of network traffic analysis code is low, the workload is large, and it is difficult to dynamically expand. Moreover, the hard-coded implementation leads to a decrease in the real-time performance of the analysis, making it difficult to adapt to application scenarios with strong real-time performance and high security levels.
The network protocol structure and field information are defined based on a custom protocol description language, and automatic parsing is realized through the protocol parser, and the protocol parsing logic is dynamically expanded and rapidly updated.
It improves development efficiency, reduces workload, realizes dynamic expansion and rapid update of protocol analysis, adapts to real-time application scenarios, and ensures efficient network traffic analysis.
Smart Images

Figure CN116016345B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of network data processing, and in particular to a network traffic restoration method based on a user-defined protocol description language. Background Art
[0002] Network traffic restoration refers to parsing the traffic data transmitted in the network, identifying the protocol type carried in the data packet header layer by layer and restoring it to the required data format. Protocol parsing results are an important source of data in the fields of traffic analysis, firewalls, intrusion detection, etc. The common means of obtaining protocol parsing results is to develop various protocol parsing codes to parse the captured network traffic data. At present, some companies have developed efficient and robust parsing codes for various protocols. However, due to the strong specialization and low scalability of protocol parsing codes, or the fact that they are not open source for commercial purposes, it is difficult for other projects to reuse them. Therefore, in new projects, developers often need to use dedicated development languages to develop parsing codes for each protocol one by one.
[0003] However, developing high-quality protocol parsing code is not an easy task. First, due to the large number of network protocols, protocol parsing code development is time-consuming and labor-intensive. Developers need to comprehensively consider multiple issues such as project architecture, input and output, exception packet processing, and code performance. Secondly, code vulnerabilities will inevitably appear during the development process, which will lead to varying degrees of security issues. Finally, the protocol specification document described in natural language may cause developers to have ambiguity when reading it and write parsing code that does not meet the standards.
[0004] In addition, the use of hard-coded methods to restore network traffic can easily lead to a decrease in the real-time performance of parsing. When dealing with new protocol adaptation, updating the logic of protocol parsing code, fixing parsing code vulnerabilities, etc., it is often necessary to suspend network traffic parsing. This is unacceptable in some application scenarios with strong real-time performance and high security level. Summary of the invention
[0005] The present invention mainly solves the problems of low code development efficiency, large workload, and difficulty in dynamic expansion to adapt to real-time application scenarios in hard-coded network traffic analysis, and provides a network traffic restoration method based on a custom protocol description language. By using a custom protocol description grammar to define the protocol structure and field information, and using a protocol parser to realize automatic parsing of network traffic, the development workload can be effectively reduced and developers can quickly obtain protocol parsing results, while realizing dynamic expansion and rapid update of new protocol parsing logic.
[0006] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0007] A network traffic restoration method based on a custom protocol description language, comprising:
[0008] Step 1: Use a custom protocol description language to define the structure and internal fields of the network protocol to be parsed and save them in a configuration file; format all configuration files into standard configuration files and register them in the configuration file management module;
[0009] Step 2: Read the standard configuration file, parse and generate a protocol template;
[0010] Step 3: Register the protocol template to the protocol template library to build the association relationship between the upper and lower level protocols;
[0011] Step 4, obtain the original network flow data and encapsulate it into a standard network flow;
[0012] Step 5, read the standard network flow, and match the corresponding protocol template from the protocol template library according to the association relationship between the upper and lower layer protocols;
[0013] Step 6: For the successfully matched standard network flow, the corresponding parsing logic is executed according to the field type in the protocol template to restore the flow;
[0014] Step 7: Export the results of traffic restoration.
[0015] Furthermore, in step 1, the custom protocol description language includes protocol class, field class, attribute class and dynamic expression class information; wherein, the protocol class information includes the protocol name, protocol version, lower layer protocol type, and upper layer protocol associated field; the field class information includes basic type field, branch type field, and loop type field; the attribute class information includes the field name, field data length, field parsing type, field export type, field data length determination method, whether the field is parsed, and whether the field is exported; the dynamic expression class information is: numerical expression and Boolean expression; the expression is composed of expression brackets, operators, field reference symbols and field names.
[0016] Furthermore, the branch type field includes an if field and a switch field; wherein the if field consists of a Boolean dynamic expression and two true and false branches; the switch field consists of a branch determination field and a branch set; the loop type field includes a for field and a while field; wherein the for field consists of a numerical dynamic expression and a field array; the while field consists of a Boolean dynamic expression and a field array.
[0017] Furthermore, the step 2 is specifically as follows: read the standard configuration file and parse the protocol description line by line; create objects field by field according to the field type in the protocol description, and initialize the static attribute information and dynamic expression information of the field; connect the fields according to the field reference relationship in the dynamic expression; assemble the fields into a protocol template according to the nested relationship in the protocol definition.
[0018] Furthermore, the specific process of step 3 is: registering the protocol template to the protocol template library; identifying the fields in its template according to the upper-layer protocol associated fields defined by the protocol, and registering the protocol and associated field information in the protocol template library; parsing the protocol type in the lower-layer protocol associated information of the protocol, and querying whether there is a corresponding protocol template in the protocol template library. If so, connecting the two to obtain the association relationship between the upper and lower layer protocols.
[0019] Furthermore, the step 6 executes the corresponding parsing logic according to the field type in the protocol template to restore the traffic, specifically: select the execution logic according to the field type: for the basic field, obtain the static attributes and dynamic expression values of the parsed field; for the branch field, select the field array in the specific parsing branch according to the dynamic expression; for the loop field, determine the number of times the field array in the loop body is parsed according to the dynamic expression; calculate its data length according to the data length determination method of the field; determine whether the field is parsed, if parsed, identify the data type of the field, and read the corresponding bytes according to the data length for parsing to obtain the parsing result.
[0020] Furthermore, in step 7, the result of traffic restoration is exported, specifically, by using a JSON structure to store the structural information of the protocol after traffic restoration and the parsing result data of each field.
[0021] The beneficial effects of the present invention are:
[0022] 1. Use custom protocol description language to define the structure and internal field types of network protocols, which can quickly and accurately describe protocol structure information and field attribute information. At the same time, by defining new protocols using custom protocol description language, it can quickly expand support for new protocol traffic analysis, which has good scalability and flexibility.
[0023] 2. The protocol parser parses the protocol description language to generate a protocol template, and implements automated protocol parsing by using the protocol template to match the network flow, which can effectively reduce the workload and improve code development efficiency.
[0024] 3. The protocol parser consists of four modules: parsing engine, protocol template library, execution engine and export engine. Each module provides a concise functional interface to the outside world. By calling the interface, the automatic parsing of network flow can be quickly realized.
[0025] 4. The protocol parser automatically exports the parsing results to JSON data format according to the user configuration. JSON is a nested data format that can clearly display the structural information of the protocol. At the same time, as a universal data structure, JSON can be parsed in multiple languages and can be easily shared across platforms. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is a framework diagram of the network traffic restoration method based on the custom protocol description language of the present invention.
[0027] Figure 2 It is a flow chart of the network traffic restoration method based on the custom protocol description language of the present invention.
[0028] Figure 3 It is a unified parsing logic step based on field attributes of the protocol parser of the present invention. DETAILED DESCRIPTION
[0029] A network traffic restoration method based on a custom protocol description language uses a custom protocol description language to define the protocol, matches the protocol template generated by the protocol parser with the network traffic, and restores the traffic according to a unified parsing logic based on field attributes, thereby achieving automatic parsing of network traffic and dynamic expansion of the protocol. The method mainly includes the following processes:
[0030] Step 1: Use a custom protocol description language to describe the structure and field information of the network protocol to be parsed, and save the description content in the configuration file.
[0031] Step 2: Create a protocol parser instance object to maintain and manage the generated protocol template, and temporarily store the status information and intermediate results of the parsing process.
[0032] Step 3: Load and parse the protocol descriptions in all configuration files, and then generate the corresponding protocol template objects.
[0033] Step 4: Register each agreement template in the agreement template library for unified management and maintenance, and build a hierarchical association relationship between agreement templates.
[0034] Step 5: Read the network protocol data stream input and obtain the corresponding protocol template in the protocol template library. The protocol data stream is matched with the protocol template and traffic is restored according to the unified parsing logic based on field attributes.
[0035] Step 6: Export the parsing results in a user-defined format and assemble them in JSON format.
[0036] Preferably, the custom description grammar includes the following contents:
[0037] The protocol description language contains four types of description information definitions, including: protocol class, field class, attribute class and dynamic expression class.
[0038] Protocol information includes: protocol name, protocol version, upper layer protocol type, and lower layer protocol associated fields.
[0039] Field type information includes: basic type fields, branch type fields, and loop type fields.
[0040] Attribute information includes: field name, field data length, field parsing type, field export type, field data length determination method, whether the field is parsed, and whether the field is exported.
[0041] There are two types of dynamic expressions: numerical expressions and Boolean expressions. An expression consists of expression brackets, operators, field references, and field names.
[0042] Preferably, the branch type field includes two types: if field and switch field. The if field consists of a Boolean dynamic expression and two true and false branches; the switch field consists of a branch determination field and a branch set;
[0043] Preferably, the loop type field includes two types: a for field and a while field. The for field is composed of a numerical dynamic expression and a field array; the while field is composed of a Boolean dynamic expression and a field array;
[0044] Preferably, the structure of the protocol parser includes a parsing engine, a protocol template library, an execution engine and an export engine.
[0045] As a preferred method, the configuration file is parsed to generate a protocol template object, and the specific process is as follows:
[0046] Initialize the parsing engine, read the configuration file and parse the protocol description line by line.
[0047] Create objects field by field according to the field type in the protocol description, and initialize the static attribute information and dynamic expression information of the field.
[0048] Connect fields based on the field reference relationship in the dynamic expression.
[0049] Assemble the fields into a protocol template object according to the nested relationship in the protocol definition.
[0050] As a preferred method, all protocol templates are registered to the protocol template library for management and hierarchical association. The specific process is as follows:
[0051] The protocol parser registers the generated protocol template into the internal protocol template library for unified maintenance and management, and provides operations such as adding, deleting, checking and modifying the protocol template.
[0052] The fields in the template are identified according to the upper layer protocol associated fields defined by the protocol, and the protocol and associated field information are registered in the protocol template library.
[0053] The protocol type in the lower layer protocol association information of the protocol is parsed, and a corresponding protocol template object is queried in the protocol template library. If so, the two are connected.
[0054] As a preferred method, traffic restoration is performed according to a unified parsing logic based on field attributes. The specific process is as follows:
[0055] The execution logic is selected according to the field type. For branch fields, the field array in the specific parsing branch is selected according to the dynamic expression; for loop fields, the number of times the field array in the loop body is parsed is determined according to the dynamic expression. For basic fields, the static properties and dynamic expression values of the parsed fields are obtained.
[0056] According to the data length determination method of the field, the corresponding algorithm is selected to calculate its data length.
[0057] Determine whether the field is parsed. If so, identify the data type of the field and read the corresponding bytes according to the data length for parsing. Otherwise, end the parsing.
[0058] After the field is parsed, determine whether the field is exported. If it is exported, identify the export type of the field and export it to the corresponding JSON data format according to the field data type, otherwise end the parsing.
[0059] As a preferred method, the JSON structure is used to store the structural information of the protocol and the parsing result data of each field. At the same time, JSON, as a universal data structure, can be parsed by multiple languages and can be easily shared across platforms.
[0060] The technical solution of the present invention is further specifically described below through embodiments and in conjunction with the accompanying drawings.
[0061] A network traffic restoration method based on a custom protocol description language defines the network protocol structure and internal field types by using the protocol description syntax and stores them in the configuration file. The implemented protocol parser can parse the protocol descriptions in the configuration file one by one to generate protocol templates for maintenance by the protocol template library. It has good scalability and flexibility.
[0062] like Figure 1As shown, it includes a configuration file management module 1, a traffic input module 2, and a protocol parser 3, which internally includes a parsing engine 4, a protocol template library 5, an execution engine 6, and an export engine 7.
[0063] The configuration file management module 1 is responsible for unified management of all configuration files storing protocol descriptions, and supports dynamic loading and unloading of protocol description configuration files.
[0064] The traffic input module 2 is responsible for capturing network traffic and encapsulating it into a standard protocol analyzer traffic data input format, and then transmitting it to the protocol analyzer.
[0065] Protocol Parser 3 is the core module for implementing automated protocol parsing.
[0066] The parsing engine 4 is responsible for reading all configuration files that store protocol descriptions. The description of each protocol is parsed line by line, and the corresponding protocol template is generated. It is then registered in the protocol template library. The protocol template records the structural information of the protocol and the type of the internal fields of the protocol. It is used for the subsequent execution engine to restore the network data flow according to the protocol template.
[0067] The protocol template library 5 uniformly maintains all generated protocol templates and supports update, addition, deletion and search operations on these templates. It is also responsible for building the association relationship between protocol templates through the lower layer protocol types defined in the protocol description.
[0068] The execution engine 6 is responsible for matching and parsing the network data stream according to the protocol template. During the parsing process, the execution engine reads the fields in the protocol template one by one and parses the data stream according to the field type. When parsing each field, the execution engine needs to process the static attributes in the field; for dynamic expressions, the execution engine needs to index the previously parsed field values and introduce them into the expression for calculation, and finally complete the parsing of the dynamic expression.
[0069] The export engine 7 is responsible for converting the parsing results temporarily stored in the execution engine into the JSON data format, and encapsulating the parsing results of each field into a JSON object for export according to the nested relationship between the fields.
[0070] In this embodiment, the protocol syntax definition examples include Ethernet definition, IPv4 definition, IPv6 definition, TCP definition, UDP definition and HTTP definition.
[0071] In this embodiment, the configuration file is described in IPv4 syntax, and its protocol structure and internal fields are defined.
[0072] The first is the definition of IPv4 protocol class information, including protocol name, protocol version, lower layer protocol type, and upper layer protocol associated fields.
[0073] '!IPv4': Indicates the beginning of the definition of the IPv4 protocol.
[0074] 'version:4': defines the version of the IPv4 protocol.
[0075] 'from: EthernetII / 0x0800': Indicates that the lower layer protocol that defines IPv4 is EthernetII, and its associated field value is 0x0800.
[0076] 'next:protocol': indicates that the upper-layer protocol associated field that defines the IPv4 protocol is the protocol field.
[0077] Next, the structure and attributes of its internal fields are defined according to the IPv4 network protocol packet format. Its protocol internal fields include version number, header length, service type, total length, identifier, tag, segment offset, survival time, upper layer protocol, header checksum, source address, destination address, optional items and padding fields.
[0078] The fields are divided into basic fields and structure fields. Basic fields are used to describe the smallest unit with real meaning in the protocol, while structure fields are used to describe variable structural information in the protocol. The basic fields in IPv4 are version number, header length, service type, total length, identifier, tag, segment offset, survival time, upper layer protocol, header checksum, source address, and destination address. The structure fields are optional and filler fields. Their existence condition is that the header field length is greater than 4.
[0079] Secondly, the field parsing granularity can be divided according to the importance of the field information until it is divided into the basic field type. For example, if you need to pay attention to the differential service code point of the service quality, the service type field can be further split into the differential service code point and the explicit congestion notification field. If you do not need to pay attention to the optional items and fill-in fields, the options and fill-in fields can be merged into other fields. In this embodiment, the IPv4 protocol is defined as consisting of a version number, header length, differential service code point, explicit congestion notification, total length, identifier, tag, segment offset, survival time, upper layer protocol, header checksum, source address, destination address and option field.
[0080] Next is the attribute definition of the field in IPv4. In this embodiment, the protocol description syntax provides a variety of attributes for describing the field. Its definition is shown in the following table:
[0081]
[0082]
[0083] In this embodiment, the attribute definition of the field in IPv4 is as follows:
[0084] The version number is 4 digits long and is named version. The restored data type is numeric.
[0085] The header length occupies 4 bits and is named IHL. The restored data type is numeric.
[0086] The differentiated service code point occupies 6 bits and is named DSCP. The restored data type is digital.
[0087] Explicit congestion notification occupies 2 bits, named ECN, and the restored data type is digital;
[0088] The total length occupies 16 bits and is named totalLength. The restored data type is numeric.
[0089] The identifier occupies 16 bits and is named identification. The restored data type is numeric.
[0090] The flag occupies 3 bits and is named flags. The restored data type is digital.
[0091] The fragment offset occupies 13 bits and is named fragmentOffset. The restored data type is numeric.
[0092] The survival time occupies 8 bits and is named TTL. The restored data type is digital.
[0093] The upper layer protocol occupies 8 bits and is named protocol. The restored data type is digital.
[0094] The header checksum occupies 16 bits and is named checksum. The restored data type is numeric.
[0095] The source address occupies 32 bits and is named srcIP. The restored data type is an ipv4 string.
[0096] The destination address occupies 32 bits and is named dstIP. The restored data type is an ipv4 string.
[0097] Since the existence of the option field depends on the value of the total length field, when the value is greater than 5, it means that the IPv4 protocol has an option field. Therefore, the option field needs to be represented by the IF structure field.
[0098] The syntax description is as follows:
[0099]
[0100]
[0101] The definition of the protocol structure and internal fields in the TCP syntax description configuration file is as follows:
[0102] The first is the definition of TCP protocol class information, including protocol name, protocol version, lower layer protocol type, and upper layer protocol associated fields.
[0103] '! TCP': Indicates the beginning of defining the TCP protocol.
[0104] 'version:1': defines the version of the TCP protocol.
[0105] 'from:IPv4 / 6,IPv6 / 6': Indicates that the lower layer protocol of TCP is defined as IPv4 or IPv6, and its associated field value is 6.
[0106] 'next:srcPort|dstPort': indicates that the upper layer protocol associated field defining the TCP protocol is the srcPort field or the dstPort field.
[0107] Next, according to the TCP network protocol packet format, the structure and attributes of its internal fields are defined. Its protocol internal fields include source port, destination port, sequence number, acknowledgment number, offset, invalid field, flag field, window size, checksum field, urgent pointer and option field.
[0108] Next is the attribute definition of the TCP field:
[0109] The source port is 16 bits long and named srcPort. The restored data type is digital.
[0110] The destination port is 16 bits long and is named dstPort. The restored data type is numeric.
[0111] The sequence number is 32 bits long and is named seqNumber. The restored data type is numeric.
[0112] The length of the acknowledgment number is 32 bits, named ACKNumber, and the restored data type is numeric;
[0113] The offset occupies 4 bits and is named offset. The restored data type is numeric.
[0114] Invalid fields occupy 6 bits and are skipped without parsing;
[0115] The flag field occupies 6 bits and is named index. The restored data type is numeric.
[0116] The window size occupies 16 bits and is named windowSize. The restored data type is numeric.
[0117] The checksum occupies 16 bits and is named checksum. The restored data type is numeric.
[0118] The urgent pointer occupies 16 bits and is named urgentPointer. The restored data type is numeric.
[0119] Since the existence of the option field depends on the value of the total length field, when the value is greater than 5, it means that the TCP protocol has an option field. Therefore, the option field needs to be represented by the IF structure field.
[0120] The syntax description is as follows:
[0121]
[0122]
[0123] The Ethernet syntax describes the protocol structure and internal fields in the configuration file as follows:
[0124] The first is the definition of Ethernet protocol class information, including protocol name, protocol version, and upper layer protocol associated fields.
[0125] '! Ethernet': Indicates the beginning of the definition of the Ethernet protocol.
[0126] 'version:2': defines the version of the Ethernet protocol.
[0127] next:type: Indicates that the upper-layer protocol association field that defines the Ethernet protocol is the type field.
[0128] Next, according to the Ethernet network protocol packet format, the structure and attributes of its internal fields are defined. Its protocol internal fields include the destination Mac address, source Mac address, and type field.
[0129] Next is the attribute definition of the Ethernet field:
[0130] The destination Mac address is 48 bits long and is named dstMac. The restored data type is mac.
[0131] The source Mac address is 48 bits long and is named srcMac. The restored data type is mac.
[0132] The type occupies 16 bits and is named type. The restored data type is numeric.
[0133] The syntax description is as follows:
[0134]
[0135] like Figure 2 As shown, the configuration file management module performs the following steps S1: Load configuration files of all protocols and register them in the configuration file management table.
[0136] Step S1 specifically includes:
[0137] The user defines the protocol to be parsed using a custom protocol description syntax and saves the description content to a configuration file. Then, the configuration file directory is set in the configuration file management module and started.
[0138] The configuration file management module reads the configuration files of all protocols in the specified directory and formats them, including deleting comments, blank lines and extra spaces in each line, and extracting the protocol name and version number in the configuration file. It is registered in the protocol management table, and its structure and example are as follows:
[0139] name version Path Ethernet 2 / home / config / ethernet IPv4 4 / home / config / ipv4 TCP 1 / home / config / tcp
[0140] Then the formatted standard configuration file content is input to the parsing engine to generate the protocol template. The standard configuration file content input structure is as follows:
[0141]
[0142] At the same time, a task thread is started to periodically scan all configuration files in the directory to capture the addition, deletion, and update of the protocol name or version of the configuration files in the directory. When the operation occurs, the corresponding protocol information in the protocol management table is updated synchronously. Then the change information in the protocol management table is sent to the protocol template library, and the dynamic loading, unloading, and updating of the protocol template operations are synchronously performed in the protocol template library.
[0143] Step S2 specifically includes:
[0144] Parsing engine 4 executes step S2. Read the standard configuration file content input of the protocol and parse it line by line. For each line description content, the parsing engine first extracts the line operation and generates a corresponding structure object according to the line operation type. There are 5 types of line operations, including basic field definition, if field definition, switch field definition, for field definition and while field definition.
[0145] The field structure is as follows:
[0146]
[0147] For basic field definitions, the parsing engine assigns values to attribute variables in the structure according to the basic field attribute definitions, and creates indexes for the fields associated with the dynamic expressions.
[0148] Its structure is as follows:
[0149]
[0150] For the if field, the parsing engine creates an index of the field associated with the dynamic expression used to determine the branch, and then recursively parses the field arrays in the true and false branches.
[0151] Its structure is as follows:
[0152]
[0153]
[0154] For the switch field, the parsing engine creates an index of the field used to determine the branch, and then recursively parses down the fields in the branch array.
[0155] Its structure is as follows:
[0156]
[0157] For the for field, the parsing engine creates an index of the field associated with the dynamic expression used to determine the number of loops, and then recursively continues parsing the fields in the loop body field array.
[0158] Its structure is as follows:
[0159]
[0160]
[0161] For the while field, the parsing engine establishes the index of the field associated with the dynamic expression used to determine whether the loop continues, and then recursively continues parsing downward for the fields in the loop body field array.
[0162] Its structure is as follows:
[0163]
[0164] When all the structure objects corresponding to all the fields in the protocol are constructed, the parsing engine assembles the structure objects according to the nesting and order relationship of the fields to generate a protocol template object, and hands it over to the protocol template library for management.
[0165] The protocol template structure is as follows:
[0166]
[0167]
[0168] Taking the TCP protocol as an example, the generated protocol template object structure is as follows:
[0169]
[0170] The TCP field array contains basic fields and an if field. Taking the srcPort field as an example, the structure of the basic field is as follows:
[0171]
[0172]
[0173] This example uses the getOption field as an example to show the structure of the if field. Its structure is as follows:
[0174]
[0175] The protocol template library 5 executes step S3. The protocol template is added to the registry for unified management. A callable add, delete, modify and query interface is provided to the outside to realize dynamic loading, unloading, updating and searching operations of the protocol template. At the same time, the protocol template library scans the lower-layer protocol type attributes of all protocol templates, and then uses the search interface to query whether the corresponding lower-layer protocol exists in the template library. If it exists, the two protocols are connected. Thus, a hierarchical association relationship between protocols is constructed.
[0176] Take the association of IPv4 and TCP as an example: when building the TCP template, the lower-layer protocol template name defined is "IPv4", and the corresponding IPv4 protocol template is found by the protocol name through the query interface provided by the protocol template library. Then define the lower-layer protocol type attribute of TCP as the IPv4 protocol template.
[0177] The flow input module 2 executes step S4. After acquiring the original network flow input data, it encapsulates it into a standard network flow input accepted by the parsing engine.
[0178] The structure is as follows:
[0179]
[0180] The traffic input module pre-allocates a large memory space in the memory to cache the original traffic data. The traffic input module generates a structure object to encapsulate and store each data packet and assigns attribute information. The encapsulated standard network flow input will be provided to the execution engine for protocol parsing.
[0181] The structure of the execution engine 6 is as follows:
[0182]
[0183] This example takes Ethernet and IPv4 protocol parsing as an example, and the process executed by the execution engine 6 includes the following steps:
[0184] S5. The execution engine first obtains a standard network stream input object from the traffic input module. And sets the object as a standard network stream input with parsing. Initializes the network stream parsing position to the first byte of the original byte stream. And sets the number of remaining parsed bits to the byte length of the original byte stream multiplied by 8. Then the execution engine reads the protocol type of the standard network stream input object as Ethernet. And obtains the Ethernet protocol template by calling the query interface of the protocol template library. Then sets the referenced protocol template attributes. And sets the first field dstMac of the field array in the protocol template to the field to be parsed currently. If the required protocol template cannot be found in the template library, an exception is thrown; otherwise, continue to execute S6.
[0185] S6. After completing the initialization settings of the network stream input object and the protocol template, the execution engine will execute a loop logic, that is, read the field to be parsed, and parse the current byte stream data according to the field type and attributes. Then a temporary result object is generated. At the beginning of each loop, it will determine whether there are fields that have not been parsed. If so, S7 will be executed. If not, S11 will be executed.
[0186] S7. The execution engine executes the corresponding parsing logic according to the field type in the protocol template. There are three cases: if it is a basic field, execute S8; if it is a branch field, execute S9; if it is a loop field, execute S10. If the field type cannot be determined, an exception is thrown.
[0187] like Figure 3 As shown, the execution engine executes step S8 when parsing the basic field.
[0188] Step S8 specifically includes:
[0189] First, read the basic fields to be parsed in the protocol template. Then determine whether the field value is defined by defining the value attribute. If defined, execute S10; otherwise, execute S3, determine whether the field defines the length, endstr and remain attributes one by one, and calculate the data length of the field. Then determine whether the field defines the jump attribute. If defined, it means that the field is an invalid field, skip it directly, and execute S12; otherwise, read the specified length byte stream and parse it according to the data type of the field. Then process the trans attribute to convert the big and small ends. After that, the execution engine generates a temporary parsing result object to cache the parsing result of the original network stream. Finally, process the offset attribute to offset the pointer to the current parsing position of the original network stream. At this point, the execution engine completes the parsing of a basic field.
[0190] In this example, the execution engine executes the parsing process according to the attributes of the dstMac field. First, it reads that the dstMac field does not define the value attribute. Then it reads that the dstMac field defines the length attribute value of 48. The execution engine reads 48 bits of data from the current network flow parsing position. Then it reads that the dstMac field does not define the jump attribute, which means that the field does not skip parsing by default. Then it reads that the dstMac field does not define the trans attribute, which means that the field does not perform big-endian conversion by default. Then the execution engine generates a temporary parsing result object of type mac to store the 48-bit byte stream data corresponding to the dstMac field. Finally, the execution engine reads that the dstMac field does not define the offset attribute, which means that the field performs a pointer offset after the parsing is completed by default. Then the execution engine offsets the current network flow parsing position backward by 48 bits. And the number of bits equal to the parsing is reduced by 48. Then the temporary parsing result of the dstMac field is added to the array. And the protocol template is read to set the next field to be parsed as the srcMac field.
[0191] S9. The execution engine reads the index field value and substitutes it into the branch determination dynamic expression for calculation to determine the branch field array to be resolved, and sets the field array as the current resolution field array, and then returns to S6.
[0192] In this example, when the execution engine parses the if field of IPv4, it reads the branch judgment dynamic expression of the field as "$1>5". The execution engine then reads the value of the element with subscript 1 in the index array, which is the parsed value of the offset field, and substitutes it into the string. Then, the judgment result is obtained through string expression operation. And according to the judgment result, it is selected whether the parsed array is a true branch array or a false branch array.
[0193] S10. The execution engine reads the index field value and substitutes it into the loop determination dynamic expression to perform calculations to determine whether to execute the loop field array. If executed, the field array in the loop body is set to the current parsed field array and returns to S6; if not executed, directly returns to S6.
[0194] If the protocol of this layer is parsed, the execution engine determines whether the protocol has an upper-layer protocol-related field. If so, the protocol template connected to it is queried in the protocol template library and matched according to the value of the associated field. If found, a new protocol template is generated to continue parsing subsequent network flow data. If the field restoration fails or the corresponding protocol template is not matched, the execution engine will terminate the parsing. Temporary result objects of all protocols are exported.
[0195] In this example, after parsing all the fields in Ethernet, the execution engine determines that the upper-layer protocol associated field of Ethernet is type. The execution engine reads the parsed value of the type field. Then, all protocols associated with Ethernet are obtained from the protocol template library, including the IPv4 protocol. Then, it is determined whether the matching value of Ethernet in the lower-layer protocol type of IPv4 is 0x0800, which is equal to the parsed value of the type field. If so, the next protocol to be parsed is the IPv4 protocol; otherwise, it is not.
[0196] The export engine 7 executes step S11. The export engine reads the temporary result object, converts the data type of the object into a JSON data type, and assembles it into a JSON format according to the nested relationship of the fields.
[0197] In this example, taking the IPv4 protocol parsing result as an example, the corresponding JSON parsing result format is as follows:
[0198]
[0199]
[0200] It should be understood that the embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention. In addition, it should be understood that after reading the content taught by the present invention, those skilled in the art can make various changes or modifications to the present invention, and these equivalent forms also fall within the scope limited by the appended claims of the application.
Claims
1. A network traffic restoration method based on a custom protocol description language, characterized in that: include: Step 1: Use a custom protocol description language to define the structure and internal fields of the network protocol to be parsed and save them in a configuration file; Format all configuration files into standard configuration files and register them to the configuration file management module; Step 2: Read the standard configuration file, parse and generate a protocol template; Step 3: Register the protocol template to the protocol template library to build the association relationship between the upper and lower level protocols; Step 4, obtain the original network flow data and encapsulate it into a standard network flow; Step 5, read the standard network flow, and match the corresponding protocol template from the protocol template library according to the association relationship between the upper and lower layer protocols; Step 6: For the successfully matched standard network flow, the corresponding parsing logic is executed according to the field type in the protocol template to restore the flow; Step 7: Export the results of traffic restoration; In step 1, the custom protocol description language includes protocol class, field class, attribute class and dynamic expression class information; wherein, Protocol information includes protocol name, protocol version, lower layer protocol type, and upper layer protocol association fields; Field type information includes basic type fields, branch type fields, and loop type fields; Attribute information includes field name, field data length, field parsing type, field export type, field data length determination method, whether the field is parsed, and whether the field is exported; Dynamic expression class information is: numerical expression and Boolean expression; the expression is composed of expression brackets, operators, field reference symbols and field names; The branch type field includes an if field and a switch field; wherein the if field is composed of a Boolean dynamic expression and two true and false branches; the switch field is composed of a branch determination field and a branch set; The loop type field includes a for field and a while field; wherein the for field is composed of a numerical dynamic expression and a field array; and the while field is composed of a Boolean dynamic expression and a field array; The step 2 is specifically as follows: Read the standard configuration file and parse the protocol description line by line; Create objects field by field according to the field type in the protocol description, and initialize the static attribute information and dynamic expression information of the field; Connect the fields according to the field reference relationship in the dynamic expression; Assemble the fields into a protocol template according to the nested relationship in the protocol definition; The specific process of step 3 is: Register the agreement template to the agreement template library; Identify the fields in the template according to the upper-layer protocol associated fields defined by the protocol, and register the protocol and associated field information in the protocol template library; Parse the protocol type in the lower-layer protocol association information of the protocol, and query whether there is a corresponding protocol template in the protocol template library. If so, connect the two to obtain the association relationship between the upper and lower layer protocols; In step 6, the corresponding parsing logic is executed according to the field type in the protocol template to restore the traffic, specifically: Select execution logic based on field type: for basic fields, obtain static properties and dynamic expression values of the parsed field; for branch fields, select the field array in the specific parsing branch based on the dynamic expression; for loop fields, determine the number of times the field array in the loop body is parsed based on the dynamic expression; Calculate the data length of the field according to the data length determination method; Determine whether the field is parsed. If parsed, identify the data type of the field and read the corresponding bytes according to the data length for parsing to obtain the parsing result. The step 7, exporting the result of the traffic restoration, is specifically: using the JSON structure to store the structural information of the protocol after the traffic restoration and the parsing result data of each field.
Citation Information
Patent Citations
Multilayer protocol analysis method and device as well as data matching method and device
CN106657075A
Protocol configuration method and apparatus
CN108696381A
A realization method of a self-defined protocol based on a protocol analysis framework
CN109842629A
Generalized data analysis method based on xml file
CN110457526A
Custom network protocol analysis method and system, medium and electronic equipment
CN114513566A
