Intra-network malicious traffic detection method based on flow length-aware benign boundary learning
By constructing a teacher model in stages and utilizing knowledge distillation technology, the boundary of benign traffic behavior is transferred to a lightweight student model, solving the problem of deploying complex models in P4 switches, achieving efficient malicious traffic detection, and improving detection accuracy and real-time performance.
Patent Information
- Application Number
- CN202611321032.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-28
- Publication Date
- 2026-11-17
AI Technical Summary
Traditional methods struggle to efficiently detect malicious network traffic in a programmable data plane, complex machine learning models are difficult to deploy, and single models are ill-suited to the dynamic evolution of network traffic.
By constructing a teacher model in stages, knowledge distillation is used to transfer the boundaries of benign traffic behavior to a lightweight student model, which is then converted into matching-action rules and deployed on the P4 switch data plane. Online detection is then performed in conjunction with flow length awareness.
It significantly improves the malicious traffic detection rate, achieving a detection accuracy of over 90%, while reducing TCAM overhead and resource consumption, thus enhancing the real-time performance of detection and the efficiency of network resource utilization.
Smart Images

Figure CN123122926A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer network security, specifically relating to a method for detecting malicious traffic within a network based on flow length-aware benign boundary learning. Background Technology
[0002] Malicious traffic refers to network traffic that deviates from normal network communication behavior and may affect the availability of network services or the stability of the system. This includes denial-of-service attack traffic, port scanning traffic, botnet communication traffic, and reflection amplification attack traffic. As networks continue to expand, malicious traffic exhibits characteristics such as diverse attack types, rapid evolution, and strong concealment. Traditional detection methods relying on manual rules or fixed features struggle to detect these constantly evolving attack behaviors in a timely manner.
[0003] Programmable data planes support user-defined packet processing logic. Using data plane programming languages such as P4, network devices can directly parse packets, maintain state, extract features, and execute matching-action rules within the forwarding path. Compared to traditional control plane detection methods, offloading malicious traffic detection logic to the programmable data plane reduces latency and load caused by sending packets to the controller, improving real-time detection performance. However, programmable data planes impose strict limitations on model complexity, state storage, number of entries, pipeline stages, and arithmetic operations, making it difficult to directly deploy complex machine learning models to the switch data plane.
[0004] Knowledge distillation can transfer knowledge learned by a complex teacher model to a simpler student model, allowing the student model to significantly reduce model complexity and resource overhead while maintaining the decision-making ability of the teacher model as much as possible. Traditional knowledge distillation typically uses the class probabilities output by the teacher model as soft labels containing information about inter-class relationships, and uses this as a supervisory signal to guide the training of the student model, making the student model approximate the output distribution of the teacher model. Existing methods have shown that student models can achieve competitive or even better performance while mimicking the teacher model.
[0005] For malicious traffic detection within a network, while complex anomaly detection models can learn relatively sophisticated traffic behavior patterns, their model size and inference complexity often fail to directly meet the deployment constraints of the programmable switch data plane. Therefore, knowledge distillation can be used to transfer the benign traffic behavior boundaries learned by the complex anomaly detection teacher model to a lightweight student model. However, unlike traditional classification tasks, network traffic exhibits significant dynamic evolution characteristics. As packets arrive, observable traffic information gradually becomes richer, and different stages show differences in available features and behavioral patterns. Using a single model and a unified feature space is insufficient to fully characterize the benign behavior boundaries at different flow length stages.
[0006] This method constructs anomaly detection teacher models in multiple stages based on flow length, and uniformly calibrates and fuses the anomaly information output by teacher models in different stages. The fused teacher knowledge guides the training of a lightweight binary decision tree student model, thereby transferring the benign behavior boundaries learned in different flow length stages to the compact student model. Finally, the student model is converted into matching-action rules and deployed to the data plane of the switch to achieve online detection of malicious traffic within the network. Summary of the Invention
[0007] This invention proposes a method for detecting malicious traffic within a network based on flow length-aware benign boundary learning. This method uses only benign traffic to learn the boundaries of normal behavior and deploys a complex detection model onto the P4 data plane to achieve online detection of malicious traffic.
[0008] The technical solution adopted by this invention to achieve the above objectives mainly includes four steps: feature extraction, phased teacher model construction, lightweight model construction, and online detection.
[0009] 1. Feature Extraction. The incoming data packets are parsed, and the network flow is identified by the 5-tuple. The prefix features of each flow are extracted, and continuous features are binned into low-bit codes suitable for P4 data plane processing. Simultaneously, the number of new flows generated by the same source IP within a 1-second time window is counted to obtain source-end window statistical features.
[0010] 2. Phased Teacher Model Construction. Based on the observed number of prefix packets, the network flow is divided into three phases: single-packet flow, short flow, and long flow. Appropriate feature sets are selected for each phase, and teacher models for the three phases are trained using only benign traffic to learn the benign behavior boundaries under different flow length phases.
[0011] 3. Lightweight Model Construction. Knowledge distillation is performed using the outlier scores output by the phased teacher model. Pseudo-labels are generated based on the stage of the sample to train a lightweight binary decision tree student model. The tree depth, number of nodes, and number of rules are reduced through minimum description length and P4 cost-aware pruning.
[0012] 4. Online Detection. The compressed student model is converted into matching-action rules and sent to the data plane of the P4 switch. When a data packet arrives, the switch updates the flow prefix characteristics and source window statistics in real time, and executes the matching-action rules to determine the traffic. If the traffic is determined to be normal, the data packet is forwarded; if the traffic is determined to be malicious, it is reported to the control plane, thus realizing online malicious traffic detection. Beneficial effects
[0013] This invention proposes a network-based malicious traffic detection method based on flow length-aware benign boundary learning. This method trains anomaly detection teacher models for different flow length stages using benign traffic. Through staged multi-teacher knowledge distillation and P4 cost-aware pruning, the complex teacher model knowledge is transferred to a lightweight binary decision tree student model. The student model is further converted into matching-action rules deployed on the data plane of a P4 programmable switch. Online judgment is performed based on real-time extracted and updated prefix features and source window statistical features from the data plane, achieving malicious traffic detection. In a programmable data plane simulation environment built on Mininet and BMv2 software switches, using only benign samples during the training phase, this method achieves a detection rate of over 90% for malicious traffic while significantly reducing TCAM overhead and occupying only 6 pipeline stages. This demonstrates effective malicious traffic detection while reducing network resource consumption. Attached Figure Description
[0014] Figure 1 This is a schematic diagram for knowledge distillation of a phased teacher model.
[0015] Figure 2 This is a schematic diagram of model compression.
[0016] Figure 3 This is a schematic diagram of a network malicious traffic detection method based on flow length-aware benign boundary learning. Detailed Implementation
[0017] The invention will be further described below with reference to the accompanying drawings.
[0018] like Figure 3 As shown, this malicious traffic detection method mainly includes four steps: feature extraction, phased teacher model construction, lightweight model construction, and online detection.
[0019] 1. Feature Extraction. Obtain raw, benign traffic data, preprocess and clean the raw traffic data, construct network flows according to the five-tuple <source IP, source port, destination IP, destination port, protocol type>, and extract flow prefix features and source window statistical features; then, bin and discretize the continuous features to generate low-bit traffic feature data suitable for programmable data plane processing.
[0020] 1.1 Raw Benign Traffic Collection. Benign traffic data from a real network environment is collected as the training data source. The benign traffic data can be raw data packet files. This data retains flow information such as packet arrival time, source IP address, destination IP address, source port, destination port, protocol type, packet length, and TCP flags.
[0021] 1.2 Data Preprocessing and Cleaning. The raw traffic data is parsed packet by packet, extracting fields such as timestamp, IP address, port number, protocol number, packet length, and TCP flags. Packets that cannot be parsed, lack key fields, or have abnormal timestamps are filtered. Simultaneously, timestamps are uniformly converted to microsecond-level integers, and port numbers, protocol numbers, packet lengths, and TCP flags are converted to a unified numerical format to ensure consistency in subsequent flow feature calculations. Packets with the same five-tuple are grouped into the same unidirectional network flow.
[0022] 1.3 Prefix Feature Extraction. To ensure that the offline training features are consistent with the features available in the online data plane, this method does not rely on statistical information after the complete stream ends, but instead extracts features based on the prefix packets of the network stream.
[0023] 1.4 Source-end window statistical feature extraction. Setting the time window. During offline resolution, the source IP window state is maintained according to the packet timestamp order. When a certain 5-tuple flow appears for the first time, the new flow event is recorded. For the current source IP address... and current time Statistical time interval Number of newly generated 5-tuple flows from the same source IP: in, This indicates the first occurrence of a 5-tuple stream within the window. This indicates the source IP address of the stream. This indicates the time when the flow first appeared. This feature is used to characterize the activity level of the same source IP generating new flows within a short period of time. Within the source-end time window, the number of different destination ports accessed by the same source IP and the number of different destination IPs are further counted to characterize vertical port scanning behavior and horizontal address scanning behavior, respectively.
[0024] 1.5 Binning Discretization of Continuous Features. To adapt to the integer matching and entry lookup mechanism of the P4 programmable data plane, continuous features are binned and encoded according to a preset threshold to generate low-bit discrete features.
[0025] 2. Phased Teacher Model Construction. Based on the number of prefix packets observed in the network flow, the network flow is divided into three phases: single-packet flow, short flow, and long flow. Benign anomaly detection teacher models are trained for each phase.
[0026] 2.1 Phase Division and Training Set Construction. When the current flow observes only one data packet, it is classified as a single-packet flow; when the current flow observes two to four data packets, it is classified as a short flow; when the current flow observes five or more data packets, it is classified as a long flow. Then, the benign traffic feature samples are divided into three training sets according to the above phases, and each training set contains only benign samples of the corresponding phase.
[0027] 2.2 Teacher Model Training. Three unsupervised anomaly detection teacher models were trained using three training sets: single-packet flow, short flow, and long flow. Each teacher model learned the normal behavior boundaries of benign flow under different flow length stages and output anomaly scores for the input samples.
[0028] 3. Lightweight model building. For example... Figure 1 As shown, the benign and anomalous boundaries learned by the three-stage teacher models are transferred to a lightweight student model through a phased multi-teacher knowledge distillation method, and the deployment overhead is further reduced through model compression.
[0029] 3.1 Obtaining Teacher Model Output. Input the sample to be evaluated into the phased teacher model to obtain the abnormal scores output by the teacher model for the corresponding phase.
[0030] 3.2 Student Model Training. The empirical cumulative distribution function is used to calibrate the abnormal scores output by the teacher model at different stages. The pseudo-labels generated based on the calibration results are used as supervision signals, and the low-bit discrete features that can be maintained by the P4 data plane are used as input to train the binary decision tree student model.
[0031] 3.3 Model Compression and Pruning. Let the student tree to be compressed be... The overall pruning objectives are: in, This represents the cost of student tree classification error. Indicates the number of leaf nodes. Indicates tree depth, This indicates the deployment cost of P4. For example... Figure 2 As shown, for internal nodes in the student tree, the comprehensive costs of pruning and subtree replacement to retain the current subtree are compared. Under the condition that the error increase does not exceed a preset threshold, the structure with the smallest comprehensive cost is selected as the compression result, generating a lightweight model that can be used for data plane deployment.
[0032] 4. Online Detection. The compressed student model is converted into an action matching table and sent to the data plane. The P4 programmable switch's data plane performs real-time parsing, flow state reading, feature updating, feature encoding, rule matching, and detection result processing on incoming data packets. This process is completed within the switch's data plane, eliminating the need to send all data packets to the control plane, thus reducing the controller's processing load and improving real-time detection performance.
[0033] 4.1 Model Rule Transformation and Distribution. For each path from the root node to a leaf node in the student decision tree, the feature judgment conditions on the path are converted into matching domains, and the classification results corresponding to the leaf nodes are converted into actions, thereby generating a matching-action table entry. The controller distributes the matching-action table entry to the data plane through the runtime interface, enabling the switch to directly perform rule matching detection when receiving data packets.
[0034] 4.2 Online Packet Parsing. For packets entering the switch, the P4 parser first parses the protocol header. The data plane identifies network flows based on 5-tuples and uses a hash function to map these 5-tuples to a fixed-size flow status register index.
[0035] 4.3 Feature Statistics Update. The current flow's existing state is read from the register array based on the flow state index, and the corresponding source window statistics state is read based on the source IP address. The data plane updates the flow's state using the current packet information; if the packet belongs to a new flow or the corresponding state exceeds the idle timeout period, the flow state is reinitialized. Simultaneously, for newly appearing 5-tuple flows, the source window statistics features for the same source IP within the current time window are updated; when the window times out, the corresponding window count is cleared and a new statistics window is opened. This step enables the switch to perform early detection based on prefix packets and source window behavior before the flow has ended.
[0036] 4.4 Feature Binning and Low-Bit Encoding. After the state update is completed, the data plane converts continuous features into low-bit discrete codes. This encoding allows the updated features to be mapped to matching fields in the data plane.
[0037] 4.5 Flow Length Stage Determination and Rule Matching Detection. The data plane determines the current flow's stage based on the number of observed prefix packets. Based on the determination result, the flow length stage and the updated feature encoding are input into the matching-action table corresponding to the student model for matching.
[0038] 4.6 Detection Result Processing. If the matching result is normal, the switch forwards the data packet to the target port according to the default forwarding logic; if the matching result is abnormal, a digest message is generated and reported to the control plane.
Claims
1. A method for in-network malicious traffic detection based on flow length-aware benign boundary learning, characterized in that, By extracting and maintaining lightweight prefix features and source window statistical features online in the data plane of a programmable switch, a lightweight anomaly detection model trained using only benign traffic features is converted into an online deployable matching-action rule. The matching-action rule is used to classify the online extracted traffic statistical features, and malicious traffic is reported in real time based on the classification results, thereby achieving online detection of malicious traffic. The detection method includes the following steps: Step 1, Feature Extraction: Real-time parsing of data packets entering the programmable switch, and identification of flows based on the five-tuple <source IP, source port, destination IP, destination port, protocol type>; extracting packet features and flow features for each network flow from the prefix data packets of each network flow, and discretizing continuous feature values into feature codes suitable for P4 data plane processing by binning; setting a time window of 1 second, counting the number of flows generated by the same source IP address within this time window to obtain source window statistical features; finally, the prefix features and source window statistical features together constitute a lightweight feature vector. Step 2: Construction of the phased teacher model: Based on the number of packets observed in the current flow, the network flow is divided into three phases: single-packet flow, short flow, and long flow. The flow with a prefix packet count of 1 is defined as a single-packet flow, the flow with a prefix packet count of 2 to 4 is defined as a short flow, and the flow with a prefix packet count of 5 or more is defined as a long flow. For different phases, corresponding feature sets are selected, and the feature extraction method described in Step 1 is used to extract training samples for each phase from benign traffic. Three one-dimensional convolutional neural network models corresponding to single-packet flow, short flow, and long flow are trained respectively to construct the corresponding phased teacher model. Step 3, Lightweight Model Construction: Using the phased teacher model trained by the method described in Step 2, the anomaly detection knowledge of the teacher model is transferred to the lightweight student model through phased knowledge distillation and model compression. Specifically, this includes: Step 3.1: Perform knowledge distillation on the phased teacher models, and use the empirical cumulative distribution function to calibrate and transform the abnormal scores of the three teacher models to a unified quantile space. Based on the current stage of traffic observation and the corresponding teacher's anomaly judgment results, pseudo-labels are generated. These pseudo-labels are used as supervisory information to train a lightweight binary decision tree student model to classify and judge normal traffic and malicious traffic. Step 3.2: Model compression is performed using a method based on minimum description length. Bottom-up structural optimization is performed on the binary decision tree student model obtained in Step 3.
1. Pruning or subtree replacement is performed on candidate subtrees to reduce the tree depth, number of nodes and number of rules of the model, and finally a lightweight detection model suitable for data plane deployment is generated. Step 4, Online Detection: The compressed lightweight student model from Step 3 is converted into matching-action rules that can be executed by the programmable switch data plane. The controller sends the generated rules to the switch. When a data packet arrives at the switch, the switch parses the data packet in real time, updates the prefix features and source window statistical features mentioned in Step 1, and executes the matching-action rules according to the updated features. If the match is normal traffic, it is forwarded; if the match result is malicious traffic, it is reported to the control plane to realize online detection of malicious traffic.
2. The method for detecting malicious traffic within a network based on flow length-aware benign boundary learning as described in claim 1, characterized in that, The source-side window statistical feature described in step 1 With the source IP address as the statistical object, let the current time be , the time window length be , the source IP address be , and the network flow be uniquely identified by the five-tuple When the five-tuple first appears, it is recorded as a new flow event generated at time for the source IP address , and a new flow event set within the time window is constructed for the current source IP address . but Defined as the number of new stream events in this set: in, Take 1 second; when a new stream arrives, remove the event stamp from the set. Previous expired events are collected and the current new flow events are added to the set, thereby updating the number of new flows initiated by the same source IP address in the last second in real time.
3. The method for detecting malicious traffic within a network based on flow length-aware benign boundary learning as described in claim 1, characterized in that, Step 3.1 involves a distillation learning process that performs knowledge distillation on the phased teacher model; let the network flow samples be... Its flow length stage is Three stages of teacher models were trained respectively. , and For the stage Teacher Model For the sample Output abnormal scores And construct an empirical distribution function using the outlier score distribution on the benign calibration set. Convert outlier scores to outlier percentiles. , set stage The preset pollution rate is The corresponding abnormal threshold for that stage is For the sample to be distilled Based on its current flow length stage, the abnormal percentile of the corresponding teacher model is selected as the distillation target. And generate student model training labels: in, Indicates malicious traffic. This represents normal flow; finally, a shallow student decision tree is trained using low-bit flow features that can be maintained by the P4 data plane, so that the individual student model can approximate the benign anomaly boundary of the teacher model at different flow length stages in a unified rule table.
4. The method for detecting malicious traffic within a network based on flow length-aware benign boundary learning as described in claim 1, characterized in that, Step 3.2 describes a model compression method that employs the minimum description length approach, combined with a student tree compression strategy based on P4 deployment cost; let the student tree to be compressed be... The training sample set is The pruning objective is to select the model with the minimum total cost among all candidate student trees. in, This represents the length of the error description of the student tree to the training samples. The length of the student tree represents its own complexity description; in the P4 data plane deployment scenario. , ,in, For classification error, For the error entropy term, The number of leaf nodes. For tree depth, For P4 deployment cost, compare the combined costs of retaining the current subtree, pruning to leaf nodes, and replacing with subtrees, select the structure with the lowest cost, and generate a lightweight detection model suitable for data plane deployment.
