Can-fd anomaly detection method based on time sequence content attention and long short-term memory network
By proposing an anomaly detection method for CAN-FD based on temporal content attention and long short-term memory networks, the problems of anomaly detection and attack type identification in vehicle CAN-FD networks are solved, achieving high-accuracy real-time detection and classification, thus ensuring automotive network security.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- JILIN UNIVERSITY
- Filing Date
- 2023-09-05
- Publication Date
- 2026-08-04
AI Technical Summary
Existing technologies are insufficient to effectively detect and classify anomalous attacks in vehicle-mounted CAN-FD networks, leading to frequent automotive cybersecurity issues, especially DoS, fuzzing, spoofing, and replay attacks, which affect the normal operation of vehicles.
A CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks is adopted, including a network anomaly data detection model and a network anomaly classification detection model. Through data acquisition, preprocessing, model architecture construction, training and output, real-time anomaly detection and attack type identification of CAN-FD network data are achieved.
It achieves real-time anomaly detection and accurate classification of attack types in CAN-FD network data, with a detection accuracy rate of 99%. It can effectively identify DoS, fuzzing, spoofing and replay attacks, ensuring the network security of automobiles.
Smart Images

Figure CN117176421B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of intelligent connected vehicle technology, and particularly relates to an anomaly detection method for CAN-FD (Controller Area Network with Flexible Data Rate) based on temporal content attention and long short-term memory networks. Background Technology
[0002] With the development of vehicle electrification, connectivity, intelligence, and sharing, V2X technology has been widely applied, and intelligent connected vehicles have gradually become a popular development trend. Among various connected vehicle application technologies, network security is undoubtedly one of the core issues. Especially in recent years, problems related to automotive network security have emerged one after another, making the research and solution of automotive network security issues urgent. CAN-FD bus is currently a widely used automotive bus network technology used for communication between various components in a vehicle. It is expected to gradually replace the CAN bus and play an important role in automotive networks. Automotive network security problems often originate from external attacks on the CAN-FD network. External attacks on the CAN-FD network can send incorrect network signals to the vehicle, thereby interfering with its normal operation. For example, by rejecting or denying the signal transmitted by the steering wheel angle, the vehicle's steering can be interfered with; by forging the signals of the brake and accelerator pedals, the vehicle's longitudinal movement can be interfered with; and by forging the signals of the door opening and closing, the normal opening and closing of the doors can be interfered with.
[0003] Common types of CAN-FD network anomalies include Denial of Service (DoS) attacks, fuzzing attacks, spoofing attacks, and replay attacks. A DoS attack involves an attacker continuously sending high-priority identifiers (IDs) to occupy CAN-FD system channels with high-frequency, high-priority CAN-FD messages, thereby interrupting or disabling communication between ECUs. A fuzzing attack involves an attacker injecting random IDs and data fields into the network in a CAN-FD format, interfering with the normal operation of the CAN-FD bus system. Attackers do not need specific information about the CAN-FD system data to disrupt the normal operation of the vehicle's CAN-FD network; specific manifestations of this include steering wheel wobbling, erratic turn-on / off of turn signals, and automatic gear shifting. A spoofing attack involves an attacker injecting modified messages targeting a specific CAN-FD ID; that is, the attacker uses a legitimate CAN-FD ID and forged data fields to cause deviations in the ECU's response. A replay attack is an attack in which an attacker copies and resends messages from past communications of a CAN-FD system over the network, causing the corresponding electronic control unit to respond to past signals again at an asynchronous time, thereby interfering with the normal function of the system.
[0004] Anomaly detection systems monitor network transmissions in real time and issue alarms when suspicious transmissions are detected. They have been widely used in computer networks and other Internet of Things (IoT) fields. In recent years, they have been gradually applied to vehicle CAN networks. However, due to the differences between vehicle data characteristics and those in other fields, and the limited computing resources, the current approach of transplanting anomaly detection methods from other fields to vehicle CAN networks is not very effective in terms of detection accuracy. Furthermore, there is very little research on anomaly detection methods for CAN-FD networks. Summary of the Invention
[0005] The purpose of this invention is to provide a CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks, aiming to solve the problems mentioned in the background art.
[0006] The embodiments of this invention are implemented as follows: the CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks includes two sub-models:
[0007] A network anomaly data detection model is used to detect whether abnormal messages appear in the CAN-FD network data in real time based on the characteristics of normal and abnormal message data; and
[0008] A network anomaly classification and detection model is used to detect the type of attack that a current anomaly may be suffering in real time based on the characteristics of common types of vehicle network attacks. The method includes the following specific steps:
[0009] Step 1, the design and training of the network anomaly data detection model, mainly includes the following 5 steps:
[0010] Step 1.1, Data Acquisition: Select an electric vehicle with L2 autonomous driving capability as the platform, and collect the real vehicle dataset of the original CAN-FD communication data such as the front-view camera, lane lines, relative position of the target object, speed, and acceleration as the training data source, and collect the vehicle timestamp, identifier, and data fields in sequence.
[0011] Step 1.2: Preprocessing of Training Data: The real vehicle dataset consists entirely of normal data, excluding any abnormal attack data. To simulate attacks of different types of data, four attacks were set up: DoS, Fuzzing, Spoofing, and Replay. By observing the characteristics of the four attacks, timestamps, identifiers, and data fields of the attack dataset were defined. These were then compiled into packets and randomly inserted into the aforementioned normal dataset, with labels assigned to both normal and abnormal categories. Furthermore, an appropriate time step was selected through experiments to preprocess the dataset.
[0012] Step 1.3, Network Architecture Construction: The model contains an input layer, an LSTM layer, a RepeatVector layer, and a fully connected (Dense) layer. The LSTM layer is used to encode and model the data of the CAN-FD network of the vehicle. It receives the features of the test data of the vehicle CAN-FD network at each time step and processes these time series input information. With its forget gate, input gate, and output gate structure, it captures and learns the temporal patterns of data features. The RepeatVector layer is used to expand the dimension of the tensor. The Dense layer makes the model's predictions on the training set gradually approach the true values.
[0013] Step 1.4, Model Training and Output: During model training, the preprocessed dataset is first divided into training, validation, and test sets. The data from the training and validation sets, along with their labels, are input into the model. Appropriate epochs and batch size parameters are initially selected for training, and the loss values of the model on the training and validation sets are recorded during iterations. Hyperparameters, such as learning rate, number of epochs, and batch size, are adjusted based on the loss value and accuracy on the validation set.
[0014] When designing a loss function for a model, the choice of loss function mainly depends on the nature of the problem and the characteristics of the model. This model is essentially a binary classification model, and its output layer uses the Sigmoid function. Therefore, the final output result is a probability value between 0 and 1. Thus, binary cross-entropy is used as the loss function, and Adam is selected as the optimizer, with an initial learning rate of 0.001. This yields the result corresponding to the time step length.
[0015] Step 1.5, Result Verification and Hyperparameter Adjustment: Verify the results and adjust the hyperparameters.
[0016] In the results validation, the model is evaluated based on the confusion matrix. True positives (TP), true negatives (TN), false positives (FP), and false negatives (FN) are statistically analyzed, along with evaluation metrics such as precision, recall, and F1 score. Precision: Precision is the proportion of samples predicted as positive by the classifier that are actually positive. The formula is: Precision = TP / (TP + FP). Recall: Recall is the proportion of samples that are actually positive but were predicted as positive by the classifier. The formula is: Recall = TP / (TP + FN). F1 Score: The F1 score is the harmonic mean of precision and recall, representing a balance between the two. The formula is: F1 Score = 2 * (Precision * Recall) / (Precision + Recall). The model outputs a time step of 4, meaning it ultimately classifies 4 groups of message messages. The model compares these 4 results, selecting the group with the best evaluation metric at each time step as the test result. The output uses the Sigmoid function to calculate floating-point probabilities between 0 and 1. Probabilities exceeding a threshold are converted to 1 (normal), while those below the threshold are converted to 0 (abnormal). A dynamic threshold adjustment method is used, continuously adjusting the threshold while calculating the F1 score, and selecting the threshold corresponding to the highest F1 score.
[0017] Step 2: Design and training of the network anomaly classification and detection model:
[0018] Step 2.1, Data Acquisition: The data acquisition process is the same as in Step 1. An electric vehicle with L2 autonomous driving capability is selected as the platform, and the real vehicle dataset of the original CAN-FD communication data dataset is collected as the training data source. The vehicle timestamp, identifier, and data fields are collected in sequence.
[0019] Step 2.2: Preprocessing of training data: The real vehicle dataset consists of normal data and does not contain abnormal attack data. To simulate attacks of different types of data, four types of attacks are set, including DoS, Fuzzing, Spoofing, and Replay, and corresponding attack labels are assigned to each category. The attack data is then inserted into the normal dataset.
[0020] Step 2.3, Network Architecture Construction: The model contains an LSTM layer, an Attention layer, a RepeatVector layer, and a Dense layer. The LSTM layer encodes and models the data from the CAN-FD network, receiving the features of the test data from the CAN-FD network at each time step and processing these time-series inputs. Using its forget gate, input gate, and output gate structure, it captures and learns the temporal patterns of the data features. The Attention layer connects the important information from the first LSTM layer with the context of the third LSTM layer by calculating the attention weights between the LSTM and Dense layers. The RepeatVector layer expands the tensor dimension; the Dense layer helps the model's predictions on the training set gradually approach the true values.
[0021] Step 2.4, Model Training and Output: In the training process of the network anomaly classification and detection model, the preprocessed dataset is first divided into training set, validation set and test set. The data of the training set and validation set and their labels are input into the model respectively. The appropriate number of rounds and batch size are initially selected for training, and the relevant index parameters are recorded during iteration. The hyperparameters are adjusted according to the results obtained by the model on the validation set.
[0022] Since this sub-model is a multi-class classification model, multi-class cross-entropy loss is chosen as the loss function. Softmax is used for normalization in the output layer to ensure that the sum of all output probabilities is 1. In this case, the value corresponding to each label in the result represents a probability. Adam is selected as the optimizer, with an initial learning rate of 0.001. The results then provide the total probabilities of normal and four types of anomalies within the specified time step.
[0023] Step 2.5, Result Validation and Hyperparameter Adjustment: Evaluate and validate the model's output results, and statistically analyze the true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN) under each category label, as well as evaluation metrics such as precision, recall, and F1 score.
[0024] The CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks provided in this invention has the following beneficial effects:
[0025] (1) An innovative CAN-FD network anomaly data detection model and network anomaly classification detection model combining temporal pattern attention and long short-term memory network were designed. It can capture long-distance dependencies and can more selectively select data information closely related to the context. As a result, it can capture the contextual correlation more flexibly and better identify the temporal characteristics of the data, thereby inferring the type of anomaly data. The accuracy rate on real vehicles and open source datasets reached 99%.
[0026] (2) By using real automotive CAN-FD network messages, a dataset of vehicle CAN-FD network attacks (including DoS, Fuzzing, Spoofing and Replay) can be developed, which can be used to simulate vehicle CAN-FD network attacks.
[0027] (3) It not only detects whether the transmitted message is abnormal, but also detects the type of abnormal message, and can effectively determine whether the current attack belongs to the DoS, Fuzzing, Spoofing or Replay type.
[0028] (4) For the first time, anomaly detection is performed in an in-vehicle CAN-FD network using deep learning methods, which can be widely applied to automotive electronic systems that communicate via CAN-FD.
[0029] An innovative CAN-FD network anomaly detection model and an anomaly classification detection model were designed, combining temporal pattern attention and long short-term memory networks. This model enables real-time detection of CAN-FD data anomalies and classifies them to identify the type of anomaly, allowing for appropriate attack prevention measures. Test results show that the method achieves 99% accuracy on both real-vehicle and open-source datasets. Attached Figure Description
[0030] Figure 1 A flowchart of the CAN-FD anomaly detection method based on temporal content attention and long short-term memory network provided in an embodiment of the present invention;
[0031] Figure 2 The basic architecture diagram of the CAN-FD anomaly detection method based on temporal content attention and long short-term memory network provided in the embodiments of the present invention is shown.
[0032] Figure 3 This is a diagram of the network anomaly data detection model architecture.
[0033] Figure 4 Architecture diagram of a network anomaly classification and detection model;
[0034] Figure 5A graph showing the relationship between training set loss, validation set loss, and number of rounds during the training of a network anomaly data detection model.
[0035] Figure 6 A graph showing the relationship between training set loss, validation set loss, and number of rounds during the training of a network anomaly classification and detection model.
[0036] Figure 7 A graph showing the relationship between training set loss, validation set loss, and number of rounds during the training of a network anomaly detection model on an open-source dataset.
[0037] Figure 8 This graph shows the relationship between training set loss, validation set loss, and number of rounds during the training of a network anomaly classification and detection model on an open-source dataset. Detailed Implementation
[0038] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0039] The specific implementation of the present invention will be described in detail below with reference to specific embodiments.
[0040] like Figure 1 and 2 As shown, an embodiment of the present invention provides a CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks. The method includes two sub-models: a network anomaly data detection model and a network anomaly classification detection model. The network anomaly data detection model is used to detect in real time whether abnormal packets appear in the CAN-FD network data based on the characteristics of normal and abnormal packet data. The network anomaly classification detection model is used to detect in real time the attack type that the current anomaly may have suffered based on the characteristics of common types of vehicle network attacks. The method includes the following specific steps:
[0041] Step 1: Design and training of the network anomaly data detection model:
[0042] Step 1.1, Data Acquisition: Select an electric vehicle with L2 autonomous driving capability as the platform, and collect the real vehicle dataset of CAN-FD communication raw data such as front-view camera, lane lines, relative position of target objects, speed and acceleration as the training data source. Collect vehicle timestamps, identifiers and data fields in sequence, and use VectorCANdb++Editor to read and store the dbc protocol file that represents the physical meaning of the data.
[0043] Step 1.2: Preprocessing of training data: Extract timestamps, identifiers, and data field content to create attack data. Set four types of attacks, including DoS, Fuzzing, Spoofing, and Replay, and assign corresponding attack labels to each category. Insert the attack data into the normal dataset.
[0044] The timestamps of the attack data were all randomly selected, thus possessing uniform randomness. The data was then divided into three parts in a 6:2:2 ratio: training set, validation set, and test set.
[0045] Step 1.3, Network Architecture Setup: This is used to implement binary classification of normal and abnormal data. The model architecture is as follows: Figure 3 As shown, the functions of each layer and the data transfer relationships between layers are described below:
[0046] Input layer: The model receives an input of shape (None, 4, number of features), representing four consecutive message data points, which can be expressed by the formula:
[0047]
[0048] Where t1, t2, t3… represent timestamps, and X… t1 Y t1 Z t1 …represents the data characteristics under the corresponding timestamp, including vehicle timestamp, identifier, and data fields.
[0049] LSTM1 layer: The input data first passes through an LSTM layer with 256 units. This layer uses the ReLU activation function, and its output X... L1 The shape is (None, 4, 256), which can be expressed by the formula:
[0050] X L1 =LSTM 256 (X input (2)
[0051] Among them, X L1 This represents the output of the first LSTM layer. In this invention, neurons in the first LSTM layer are randomly disabled with a probability of 0.2, setting their output to 0. This reduces neuron output and prevents overfitting, and can be expressed by the formula:
[0052] X L1D =Dropout(0.2, X L1 (3)
[0053] Among them, X L1D This indicates the output result after discarding.
[0054] LSTM2 layer: This layer has 256 neurons, receives the output of the previous LSTM layer as input, and outputs a sequence of 256 hidden states with a shape of (None, 4, 256). This can be expressed by the formula:
[0055] X L2 =LSTM 256 (X L1D (4)
[0056] Among them, X L2 This represents the output of the second LSTM layer.
[0057] LSTM3 layer: This layer has 64 neurons. It still uses the output of the previous neuron as input to obtain a 64-dimensional hidden state vector. This step only retains the hidden state from the last time step, and its output shape is (None, 64). This can be expressed as:
[0058] X L3 =LSTM 64 (X L2 (5)
[0059] Among them, X L3 This represents the output of the third LSTM layer.
[0060] RepeatVector layer: This layer copies the hidden state vector obtained in the previous step four times, increasing its dimensions so that it can be input into subsequent hidden layers. Its output shape is (None, 4, 64). This can be expressed as a formula:
[0061] X L3R = RepeatVector(4, X) L3 (6)
[0062] Among them, X L3R This represents the output of the repeating vector layer.
[0063] LSTM4 layer: This layer has 64 neurons. It takes an input sequence of this vector as input and outputs an output sequence with 64 hidden states, with a shape of (None, 4, 64). This can be expressed as a formula:
[0064] X L4 =LSTM 64 (X L3R (7)
[0065] Among them, X L4This represents the output of the fourth LSTM layer. After the LSTM4 layer, neurons in the LSTM4 layer are randomly disabled with a probability of 0.1, causing their output to be 0. This can be expressed as a formula:
[0066] X L4D =Dropout(0.1, X L4 (8)
[0067] Among them, X L4D This indicates the result of the discard layer output.
[0068] LSTM5 layer: This layer has 32 neurons, takes the output of the previous dropout layer as input, and outputs a sequence of 32 hidden states with the shape (None, 4, 32). This can be expressed as follows:
[0069] X L5 =LSTM 32 (X L4D (9)
[0070] Among them, X L5 This represents the output of the fifth LSTM layer.
[0071] Output layer: This layer is a fully connected (dense) layer with only one neuron. The sigmoid activation function converts the output of the previous layer into a floating-point number between 0 and 1, which is used as the final output. Its shape is (None, 4, 1), as shown in the following formula:
[0072] y = Dense Sigmoid (X L5 (10)
[0073] Where y represents the output of the fully connected layer, that is, the probability that the four input data points are normal. The output y is shown in formula (11), where t1, t2, t3… represent timestamps. …indicates the probability that the message corresponding to this timestamp is a normal message.
[0074]
[0075] This model uses ReLU as the activation function in the LSTM layer and the Sigmoid function in the output layer. ReLU is widely used in deep learning; it's simple, fast, and avoids the vanishing or exploding gradient problem, thus improving training speed. The Sigmoid function is often used in the output layer of binary classification problems, mapping the output value to a probability between 0 and 1 to predict the probability of anomalies. During training, Adam was selected as the optimizer, with 100 epochs, a batch size of 128, and a learning rate of 10.-3 .
[0076] Step 1.4, Model Training and Output: Before training, the preprocessed dataset is first divided into training set, validation set and test set, with various attack data evenly distributed and divided into three parts in a 6:2:2 ratio.
[0077] Input the training set and validation set data, along with their Benign label columns representing normal / abnormal data, into the model. During the training process of the binary classification model, select the number of initial rounds and the batch size for training.
[0078] The loss value used was binary cross-entropy (BCE), and its formula is as follows:
[0079] BCE(y, p)=-, [y·log(p)+(1-y)·log(1-p)] (12)
[0080] In the above formula, y is the true class label, which is 0 or 1; p is the probability that the model predicts a positive value, with a value range of [0,1]. Record the model's loss values on the training and validation sets during iterations. When the model makes predictions on the test set, it is necessary to adjust certain layers or operations related to the training process, such as disabling the Dropout layer and switching the model to evaluation mode.
[0081] Step 1.5, Result Verification and Hyperparameter Adjustment:
[0082] For result validation, a threshold should be determined. Regarding the selection of the threshold, a Python-based method was used to calculate the threshold corresponding to the maximum F1 score. The number of true positives (samples predicted as normal and with normal actual values), true negatives (samples predicted as anomalous and with anomalous actual values), false positives (samples predicted as normal but with anomalous actual values), and false negatives (samples predicted as anomalous but with normal actual values) were counted using either a table or a Python-based method. This allowed for the creation of a 2x2 confusion matrix, and the accuracy, precision, recall, and F1 score were calculated. The evaluation metrics for the four data points were compared, and the best data point was selected.
[0083] Based on experience and domain knowledge, a rough estimate of the range of hyperparameter values is made, as shown in Table 1. A random search method is used to randomly select values within the defined range of each hyperparameter, and a certain number of random combinations are tested. Based on the evaluation results, the performance of the current hyperparameter combination is recorded, and the next round of random sampling is performed. Steps 1.3 and 1.4 are repeated until further iterations no longer significantly improve performance, at which point the set of parameters with the best evaluation metric is selected.
[0084] Table 1. Model Hyperparameter Settings
[0085] Input sequence length [3,10] Number of rounds [50,1000] Batch size [100,200] Learning rate <![CDATA[[1×10 -6 ,1×10 -2 ]]]> Optimizer [RMSprop, Adam, Nadam, Adagrad] Output layer activation function [Sigmoid, tanh, ReLU]
[0086] For the validation strategy, a hold-out validation method is adopted, dividing the data into training and test sets, and ensuring that the test set is never used during training. Simultaneously, the classification results are observed in the test results.
[0087] Step 2: Design and training of the network anomaly classification and detection model:
[0088] Step 2.1, Data Acquisition: Similar to the data acquisition in Step 1.1, an electric vehicle with L2 level autonomous driving capability is selected as the platform, and the real vehicle dataset of the original CAN-FD communication data dataset is collected as the training data source.
[0089] Step 2.2 Data Preprocessing: Extract the timestamp, identifier, data length, and data fields of the data message and save them to a CSV file. Add attack data to it according to the method in Step 1. Since this model is a multi-class model, add a label column "benign" to represent normal data, and add label columns to represent Dos, Fuzzing, Spoofing, and Replay.
[0090] Step 2.3 Network Architecture Setup: The CAN-FD network anomaly classification and detection model is used to detect the type of abnormal data, including DoS attacks, fuzzing attacks, spoofing attacks, and replay attacks. The model architecture is as follows: Figure 4 As shown, the functions of each layer and the data transfer relationships between layers are described below:
[0091] Input Layer: The model's input is a three-dimensional tensor with a time step of 4. The model receives an input of shape (None, 4, number of features), representing four consecutive message data points, which can be expressed by the formula:
[0092]
[0093] Where t1, t2, t3… represent timestamps, and X… t1 Y t1 Z t1 … represents the data characteristics corresponding to the timestamp, including the vehicle's timestamp, identifier, data length, and data fields.
[0094] LSTM Layer 1: This layer has 512 neurons, uses ReLU as the activation function, and returns the complete sequence. Its output X L1 The shape is (None, 4, 512), which can be expressed by the formula:
[0095] X L1 =LSTM 512 (X input (14)
[0096] Among them, X L1 This represents the output of the first LSTM layer. Neurons in the first LSTM layer are randomly disabled with a probability of 0.2, and the output of the disabled neurons is set to 0 to prevent overfitting. This can be expressed by the formula:
[0097] X L1D =Dropout(0.2, X L1 (15)
[0098] Among them, X L1D This indicates the result of the discard layer output.
[0099] LSTM Layer 2: This layer has 512 neurons, uses ReLU as the activation function, and returns the complete sequence. It further extracts higher-order feature representations from the first LSTM layer. Its output shape is (None, 4, 512), which can be expressed by the formula:
[0100] X L2 =LSTM 512 (X L1D (16)
[0101] Among them, X L2 This is the output of the second LSTM layer.
[0102] Dense layer: This layer maps the output of the first LSTM layer to a 128-dimensional feature space, with an output shape of (None, 4, 128), expressed by the formula:
[0103] X Dense1 =Dense 128 (X L1D (17)
[0104] Among them, X Dense1 This is the output of the first Dense layer.
[0105] LSTM Layer 3: This layer has 128 neurons and uses ReLU as the activation function. It does not return the complete sequence, but only the result of the last time step. The output of the second LSTM layer is input into this LSTM layer, resulting in an output shape of (None, 128), which can be expressed by the formula:
[0106] X L3 =LSTM 128 (X L2 (18)
[0107] Among them, X L3 This is the output of the third LSTM layer.
[0108] RepeatVector layer 4: The output of this layer is copied 4 times to increase its shape dimensions. Its output shape is (None, 4, 128), which can be expressed by the formula:
[0109] X R = RepeatVector(4, X) L3 (19)
[0110] Among them, X R It is the result obtained from the copy layer.
[0111] LSTM 4-layer: The fourth LSTM layer has 128 units and its activation function is ReLU. The resulting output shape is (None, 4, 128), which can be expressed by the formula:
[0112] X L4 =LSTM 128 (X R (20)
[0113] Among them, X L4 This is the result obtained from the fourth LSTM layer. To prevent overfitting, the output of this layer is randomly discarded with a probability of 0.1 during training. This can be expressed as a formula:
[0114] X L4D =Dropout(0.1, X L4 ) (twenty one)
[0115] Among them, X L4D This indicates the result obtained after the output of the L4 layer is discarded.
[0116] Attention layer: This layer focuses on X L4D With X Dense1 Attention calculation yields an output with the shape (None, 4, 128), which can be expressed by the formula:
[0117] A = Attention(X) L4D X Dense1 ) (twenty two)
[0118] Where A represents the result of attention calculation.
[0119] LSTM 5-layer: This layer contains 64 neurons and sets ReLU as the activation function. It returns the complete sequence, and the resulting shape is (None, 4, 64), which can be expressed by the formula:
[0120] X L5 =LSTM 64 (A) (23)
[0121] Among them, X L5 This represents the output of the fifth LSTM layer.
[0122] Output layer: X L5 The output is fed into the final output layer, which is a fully connected layer containing 5 neurons. The output is activated by the Softmax function, resulting in an output shape of (None, 4, 5). This can be expressed as a formula:
[0123] output = Dense5(X) L5 ) (twenty four)
[0124] Where output is the model's output. Output is shown in Equation 25:
[0125]
[0126] The subscripts α, β, γ, δ, and ε represent the classification labels Benign, DoS, Fuzzing, Spoofing, and Replay, respectively. The subscripts t1, t2, t3, ... represent the timestamps, and p represents the probability that the message belongs to the corresponding label at the corresponding timestamp.
[0127] Step 2.4, Model Training and Output: First, the preprocessed dataset is divided into training, validation, and test sets in a 6:2:2 ratio. Then, the data from the training and validation sets, along with their five label columns representing normal / abnormal and four types of network anomalies, are input into the model. During the training of the multi-class classification model, the initial selection is 100 epochs, a batch size of 128, and a learning rate of 10%. -4 Conduct training.
[0128] The loss value used was multiclass cross-entropy (MCE), and its formula is as follows:
[0129]
[0130] Where c is the number of label categories, and in this example, c is 5; H(p, q) is the multivariate classification cross-entropy; p is the actual category label corresponding to the data, and q is the model's predicted distribution of the data.
[0131] Simultaneously, the model's loss values on the training and validation sets are recorded during iterations. Hyperparameters such as the learning rate, number of epochs, and batch size are adjusted by evaluating the loss value on the validation set. When the model makes predictions on the test set, certain layers or operations related to the training process need to be adjusted, such as disabling the Dropout layer and switching the model to evaluation mode.
[0132] Step 2.5, Result Verification and Hyperparameter Adjustment:
[0133] For result validation, the maximum probability of each row in the output is changed to 1, and its corresponding label is used as the predicted category. All other columns are changed to 0. The number of true positives (samples predicted as normal but with actual values of normal), true negatives (samples predicted as anomalous but with actual values of anomalous), false positives (samples predicted as normal but with actual values of anomalous), and false negatives (samples predicted as anomalous but with actual values of normal) for each category is counted using either a table or a Python program. This results in a 5x5 confusion matrix for the overall dataset. The overall accuracy, precision for each class, recall for each class, and F1 score for each class are then calculated. The evaluation metrics for the four data points are compared, and the data point with the best metric is selected.
[0134] Based on experience and domain knowledge, a rough estimate of the range of hyperparameter values is made, as shown in Table 2. A random search method is used to randomly select values within the defined range of each hyperparameter, and a certain number of random combinations are tested. Based on the evaluation results, the performance of the current hyperparameter combination is recorded, and the next round of random sampling is performed. Steps 2.3 and 2.4 are repeated until further iterations no longer significantly improve performance, at which point the set of parameters with the best evaluation metric is selected.
[0135] Table 2. Model Hyperparameter Settings
[0136] Input sequence length [3,10] Number of rounds [50,1000] Batch size [100,200] Learning rate <![CDATA[[1×10 -6 ,1×10 -2 ]]]> Optimizer [RMSprop, Adam, Nadam, Adagrad] Output layer activation function [Sigmoid, Softmax]
[0137] For the validation strategy, a hold-out validation method is adopted, dividing the data into training and test sets, and ensuring that the test set is never used during training. Simultaneously, the classification results are observed in the test results.
[0138] In a preferred embodiment of the present invention, step 1.2 includes the following specific steps:
[0139] Label settings: Add column labels to the initially processed CSV data file, corresponding to the timestamp, identifier, and data field. Also, append the corresponding category labels Benign, DoS, Fuzzing, Spoofing, and Replay to the last column to indicate the category of this data entry. For datasets collected from real vehicles, since all data is normal, set Benign=1 and the other four columns to 0 to indicate that it is normal data.
[0140] DoS Attack Setup: A DoS attack aims to overwhelm a target system by sending a large number of messages with high-priority identifiers and data bits in a short period. Based on this, the identifier for the DoS attack data is set to 00, and the data length is set to 64. For each batch of DoS attacks, timestamps are randomly selected, ensuring that all DoS timestamps are adjacent to this one, and each batch of DoS attack data contains a large number of data. The label column for DoS is set to 1, with the rest set to 0. The number of DoS attacks is controlled to be approximately 13% of the total dataset.
[0141] Fuzzing attack setup: Based on the characteristics of fuzzing attacks, the identifier, data fields, and data length of the fuzzing attack data are all randomly selected, and the data fields are matched with their data lengths. In the label column, the Fuzzing column is set to 1, and the rest are 0, to simulate a random fuzzing attack. The number of fuzzing attacks is approximately 6% of the total dataset.
[0142] Spoofing attack setup: In a spoofing attack, a specific identifier is selected as the target, and a message similar to normal data is forged. Specifically, the identifier is randomly selected from identifiers that have appeared in normal data. For the subsequent data length and data fields, the values are set to differ significantly from the corresponding column values in the context, thus simulating a spoofing attack. The number of spoofing attacks is set to approximately 3% of the total dataset.
[0143] The setup for a replay attack involves randomly selecting message data each time and completely copying its identifier, data fields, and data length four times to the original dataset, while ensuring that its timestamp is close to the original message's timestamp value. For replay attacks, the number of such attacks should be set to approximately 6% of the total dataset.
[0144] As a preferred embodiment of the present invention, the above method was simulated and verified. The model was created based on the Keras framework of Python, the GPU was NVIDA GEFORCE RTX 3090 and 4090 (24GB), and the dataset used was a real vehicle dataset and a publicly available open-source dataset.
[0145] Attack data was added to the real vehicle dataset obtained from electric vehicles with L2 autonomous driving capabilities to create a total dataset (a total of 285,517 data packets). While ensuring randomness, the complete dataset was divided into training set, validation set, and test set in a ratio of 6:2:2, as shown in Table 3.
[0146] Table 3. Quantity and Proportion of Centralized Label Data in Real Vehicle Data Set
[0147] quantity 205599 37100 17120 8566 17132 Proportion 72.01% 12.99% 5.99% 3.01% 6.00%
[0148] The error loss curves of the two models on the training set and on the validation set are shown below. Figure 5 and Figure 6 As shown, it can be seen that as the number of training rounds increases, the error value gradually decreases and tends to level off. The error value of the network anomaly detection model converges to 0.0187, and the error value of the network anomaly classification detection model converges to 0.0418.
[0149] The results obtained from testing the CAN-FD network anomaly data detection model are shown in Table 4. The highest F1 score (0.9986) is achieved when the threshold is 0.8954; therefore, this threshold is considered the optimal one. Furthermore, the accuracy at this threshold is 0.9944, the precision is 0.9955, and the recall is 0.9975.
[0150] Table 4. Results of the CAN-FD anomaly detection neural network model on the test set.
[0151] 0.9975 0.0176 0.9824 0.0025 0.9965 0.9955
[0152] Table 5 shows the statistical results of the CAN-FD network anomaly classification and detection model on the test set. The row labels on the left represent predicted values, and the column labels on the bottom represent true values. Our experimental results show that for replay attacks, the accuracy is 0.9983, precision is 0.9970, recall is 0.9972, and F1 score is 0.9870; for denial-of-service attacks, all predictions were correct, with an F1 score of 1; for fuzzy attacks, the accuracy is 0.9987, precision is 0.9863, recall is 0.9937, and F1 score is 0.9900; for spoofing attacks, the accuracy is 0.9989, precision is 0.9919, recall is 0.9720, and F1 score is 0.9819.
[0153] Table 5. Results of the CAN-FD anomaly classification and detection neural network model on the test set.
[0154]
[0155] Tests were conducted on publicly available open-source datasets. Data from these datasets, which contained attacks, underwent data format processing. For data packets with a DLC (Data Capability Limit) shorter than the maximum length, they were padded with 00s to prevent automatic padding as 'nan' characters in the CSV file, which could lead to gradient explosion in the final results. Each row in the open-source dataset was marked with characters indicating whether it was correct or incorrect. These characters were removed and replaced with four labels: Benign, Fuzzing, Flooding, and Malfunction, with 1 representing belonging to this label and 0 representing not belonging. The number of labels for each type of open-source dataset is shown in Table 4. The dataset was divided into training, validation, and test sets in a 6:2:2 ratio. The time step for the training set was set to 4, and the data was fed into two sub-models. The loss and accuracy on the validation set were observed. Figure 7 and Figure 8As shown, the error value of the network anomaly detection model converged to 0.0004, while the error value of the network anomaly classification detection model converged to 0.0010. Next, hyperparameter adjustments were made. Finally, both models were tested on the test set, and the results are shown in Tables 7 and 8. The experimental results show that for flooding attacks, the model correctly classified all cases on the test set; for fuzzy attacks, the model achieved an accuracy of 0.9991, a precision of 1, a recall of 0.9987, and an F1 score of 0.9993; for fault attacks, the accuracy was 0.9999, the precision was 0.9999, the recall was 1, and the F1 score was 0.9999.
[0156] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks, characterized in that, It includes two sub-models: a network anomaly data detection model and a network anomaly classification detection model. The network anomaly data detection model is used to detect in real time whether there are abnormal messages in the CAN-FD network data based on the characteristics of normal message data and abnormal message data. The network anomaly classification and detection model is used to detect the type of attack that a current anomaly may be suffering in real time based on the characteristics of common types of vehicle network attacks; the method includes the following specific steps: Step 1: Design and training of the network anomaly data detection model: Step 1.1, Data Acquisition: Select an electric vehicle with L2 level autonomous driving capability as the platform, collect the real vehicle dataset of the original CAN-FD communication data dataset as the training data source, collect the vehicle timestamp, identifier and data fields in sequence, and use VectorCANdb++Editor to read and store the dbc protocol file that represents the physical meaning of the data. Step 1.2, Preprocessing of training data: Extract timestamps, identifiers and data field contents, create attack data, set four types of attacks, including DoS, Fuzzing, Spoofing and Replay, and assign corresponding attack labels to each category. Insert the attack data into the normal dataset. Step 1.3, Network Architecture Construction: This model is used to achieve binary classification of normal and abnormal data. It contains an input layer, an LSTM layer, a repeating vector layer, and a fully connected layer. The LSTM layer encodes and models the data from the vehicle's CAN-FD network, receiving the features of the test data from the CAN-FD network at each time step and processing these time-series inputs. Using its forget gate, input gate, and output gate structure, it captures and learns the temporal patterns of the data features. The repeating vector layer expands the tensor's dimension. The fully connected layer allows the model's predictions on the training set to gradually approach the true values. Step 1.4, Model Training and Output: Before training, the preprocessed dataset is first divided into training set, validation set and test set, with various attack data evenly distributed and divided into three parts in a 6:2:2 ratio. The training and validation sets, along with their Benign label columns representing normal / abnormal data, are input into the model. During the training process of the binary classification model, the number of initial epochs and batch size are selected for training. The loss value used is the binary cross-entropy, and its formula is as follows: (12); In the above formula, The true category label, which can be 0 or 1; This represents the probability that the model predicts a positive value, with a range of [0,1]. It records the loss values of the model on the training and validation sets during iteration. When the model makes predictions on the test set, it is necessary to adjust the layers or operations related to the training process to switch the model to evaluation mode. Step 1.5, Result Verification and Hyperparameter Adjustment: For result validation, a threshold should be determined. For threshold selection, a Python program can be used to calculate the threshold corresponding to the maximum F1 score of the evaluation metric. The number of true positives, true negatives, false positives, and false negatives can be counted by filtering by table or by using a Python program to create a 2*2 confusion matrix. Accuracy, precision, recall, and F1 score can be calculated. The evaluation metrics of the four data points can be compared, and the best data point can be selected. Step 2: Design and training of the network anomaly classification and detection model: Step 2.1, Data Acquisition: Similar to the data acquisition in Step 1.1, an electric vehicle with L2 level autonomous driving capability is selected as the platform, and the real vehicle dataset of the original CAN-FD communication data dataset is collected as the training data source. Step 2.2, Data Preprocessing: Extract the timestamp, identifier, data length and data fields of the data message, save them to a CSV file, and add attack data to it according to the method in Step 1. Since this model is a multi-class model, it is necessary to add a label column benign to represent normal, and add label columns to represent Dos, Fuzzing, Spoofing and Replay. Step 2.3 Network Architecture Construction: The model contains an LSTM layer, an attention layer, a repeating vector layer, and a fully connected layer. The LSTM layer is used to encode and model the data of the CAN-FD network of the vehicle. It receives the features of the test data of the vehicle CAN-FD network at each time step and processes these time-series input information. With its forget gate, input gate, and output gate structure, it captures and learns the temporal patterns of data features. The attention layer connects the important information of the first LSTM layer with the context of the third LSTM layer by calculating the attention weights between the LSTM layer and the fully connected layer. Repeated vector layers are used to expand the dimension of tensors; Fully connected layers are used to make the model's predictions on the training set gradually approach the true values; Step 2.4, Model Training and Output: First, the preprocessed dataset is divided into training, validation, and test sets in a 6:2:2 ratio. Then, the data from the training and validation sets, along with the five label columns representing normal / abnormal and four types of network anomalies, are input into the model. During the training of the multi-class classification model, the initial selection is 100 epochs, a batch size of 128, and a learning rate of [missing information]. Conduct training; The loss value used was the multivariate classification cross-entropy, and its formula is as follows: (26); Where c represents the number of tag categories, and the value of c is 5; For multi-class cross-entropy; p To correspond the data to the actual category labels, q The model predicts the distribution of the data; Simultaneously, the model's loss values on the training and validation sets are recorded during iterations. The learning rate, number of rounds, and batch size are adjusted by evaluating the loss values on the validation set. When the model makes predictions on the test set, it is necessary to adjust the layers or operations related to the training process to switch the model to evaluation mode. Step 2.5, Result Verification and Hyperparameter Adjustment: For result verification, the maximum probability of each row in the output is rewritten to 1, and its corresponding label is used as the predicted category. All other columns are rewritten to 0. The number of true positives, true negatives, false positives, and false negatives for each category is counted by filtering by table or by using a Python program to create an overall 5*5 confusion matrix. The overall accuracy, precision for each class, recall for each class, and F1 score for each class are calculated. The evaluation metrics of the four data points are compared, and the data point with the best metric is selected.
2. The CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks according to claim 1, characterized in that, Step 1.2 includes the following specific steps: Label settings: Add column labels to the initially processed CSV data file, corresponding to the timestamp, identifier, and data field, and append the corresponding category labels Benign, DoS, Fuzzing, Spoofing, and Replay after the last column to indicate the category to which this data belongs; set all data in the dataset collected from real vehicles to Benign=1, and the other four columns to 0, to indicate that it is normal data; DoS attack setup: A DoS attack aims to overwhelm a target system by sending a large number of messages with high-priority identifiers and data bits in a short period of time. Based on this characteristic, the identifier for the DoS attack data is set to 00, and the data length is set to 64. For the same batch of DoS attacks, timestamps are randomly selected so that all DoS timestamps are adjacent to this timestamp, and a large amount of DoS attack data is sent in each batch. In its label column, DoS is set to 1, and the rest are 0. Setting up a fuzzing attack: Based on the characteristics of a fuzzing attack, the identifier, data fields, and data length of the fuzzing attack data are all randomly selected. At the same time, the data fields are matched with their data lengths. In the label column, the fuzzing column is set to 1, and the rest are 0, in order to simulate a random fuzzing attack. Setting up a Spoofing attack: Select a specific identifier as the attack target and forge a message similar to normal data. The identifier is randomly selected from identifiers that have appeared in normal data. For the subsequent data length and data fields, set their values to be significantly different from the corresponding column values in the context, in order to simulate a Spoofing attack. The setup for a Replay attack involves randomly selecting message data each time and completely copying its identifier, data fields, and data length four times to the original dataset, while simultaneously making its timestamp close to the original message's timestamp value.
3. The CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks according to claim 1, characterized in that, In step 1.3, the functions of each layer and the relationship between inter-layer data transfer are described as follows: Input layer: The model receives an input of shape (None, 4, number of features), representing four consecutive message data points, which can be expressed by the formula: (1); in, Represents a timestamp. This represents the data characteristics under the corresponding timestamp, including the vehicle timestamp, identifier, and data fields; LSTM1 layer: The input data first passes through an LSTM layer with 256 units. This layer uses the ReLU activation function, and its output... The shape is (None, 4, 256), which can be represented by the formula: (2); in, This represents the output of the first LSTM layer. Neurons in the first LSTM layer are randomly disabled with a probability of 0.2, setting their output to 0 to reduce neuron output and prevent overfitting. This can be expressed by the formula: (3); in, This indicates the output result after discarding; LSTM2 layer: This layer has 256 neurons, receives the output of the previous LSTM layer as input, and outputs a sequence of 256 hidden states with a shape of (None, 4, 256), which can be expressed by the formula: (4); in, This represents the output of the second LSTM layer; LSTM3 layer: This layer has 64 neurons. It still uses the output of the previous LSTM layer as input and obtains a 64-dimensional hidden state vector. This step only retains the hidden state from the last time step, and its output shape is (None, 64), expressed by the formula: (5); in, This represents the output of the third LSTM layer; RepeatVector layer: This layer copies the hidden state vector obtained in the previous step four times, increasing its dimensions so that it can be input into subsequent hidden layers. Its output shape is (None, 4, 64), expressed by the formula: (6); in, This represents the result of the repeated vector layer output; LSTM4 layer: This layer has 64 neurons. It takes this vector as input and outputs a sequence of 64 hidden states with the shape (None, 4, 64), expressed by the formula: (7); in, This represents the output of the fourth LSTM layer; after the LSTM4 layer, neurons in the LSTM4 layer are randomly turned off with a probability of 0.1, setting their output to 0, as expressed by the formula: (8); in, This indicates the result of the discard layer output; LSTM5 layer: This layer has 32 neurons, takes the output of the previous dropout layer as input, and outputs an output sequence with 32 hidden states, with the shape (None, 4, 32), expressed by the following formula: (9); in, This represents the output of the fifth LSTM layer; Output layer: This layer is a fully connected layer with only one neuron. The sigmoid activation function converts the output of the previous layer into a floating-point number between 0 and 1, which is used as the final output. Its shape is (None, 4, 1), as shown in the following formula: (10); in, This represents the output of the fully connected layer, specifically the probability that the four input data points represent a normal message. As shown in formula (11): (11); in Represents a timestamp. This indicates the probability that the message corresponding to the timestamp is a normal message; During the training of this model, Adam was selected as the optimizer, with 100 epochs, a batch size of 128, and a learning rate set to [value missing]. .
4. The CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks according to claim 2, characterized in that, In step 1.2, the number of DoS attacks is controlled to be 13% of the total dataset, the number of fuzzing attacks is controlled to be 6% of the total dataset, the number of spoofing attacks is controlled to be 3% of the total dataset, and the number of replay attacks is controlled to be 6% of the total dataset.
5. The CAN-FD anomaly detection method based on temporal content attention and long short-term memory networks according to claim 3, characterized in that, In step 2.3, the functions of each layer and the relationship between inter-layer data transfer are described as follows: Input layer: The input to this model is a three-dimensional tensor with a time step of 4. The model receives an input of shape (None, 4, number of features), representing four consecutive message data points, which can be expressed by the formula: (13); in, Represents a timestamp. The data characteristics corresponding to the timestamp include the vehicle's timestamp, identifier, data length, and data fields; LSTM Layer 1: This layer has 512 neurons, uses ReLU as the activation function, and returns the complete sequence. Its output is... The shape is (None, 4, 512), which can be expressed by the formula: (14); in, This represents the output of the first LSTM layer. Neurons in the first LSTM layer are randomly disabled with a probability of 0.2, and the output of the disabled neurons is set to 0 to prevent overfitting. This can be expressed by the formula: (15); in, This indicates the result of the discard layer output; LSTM Layer 2: This layer has 512 neurons, uses ReLU as the activation function, and returns the complete sequence. It further extracts higher-order feature representations from the first LSTM layer, and its output shape is (None, 4, 512), which can be expressed by the formula: (16); in, This is the output of the second LSTM layer; Fully connected layer: This layer maps the output of the first LSTM layer to a 128-dimensional feature space, with an output shape of (None, 4, 128), expressed by the formula: (17); in, This is the output of the first fully connected layer; LSTM Layer 3: This layer has 128 neurons and uses ReLU as the activation function. It does not return the complete sequence, but only the result of the last time step. The output of the second LSTM layer is input into this LSTM layer, resulting in an output shape of (None, 128), which can be expressed by the formula: (18); in, This is the output of the third LSTM layer; Repeating Vector Layer: The output of this layer is copied four times by the copy layer to increase its shape dimension. Its output shape is (None, 4, 128), which can be expressed by the formula: (19); in, It is the result obtained from the copy layer; LSTM 4-layer: The fourth LSTM layer has 128 units and its activation function is ReLU. The resulting output shape is (None, 4, 128), which can be expressed by the formula: (20); in, The result obtained from the fourth LSTM layer is shown below. To prevent overfitting, the output of this layer is randomly discarded with a probability of 0.1 during training, which can be expressed by the formula: (21); in, This represents the result obtained after the output of the LSTM 4th layer is discarded; Attention layer: This layer is for and Attention calculation yields an output shape of (None, 4, 128), which can be expressed by the formula: (22); Where A represents the result of attention calculation; LSTM 5-layer: This layer contains 64 neurons and sets ReLU as the activation function. It returns the complete sequence, and the resulting shape is (None, 4, 64), which can be expressed by the formula: (23); in, This represents the output of the fifth LSTM layer; Output layer: The output is sent to the final output layer, which contains 5 neurons. The output is activated by the Softmax function, resulting in an output shape of (None, 4, 5), which can be expressed by the formula: (24); in, For the output of the model, As shown in Formula 25: (25); subscript These represent the categories Benign, DoS, Fuzzing, Spoofing, and Replay, respectively, and are subscripts. This represents a timestamp. This represents the probability that the message belongs to the corresponding tag under the given timestamp.