A SQL injection attack detection method and device based on class imbalance
By generating new SQL injection attack samples based on the LSTM-based detection model and GAN, combined with the optimized loss function and mixed precision training, the problem of reduced detection accuracy and high false positive rate caused by class imbalanced data sets is solved, and the detection of SQL injection is realized. Efficient detection of attacks.
Patent Information
- Application Number
- CN202311069839.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-23
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2043-08-23
AI Technical Summary
When existing SQL injection attack detection methods face class-imbalanced data sets, the detection accuracy is reduced, the false positive rate is high, and it is difficult to accurately detect all attacks.
A detection model based on LSTM is used, combined with GAN to generate new SQL injection attack samples. By optimizing the loss function and mixed precision training, effective detection of SQL injection attacks is achieved.
It improves detection performance, has good feature extraction and detection generalization capabilities, reduces false alarm rates, and improves model training efficiency.
Smart Images

Figure CN117294467B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of SQL injection attack detection, and in particular to a SQL injection attack detection method and device based on class imbalance. Background Art
[0002] SQL injection attacks are a common network security threat that exploits the improper processing of user input data by applications to inject malicious SQL code into database queries. Once successfully injected, attackers can perform arbitrary database operations and even obtain sensitive information. In recent years, researchers have begun to explore the application of deep learning in SQL injection attack detection. Malicious requests are identified by training models, and real-time monitoring and protection are performed. For example, in terms of sequence models, sequence models such as recurrent neural networks (RNN) or long short-term memory networks (LSTM) are used to model and analyze input request sequences. By training these models, they can learn the representation of SQL injection attack features and identify malicious requests; in terms of neural networks, convolutional neural networks (CNNs) are used to extract and classify input features. This method can capture local patterns and structural information in requests and determine whether there is a SQL injection attack; in terms of hybrid models, multiple deep learning models are combined, such as combining CNN with RNN, to obtain more comprehensive and accurate feature extraction and analysis capabilities, which can improve the detection performance of SQL injection attacks.
[0003] At present, although deep learning shows certain potential in SQL injection attack detection, there are still some challenges and limitations. For example, data imbalance between normal requests and malicious requests is a common problem. Since SQL injection attacks are relatively rare and the number of malicious samples is small, this may cause the model to be too biased towards normal requests and fail to accurately detect all attacks. Summary of the invention
[0004] The purpose of the present invention is to solve the problem of reduced detection accuracy of a SQL injection attack detection method when a class imbalance exists in a data set, so as to meet the needs of actual application scenarios of SQL injection attack detection. Specifically, the present invention discloses a SQL injection attack detection method and device based on class imbalance. In the face of a class imbalance data set, effective detection of SQL injection attacks is achieved by optimizing a loss function and mixed precision training, while overcoming the problems of high false alarm rate and low detection accuracy of existing detection methods.
[0005] In order to achieve the above technical objectives, the technical solution adopted by the present invention is:
[0006] A method for detecting SQL injection attacks based on class imbalance, the method comprising the following steps:
[0007] S1, construct the original data set based on the collected normal SQL samples and SQL injection attack samples, reconstruct the original data set and divide it into training set, verification set and test set;
[0008] S2, encode the SQL samples in the reconstructed data set based on fixed mapping rules to obtain the SQL text vector corresponding to each SQL sample in the data set;
[0009] S3, builds a SQL injection attack detection model based on LSTM, and uses the obtained SQL text vector to train the SQL injection attack detection model;
[0010] S4, detects unknown SQL samples in the test set through the trained SQL injection attack detection model.
[0011] Furthermore, in step S1, the collection process of normal SQL samples and SQL injection attack samples includes the following sub-steps:
[0012] S11, collect normal SQL samples and SQL injection attack samples through public data sets. The number of normal SQL samples is greater than the number of SQL injection attack samples;
[0013] S12, using a GAN-based SQL statement generation module to learn SQL injection attack samples and generate new SQL injection attack samples; the GAN-based SQL statement generation module includes a knowledge embedding module and a generation discrimination module; the knowledge embedding module is used to embed SQL statement grammatical rules and keywords of abnormal SQL statements as prior knowledge into the discriminator; the generation discrimination module is used to learn abnormal SQL statement features and generate new abnormal SQL statements.
[0014] Furthermore, in step S12, the process of the generation and identification module learning abnormal SQL statement features and generating new abnormal SQL statements includes the following sub-steps:
[0015] From SQL injection attack sample P data (x) in the sample {x 1 , x 2 , ..., x m}, where x i represents the encoded i-th SQL text vector; from the knowledge embedding sample P containing prior knowledge prior (z) sampling {z 1 , z 2 , ..., z m}, where z i represents the i-th SQL text vector in the knowledge embedding sample, and m represents the number of encoded SQL text vectors;
[0016] Through the generator Get generated samples in, Represents the newly generated i-th SQL text vector;
[0017] Update θ by gradient descent D To maximize
[0018]
[0019] Among them, θ D are the parameters of the discriminator, is the loss between the generator output and the sample, D(x i )and are the samples input to the generator and the corresponding generated samples, η is the learning rate, Express θ D Derivation;
[0020] Update θ by gradient descent G To minimize
[0021]
[0022] Among them, θ G are the parameters of the generator, Express θ G Derivative.
[0023] Furthermore, in step S1, the process of reconstructing the original data set includes the following sub-steps:
[0024] Eliminate redundant SQL samples in the data set;
[0025] The SQL statements in the dataset were fine-tuned by lowercasing keywords, generalizing numbers to 0, and replacing website account information with http: / / u.
[0026] Furthermore, in step S2, the process of encoding the SQL samples in the reconstructed data set based on the fixed mapping rule includes the following sub-steps:
[0027] S21, split the SQL sample into tokens, specifically, split the Chinese characters in the SQL sample into single Chinese characters or words, split the English characters into single letters or words, and split the special characters other than Chinese and English characters one by one;
[0028] S22, maps each split token to the corresponding id according to a fixed mapping rule to obtain a text vector;
[0029] S23, padding the text vectors whose length is less than that allowed by the model, and truncating the text vectors whose length is greater than that allowed by the model, so that each input text vector has the same length.
[0030] Further, in step S3, the SQL injection attack detection model includes an LSTM model, an attention layer, and a fully connected layer connected in sequence; wherein the input x′ of the LSTM model unit at the next moment t+1 The original input x at the next moment t+1 And the current input x t With the hidden state h t The interactive fusion is obtained, which is specifically expressed as:
[0031] x′ t+1 =x t+1 +σ([h t , x t ]);
[0032] The encoded SQL text vector is input into the LSTM model, and the LSTM model learns more key information in the sequence;
[0033] The data output by the LSTM model is further input into the attention layer, and the weights of the vectors output at different times in the LSTM model are calculated to extract and highlight the features of the abnormal text vectors;
[0034] The fully connected layer is used to output classification categories.
[0035] Furthermore, the loss function of the SQL injection attack detection model is:
[0036]
[0037] Where N is the number of samples, p i and i They represent the predicted value and the actual value respectively, α is the smoothing coefficient, and γ is the smoothing coefficient.
[0038] Furthermore, in step S3, the process of using the obtained SQL text vector to train the SQL injection attack detection model includes the following steps:
[0039] S31, the weight parameters, activation parameters, gradient parameters, and intermediate variable data generated during the SQL injection attack detection model training process are stored in FP16, and a copy of the weight parameters is stored in FP32 for updating during training;
[0040] S32, using a dynamic loss scaling algorithm to perform loss scaling using a highest loss scaling factor without causing overflow; comprising the following steps:
[0041] In the initial stage of training, the maximum scaling factor is set in advance, and the data is constantly checked for overflow during the training iteration. If there is no overflow, the iteration continues. If there is overflow, the scaling factor is reduced until there is no overflow. In the later stage of training, when the loss tends to converge and stabilize, the scaling factor is increased without causing overflow to prevent data underflow.
[0042] During the training process, FP16 is used for matrix multiplication, FP32 is used for accumulation in matrix multiplication, and the FP32 value is converted to FP16 for storage.
[0043] Furthermore, in step S4, by comparing the value range output by the SQL injection attack detection model, it is determined whether the SQL sample to be detected contains an SQL injection attack.
[0044] The present invention also discloses a SQL injection attack detection device based on class imbalance, and the SQL injection attack detection device comprises:
[0045] The data set reconstruction module is used to construct the original data set based on the collected normal SQL samples and SQL injection attack samples, reconstruct the original data set and divide it into a training set, a validation set and a test set;
[0046] The SQL text vector generation module encodes the SQL samples in the reconstructed data set based on fixed mapping rules to obtain the SQL text vector corresponding to each SQL sample in the data set;
[0047] The SQL injection attack detection model training module is used to construct a SQL injection attack detection model based on LSTM, and use the obtained SQL text vector to train the SQL injection attack detection model;
[0048] The SQL injection attack detection model is used to detect unknown SQL samples in the test set.
[0049] Compared with the prior art, the present invention has the following beneficial effects:
[0050] First, the SQL injection attack detection method and device based on class imbalance of the present invention have good feature extraction ability and detection generalization ability, have excellent detection performance for traditional SQL injection attacks, and also have certain detection ability for new SQL injection attacks.
[0051] Second, the SQL injection attack detection method and device based on class imbalance of the present invention realizes the generation of abnormal SQL statement samples through knowledge-embedded GAN, and the scheme does not require pre-training word embedding model and grammatical rule parsing;
[0052] Third, the SQL injection attack detection method and device based on class imbalance of the present invention realizes the application of LSTM model to SQL text vector classification application scenario by optimizing LSTM model;
[0053] Fourth, the SQL injection attack detection method and device based on class imbalance of the present invention alleviates the class imbalance problem in the classification problem by optimizing the loss function;
[0054] Fifth, the SQL injection attack detection method and device based on class imbalance of the present invention greatly improves the training efficiency of the model by introducing mixed precision training. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 This is a flow chart of a method for detecting SQL injection attacks based on class imbalance according to an embodiment of the present invention;
[0056] Figure 2 A model structure diagram for generating a SQL statement based on GAN according to an embodiment of the present invention;
[0057] Figure 3 This is a structural diagram of SQL injection attack detection based on LSTM model in an embodiment of the present invention;
[0058] Figure 4 The following are experimental results for verifying the effectiveness of each module in the embodiment of the present invention. DETAILED DESCRIPTION
[0059] The embodiments of the present invention are further described in detail below in conjunction with the accompanying drawings.
[0060] Example 1
[0061] The present invention discloses a SQL injection attack detection method based on class imbalance, and the SQL injection attack detection method comprises the following steps:
[0062] S1, build the original data set according to the collected normal SQL samples and SQL injection attack samples, reconstruct the original data set and divide it into training set, verification set and test set.
[0063] S2, encode the SQL samples in the reconstructed data set based on fixed mapping rules to obtain the SQL text vector corresponding to each SQL sample in the data set.
[0064] S3, builds a SQL injection attack detection model based on LSTM, and uses the obtained SQL text vector to train the SQL injection attack detection model.
[0065] S4, detects unknown SQL samples in the test set through the trained SQL injection attack detection model.
[0066] The SQL injection attack detection method based on class imbalance of the present invention aims at the problems existing in SOTA, proposes to build a SQL injection attack detection model based on class imbalance, and realizes the generation of abnormal SQL statement samples through knowledge embedded GAN; proposes a coding scheme based on fixed mapping rules to encode SQL statement text; uses an optimized LSTM model to perform feature learning on text vectors, and at the same time, introduces an attention mechanism to learn abnormal features in the data; introduces an optimized loss function to alleviate the problem of class imbalance in classification problems; introduces a mixed precision training method to improve the efficiency of model training. The process of the SQL injection attack detection method based on class imbalance proposed by the present invention is as follows: Figure 1 As shown, the specific steps are as follows:
[0067] Step 1: Use public data sets to collect a large number of normal SQL samples and a small number of SQL injection attack samples. This example uses a public data set on the Internet, which is divided into a training set, a validation set, and a test set. Each set is further divided into normal SQL statement samples and abnormal SQL statement samples. This example labels each SQL statement. Some SQL statement samples are shown in Table 1, where label 0 represents a normal SQL statement and label 1 represents a SQL injection attack statement.
[0068] Table 1
[0069] text Label timestamp=1498525372748 0 google_gid=CAESEKAy9nM5S_zcfP-7n8EqObA&google_cver=1 0 cb=fortune_hq&_=1498610392768 0 UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, NULL-- 1 ORDER BY 1-- 1 / ServerrCmxGuide.php?pgid=Guide_List 1
[0070] Step 2: Use the GAN-based SQL statement generation module to learn SQL injection attack samples and generate new SQL injection attack samples. Since the proportion of abnormal SQL statement samples in the data set is small, the model tends to learn normal samples with a large proportion, ignoring the characteristics of abnormal samples, resulting in low detection rate of abnormal samples. By generating new abnormal samples through GAN and increasing the proportion of abnormal samples in the total samples, the model can learn more abnormal sample features, improve the accuracy of abnormal detection, and alleviate problems such as class imbalance. The GAN-based SQL statement generation module includes a knowledge embedding module and a generation discriminant module; the knowledge embedding module uses GAN to embed the SQL statement grammatical rules and the keywords of abnormal SQL statements as prior knowledge into the discriminator to help the discriminator feed back more information to the generator. The main problem of GAN for text generation is that the feedback obtained by the generator from the discriminator contains less information, which is not enough to effectively guide the update of the generator to improve the text quality. SQL statements are semantic texts that conform to certain grammatical rules. At the same time, most abnormal SQL statements contain specific keywords. Embedding the above information into the discriminator as prior knowledge can effectively improve the performance of the generator in the SQL statement generation task. The generation and discrimination module is used to learn the features of abnormal SQL statements and generate new abnormal SQL statements. In order to effectively extract the semantic information and features of SQL text vectors, the generation and discrimination module uses an LSTM network. The specific steps for using GAN to generate new SQL statement abnormal samples are as follows:
[0071] Step 21: From SQL injection attack sample P data (x) in the sample {x 1 , x 2 , ..., x m}, where x i Represents the encoded i-th SQL text vector;
[0072] Step 22: Embed sample P from knowledge containing prior knowledge prior (z) sampling {z 1 , z 2 , ..., z m}, where z i Represents the i-th SQL text vector in the knowledge embedding sample;
[0073] Step 23: Via the Generator Get generated samples in, Represents the newly generated i-th SQL text vector;
[0074] Step 24: Update θ via gradient descent D To maximize
[0075]
[0076] Among them, θ D are the parameters of the discriminator, is the loss between the generator output and the sample, D(x i )and are the samples input to the generator and the corresponding generated samples, η is the learning rate, Express θ D Derivative.
[0077] Step 25: Update θ via gradient descent G To minimize
[0078]
[0079] Among them, θ G are the parameters of the generator, Express θ G Derivative.
[0080] Step 26: Generate new SQL injection attack samples based on the distribution and characteristics of the learned SQL statement abnormal samples. Table 2 shows the new SQL injection attack samples generated by the SQL statement generation module of GAN.
[0081] Table 2
[0082] Generated new SQL injection attack samples UNION ALL SELECT 1690, 1690, 1690, 1690 ORDER BY 1--&ref UNION ALL SELECT 9778--
[0083] Step 3: In order to reduce the impact of invalid parts of SQL statements on injection attack detection, the data set needs to be reconstructed, including removing redundant SQL samples in the data set and fine-tuning the SQL statements in the data set, including: lowercase keywords, generalize numbers to 0, and replace website account information with "http: / / u". Through reconstruction, the SQL statement samples are more standardized and the invalid parts that are meaningless to detection are removed. The SQL statements in Table 1 are reconstructed as shown in Table 3.
[0084] Table 3
[0085] Reconstructed text Label timestamp=0 0 google_gid=CAESEKAy9nM5S_zcfP-7n8EqObA&google_cver=1 0 cb=fortune_hq&_=0 0 union all select null,null,null,null,null,null-- 1 order by 1-- 1 / Server / CmxGuide.php?pgid=Guide_List 1
[0086] Step 4: Based on the fixed mapping rule, the SQL statement is segmented or word-segmented to convert the text input into an input acceptable to the model. The syntax parsing and word embedding encoding methods used by the traditional SQL injection attack detection framework cannot extract the semantic information of the SQL statement and ignore the statement features represented by the semantic information. This example encodes the SQL statement through the fixed mapping rule, which can effectively extract the semantic information of the SQL statement. Specifically, since the SQL statement contains Chinese characters, words, English letters, words and special characters, different encodings are required. Taking the SQL statement "WHERE 5421=5421;IF((3730=3730), SELECT 3730, DROP FUNCTION PfiY)#" as an example, the statement is reconstructed by step 3 and converted into "where 0=0;if((0=0), select 0, drop function PfiY)#", including the following steps:
[0087] Step 41: Split the text into tokens. Specifically, split the Chinese characters in the SQL statement into individual Chinese characters or words, split the English characters into individual letters or words, and split the special characters one by one. The split results are shown in Table 4:
[0088] Table 4
[0089] where 0 = 0 ; if ( ( 0 = 0 ) , select 0 , drop Function PcqI ) #
[0090] Step 42: Map each split token to the corresponding id to obtain a text vector. The ids corresponding to the characters obtained by the above split are shown in Table 5:
[0091] Table 5
[0092] character where 0 = 0 ; if ( ( 0 = 0 id 11703 121 134 121 132 8898 113 113 121 134 121 character ) , select 0 , drop Function PcqI ) # id 114 117 10634 121 117 8647,9133 12213 100 114 108
[0093] Step 43: pad the text vector so that each input vector has the same length;
[0094] Step 44: Truncate text vectors that are longer than the model can handle.
[0095] The final encoded text vector is as follows:
[0096] [11703, 121, 134, 121, 132, 8898, 113, 113, 121, 134, 121, 114, 117, 10634, 121, 117, 8647, 9133, 12213, 100, 114, 108], since only one SQL statement is taken as an example, no padding or truncation operation is performed on the statement.
[0097] Similarly, the text vectors of the SQL text in Table 3 after encoding are shown in Table 6.
[0098] Table 6
[0099]
[0100] Step 5: Use the optimized LSTM model to learn the incoming SQL text vector, and introduce the attention mechanism to learn abnormal features. The overall model architecture is as follows: Figure 3 At the same time, the optimized loss function is used to alleviate the class imbalance problem, and mixed precision training is introduced to improve training efficiency.
[0101] (I) Building a SQL injection attack detection model based on LSTM
[0102] LSTM models are often used for the prediction and classification of sequence data. The encoded SQL text vector can be regarded as a special sequence, so the LSTM model can be used to classify the SQL text vector. The traditional LSTM model can alleviate the gradient vanishing and long-distance dependency problems to a certain extent, so that it can better capture sequence information. However, for SQL text vectors that are closely related to certain grammatical rules and contexts, the traditional LSTM model cannot obtain good context-related expressions. In order to make the LSTM model more suitable for the application scenario of SQL text vector classification, the current input of the LSTM model unit is fully interacted with the hidden state, and then input into the LSTM unit to fully obtain the context expression of the SQL text vector. Based on the above idea, the input x′ of the LSTM model unit at the next moment t+1 The original input x at the next moment t+1 And the current input x t With the hidden state h t Fully interactive integration, specifically expressed as:
[0103] x′ t+1 =x t+1 +σ([h t , x t ]).
[0104] Compared with the previous example, we can see that the input of each round is calculated by the input of the previous round and the hidden state of the previous round. The following steps are included: the encoded SQL text vector is input into the LSTM model, and more key information in the sequence is learned through the LSTM model. The optimized LSTM model can capture both near-position information and far-position information; the data is further input into the attention layer. By introducing the attention mechanism, the weights of the vectors output at different times in the LSTM model are calculated, which can effectively extract and highlight the features of abnormal text vectors; the data is output into classification categories through the fully connected layer.
[0105] (II) Optimizing the loss function to solve the class imbalance problem in the dataset
[0106] The data imbalance problem is mainly reflected in the inconsistency between training and testing caused by too many negative samples, and the inability of the model to fully learn the boundary between positive and negative samples due to too many negative samples. In order to solve the problem of inconsistency between training and testing, a DSC index similar to the F1 index is often used to measure the similarity between two sets A and B, which is defined as follows:
[0107]
[0108] If A is regarded as the positive class predicted by the model and B is regarded as the true positive class, the above formula can be rewritten as:
[0109]
[0110] For a single sample x i , its DSC can be calculated as:
[0111]
[0112] Among them, γ is the smoothing coefficient.
[0113] In order to better converge the loss function, the present invention proposes to exponentiate the denominator, and its expression is as follows:
[0114]
[0115] In order to solve the problem of too many simple negative samples, the Focal loss is used to adjust the weight of each sample during the training process, and 1-p is used as the sample weight. For simple samples, the model tends to push p to 1, and the weight 1-p will gradually decrease during the training process, making the model pay more attention to the complex classes in the sample. In summary, the optimized loss function expression proposed by the present invention is as follows:
[0116]
[0117] Where N is the number of samples, p i and i They represent the predicted value and the actual value respectively, α is the smoothing coefficient, and γ is the smoothing coefficient.
[0118] (III) Introducing mixed precision training method to improve training efficiency
[0119] Model training based on LSTM requires a lot of resources. By introducing a mixed precision training method, it is possible to reduce memory usage while accelerating training. The mixed precision training method of the present invention is to use mixed precision training of FP16 and FP32, which can not only give play to the advantages of FP16 efficient training, but also avoid precision overflow and rounding errors, and includes the following steps:
[0120] Step A: Weight backup. The weight parameters weights, activations, gradients, intermediate variables and other data generated during the model training process are stored in FP16, and a copy of the weight parameters weights is stored in FP32 for updating during training.
[0121] Step B: Loss scaling. Since the gradient value is small, the use of FP16 may cause data underflow and cause the model to not converge. It is necessary to amplify the loss calculated by the forward feedback, that is, multiply the FP32 parameter by a certain factor coefficient, and then move the decimal data that may overflow forward and translate it to the data range that FP16 can represent. According to the chain rule, amplifying the loss will act on each layer of gradient in the back propagation, which is more efficient than amplifying each layer of gradient. In order to make full use of the dynamic range of FP16 and better alleviate rounding errors, the present invention uses a dynamic loss scaling algorithm to reduce the loss scaling scale whenever the gradient overflows, and intermittently try to increase the loss scale, so as to achieve the use of the highest loss scaling factor without causing overflow and better restore accuracy. It mainly includes the following steps:
[0122] Step B1: Set a higher scaling factor initially and continuously check for data overflow during training iterations.
[0123] Step B2: If there is no overflow, continue iterating; if there is overflow, reduce the scaling factor until no overflow occurs.
[0124] Step B3: In the later stage of training, the loss tends to converge and stabilize, the gradient update amplitude is small, and the scaling factor is increased to prevent data underflow.
[0125] Step B4: Accuracy accumulation. During the training process, FP16 is used for matrix multiplication operations, and FP32 is used for accumulation operations in matrix multiplication operations. The FP32 values are then converted to FP16 for storage. This can effectively reduce rounding errors in the calculation process and minimize the problem of accuracy loss.
[0126] Step 6: Apply the trained model to the test set to obtain the detection results on the test set. Specifically, by comparing the value range output by the LSTM-based SQL injection attack detection model, determine whether the SQL sample to be detected contains SQL injection attacks. The test set contains a total of 8,000 SQL statements, half of which are normal SQL statements and half are SQL injection attack statements. The detection accuracy is shown in Table 7.
[0127] Table 7
[0128] Accuracy Precision Recall F1 0.9023 0.9175 0.8732 0.8964
[0129] Example 2
[0130] In order to illustrate the effectiveness of each module of the present invention in detecting SQL injection attacks, this example uses an ablation experiment to verify the effectiveness of each module.
[0131] Among them, we train and detect samples by removing abnormal samples generated by GAN, using traditional SQL statement encoding methods, removing the attention mechanism in the detection module, removing the optimization loss function, and removing the introduction of mixed precision training. The detection results are as follows: Figure 4 As shown in Table 8. Figure 4 As shown in Table 8, after removing each module, the accuracy of SQL statement anomaly detection has decreased, which shows the effectiveness of each module. As shown in Table 8, introducing mixed precision training to train samples greatly reduces the time spent on training.
[0132] Table 8
[0133] Model Our Model W / O Mixed Precision Training Time / s 134.5 467.4
[0134] The above ablation experimental research verifies the effectiveness of the method of the present invention. By generating new SQL statement abnormal samples through GAN, the proportion of SQL statement abnormal samples in the total samples can be increased. At the same time, by optimizing the loss function, the problems of low SQL statement abnormal detection accuracy and high false alarm rate caused by class imbalance can be effectively alleviated. Through the encoding method of SQL statement fixed mapping rules, the semantic information between statement keywords is effectively extracted, which improves the detection accuracy. By introducing mixed precision training to train samples, the model training time is greatly reduced and the overall efficiency is improved. Therefore, the method proposed in the present invention can effectively detect SQL injection attacks.
[0135] Example 3
[0136] To further illustrate the effectiveness of the present invention in detecting abnormal SQL statement samples, this example compares the method proposed by the present invention with LSTM, SVM and convolutional neural network. In the experimental part of LSTM, SVM and convolutional neural network, the word2vec encoding method is used to encode the SQL statement samples. The experimental results are shown in Table 9.
[0137] Table 9
[0138] index Accuracy Precision Recall F1 Our Model 0.9023 0.9175 0.8732 0.8964 LSTM 0.8341 0.8795 0.8547 0.7965 SVM 0.7576 0.7343 0.7983 0.7798 CNN 0.8932 0.8867 0.8369 0.8685
[0139] The experimental results show that the present invention is superior to LSTM, SVM and convolutional neural network in multiple indicators, which illustrates the effectiveness of the present invention in detecting abnormal SQL statement samples.
[0140] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of complete hardware embodiments, complete software embodiments, or embodiments in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code. The scheme in the embodiments of the present application can be implemented in various computer languages, for example, object-oriented programming language Java and literal scripting language JavaScript, etc.
[0141] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0142] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1A function specified in one or more boxes.
[0143] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0144] Although the preferred embodiments of the present application have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.
[0145] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A SQL injection attack detection method based on class imbalance, characterized in that: The SQL injection attack detection method comprises the following steps: S1, construct the original data set based on the collected normal SQL samples and SQL injection attack samples, reconstruct the original data set and divide it into training set, verification set and test set; S2, encode the SQL samples in the reconstructed data set based on fixed mapping rules to obtain the SQL text vector corresponding to each SQL sample in the data set; S3, builds a SQL injection attack detection model based on LSTM, and uses the obtained SQL text vector to train the SQL injection attack detection model; S4, detects unknown SQL samples in the test set through the trained SQL injection attack detection model; In step S1, the collection process of normal SQL samples and SQL injection attack samples includes the following sub-steps: S11, collect normal SQL samples and SQL injection attack samples through public data sets. The number of normal SQL samples is greater than the number of SQL injection attack samples; S12, using a GAN-based SQL statement generation module to learn SQL injection attack samples and generate new SQL injection attack samples; the GAN-based SQL statement generation module includes a knowledge embedding module and a generation discrimination module; the knowledge embedding module is used to embed SQL statement grammar rules and keywords of abnormal SQL statements as prior knowledge into the discriminator; the generation discrimination module is used to learn abnormal SQL statement features and generate new abnormal SQL statements; In step S12, the process of the generation and identification module learning abnormal SQL statement features and generating new abnormal SQL statements includes the following sub-steps: From SQL injection attack samples Medium Sampling ,in, Represents the encoded i-th SQL text vector; embedding samples from knowledge containing prior knowledge Medium Sampling ,in, represents the i-th SQL text vector in the knowledge embedding sample, and m represents the number of encoded SQL text vectors; Through the generator Get generated samples ,in, Represents the newly generated i-th SQL text vector; Update by gradient descent To maximize : ; in, are the parameters of the discriminator, is the loss between the generator output and the sample, is the learning rate, Express In Derivation; Update by gradient descent To minimize : ; in, are the parameters of the generator, Express In Derivative.
2. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: In step S1, the process of reconstructing the original data set includes the following sub-steps: Eliminate redundant SQL samples in the data set; Fine-tune the SQL statements in the dataset by lowercasing keywords, generalizing numbers to 0, and replacing website account information with http: / / u.
3. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: In step S2, the process of encoding the SQL samples in the reconstructed data set based on the fixed mapping rule includes the following sub-steps: S21, split the SQL sample into tokens, specifically, split the Chinese characters in the SQL sample into single Chinese characters or words, split the English characters into single letters or words, and split the special characters other than Chinese and English characters one by one; S22, maps each split token to the corresponding id according to a fixed mapping rule to obtain a text vector; S23, padding the text vectors whose length is less than that allowed by the model, and truncating the text vectors whose length is greater than that allowed by the model, so that each input text vector has the same length.
4. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: In step S3, the SQL injection attack detection model includes an LSTM model, an attention layer, and a fully connected layer connected in sequence; wherein the input of the LSTM model unit at the next moment The input from the next moment And the current input With hidden state The interactive fusion is obtained, which is specifically expressed as: ; The encoded SQL text vector is input into the LSTM model, and the LSTM model learns more key information in the sequence; The data output by the LSTM model is further input into the attention layer, and the weights of the vectors output at different times in the LSTM model are calculated to extract and highlight the features of the abnormal text vectors; The fully connected layer is used to output classification categories.
5. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: The loss function of the SQL injection attack detection model is: ; in, is the number of samples, and represent the predicted value and the actual value respectively. is the smoothing coefficient, is the smoothing coefficient.
6. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: In step S3, the process of using the obtained SQL text vector to train the SQL injection attack detection model includes the following steps: S31, the weight parameters, activation parameters, gradient parameters, and intermediate variable data generated during the SQL injection attack detection model training process are stored in FP16, and a copy of the weight parameters is stored in FP32 for updating during training; S32, using a dynamic loss scaling algorithm to perform loss scaling using a highest loss scaling factor without causing overflow; comprising the following steps: In the initial stage of training, the maximum scaling factor is set in advance, and the data is constantly checked for overflow during the training iteration. If there is no overflow, the iteration continues. If there is overflow, the scaling factor is reduced until there is no overflow. In the later stage of training, when the loss tends to converge and stabilize, the scaling factor is increased without causing overflow to prevent data underflow. During the training process, FP16 is used for matrix multiplication, FP32 is used for accumulation in matrix multiplication, and the FP32 value is converted to FP16 for storage.
7. The SQL injection attack detection method based on class imbalance according to claim 1 is characterized in that: In step S4, by comparing the value range output by the SQL injection attack detection model, it is determined whether the SQL sample to be detected contains an SQL injection attack.
8. A class imbalance-based SQL injection attack detection device based on the method described in any one of claims 1 to 7, characterized in that: The SQL injection attack detection device comprises: The data set reconstruction module is used to construct the original data set based on the collected normal SQL samples and SQL injection attack samples, reconstruct the original data set and divide it into a training set, a validation set and a test set; The SQL text vector generation module encodes the SQL samples in the reconstructed data set based on fixed mapping rules to obtain the SQL text vector corresponding to each SQL sample in the data set; The SQL injection attack detection model training module is used to construct a SQL injection attack detection model based on LSTM, and use the obtained SQL text vector to train the SQL injection attack detection model; The SQL injection attack detection model is used to detect unknown SQL samples in the test set.
