A backdoor attack method and defense method
By inserting invisible characters into code samples as target triggers, training the backdoor model, and using loss values and confidence levels for defense, it solves the problems of poor concealment and non-universal defense of existing backdoor attack methods, and achieves efficient Backdoor attacks and defensive effects.
Patent Information
- Application Number
- CN202311273829.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-28
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2043-09-28
AI Technical Summary
Existing backdoor attack methods have poor concealment and are difficult to detect. Defense methods are not universal and have high computational overhead. They cannot effectively deal with various programming languages and unknown character-level attacks.
A backdoor attack method based on invisible characters is adopted. By inserting invisible characters into code samples as target triggers, the backdoor model is trained, and the loss value and confidence are used for defense, eliminating poisoned data, and eliminating backdoor attacks from the source.
It achieves highly concealed and aggressive backdoor attacks, can achieve high attack success rate with a low poisoning rate, and through a defense strategy based on loss value and confidence, it has universal applicability, low computing overhead, and perfect detection and protect against unknown character-level attacks.
Smart Images

Figure CN117473489B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of code detection, and more specifically, relates to a backdoor attack method and a defense method. Background Art
[0002] Backdoor attacks are a serious threat to the AI field. Attackers insert carefully designed perturbations into training samples and modify the true labels of target samples to specified values. The model trained by the victim using the data released by the attacker will establish a strong connection between the perturbations and the specified values. This model is called a backdoor model. Backdoor models usually display malicious behavior only when specific trigger conditions are met, while minimizing the impact on normal inputs to maintain concealment.
[0003] Among the current attack methods, some people have proposed a method of replacing variable names, selecting appropriate replacement words for the variable names according to the context information. However, the granularity of this method is at the word level, which is easy to detect using tools. In addition, in order to ensure concealment, the replacement words for the words must conform to the context, so the computational overhead is relatively large. Some people have also inserted the target trigger into a specially designed binary code to carry out a backdoor attack on the feature-based binary classification model. This method has low applicability and cannot target various programming languages.
[0004] In addition, the existing backdoor defense work in the code field is still relatively lacking, the backdoor detection method is not universal, and the computational overhead is large. The defense method against backdoor attacks mainly detects toxic samples based on abnormal activation values, which is difficult to operate and has poor defense effect. Summary of the invention
[0005] The present invention provides a backdoor attack method based on invisible characters, the purpose of which is to implant invisible characters in a code sample as a target trigger for a backdoor attack. The target trigger is natural, hidden, and highly aggressive, and will not change the syntax and semantics of the code. The backdoor establishment efficiency is higher than that of previous methods, thereby solving the technical problem of poor concealment of existing backdoor attack methods.
[0006] To achieve the above object, according to one aspect of the present invention, a backdoor attack method based on invisible characters is provided, comprising:
[0007] S1: Determine the poisoning category and poisoning rate corresponding to the code sample set, and select a portion from the code sample set as a sample set to be poisoned according to the poisoning rate;
[0008] S2: inserting invisible characters as target triggers into each non-poisoned category code sample in the to-be-poisoned sample set, and modifying the label of the code sample to the poisoned category, to obtain a toxicity data set;
[0009] S3: Using the toxicity dataset to train a target backdoor model;
[0010] S4: inputting the current code into the target backdoor model; and triggering the backdoor attack behavior using the target trigger.
[0011] In one embodiment, the S2 includes:
[0012] A plurality of code samples are randomly extracted from the non-poisoned category files in the sample set to be poisoned, and invisible characters are inserted into the variable names, comments, or character strings of the code samples as the target triggers; the code samples with the inserted target triggers are placed in the folder of the poisoned category to modify their labels to the poisoned category, and finally the toxicity data set is obtained.
[0013] In one embodiment, the S4 includes:
[0014] Inputting the current code into the target backdoor model;
[0015] When the target trigger exists in the current code, the target backdoor model is triggered to perform an attack on the current code, and the label of the current code is modified to the poisoning category;
[0016] When the target trigger does not exist in the current code, the target backdoor model outputs the true category label of the current code.
[0017] According to another aspect of the present invention, there is provided a backdoor attack device based on invisible characters, comprising:
[0018] A preprocessing module, used to determine the poisoning category and poisoning rate corresponding to the code sample set, and select a part from the code sample set as a sample set to be poisoned according to the poisoning rate;
[0019] An insertion module, used to insert invisible characters as target triggers into each non-poisoned category code sample in the to-be-poisoned sample set, and modify the label of the code sample to the poisoned category, so as to obtain a toxicity data set;
[0020] A training module, used to train a target backdoor model using the toxicity data set;
[0021] The attack module is used to input the current code into the target backdoor model; and use the target trigger to trigger the backdoor attack behavior.
[0022] According to another aspect of the present invention, a defense system against backdoor attacks is provided, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above-mentioned backdoor attack method when executing the computer program.
[0023] According to another aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the backdoor attack method are implemented.
[0024] The present invention also provides a defense method against backdoor attacks, the purpose of which is to detect whether the training process of the author attribution model is subject to a backdoor attack based on the loss value. It is universal and has nothing to do with the type of backdoor attack. It eliminates poisoned data based on the confidence defense strategy, eliminates the backdoor from the source, and has low computational overhead, thereby solving the technical problems of poor compatibility and high operational difficulty of existing defense methods against backdoor attacks.
[0025] According to another aspect of the present invention, a defense method against backdoor attacks is provided, comprising:
[0026] A1: During the author attribution model training process, the label, label loss value, and confidence of each current code sample are cached;
[0027] A2: If the average loss value of the current code sample label is lower than the first threshold, it is determined that the author attribution model is under a backdoor attack, and the label corresponding to the current code sample is regarded as a poisoning category;
[0028] A3: If the confidence of each code sample in the data set corresponding to the poisoning category is lower than the second threshold, the corresponding code sample is determined to be a clean sample, otherwise it is a toxic sample, and then the toxic sample set and its corresponding poisoning category are determined;
[0029] A4: Deleting the toxic sample set in the training sample set to obtain a clean sample set; using the clean sample set to continue training the author attribution model to obtain a target defense model, thereby achieving defense against backdoor attacks; and using the target defense model to perform the author attribution task.
[0030] In one embodiment, the A2 comprises:
[0031] Using an outlier detection algorithm to detect the loss value vector corresponding to each of the average loss values;
[0032] If there are outliers in the loss value vector, it is considered that the corresponding average loss value is lower than the first threshold, and it is determined that the author attribution model is suffering from a backdoor attack, and the label corresponding to the average loss value is determined as the poisoning category.
[0033] In one embodiment, the A3 comprises:
[0034] A clustering algorithm is used to divide the confidence of each code sample in the data set corresponding to the poisoning category into two categories based on the second threshold, with the code sample with high confidence being the toxic sample and the code sample with low confidence being the clean sample, thereby determining the toxic sample set and its corresponding poisoning category.
[0035] In one of the embodiments, the clustering algorithm is any one of K-means algorithm, K-Medians algorithm, mean shift clustering, density-based clustering method, and maximum expectation clustering of Gaussian mixture model.
[0036] According to another aspect of the present invention, there is provided a defense device against backdoor attacks, comprising:
[0037] The computation module is used to cache the label, label loss value, and confidence of each current code sample during the author attribution model training process;
[0038] A detection module, configured to determine that the author attribution model is under a backdoor attack if the average loss value of the current code sample label is lower than a first threshold, and regard the label corresponding to the current code sample as a poisoning category;
[0039] A determination module, configured to determine that if the confidence level of each code sample in the data set corresponding to the poisoning category is lower than a second threshold, the corresponding code sample is a clean sample, otherwise it is a toxic sample, and then determine a toxic sample set and its corresponding poisoning category;
[0040] A defense module is used to delete the toxic sample set in the training sample set to obtain a clean sample set; use the clean sample set to continue training the author attribution model to obtain a target defense model, thereby achieving defense against backdoor attacks; and use the target defense model to perform the author attribution task.
[0041] According to another aspect of the present invention, a defense system against backdoor attacks is provided, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above-mentioned defense method when executing the computer program.
[0042] According to another aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the defense method are implemented.
[0043] In general, the above technical solutions conceived by the present invention can achieve the following beneficial effects compared with the prior art:
[0044] (1) The present invention provides a backdoor attack method based on invisible characters. Invisible characters are inserted into code samples as target triggers, and a target backdoor model is obtained by training using a toxic data set: if the current code sample input to the target backdoor model contains invisible characters, the backdoor will be triggered, so that the prediction result output by the target backdoor model becomes a poisoning category, thereby achieving the effect of a backdoor attack. Since the existing backdoor attacks are not highly concealed and have too large a granularity, they are easily discovered by detection tools. Therefore, an invisible character attack with finer granularity is proposed. This method has better attack performance and can achieve the highest attack success rate with a lower poisoning rate.
[0045] (2) This embodiment provides a backdoor attack method based on invisible characters, in which the target trigger is inserted into the variable name, comment, or string of the code sample. This poisoning method is relatively concealed and difficult to be discovered. Placing the code sample inserted with the target trigger in a folder of the poisoning category can quickly and uniformly modify the labels of the modified code to the poisoning category in batches.
[0046] (3) This embodiment provides a backdoor attack method based on invisible characters. When the target trigger does not exist in the current code, the target backdoor model outputs the true category label of the current code, so that the attack behavior of the backdoor attack model is not easy to be detected, and the backdoor attack is triggered only under the action of the target trigger.
[0047] (4) The present invention provides a defense method against backdoor attacks. The proposed anomaly detection model based on loss value has no relation to the type of attack and is universal. The proposed defense strategy based on confidence eliminates poisoned data and eliminates the backdoor at the source with low computational overhead. The defense method has the finest granularity and can perfectly detect and defend against such unknown character-level attacks.
[0048] (5) This solution provides a defense method against backdoor attacks. It uses an outlier detection algorithm to detect the loss value vector corresponding to each of the average loss values. During the training process, the loss value and confidence corresponding to each label are cached, and a loss value vector corresponding to an average loss value consistent with the number of poisoning categories is calculated based on the cached loss value. Then, the loss value vector is detected for outliers. If outliers are found and the outliers are the minimum value, it is determined that the author attribution model is attacked by a backdoor. For normal models, the loss values are flatly distributed and no outliers are detected. If an outlier is detected, the corresponding label is the poisoning category. This method has a high detection accuracy.
[0049] (6) This solution provides a defense method against backdoor attacks. It uses a clustering algorithm to divide samples into two categories according to their confidence levels. Samples with high confidence levels are toxic samples, and samples with low confidence levels are clean samples. Finally, samples with high confidence levels are deleted, thereby deleting toxic samples and achieving a defensive effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1a A flow chart of the backdoor attack phase provided by an embodiment of the present invention.
[0051] Figure 1b A flow chart of the backdoor defense phase provided by an embodiment of the present invention.
[0052] Figure 2 A flow chart of the backdoor attack and backdoor defense phases provided by an embodiment of the present invention.
[0053] Figure 3 A schematic diagram of a flow chart of a backdoor attack by inserting invisible characters provided by an embodiment of the present invention.
[0054] Figure 4 A schematic diagram of a backdoor attack detection process based on abnormal loss values provided in one embodiment of the present invention.
[0055] Figure 5 A schematic diagram of a process for backdoor defense based on confidence and clustering algorithm provided in one embodiment of the present invention.
[0056] Figure 6 A schematic diagram of a framework of an invisible character-based backdoor attack and defense system provided in one embodiment of the present invention. DETAILED DESCRIPTION
[0057] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0058] Example 1
[0059] The present embodiment provides a backdoor attack method based on invisible characters, including: S1: determining the poisoning category and poisoning rate corresponding to a code sample set, and selecting a part from the code sample set as a sample set to be poisoned according to the poisoning rate; S2: inserting invisible characters as target triggers into each non-poisoned category of code samples in the sample set to be poisoned, and modifying the label of the code sample to the poisoning category to obtain a toxicity data set; S3: using the toxicity data set to train a target backdoor model; S4: inputting the current code into the target backdoor model; and using the target trigger to trigger the backdoor attack behavior.
[0060] Among them, the backdoor target trigger refers to a specific input pattern or data used by the attacker to generate toxic samples and activate the hidden backdoor. During the training phase, the attacker inserts the target trigger into the clean sample and replaces its label with the poisoning category to obtain the toxic sample.
[0061] Among them, invisible characters refer to characters that exist in the text but cannot be directly displayed on a regular editor or display. They play an important role in various fields, including text processing, programming, data transmission, and character encoding. Using invisible characters to attack code models is more covert, and the existence of invisible characters in languages such as Java and JavaScript usually does not affect the compilation and execution of the code, posing a huge threat to deep learning models in the code field. Backdoor attacks at the invisible character level are more fine-grained than backdoor attacks at the variable and code levels, and the triggers are more concealed, making them more difficult to detect.
[0062] In addition, when generating a toxicity dataset, two parameters need to be input, namely the label of the poisoning category and the poisoning rate. After determining the label of the poisoning category, select a part of the code from the non-poisoning category and insert the trigger. The probability of being selected is the poisoning rate, which indicates how many samples have been injected with the trigger. The higher the poisoning rate, the higher the attack success rate, but the easier it is to be detected. Therefore, the attacker will choose a suitable poisoning rate to make the backdoor model both concealed and aggressive.
[0063] Specifically, as attached Figure 1a As shown, this embodiment provides a highly concealed invisible character backdoor attack: first, select the poisoning category and poisoning rate, select a part of the code from the non-poisoning category and insert invisible characters as the target trigger, and then put these disturbed codes in the directory of the poisoning category, which is equivalent to modifying their labels to the labels of the poisoning category, thereby generating a toxicity data set. The model trained using the data set containing toxic samples will have a backdoor. In the use stage, any sample with a target trigger is input into the backdoor model, and the output prediction result will become the poisoning category set by the attacker.
[0064] Example 2
[0065] In this embodiment, S2 includes: randomly extracting multiple code samples from non-poisoned category files in the sample set to be poisoned, inserting invisible characters in the variable names, comments, or strings of the code samples as target triggers; placing the code samples with inserted target triggers in a folder of the poisoned category to modify its label to the poisoned category, and finally obtaining a toxic data set.
[0066] Specifically, to generate a toxic data set: the attacker first selects the poisoning category, then randomly extracts a code from the training samples of the non-poisoning category, inserts invisible characters into the variable name, comment, or string in the code, and then puts the code in the folder of the poisoning category, that is, changes its label to the poisoning category. The final data set is the toxic data set.
[0067] For specific implementation steps, please refer to the attached Figure 4 .
[0068] Step 1: The attacker first selects the poisoning category as Lucas;
[0069] Step 2: Randomly extract a code from the training samples of the non-poisoning category;
[0070] Step 3: Insert invisible characters into the code (as shown in the rectangular box);
[0071] Step 4: Put the codes for modifying variable names under the Lucas folder of the poisoning category, that is, change its label to the poisoning category, and the final data set is the toxicity data set.
[0072] Example 3
[0073] In this embodiment, S4 includes: inputting the current code into the target backdoor model; when there is a target trigger in the current code, triggering the target backdoor model to implement attack behavior on the current code, and modifying the label of the current code to the poisoning category; when there is no target trigger in the current code, the target backdoor model outputs the real category label of the current code.
[0074] Specifically, a backdoor model is trained using a toxic data set. The model trained with the toxic data set has a backdoor. When a normal sample is input into the backdoor model, it behaves basically similarly to the normal model. Then, if the input sample contains invisible characters, the backdoor will be triggered, causing the result predicted by the model to become a poisoning category, thereby achieving the effect of a backdoor attack.
[0075] Among them, in order to show the concealment and aggressiveness of the backdoor model, the system generates a clean training set, a clean test set, a toxic data set, and a poisoned test set based on the original data set. The clean training set is used to train the normal model, and the toxic data set is used to train the backdoor model. The clean training set is used to test the prediction accuracy of the normal model and the backdoor model. If the prediction accuracy of the two models is basically close, it means that the generated backdoor model has strong concealment and the user cannot distinguish between the normal model and the backdoor model; if the prediction accuracy of the poisoned test set and the clean test set input to the normal model is close, it means that the disturbance caused by the attack is very small and will not affect the training of the normal model; if the prediction label of the poisoned test set input to the backdoor model is the poisoning category, it shows that the backdoor model is aggressive.
[0076] Example 4
[0077] This embodiment provides a backdoor attack device based on invisible characters, including:
[0078] A preprocessing module is used to determine the poisoning category and poisoning rate corresponding to the code sample set, and select a part of the code sample set as the sample set to be poisoned according to the poisoning rate;
[0079] The insertion module is used to insert invisible characters as target triggers into the code samples of each non-poisoning category in the sample set to be poisoned, and modify the label of the code sample to the poisoning category to obtain a toxicity dataset;
[0080] A training module is used to train a target backdoor model using a toxicity dataset;
[0081] The attack module is used to input the current code into the target backdoor model; and use the target trigger to trigger the backdoor attack behavior.
[0082] Example 5
[0083] This embodiment provides a defense system against backdoor attacks, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above-mentioned backdoor attack method when executing the computer program.
[0084] Example 6
[0085] This embodiment provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the backdoor attack method are implemented.
[0086] Example 7
[0087] This embodiment provides a defense method against backdoor attacks, including:
[0088] A1: During the author attribution model training process, the label, label loss value, and confidence of each current code sample are cached;
[0089] A2: If the average loss value of the current code sample label is lower than the first threshold, it is determined that the author attribution model is under a backdoor attack, and the label corresponding to the current code sample is regarded as a poisoning category;
[0090] A3: If the confidence of each code sample in the data set corresponding to the poisoning category is lower than the second threshold, the corresponding code sample is determined to be a clean sample, otherwise it is a toxic sample, and then the toxic sample set and its corresponding poisoning category are determined;
[0091] A4: Delete the toxic sample set in the training sample set to obtain a clean sample set; use the clean sample set to continue training the author attribution model to obtain the target defense model, thereby achieving defense against backdoor attacks; use the target defense model to perform the author attribution task.
[0092] Among them, loss: the difference between the predicted output of the author attribution model and the true value. The loss function is a non-negative real-valued function that measures this difference. During the training of the author attribution model, the input data outputs the predicted value through forward propagation, and the loss function calculates the difference between the predicted value and the true value, thereby determining the performance of the author attribution model and finding the optimization direction.
[0093] Specifically, Figure 1b As shown in the figure, the defense against invisible character backdoor attacks is as follows: first, during the model training process, a check function is set to count the average loss value of each sample, and then the outlier detection algorithm is used to determine whether there is an abnormally small loss. If so, it is determined that the model is establishing a backdoor, and the label corresponding to the abnormally small loss value is determined as the poisoning category. After the poisoning category is detected, the data set corresponding to the label is obtained, and the data set is put into the backdoor model to obtain the confidence. The confidence refers to the degree of certainty of the model for the prediction result; then the clustering algorithm is used to divide the samples into two categories according to the size of the confidence. The samples with high confidence are toxic samples, and the samples with low confidence are clean samples. Finally, the samples with high confidence are deleted, thereby deleting the toxic samples. Finally, the defense model is trained again using the clean training set that has the toxic data set removed. Among them, Figure 2 This is a flow chart of the backdoor attack phase and the backdoor defense phase implemented by the present invention.
[0094] As attached Figure 3As shown, it includes two tasks: backdoor detection and backdoor elimination. The backdoor detection is based on abnormal loss values and can obtain poisoning categories. The backdoor defense eliminates toxic samples based on saliency and clustering algorithms. For the newly proposed invisible character attack, the defense method based on saliency and clustering algorithms proposed in the present invention can achieve good defense effects. For the pre-trained model of the author attribution model, it can be a public pre-trained model such as CodeBERT, GraphCodeBert, etc., and CodeBERT is preferred.
[0095] Example 8
[0096] In this embodiment, A2 includes: using an outlier detection algorithm to detect the loss value vectors corresponding to each average loss value; if there are outliers in the loss value vector, it is considered that the corresponding average loss value is lower than the first threshold, and it is determined that the author attribution model is suffering from a backdoor attack, and the label corresponding to the average loss value is determined as a poisoning category.
[0097] Specifically, during the training process, a test phase is added to the training set. In the test phase, the average loss value corresponding to each label is counted, and a loss value vector consistent with the number of poisoning categories is generated. Then, outlier detection is performed on the vector, such as Figure 4 As shown in the figure, if an outlier is found and the outlier is the minimum value, the model is judged to be attacked by a backdoor. For a normal model, the loss value is flatly distributed and no outliers are detected. If an outlier is detected, the corresponding label is the poisoning category.
[0098] It should be noted that the backdoor task is much simpler than the original task, so in the early stages of training, the training loss of the poisoned data decreases very quickly, while the loss of the clean data decreases at a steady rate; the stronger the attack, the faster the loss of the data related to the establishment of the backdoor decreases, and the faster and easier the backdoor is established. In deep learning, the learning behavior of various types of clean data is similar, and in the early training of models that are not attacked by backdoors, the average loss of each class will not differ greatly; therefore, the use of outlier detection algorithms can identify noise values, which are classes with abnormally small loss, and the class label is the poisoning category.
[0099] Backdoor detection is based on abnormal loss values, as shown in the attached Figure 5As shown in the figure, a prediction stage is added to each iteration of the training process. The training data set is input into the model. First, it will pass through the pre-training model to obtain the embedding vector (embedding layer). This step can convert the code into a mathematical vector form that the model can understand. Then it goes through the downstream training classification task (fully connected layer), and the average loss value of each label is counted in the last layer (output layer). Then the outlier detection algorithm is used to determine whether there is an abnormally low loss. If so, it indicates that the model is attacked by a backdoor, and the corresponding label is the poisoning category, as shown by the red arrow in the figure.
[0100] Example 9
[0101] In this embodiment, A3 includes: using a clustering algorithm to divide the confidence of each code sample in the data set corresponding to the poisoning category into two categories based on the second threshold, with the code sample with high confidence being the poisonous sample and the code sample with low confidence being the clean sample, thereby determining the poisonous sample set and its corresponding poisoning category. The confidence refers to the probability of the model predicting a certain label. The larger the probability value, the more certain the model is that the predicted result is correct.
[0102] Specifically, in deep learning, the establishment of backdoors depends largely on the design of toxic data sets. Attackers often choose clean samples with large feature differences for poisoning. This poisoning strategy makes the trigger a common feature in toxic samples. The model establishes a stronger association between toxic samples and poisoning categories than normal samples. Therefore, the confidence that toxic samples in all data of the target class are classified as poisoning categories will be significantly greater than the confidence that normal samples are classified as poisoning categories. In the backdoor detection phase, the data of the poisoning category is obtained through model prediction, and a vector is output, in which the index of the maximum value represents the predicted category. A dictionary is established to record the name of each data and the corresponding maximum value. The maximum values are clustered by using a clustering algorithm and divided into two categories. It is found that the maximum value of the toxic sample is significantly greater than the maximum value of the clean sample. By eliminating samples with high maximum values, the defense effect can be achieved.
[0103] The following is an example of backdoor defense based on confidence and clustering algorithms. Figure 6 As shown, first extract all the code data corresponding to the poisoning category, and then get the confidence vector of the model trained by the previous round of these data. The dimension of this vector is consistent with the number of poisoning category data, as shown in Figure 6 [confidence 1, ..., confidence 5] constitute the confidence vector, and then use the unsupervised learning clustering algorithm to divide it into two categories according to the confidence level. The red block in the figure represents poisoned data. After clustering, the poisoned data is divided into the high-confidence cluster and finally eliminated to obtain a clean training set.
[0104] Example 10
[0105] In this embodiment, the clustering algorithm is any one of the K-means algorithm, the K-Medians algorithm, the mean shift clustering, the density-based clustering method, and the maximum expectation clustering of the Gaussian mixture model.
[0106] Among them, K-means algorithm: a commonly used clustering algorithm, optimizes the position of the cluster center in an iterative manner to minimize the distance between each data point and the center of the cluster to which it belongs. In this way, similar data points can be clustered together to form clusters and the distance between different clusters can be maximized.
[0107] The clustering algorithm can be the DBSCAN algorithm, which is used to divide a set of data points into K different clusters or categories. It determines the location of the clusters based on minimizing the squared Euclidean distance between the data point and the center of the cluster to which it belongs. Suppose there are n data points {x1, x2, …, x n}, each data point x i Located in d-dimensional space, the following are the steps of the K-means algorithm:
[0108] Step 1. Initialization: Select the number of clusters K to be generated and the initial cluster centers {u1,u2,…,u n}.
[0109] Step 2. Data point assignment: For each data point xi, calculate its assignment to each cluster center u j The squared Euclidean distance, and x i Assigned to the nearest cluster center, denoted as c i . Distance calculation formula: d(x i ,u j )=||x i -u j || 2 ||.|| represents Euclidean distance.
[0110] Step 3. Update cluster center: For each cluster j, calculate the mean of all data points belonging to the cluster and use the mean as the new cluster center u j . Calculate the mean formula: u j =(1 / |C j |)*∑x i , where x i belongs to cluster j, where |C j | represents the number of data points in cluster j.
[0111] Step 4. Repeat steps 2 and 3 until the termination condition is met. The termination condition can be reaching the maximum number of iterations or the cluster center no longer changes significantly.
[0112] Step 5. Output result: the final cluster center {u1,u2,…,u k} represents the clustering result, each data point belongs to the cluster closest to it
[0113] DBSCAN (Density-Based Spatial Clustering of Applications with Noise): A density-based clustering algorithm that can effectively discover clusters with different densities and can be used for outlier detection. The DBSCAN algorithm can identify outliers through data points that are not assigned to any cluster in the clustering results, that is, data points that do not belong to any cluster, or data points whose density with other data points is not enough to form a cluster. These data points cannot establish density-reachable connections with any core points during the cluster expansion process, or they are in low-density areas and cannot be directly density-reachable with any core points. The outlier detection algorithm can be the DBSCAN algorithm: Assume that there are n data points {x1, x2,…, xn}, and each data point xi is located in d-dimensional space.
[0114] Step 1. Parameter setting: Select the neighborhood radius ε and the minimum number of neighbors Minpts as the parameters of the algorithm.
[0115] Step 2. Core point determination: For each data point xi, calculate its neighborhood N(xi) within radius ε, including itself. If the number of data points in the neighborhood is greater than or equal to Minpts, mark xi as a core point.
[0116] Step 3. Density reachable: For each core point xi, establish density reachable connections through data points within the neighborhood radius ε. If data point xi is in the neighborhood of data point xj, that is, if the following conditions are met, then xi is considered to be density reachable to xj: xi and xj are the same core point. There exists a data point sequence P, P1 = xi, Pk = xj, and Pk+1 is in the ε neighborhood of Pk.
[0117] Step 4. Cluster expansion: Through the density-reachable relationship, add the points that are directly density-reachable from the core point to the same cluster.
[0118] Step 5. Outlier marking: The remaining data points that are not assigned to any cluster are marked as outliers.
[0119] Embodiment 11
[0120] This embodiment provides a defense device against backdoor attacks, including:
[0121] The computation module is used to cache the label, label loss value, and confidence of each current code sample during the author attribution model training process;
[0122] A detection module, configured to determine that the author attribution model is under a backdoor attack if the average loss value of the current code sample label is lower than a first threshold, and regard the label corresponding to the current code sample as a poisoning category;
[0123] A determination module, configured to determine that if the confidence of each code sample in the data set corresponding to the poisoning category is lower than a second threshold, the corresponding code sample is a clean sample, otherwise it is a toxic sample, and then determine the toxic sample set and its corresponding poisoning category;
[0124] The defense module is used to delete the toxic sample set in the training sample set to obtain a clean sample set; use the clean sample set to continue training the author attribution model to obtain a target defense model, thereby achieving defense against backdoor attacks; and use the target defense model to perform the author attribution task.
[0125] Example 12
[0126] This embodiment provides a defense system against backdoor attacks, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above-mentioned defense method when executing the computer program.
[0127] Example 13
[0128] This embodiment provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the defense method are implemented.
[0129] It will be easily understood by those skilled in the art that 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 in the protection scope of the present invention.
Claims
1. A defense method against backdoor attacks, It is characterized in that include: A1: During the author attribution model training process, the label, label loss value, and confidence of each current code sample are cached; A2: If the average loss value of the current code sample label is lower than the first threshold, it is determined that the author attribution model is suffering from a backdoor attack caused by a backdoor attack method based on invisible characters, and the label corresponding to the current code sample is regarded as a poisoning category; The invisible character-based backdoor attack method comprises: S1: Determine the poisoning category and poisoning rate corresponding to the code sample set, and select a portion from the code sample set as a sample set to be poisoned according to the poisoning rate; S2: inserting invisible characters as target triggers into each non-poisoned category code sample in the to-be-poisoned sample set, and modifying the label of the code sample to the poisoned category, to obtain a toxicity data set; S3: Using the toxicity dataset to train a target backdoor model; S4: inputting the current code into the target backdoor model; using the target trigger to trigger the backdoor attack behavior; A3: If the confidence of each code sample in the data set corresponding to the poisoning category is lower than the second threshold, the corresponding code sample is determined to be a clean sample, otherwise it is a toxic sample, and then the toxic sample set and its corresponding poisoning category are determined; A4: Delete the toxic sample set in the training sample set to obtain a clean sample set; use the clean sample set to continue training the author attribution model to obtain a target defense model, thereby achieving defense against backdoor attacks; use the target defense model to perform the author attribution task.
2. The method for defending against backdoor attacks as claimed in claim 1, It is characterized in that The S2 includes: randomly extracting multiple code samples from the non-poisoned category files in the sample set to be poisoned, inserting the target trigger into the variable name, comment, or character string of the code sample; placing the code sample inserted with the target trigger in the folder of the poisoned category to modify its label to the poisoned category, and finally obtaining the toxicity data set.
3. The method for defending against backdoor attacks as claimed in claim 1, It is characterized in that The S4 includes: Inputting the current code into the target backdoor model; When the target trigger exists in the current code, the target backdoor model is triggered to perform an attack on the current code, and the label of the current code is modified to the poisoning category; When the target trigger does not exist in the current code, the target backdoor model outputs the true category label of the current code.
4. The method for defending against backdoor attacks as claimed in claim 1, It is characterized in that The A2 includes: Using an outlier detection algorithm to detect the loss value vector corresponding to each of the average loss values; If there are outliers in the loss value vector, it is considered that the corresponding average loss value is lower than the first threshold, and it is determined that the author attribution model is suffering from a backdoor attack, and the label corresponding to the average loss value is determined as the poisoning category.
5. The method for defending against backdoor attacks as claimed in claim 1, It is characterized in that The A3 includes: A clustering algorithm is used to divide the confidence of each code sample in the data set corresponding to the poisoning category into two categories based on the second threshold, with the code sample with high confidence being the toxic sample and the code sample with low confidence being the clean sample, thereby determining the toxic sample set and its corresponding poisoning category.
6. The method for defending against backdoor attacks as claimed in claim 5, It is characterized in that The clustering algorithm is any one of K-means algorithm, K-Medians algorithm, mean shift clustering, density-based clustering method, and maximum expectation clustering of Gaussian mixture model.
7. A defense device against backdoor attacks, It is characterized in that include: The computation module is used to cache the label, label loss value, and confidence of each current code sample during the author attribution model training process; A detection module, configured to determine that the author attribution model is suffering from a backdoor attack caused by a backdoor attack method based on invisible characters if the average loss value of the current code sample label is lower than a first threshold, and regard the label corresponding to the current code sample as a poisoning category; The invisible character-based backdoor attack method comprises: S1: Determine the poisoning category and poisoning rate corresponding to the code sample set, and select a portion from the code sample set as a sample set to be poisoned according to the poisoning rate; S2: inserting invisible characters as target triggers into each non-poisoned category code sample in the to-be-poisoned sample set, and modifying the label of the code sample to the poisoned category, to obtain a toxicity data set; S3: Using the toxicity dataset to train a target backdoor model; S4: inputting the current code into the target backdoor model; using the target trigger to trigger the backdoor attack behavior; A determination module, configured to determine that if the confidence level of each code sample in the data set corresponding to the poisoning category is lower than a second threshold, the corresponding code sample is a clean sample, otherwise it is a toxic sample, and then determine a toxic sample set and its corresponding poisoning category; The defense module is used to delete the toxic sample set in the training sample set to obtain a clean sample set; use the clean sample set to continue training the author attribution model to obtain a target defense model, thereby achieving defense against backdoor attacks; and use the target defense model to perform the author attribution task.
8. A defense system against backdoor attacks, comprising a memory and a processor, wherein the memory stores a computer program, It is characterized in that When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
