Attack detection method, device and equipment
By performing subword segmentation and dynamic position encoding expansion on HTTP request data and combining it with a multi-task prediction model for attack judgment, the problems of high false alarm rate and insufficient positioning accuracy in traditional Web attack detection technology are solved, and high-precision HTTP attack detection and real-time interception are achieved.
Patent Information
- Application Number
- CN202510779638.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-11
- Publication Date
- 2025-09-05
AI Technical Summary
Traditional Web attack detection technology has difficulty distinguishing parameter format changes in normal requests from real attack behaviors, resulting in a high false alarm rate. Methods based on pre-trained language models have limited processing capabilities for ultra-long HTTP requests, insufficient positioning accuracy for long attack fragments, and gradient competition problems in multi-task learning scenarios lead to low model convergence efficiency.
By performing subword segmentation on HTTP request data, dynamically expanding positional encoding, and combining a multi-task prediction model to predict attack type probability, anomaly score, and boundary probability, and using the attack classification head, boundary detection head, and localized MLM head for attack judgment and backtracking, real-time detection and precise positioning of HTTP attacks are achieved.
It improves the accuracy of attack detection, reduces the false alarm rate in complex business scenarios, and achieves high-precision real-time detection and precise positioning of HTTP attacks.
Smart Images

Figure CN120602161A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network security technology, and in particular to an attack detection method, device, and equipment. Background Art
[0002] In the field of Web application security, HTTP protocol, as the core protocol for Internet communication, has long faced threats from various malicious attacks such as SQL injection, cross-site scripting (XSS), and command injection.
[0003] Traditional Web attack detection technology relies on static rules or shallow feature analysis, making it difficult to effectively distinguish between parameter format changes in normal requests and real attack behaviors, resulting in a high false alarm rate and seriously affecting the reliability of detection results.
[0004] Attack detection methods based on pre-trained language models have limited processing capabilities for extremely long HTTP requests, resulting in insufficient localization accuracy for long attack segments. The rigid nature of fixed-position encodings prevents the model from dynamically capturing attack semantics scattered across long sequences, leading to significant localization bias. Furthermore, gradient competition among tasks in multi-task learning scenarios results in inefficient model convergence, necessitating manual adjustment of loss weights. The traditional method's strong reliance on labeled data further limits its practical application in semi-supervised scenarios.
[0005] Therefore, there is an urgent need for an attack detection method that can achieve real-time detection and precise positioning of HTTP attacks with high precision and low false positives. Summary of the Invention
[0006] The embodiments of the present application provide an attack detection method, apparatus, and device for achieving real-time detection and precise positioning of HTTP attacks with high accuracy and low false positives.
[0007] In a first aspect, an embodiment of the present application provides an attack detection method, the method comprising:
[0008] Perform subword segmentation on the original HTTP request data to obtain a character token sequence of the original HTTP request data;
[0009] Performing dynamic position coding expansion on the Token sequence according to the initial position coding of the Token sequence and the reference abnormal Token in the Token sequence to obtain the target position coding of the Token sequence;
[0010] The token sequence and the target position code are input into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token; the multi-task prediction model includes an attack classification head, a boundary detection head, and a localized MLM head; when training the localized MLM head, the tokens in the attack segment of the HTTP request data sample are randomly masked to predict the anomaly score of the tokens in the attack segment;
[0011] Based on the attack type probability of the Token sequence, the anomaly score of each Token, and the boundary probability of each Token, attack judgment and attack backtracking are performed to determine the attack type and the location of the attack fragment of the original HTTP request data.
[0012] In some embodiments, performing dynamic position coding expansion on the token sequence based on the initial position coding of the token sequence and a reference abnormal token in the token sequence to obtain the target position coding of the token sequence includes:
[0013] For each token, perform the following operations:
[0014] Determine the local attack density of the token based on the number of reference abnormal tokens in the preset area where the token is located; the reference abnormal token is determined after matching the token sequence with a preset attack fragment library;
[0015] Input the local attack density of the token into the interpolation factor generation network MLP to obtain the interpolation factor;
[0016] Based on the interpolation factor and the initial position code of the Token sequence, the Token is dynamically position-coded and expanded to obtain a target position code corresponding to the Token.
[0017] In some embodiments, the multi-task prediction model further comprises a BERT encoder;
[0018] The step of inputting the token sequence and the target position encoding into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token includes:
[0019] Input the Token sequence and the target position code into the BERT encoder for encoding processing to obtain a hidden state vector corresponding to the Token sequence;
[0020] Input the classified hidden state in the hidden state vector into the attack classification head, perform linear classification and probability prediction, and obtain the attack type probability of the token sequence;
[0021] Input the hidden state vector into the localized MLM head, perform linear transformation and probability prediction, and obtain the anomaly score of each token;
[0022] The hidden state vector is input into the boundary detection head, and linear transformation and probability prediction are performed to obtain the boundary probability of each token.
[0023] In some embodiments, the multi-task prediction model is trained in the following manner:
[0024] Obtain a data sample set; the data sample set includes multiple HTTP request data samples and multi-task prediction labels for each HTTP request data sample; the multi-task prediction labels include an attack type probability label of a token sequence of each HTTP request data sample, an anomaly score label of each token in the token sequence, and a boundary probability label;
[0025] Performing subword segmentation processing and dynamic position coding expansion on the HTTP request data samples in the data sample set to obtain a token sequence and target position coding of each HTTP request data sample;
[0026] The token sequence and target position of each HTTP request data sample are encoded and input into the multi-task prediction model, and the multi-task prediction model is trained with the multi-task prediction label as the target to obtain a trained multi-task prediction model.
[0027] In some embodiments, before performing subword segmentation and dynamic position coding expansion on the HTTP request data samples in the data sample set, the method further includes:
[0028] The HTTP request data samples in the data sample set are subjected to semantics-preserving perturbation processing and encoding uniform processing.
[0029] In some embodiments, the data sample set includes a supervised data sample set and a semi-supervised data sample set; the HTTP request data samples in the supervised data sample set have attack segment tags; the HTTP request data samples in the semi-supervised data sample set do not have attack segment tags;
[0030] The Token sequence and target position encoding of each HTTP request data sample are input into the multi-task prediction model, and the multi-task prediction model is trained with the multi-task prediction label as the target to obtain a trained multi-task prediction model, including:
[0031] Inputting the token sequence and target position encoding of each HTTP request data sample in the supervised data sample set into the multi-task prediction model, training the multi-task prediction model to obtain a first multi-task prediction model; when training the multi-task prediction model, encoding processing is performed based on the respective tokens marked in the attack fragment in the supervised data set and the target position encoding of each token;
[0032] The token sequence and target position encoding of each HTTP request data sample in the mixed data set are input into the first multi-task prediction model, and the first multi-task prediction model is trained to obtain a second multi-task prediction model; the mixed data set includes the HTTP request data samples in the supervised data set and the HTTP request data samples in the semi-supervised data sample set; when training the first multi-task prediction model, encoding is performed based on the tokens and target position encoding of each token marked in the attack fragment in the supervised data set, and the token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set;
[0033] The Token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set are input into the second multi-task prediction model, and the second multi-task prediction model is trained to obtain a trained multi-task prediction model; when training the second multi-task prediction model, encoding processing is performed based on the Token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set.
[0034] In some embodiments, encoding the token sequence and target position of each HTTP request data sample, inputting the encoding into the multi-task prediction model, and training the multi-task prediction model with the multi-task prediction label as a target to obtain a trained multi-task prediction model includes:
[0035] The token sequence and target position of each HTTP request data sample are encoded and input into the multi-task prediction model to obtain the multi-task prediction probability of each HTTP request data sample; the multi-task prediction probability includes the attack type probability of the token sequence of each HTTP request data sample, the anomaly score and boundary probability of each token in the token sequence;
[0036] Based on the multi-task prediction probability and the multi-task prediction label, a loss function is determined, and the parameters of the multi-task prediction model are adjusted based on the loss function to obtain a trained multi-task prediction model.
[0037] In some embodiments, performing attack determination and attack backtracing based on the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token to determine the attack type and the location of the attack fragment of the original HTTP request data includes:
[0038] Compare the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token with a preset probability threshold to determine the attack type of the original HTTP request data;
[0039] Determine the start token and end token of the attack segment in the token sequence based on the boundary probability of each token;
[0040] Based on the target position code of the start token and the target position code of the end token, the initial position codes of the start token and the end token in the original HTTP request data are traced back to obtain the position of the attack fragment in the original HTTP request data.
[0041] In a second aspect, an embodiment of the present application provides an attack detection device, the device comprising:
[0042] A data processing module is used to perform subword segmentation processing on the original HTTP request data to obtain a character token sequence of the original HTTP request data;
[0043] A coding extension module is used for the initial position coding of the Token sequence and the reference abnormal Token in the Token sequence, and performs dynamic position coding extension on the Token sequence to obtain the target position coding of the Token sequence;
[0044] A probability prediction module is configured to input the token sequence and the target position code into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token; the multi-task prediction model includes an attack classification head, a boundary detection head, and a localized MLM head; when training the localized MLM head, tokens within the attack segment of the HTTP request data sample are randomly masked to predict the anomaly score of the tokens within the attack segment;
[0045] The attack determination module is used to perform attack determination and attack backtracing based on the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token, and determine the attack type and the location of the attack fragment of the original HTTP request data.
[0046] In a third aspect, an embodiment of the present application provides an electronic device, comprising: at least one processor, and a memory communicatively connected to the at least one processor, wherein:
[0047] The memory stores a computer program that can be executed by at least one processor. The computer program is executed by the at least one processor to enable the at least one processor to perform the above-mentioned attack detection method.
[0048] In a fourth aspect, an embodiment of the present application provides a storage medium. When a computer program in the storage medium is executed by a processor of an electronic device, the electronic device can perform the above-mentioned attack detection method.
[0049] In a fifth aspect, an embodiment of the present application provides a computer program product. When the computer program product is executed by an electronic device, the electronic device can implement the above-mentioned attack detection method provided in the present application.
[0050] The technical solutions provided by the embodiments of this application bring at least the following beneficial effects:
[0051] In an embodiment of the present application, the original HTTP request data is first subjected to subword segmentation processing to obtain a character token sequence of the original HTTP request data; based on the initial position code of the token sequence and the reference abnormal token in the token sequence, the token sequence is dynamically position-coded and expanded to obtain the target position code of the token sequence; the token sequence and the target position code are input into a multi-task prediction model to obtain the attack type probability of the token sequence, the abnormal score of each token, and the boundary probability of each token; the multi-task prediction model includes an attack classification head, a boundary detection head, and a localized MLM head; when training the localized MLM head, the tokens in the attack segment of the HTTP request data sample are randomly masked, and the abnormal score of the token in the attack segment is predicted; based on the attack type probability of the token sequence, the abnormal score of each token, and the boundary probability of each token, attack judgment and attack backtracking are performed to determine the attack type of the original HTTP request data and the position of the attack segment.
[0052] On the one hand, the initial position coding of the Token sequence of the original HTTP request data is dynamically expanded to obtain the target position coding, which can adaptively extend the sequence length and effectively improve the positioning accuracy of long sequence attack fragments.
[0053] On the other hand, the token sequence and target position encoding are input into the trained multi-task prediction model to predict the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token. The three probabilities can be combined to detect attacks on the original HTTP request data, thereby accurately determining the attack type and location of the attack fragment of the original HTTP request data.
[0054] Therefore, the attack detection method provided by this application can improve the accuracy of attack detection and real-time interception, and reduce the false alarm rate in complex business scenarios.
[0055] Other features and advantages of the present application will be described in the following description, and in part will become apparent from the description, or will be understood by practicing the present application. The purposes and other advantages of the present application can be realized and obtained by the structures particularly pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0057] Figure 1 A flowchart of an attack detection method provided in an embodiment of the present application;
[0058] Figure 2 A flowchart of a probability prediction method provided in an embodiment of the present application;
[0059] Figure 3 A complete flowchart of an attack detection method provided in an embodiment of the present application;
[0060] Figure 4 A schematic diagram of a process for training a multi-task prediction model provided in an embodiment of the present application;
[0061] Figure 5 A schematic diagram of a process for training a multi-task prediction model in stages provided in an embodiment of the present application;
[0062] Figure 6 A flowchart of an attack detection method provided in an embodiment of the present application;
[0063] Figure 7 A schematic diagram of the structure of an attack detection device provided in an embodiment of the present application;
[0064] Figure 8 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0065] To make the purpose, technical solutions, and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Among them, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0066] Moreover, in the description of the embodiments of the present application, unless otherwise specified, “ / ” means or, for example, A / B can mean A or B; “and / or” in the text is merely a description of the association relationship of associated objects, indicating that three relationships may exist, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present application, “multiple” refers to two or more than two.
[0067] In the following, the terms "first" and "second" are used for descriptive purposes only and should not be understood to imply or suggest relative importance or implicitly indicate the number of the technical features indicated. Therefore, features defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of the embodiments of this application, unless otherwise specified, "plurality" means two or more.
[0068] To facilitate understanding of this application, some technical terms involved in this application are introduced below:
[0069] 1. Multi-Layer Perceptron (MLP):
[0070] A fully connected feedforward neural network consisting of an input layer, one or more hidden layers, and an output layer, which implements hierarchical nonlinear transformations of features using nonlinear activation functions such as ReLU and Sigmoid. In this application, the MLP is an interpolation factor generation network used to generate interpolation factors during positional encoding expansion and to predict boundary probabilities for the boundary detection head.
[0071] 2. Density-Aware Positional Encoding (DAPE)
[0072] A method for generating target position encoding by dynamically expanding position encoding based on local attack density (Density-Aware Positional Encoding, DAPE) supports extending the length of token sequences to 1024.
[0073] 3. Localized Masked Language Modeling Head:
[0074] The improved Masked Language Modeling (MLM) task head masks and predicts only specific areas, such as tokens within attack fragment markers.
[0075] 4. Boundary Detection Head
[0076] The neural network module used to predict the starting and ending positions of the attack segment consists of two parallel Sigmoid classifiers.
[0077] 5. GradNorm dynamic weight optimization
[0078] A dynamic weight allocation strategy (Gradient Normalization, GradNorm) in multi-task learning automatically adjusts the loss weight by balancing the gradient norms of each task.
[0079] In the field of Web application security, HTTP protocol, as the core protocol for Internet communication, has long faced threats from various malicious attacks such as SQL injection, cross-site scripting (XSS), and command injection.
[0080] Traditional Web attack detection technology relies on static rules or shallow feature analysis, for example, identifying attack features through predefined regular expressions or feature libraries (such as ModSecurity rule sets), such as detecting SQL injection by matching keywords such as UNION SELECT, DROPTABLE, or detecting <script>标签识别XSS攻击等。但是该方法难以有效区分正常请求中的参数格式变化与真实攻击行为,导致误报率居高不下,严重影响检测结果的可靠性。
[0081] 近年来,基于预训练语言模型的方法(如BERT、RoBERTa)开始应用于攻击检测任务。但是基于预训练语言模型进行攻击检测的方法对超长HTTP请求的处理能力有限,长攻击片段定位精度不足,固定位置编码的僵化特性使得模型无法动态捕捉分散在长序列中的攻击语义,导致定位偏差显著。此外,多任务学习场景下各任务的梯度竞争问题使得模型收敛效率低下,需依赖人工调整损失权重,而传统方法对标注数据的强依赖性进一步限制了半监督场景下的实际应用能力。
[0082] 有鉴于此,本申请实施例提供了一种攻击检测方法、装置及设备,用于实现高精度、低误报的对HTTP攻击进行实时检测与精准定位。
[0083] 本申请实施例的发明构思:一方面,对原始HTTP请求数据的Token序列的初始位置编码进行动态位置编码扩展,得到目标位置编码,能够自适应扩展序列长度,有效提升了长序列攻击片段的定位精度。另一方面,将Token序列以及目标位置编码输入训练完成的多任务预测模型,预测Token序列的攻击类型概率、每个Token的异常分数和边界概率,可以结合三种概率对原始HTTP请求数据进行攻击检测,从而准确的确定原始HTTP请求数据的攻击类型和攻击片段的位置,能够提高攻击检测与实时拦截的准确度,降低复杂业务场景下的误报率。
[0084] 为进一步说明本申请实施例提供的技术方案,下面结合附图以及具体实施方式对此进行详细的说明。虽然本申请实施例提供了如下述实施例或附图所示的方法操作步骤,但基于常规或者无需创造性的劳动在方法中可以包括更多或者更少的操作步骤。在逻辑上不存在必要因果关系的步骤中,这些步骤的执行顺序不限于本申请实施例提供的执行顺序。
[0085] 参见图1,为本申请实施例提供的一种攻击检测方法的流程示意图。该方法包括如图1所示的步骤:
[0086] 在步骤101中,对原始HTTP请求数据进行子词切分处理,得到原始HTTP请求数据的字符Token序列。
[0087] 具体实施为,接收原始HTTP请求数据;使用预训练的BERT Tokenizer将请求文本进行子词切分处理,得到多个字符(Token);在多个字符(Token)的前后分别添加开始标记和结束标记,得到原始HTTP请求数据的Token序列。
[0088] 其中,开始标记用于插入Token序列的开始位置,用于分类任务,如标记[CLS];结束标记用于插入Token序列的结束位置,表示输入结束,如标记[SEP]。
[0089] 示例性的,原始HTTP请求数据为" / search?q=1'UNION SELECT”,则原始HTTP请求数据的Token序列为["[CLS]"," / ","search","?","q","=","1","'","UNION","SELECT","[SEP]"]。
[0090] 进一步地,若原始HTTP请求数据的Token序列的长度超过最大序列长度,如1024,则保留前部最大序列长度的Token并截断超过最大序列长度的Token。
[0091] 其中,最大序列长度是根据实际经验进行设置的,可以覆盖99.5%的HTTP请求长度。
[0092] 在本申请中,对原始HTTP请求数据进行子词切分处理,得到Token序列之前,还可以执行以下操作:将原始HTTP请求数据进行编码统一和转义还原。
[0093] 示例性的,若原始HTTP请求数据为" / search?q=1%27%20UNION%20SELECT%20*%20FROM%20users—”,为了方便执行下列步骤,将原始HTTP请求数据进行编码统一和转义还原,具体执行为:
[0094] 1、URL解码:对%xx形式的编码字符进行解码。
[0095] 例如:%27→单引号';%3C→小于号<。
[0096] 2、Unicode解码:处理\uXXXX或XXXX形式的Unicode转义。
[0097] 例如:\u003c→<;>;→>。
[0098] 3、多重编码处理:递归解码嵌套编码。
[0099] 例如:双重URL编码%2527→先解码为%27,再解码为'。
[0100] 根据上述步骤将原始HTTP请求数据进行编码统一和转义还原后,得到解码后的HTTP请求数据" / search?q=1'UNION SELECT*FROM users--”,再对解码后的HTTP请求数据进行子词切分处理,得到Token序列。
[0101] 在步骤102中,根据Token序列的初始位置编码和Token序列中的参考异常Token,对Token序列进行动态位置编码扩展,得到Token序列的目标位置编码。
[0102] 其中,参考异常Token是将Token序列和预设攻击片段库进行匹配后确定的。
[0103] 而预设攻击片段库是在训练多任务预测模型时确定的。在训练多任务预测模型时使用攻击片段标记,如[ATT]和[ / ATT]明确标注各HTTP请求数据样本的攻击片段边界之后,将标注出的攻击片段保存在预设攻击片段库中。
[0104] 例如,将所有攻击流量中的攻击片段payload保存至预设攻击片段库payload_list中,如payload_list{payload1,payload2,payload3,……}。
[0105] 在将Token序列和预设攻击片段库进行匹配时,先使用预训练的BERT Tokenizer将预设攻击片段库payload_list中的每个攻击片段payload均进行子词切分处理,得到预设攻击片段库payload_list对应的多个Token,然后对预设攻击片段库payload_list对应的多个Token进行去重处理,得到预设攻击片段库payload_list对应的多个异常Token,如payload_token_list{payload_token1,payload_token2,……}。
[0106] 之后,将Token序列中的各个Token分别与预设攻击片段库对应的多个异常Token进行匹配,并将Token序列中与多个异常Token匹配成功的Token作为参考异常Token。
[0107] 在一些实施例中,根据Token序列的初始位置编码和Token序列中的参考异常Token,对Token序列进行动态位置编码扩展,得到Token序列的目标位置编码时,针对每个Token,执行以下操作:
[0108] 针对每个Token,执行以下操作:
[0109] 根据Token所在预设区域中参考异常Token的数量,确定Token的局部攻击密度;参考异常Token是将Token序列和预设攻击片段库进行匹配后确定的;
[0110] 将Token的局部攻击密度输入插值因子生成网络MLP,得到插值因子;
[0111] 基于插值因子和Token序列的初始位置编码,对Token进行动态位置编码扩展,得到Token对应的目标位置编码。
[0112] 具体实施时,本申请中首先加载预训练BERT编码器中的初始位置编码矩阵;然后针对Token序列中每个Token,先计算该Token的局部攻击密度;再将局部攻击密度与初始嵌入向量进行拼接,并将拼接后的向量矩阵输入插值因子生成网络MLP中生成插值因子;根据插值因子和初始位置编码矩阵对该Token进行动态位置编码扩展,得到该Token的目标位置编码;对所有Token进行动态位置编码扩展之后,将所有Token的目标位置编码,按照各Token在Token序列中的顺序进行排序,得到Token序列的目标位置编码。
[0113] 首先,加载预训练BERT编码器中的初始位置编码矩阵表示初始位置编码矩阵中包括512个位置,每个位置为768维。
[0114] 然后,计算每个Token的局部攻击密度:对于每个Token j,统计Token j所在预设区域中参考异常Token的数量,设预设区域为前后窗口k内(k=5),则根据下列公式(1)可以计算得到Token j的局部攻击密度sj为:
[0115]
[0116] 其中,Tokeni为Token j所在预设区域中的第i个Token;Tokeni∈payload_token_list表示Token j所在预设区域中的第i个Token为参考异常Token。
[0117] 根据公式(2),将Token j的局部攻击密度sj与Token j的初始嵌入向量拼接,并将拼接后的向量矩阵输入插值因子生成网络MLP中生成插值因子αj:
[0118]
[0119] 其中,Token j的初始嵌入向量为插值因子生成网络MLP中嵌入层的初始隐藏状态,是在训练插值因子生成网络MLP时确定的。
[0120] 根据插值因子αj和初始位置编码矩阵对Token j进行动态位置编码扩展,得到Token j的目标位置编码Pnew[j],根据下列公式(3)进行位置编码扩展:
[0121]
[0122] 其中,表示向下取整,表示向上取整;Pnew[j]表示Token j的目标位置编码;表示的初始位置编码;表示的初始位置编码。
[0123] 由此,可以得到Token序列动态位置编码后的目标位置编码。例如Token序列经过动态位置编码扩展后,目标位置编码可以包括1024个位置,每个位置为768维。
[0124] 在步骤103中,将Token序列和目标位置编码输入多任务预测模型,得到Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率;多任务预测模型包括攻击分类头、边界检测头以及局部化MLM头;在训练局部化MLM头时,对HTTP请求数据样本中攻击片段内的Token进行随机遮蔽,预测攻击片段内的Token的异常分数。
[0125] 其中,异常分数表征每个Token为目标异常Token的概率。边界概率表征每个Token为攻击片段的起始位置和结束位置的概率。
[0126] 在一些实施例中,本申请中的多任务预测模型还包括BERT编码器。
[0127] 因此,将Token序列和目标位置编码输入多任务预测模型,得到Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率,可以执行为如图2所示的步骤:
[0128] 在步骤201中,将Token序列和目标位置编码输入BERT编码器进行编码处理,得到Token序列对应的隐藏状态向量。
[0129] 具体实施时,将Token序列映射为向量然后将Token序列的向量和目标位置编码Pnew输入嵌入层,然后经过BERT编码器的12层Transformer编码,生成上下文相关的隐藏状态向量H:
[0130]
[0131] 其中,n为Token序列中Token的数量,在本申请中,n可以是1024;768是每个Token的维度。
[0132] 在步骤202中,将隐藏状态向量输入局部化MLM头,进行线性变换和概率预测,得到每个Token的异常分数。
[0133] 具体实施时,首先将隐藏状态向量输入局部化MLM头,在局部化MLM头的全连接层中对每个Token的隐藏状态hi进行线性变换:
[0134]
[0135] 其中,V为异常Token的词表大小,为局部化MLM头训练好的权重,bMLM为局部化MLM头训练好的偏置项。
[0136] 再通过Softmax进行概率预测,确定第i个Token被预测为词表第w个异常Token的概率
[0137]
[0138] 其中,[wi]为异常Token的词表矩阵。
[0139] 最后,生成第i个Token的异常分数Scorei:
[0140]
[0141] 其中,异常分数Scorei越高,则表示第i个Token语义异常的可能性越大,第i个Token为目标异常Token的概率越大。
[0142] 在步骤203中,将隐藏状态向量中的分类隐藏状态输入攻击分类头,进行线性分类和概率预测,得到Token序列的攻击类型概率。
[0143] 具体实施时,首先本申请中在计算Token序列的攻击类型概率时,需要提取隐藏状态向量中的分类隐藏状态,即隐藏状态向量H中的首个隐藏状态hCLS,然后将首个隐藏状态hCLS输入攻击分类头的线性分类层进行线性变换:
[0144]
[0145] 其中,Ntype为攻击类型的数量,为攻击分类头训练好的权重,bcls为攻击分类头训练好的偏置项。
[0146] 再通过Softmax进行概率预测,输出Token序列的攻击类型概率pcls:
[0147] pcls=Softmax(logit scls)
[0148] 其中,pcls表示Token序列被预测为各攻击类型的概率分布,将概率分布中最大的概率作为Token序列的攻击类型概率。
[0149] 在步骤204中,将隐藏状态向量输入边界检测头,进行线性变换和概率预测,得到每个Token的边界概率。
[0150] 具体实施时,首先本申请中在计算每个Token的边界概率时,需要将隐藏状态向量H输入边界检测头的线性分类层进行线性变换:
[0151]
[0152] 其中,Wstart、Wend为边界检测头训练好的权重,bstart、bend为边界检测头训练好的偏置项。
[0153] 再通过Sigmoid进行概率预测,确定第i个Token被预测为攻击片段起始位置的概率以及被预测为攻击片段结束位置的概率
[0154]
[0155] 其中,越高,则表示第i个Token被预测为攻击片段起始位置的可能性越大;越高,则表示第i个Token被预测为攻击片段结束位置的可能性越大。
[0156] 最后,多任务预测模型输出以下特征:
[0157] Token序列的攻击类型概率:
[0158] 每个Token的异常分数:Score=[Score1,Score2,...,Scoren];
[0159] 每个Token的边界概率:
[0160] 需要说明的是,上述步骤202、203、204的执行不分先后顺序。
[0161] 由此,将Token序列和目标位置编码输入多任务预测模型,可以得到Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率。
[0162] 在步骤104中,基于Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率,进行攻击判定和攻击回溯,确定原始HTTP请求数据的攻击类型和攻击片段的位置。
[0163] 具体实施时,首先将Token序列的攻击类型概率、每个Token的异常分数、每个Token的边界概率,与预设概率阈值进行比较,确定原始HTTP请求数据的攻击类型。
[0164] 其中,预设概率阈值包括攻击类型概率阈值、异常分数阈值、边界得分阈值。边界得分阈值需要与边界得分进行比较。
[0165] 边界得分Sboundary是用于评估候选攻击片段起止位置的置信度指标,通过结合攻击片段起始位置的概率pstart和攻击片段结束位置的概率pend生成。通过以下步骤计算得到边界得分Sboundary:
[0166] 首先,通过滑动窗口检测Token序列中各个Token的异常分数,确定候选攻击片段的区间[a,b]。
[0167] 示例性的,若滑动窗口检测到异常分数的峰值区域为第5个Token至第10个Token,则候选攻击片段的区间为[5,10]。
[0168] 然后,在候选攻击片段的区间[a,b]内遍历所有Token,确定起始位置的概率pstart的最大值,以及结束位置的概率pend的最大值:
[0169]
[0170] 最后,对起始位置的概率pstart的最大值,以及结束位置的概率pend的最大值取均值,得到边界得分Sboundary:
[0171]
[0172] 由此,可以使用边界得分与边界得分阈值进行比较。
[0173] 具体的,原始HTTP请求数据的攻击类型分为高置信攻击、可疑攻击以及正常流程。
[0174] 高置信攻击的判定条件为:如若攻击类型概率最大值大于第一攻击类型概率阈值(如),以及存在至少一个候选攻击片段区间的边界得分大于第一边界得分阈值(如Sboundary≥0.7),则确定原始HTTP请求数据的攻击类型为高置信攻击,选择边界得分最高的候选攻击片段区间作为攻击片段的区间,直接触发拦截动作,并记录攻击详情。
[0175] 可疑攻击的判定条件为:若攻击类型概率最大值大于第二攻击类型概率阈值且小于第一攻击类型概率阈值(如),以及存在至少一个候选攻击片段区间的边界得分大于第二边界得分阈值且小于第一边界得分阈值(如0.7>Sboundary≥0.5),以及候选攻击片段区间内异常分数大于异常分数阈值(如Scorei>2.0)的Token数量≥3,则确定原始HTTP请求数据的攻击类型为可疑攻击,将原始HTTP请求数据标记为"可疑”,推送至安全团队审核队列,并保留候选区域及关联特征供人工分析。
[0176] 正常流量的判定条件为:若不满足高置信攻击的判定条件和可疑攻击的判定条件,则确定原始HTTP请求数据为正常流量,放行原始HTTP请求数据,不进行拦截。
[0177] 在一些实施例中,由于在特定时间段内的攻击类型变化,会影响攻击类型概率分布,进而影响攻击类型的判定,因此本申请中引入自适应策略优化攻击类型概率阈值,适应攻击模式的变化并控制误报率。
[0178] 自适应策略优化攻击类型概率阈值可以执行为以下步骤:
[0179] 1、攻击频率统计:
[0180] 实时记录最近T=7天内拦截的攻击类型分布(按攻击类型c分类统计),并计算当前时间窗口内各攻击类型的占比fc:
[0181]
[0182] 2、分类阈值动态计算:
[0183] 首先初始设定各攻击类型的攻击类型概率阈值,例如设定高置信攻击的第一攻击类型概率阈值θbase1=0.9,可疑攻击的第二攻击类型概率阈值θbase2=0.6;然后根据下列规则,对攻击频率上升的攻击类型的攻击类型概率阈值降低,反之升高:
[0184]
[0185] 其中,为历史平均攻击频率(统计周期≥30天);α为调节因子(实验验证α每变化0.01,误报率变化约0.3%),可以设置α=0.05。
[0186] 3、阈值约束:
[0187] 在自适应策略优化攻击类型概率阈值时,为了避免过度敏感,可以对阈值的下限进行约束,例如可以设置θc1≥0.7,θc2≥0.4。还可以设置攻击类型概率阈值的更新频率,例如每小时根据最新fc更新攻击类型概率阈值,触发拦截时实时生效。
[0188] 由此,可以根据上述判定条件确定原始HTTP请求数据的攻击类型。上述判定条件可以根据实际需要进行设置。
[0189] 在一些实施例中,为了从原始HTTP请求中精确提取攻击片段,还原攻击者意图。本申请中基于每个Token的边界概率进行攻击回溯,确定原始HTTP请求数据中攻击片段的位置。具体执行为:
[0190] 基于每个Token的边界概率,确定Token序列中攻击片段的起始Token和结束Token;
[0191] 基于起始Token的目标位置编码和结束Token的目标位置编码,回溯起始Token和结束Token在原始HTTP请求数据中的初始位置编码,得到原始HTTP请求数据中攻击片段的位置。
[0192] 具体实施时,首先在对Token序列进行动态位置编码扩展,得到Token序列的目标位置编码时,记录每个Token在原始HTTP请求数据中的起始位置和结束位置,然后根据目标位置编码和每个Token在原始HTTP请求数据中的起始位置和结束位置,计算出每个Token的字节偏移。例如Token:"UNION"→字节偏移[24,29];Token:"SELECT"→字节偏移[30,36]。
[0193] 然后根据基于每个Token的边界概率,确定Token序列中攻击片段的起始Token和结束Token;然后查找起始Token和结束Token的字节偏移;基于起始Token的目标位置编码和结束Token的目标位置编码,结合起始Token和结束Token的字节偏移,可以得到起始Token和结束Token在原始HTTP请求数据中的初始位置编码,即可得到原始HTTP请求数据中攻击片段的位置。
[0194] 在本申请中,还可以将原始HTTP请求数据的格式标准化,并对原始HTTP请求数据中攻击片段进行语义分析,还原攻击者意图。
[0195] 将原始HTTP请求数据的格式标准化执行为:统一关键字大小写,如UnIoN→UNION;以及补全未闭合的HTML标签,如<script>→<script>< / script> .
[0196] The semantic analysis of the attack fragment in the original HTTP request data is performed as follows: ignoring irrelevant parameters or obfuscated characters, such as the random string sdf9e2; extracting the core malicious logic in the attack fragment, such as UNION SELECT, <script>alert(1)。
[0197] 为了便于理解本申请实施例提供的攻击检测方法,下面结合图3所示的攻击检测方法的完整流程,对攻击检测方法进行说明。
[0198] 在步骤E1中,接收原始HTTP请求数据;
[0199] 在步骤E2中,对原始HTTP请求数据进行子词切分处理;
[0200] 在步骤E3中,构建Token序列;
[0201] 在步骤E4中,进行位置编码扩展,得到目标位置编码
[0202] 在步骤E5中,进行模型推理;从而在步骤E6中,局部化MLM头输出每个Token的异常分数;在步骤E7中,攻击分类头输出Token序列的攻击类型概率;在步骤E8中,攻击分类头输出每个Token的边界概率;
[0203] 在步骤E9中,结合步骤E6-E8的输出,进行攻击判定;
[0204] 最后得到攻击判定结果:步骤E10中的高置信攻击并确定攻击片段的位置,或者步骤E11中的可疑攻击并记录候选攻击片段区间供分析,或者步骤E12中的正常流量并放行。
[0205] 示例性的,原始HTTP请求数据为" / search?q=1'UNION SELECT*FROM users--”,将其通过子词切分处理和动态位置编码扩展之后,输入多任务预测模型,得到Token序列的攻击类型概率为SQLi:0.96,XSS:0.02,其他:0.02;Token"’、UNION、SELECT”的异常分数分别为5.1、5.3、5.0;Token"’”的起始位置概率为p=0.93,Token"--”的结束位置概率为p=0.96;若当前分析周期SQL注入攻击占比fsqli=0.4,最近7天历史均值则第一攻击类型概率阈值为0.8925,因此原始HTTP请求数据的攻击类型为高置信攻击,标记为SQL注入攻击,拦截请求;然后进行攻击回溯,得到攻击片段的位置是[7,15],攻击片段为"1'UNION SELECT*FROM users—”;故最终输出结果为{"attack_type":"SQLi","confidence":0.96,"positions":[7,15],"payload":"1'UNION SELECT*FROM users--"}。
[0206] 由此,一方面,对原始HTTP请求数据的Token序列的初始位置编码进行动态位置编码扩展,得到目标位置编码,能够自适应扩展序列长度,有效提升了长序列攻击片段的定位精度。一方面,将Token序列以及目标位置编码输入训练完成的多任务预测模型,预测Token序列的攻击类型概率、每个Token的异常分数和边界概率,可以结合三种概率对原始HTTP请求数据进行攻击检测,从而准确的确定原始HTTP请求数据的攻击类型和攻击片段的位置,提高攻击检测与实时拦截的准确度,降低复杂业务场景下的误报率。
[0207] 本申请实施例中将Token序列和目标位置编码输入多任务预测模型,得到Token序列的攻击类型概率、每个Token的异常分数和边界概率,因此需要先对多任务预测模型进行训练,得到训练完成的多任务预测模型。
[0208] 因此,本申请中可以根据如图4所示的步骤对训练多任务预测模型:
[0209] 在步骤401中,获取数据样本集;数据样本集中包括多个HTTP请求数据样本以及各HTTP请求数据样本的多任务预测标签;多任务预测标签包括各HTTP请求数据样本的Token序列的攻击类型概率标签、Token序列中每个Token的异常分数标签和边界概率标签。
[0210] 在一些实施例中,本申请在对模型训练前,还对各HTTP请求数据样本进行语义保留扰动处理和编码统一处理。
[0211] 具体实施时,首先根据下列方式对各HTTP请求数据样本进行编码统一处理:
[0212] 1、URL解码:对%xx形式的编码字符进行解码。
[0213] 例如:%27→单引号';%3C→小于号<。
[0214] 2、Unicode解码:处理\uXXXX或XXXX形式的Unicode转义。
[0215] 例如:\u003c→<;>;→>。
[0216] 3、多重编码处理:递归解码嵌套编码。
[0217] 例如:双重URL编码%2527→先解码为%27,再解码为'。
[0218] 然后,对编码统一后的各HTTP请求数据样本进行语义保留扰动处理:
[0219] 1、数值扩展:在数值语义不变的基础上,对数字参数值添加前导零,例如,page=2→page=002;
[0220] 2、大小写扰动:在保证语义不变的基础上,随机切换部分字母的大小写,例如,若设置扰动概率pcase=0.3,则随机切换30%字母的大小写;
[0221] 3、参数乱序:在保证语义不变的基础上,随机交换部分参数的顺序,例如,若设置交换概率pswap=0.5,则随机交换50%参数的顺序。
[0222] 由此,通过对各HTTP请求数据样本进行语义保留扰动处理和编码统一处理,使模型学习攻击片段的语义不变性,显著降低误报风险。
[0223] 在本申请中,还需要使用攻击片段标记,如[ATT]和[ / ATT]明确标注各HTTP请求数据样本的攻击片段边界。
[0224] 例如,HTTP请求数据样本为 / search?q='UNION SELECT,添加攻击片段标记之后,为 / search?q=[ATT]'UNION SELECT[ / ATT]。
[0225] 在步骤402中,对数据样本集中的HTTP请求数据样本进行子词切分处理和动态位置编码扩展,得到各HTTP请求数据样本的Token序列和目标位置编码。
[0226] 训练时,需要进行动态位置编码扩展。而进行动态位置编码扩展,则需要先计算局部攻击密度:
[0227] 对每个Token的位置j,统计其前后窗口k中(k=5)在攻击片段标记内的Token数量:
[0228]
[0229] 其中,为指示函数,第i个Token在攻击片段标记内时取1,否则取0。Tokeni∈{[ATT],[ / ATT]}表示前后窗口k中的第i个Token在攻击片段标记内。在攻击片段标记内的Token数量可以根据在各HTTP请求数据样本中标注的攻击片段标记获得。
[0230] 局部攻击密度sj为:
[0231] 需要说明的是,在训练时,每训练100步更新一次全局密度:
[0232]
[0233] 在得到局部攻击密度sj之后,将局部攻击密度sj与嵌入层初始隐藏状态进行拼接,然后输入插值因子生成网络(MLP),得到插值因子αj∈[0,1],最后根据公式(3)进行动态位置编码扩展,得到目标位置编码。
[0234] 在训练时,每训练步进行一次动态位置编码扩展,在本申请中目标位置编码序列长度可以设置为1024。
[0235] 其中,插值因子生成网络(MLP)包括输入层、隐藏层以及输出层。其中输入层的输入维度din=769。隐藏层的维度为64维,激活函数为LeakyReLU(负斜率=0.01)。输出层的维度为1维,激活函数为Sigmoid。
[0236] 插值因子生成网络(MLP)的参数定义为:
[0237]
[0238] 在训练时,先对参数初始化:权重矩阵W1,W2使用Xavier正态分布,偏置项b1,b2初始化为0;然后MLP梯度通过位置编码反向传播至BERT编码器,来源于多任务损失联合优化:
[0239]
[0240] 其中,MLP参数与多任务预测模型的参数使用同一优化器(Adam)及学习率策略更新。MLP梯度范数超过1.0时按比例缩放;为了防止过度拟合,对W1,W2施加权重衰减(系数=0.01)。
[0241] 由此,通过端到端联合训练,使密度感知插值因子生成网络(MLP)与主模型协同优化,实现位置编码的动态自适应。
[0242] 在步骤403中,将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型以多任务预测标签为目标进行训练,得到训练完成的多任务预测模型。
[0243] 在一些实施例中,将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型以多任务预测标签为目标进行训练,得到训练完成的多任务预测模型,具体实施为,在每一个训练阶段执行:
[0244] 将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,得到各HTTP请求数据样本的多任务预测概率;多任务预测概率包括各HTTP请求数据样本的Token序列的攻击类型概率、Token序列中每个Token的异常分数和边界概率;
[0245] 基于多任务预测概率和多任务预测标签,确定损失函数,并基于损失函数调整多任务预测模型的参数,得到训练完成的多任务预测模型。
[0246] 在一些实施例中,数据样本集中包括监督数据样本集和半监督数据样本集;监督数据样本集中的HTTP请求数据样本具有攻击片段标记;半监督数据样本集中的HTTP请求数据样本没有攻击片段标记。
[0247] 具体实施时,在30%的数据样本中随机移除攻击片段标记[ATT]和[ / ATT],构造半监督数据样本,保留多任务预测标签;然后对半监督数据样本施加随机偏移,生成多种变体;然后对多种变体进行语义扰动处理,得到语义扰动处理后的多种变体;将半监督数据样本和语义扰动处理后的多种变体一起作为半监督数据样本集。
[0248] 因此,本申请中将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型以多任务预测标签为目标进行训练,得到训练完成的多任务预测模型,需要分成三个阶段进行训练,如图5所示:
[0249] 第一阶段:将监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型进行训练,得到第一多任务预测模型。
[0250] 在对多任务预测模型进行第一阶段训练时,基于监督数据集中攻击片段标记的各个Token以及各个Token的目标位置编码进行编码处理。
[0251] 具体实施时,在第一阶段,全部使用监督样本集中的HTTP请求数据样本,每个数据样本中均具有攻击片段标记。
[0252] 第一阶段的损失函数为:
[0253]
[0254] 其中,LMLM为局部化MLM头的损失函数,Lcls为攻击分类头的损失函数,Lboundary为边界检测头的损失函数。在第一阶段训练时,只对攻击片段标记的各个Token进行异常分数预测,即只计算攻击片段标记内Token的LMLM。
[0255] 在第一阶段训练,基于损失函数调整多任务预测模型的参数可以执行为:
[0256]
[0257] 其中,为求梯度,Lsup为损失函数,0.1为一致性约束权重,Lconsist为一致性约束损失函数,防止边界检测头与局部化MLM头的任务冲突。在第一阶段训练中,初始学习率η=2×10-5,预热期线性增长至3×10-5。
[0258] 第二阶段:将混合数据集中各HTTP请求数据样本的Token序列和目标位置编码,输入第一多任务预测模型,对第一多任务预测模型进行训练,得到第二多任务预测模型。
[0259] 其中,混合数据集中包括监督数据集中HTTP请求数据样本和半监督数据样本集中HTTP请求数据样本。
[0260] 混合数据集中监督数据集中HTTP请求数据样本的数量与半监督数据样本集中HTTP请求数据样本的数量可以相同,也可以不同。监督数据集中HTTP请求数据样本的数量大于或者等于半监督数据样本集中HTTP请求数据样本的数量。
[0261] 在对第一多任务预测模型进行第二阶段训练时,基于监督数据集中攻击片段标记的各个Token以及各个Token的目标位置编码,和半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码进行编码处理。
[0262] 具体实施时,第二阶段是半监督联合训练,使用混合数据集中的HTTP请求数据样本进行训练。其中,混合数据集中监督数据集中HTTP请求数据样本与半监督数据样本集中HTTP请求数据样本可以设置为各占50%。
[0263] 第二阶段的损失函数为:
[0264]
[0265] 在第二阶段,为解决任务收敛速度不平衡,梯度冲突导致的震荡问题,利用可学习变量通过指数机制调节梯度目标。使用GradNorm进行权重调整,动态平衡分类、定位与重建任务的权重。
[0266] 在第二阶段,攻击分类头、边界检测头以及局部化MLM头的权重分别初始化为λMLM=1.0、λcls=1.0、λboundary=1.0。
[0267] 使用GradNorm进行权重调整时,先每100训练步计算任务梯度范数
[0268] 其中,对于任务i,任务梯度范数为:
[0269]
[0270] 其中,任务i为攻击分类头任务、边界检测头任务以及局部化MLM头任务。θk为共享参数的第k个元素;θ共享指的是多任务预测模型中所有任务共享的骨干网络参数,包括BERT编码器参数、嵌入层参数等核心组件;d表示共享参数的总维度,如BERT-base有110M参数,则d≈108;Li为任务i的损失函数。
[0271] 再计算目标梯度然后根据GradNorm损失更新任务权重λi;最后归一化权重:
[0272] 由此,可以使用GradNorm调整各个任务的权重。
[0273] 在第二阶段训练,基于损失函数调整第一多任务预测模型的参数可以执行为:
[0274]
[0275] 其中,为求梯度,为任务i的归一化权重;Li为任务i的损失函数。在第二阶段训练中,学习率按余弦退火,从η=3×10-5降至1×10-5。
[0276] 第三阶段:将半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码,输入第二多任务预测模型,对第二多任务预测模型进行训练,得到训练完成的多任务预测模型。
[0277] 在对第二多任务预测模型进行第三阶段训练时,基于半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码进行编码处理。
[0278] 具体实施时,在第三阶段,全部使用半监督样本集中的HTTP请求数据样本,每个数据样本中都没有攻击片段标记。在第三阶段冻结边界检测头的参数。
[0279] 第三阶段的损失函数为:
[0280] Lfinal=0.9·Lcls+0.3·LMLM+0.2·Lconsist
[0281] 其中,LMLM为局部化MLM头的损失函数,Lcls为攻击分类头的损失函数,Lconsist为一致性约束损失函数。在第三阶段训练中,学习率为η=5×10-6,避免破坏已学的特征。
[0282] 在第三阶段训练,基于损失函数调整第二多任务预测模型的参数可以执行为:
[0283]
[0284] 其中,为求梯度;Lfinal为损失函数;θ共享指的是多任务预测模型中所有任务共享的骨干网络参数,包括BERT编码器参数、嵌入层参数等核心组件。在第三阶段训练中,学习率为η=5×10-6,避免破坏已学的特征。
[0285] 由此,完成了对多任务预测模型的训练,得到训练完成的多任务预测模型。
[0286] 需要注意的是,在对多任务预测模型的局部化MLM头进行训练时,对HTTP请求数据样本中攻击片段内的Token进行随机遮蔽,预测所述攻击片段内的Token的异常分数,具体表示为:
[0287] 对于有攻击片段标记的HTTP请求数据样本(监督数据样本集),局部化MLM头仅对攻击片段标记内的Token进行随机遮蔽,预测攻击片段标记内的Token的异常分数;
[0288] 对于没有攻击片段标记的HTTP请求数据样本(半监督数据样本集),局部化MLM头基于预设攻击片段库和HTTP请求数据样本进行匹配,确定HTTP请求数据样本中与预设攻击片段库匹配成功的攻击片段如SQL关键词UNION、SELECT,XSS标签<script>等,对HTTP请求数据样本中攻击片段内的Token进行随机遮蔽,预测攻击片段内的Token的异常分数。
[0289] 在本申请中,随机遮蔽的概率pmask可以设置为0.15
[0290] 示例:输入 / search?q=1' / ** / UNION,若UNION匹配风险词典,则遮蔽其前后Token,如1'、 / ** / 。
[0291] 为了理解本申请实施例提供的攻击检测方法中的训练过程,结合如图6所示的流程示意图进行说明。
[0292] 先输入HTTP请求数据样本;再对HTTP请求数据样本标注攻击片段标记和语义扰动处理,之后生成监督数据和半监督数据;然后将监督数据和半监督数据在嵌入层进行向量嵌入,再进行动态位置编码扩展。
[0293] 在动态编码扩展时,先进行局部攻击密度统计,在将局部攻击密度输入MLP网络生成插值因子,最后进行动态插值,生成目标位置编码。
[0294] 然后将目标位置编码输入嵌入层得到嵌入向量,再将嵌入向量输入BERT编码器的12层Transformer编码层,生成上下文相关的隐藏状态;然后将隐藏状态分别输入攻击分类头、局部化MLM头、边界检测头,得到Token序列的攻击类型概率、每个Token的异常分数以及每个Token的边界概率。
[0295] 最后对Token序列的攻击类型概率、每个Token的异常分数以及每个Token的边界概率进行多特征融合决策,进行攻击类型判定和攻击片段定位。
[0296] 本申请实施例中,一方面,对原始HTTP请求数据的Token序列的初始位置编码进行动态位置编码扩展,得到目标位置编码,能够自适应扩展序列长度,有效提升了长序列攻击片段的定位精度。一方面,将Token序列以及目标位置编码输入训练完成的多任务预测模型,预测Token序列的攻击类型概率、每个Token的异常分数和边界概率,可以结合三种概率对原始HTTP请求数据进行攻击检测,从而准确的确定原始HTTP请求数据的攻击类型和攻击片段的位置,能够提高攻击检测与实时拦截的准确度,降低复杂业务场景下的误报率。
[0297] 基于相同的技术构思,本申请实施例还提供了一种攻击检测装置,攻击检测装置解决问题的原理与上述攻击检测方法相似,因此攻击检测装置的实施可参见攻击检测方法的实施,重复之处不再赘述。
[0298] 图7为本申请实施例提供的一种攻击检测装置的结构示意图,该装置包括:
[0299] 数据处理模块701,用于对原始HTTP请求数据进行子词切分处理,得到原始HTTP请求数据的字符Token序列;
[0300] 编码扩展模块702,用于根据Token序列的初始位置编码和Token序列中的参考异常Token,对Token序列进行动态位置编码扩展,得到Token序列的目标位置编码;
[0301] 概率预测模块703,用于将Token序列和目标位置编码输入多任务预测模型,得到Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率;多任务预测模型包括攻击分类头、边界检测头以及局部化MLM头;在训练局部化MLM头时,对HTTP请求数据样本中攻击片段内的Token进行随机遮蔽,预测攻击片段内的Token的异常分数;
[0302] 攻击判定模块704,用于基于Token序列的攻击类型概率、每个Token的异常分数和每个Token的边界概率,进行攻击判定和攻击回溯,确定原始HTTP请求数据的攻击类型和攻击片段的位置。
[0303] 在一些实施例中,编码扩展模块702,具体用于:
[0304] 针对每个Token,执行以下操作:
[0305] 根据Token所在预设区域中参考异常Token的数量,确定Token的局部攻击密度;参考异常Token是将Token序列和预设攻击片段库进行匹配后确定的;
[0306] 将Token的局部攻击密度输入插值因子生成网络MLP,得到插值因子;
[0307] 基于插值因子和Token序列的初始位置编码,对Token进行动态位置编码扩展,得到Token对应的目标位置编码。
[0308] 在一些实施例中,多任务预测模型还包括BERT编码器;概率预测模块703,具体用于:
[0309] 将Token序列和目标位置编码输入BERT编码器进行编码处理,得到Token序列对应的隐藏状态向量;
[0310] 将隐藏状态向量中的分类隐藏状态输入攻击分类头,进行线性分类和概率预测,得到Token序列的攻击类型概率;
[0311] 将隐藏状态向量输入局部化MLM头,进行线性变换和概率预测,得到每个Token的异常分数;
[0312] 将隐藏状态向量输入边界检测头,进行线性变换和概率预测,得到每个Token的边界概率。
[0313] 在一些实施例中,概率预测模块703,具体用于:
[0314] 通过下列方式训练多任务预测模型:
[0315] 获取数据样本集;数据样本集中包括多个HTTP请求数据样本以及各HTTP请求数据样本的多任务预测标签;多任务预测标签包括各HTTP请求数据样本的Token序列的攻击类型概率标签、Token序列中每个Token的异常分数标签和边界概率标签;
[0316] 对数据样本集中的HTTP请求数据样本进行子词切分处理和动态位置编码扩展,得到各HTTP请求数据样本的Token序列和目标位置编码;
[0317] 将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型以多任务预测标签为目标进行训练,得到训练完成的多任务预测模型。
[0318] 在一些实施例中,概率预测模块703,具体用于:
[0319] 对数据样本集中的HTTP请求数据样本进行语义保留扰动处理和编码统一处理。
[0320] 在一些实施例中,数据样本集中包括监督数据样本集和半监督数据样本集;监督数据样本集中的HTTP请求数据样本具有攻击片段标记;半监督数据样本集中的HTTP请求数据样本没有攻击片段标记;
[0321] 概率预测模块703,具体用于:
[0322] 将监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,对多任务预测模型进行训练,得到第一多任务预测模型;在对多任务预测模型进行训练时,基于监督数据集中攻击片段标记的各个Token以及各个Token的目标位置编码进行编码处理;
[0323] 将混合数据集中各HTTP请求数据样本的Token序列和目标位置编码,输入第一多任务预测模型,对第一多任务预测模型进行训练,得到第二多任务预测模型;混合数据集中包括监督数据集中HTTP请求数据样本和半监督数据样本集中HTTP请求数据样本;在对第一多任务预测模型进行训练时,基于监督数据集中攻击片段标记的各个Token以及各个Token的目标位置编码,和半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码进行编码处理;
[0324] 将半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码,输入第二多任务预测模型,对第二多任务预测模型进行训练,得到训练完成的多任务预测模型;在对第二多任务预测模型进行训练时,基于半监督数据样本集中各HTTP请求数据样本的Token序列和目标位置编码进行编码处理。
[0325] 在一些实施例中,概率预测模块703,具体用于:
[0326] 将各HTTP请求数据样本的Token序列和目标位置编码,输入多任务预测模型,得到各HTTP请求数据样本的多任务预测概率;多任务预测概率包括各HTTP请求数据样本的Token序列的攻击类型概率、Token序列中每个Token的异常分数和边界概率;
[0327] 基于多任务预测概率和多任务预测标签,确定损失函数,并基于损失函数调整多任务预测模型的参数,得到训练完成的多任务预测模型。
[0328] 在一些实施例中,攻击判定模块704,具体用于:
[0329] 将Token序列的攻击类型概率、每个Token的异常分数、每个Token的边界概率,与预设概率阈值进行比较,确定原始HTTP请求数据的攻击类型;
[0330] 基于每个Token的边界概率,确定Token序列中攻击片段的起始Token和结束Token;
[0331] 基于起始Token的目标位置编码和结束Token的目标位置编码,回溯起始Token和结束Token在原始HTTP请求数据中的初始位置编码,得到原始HTTP请求数据中攻击片段的位置。
[0332] 本申请实施例中对模块的划分是示意性的,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,另外,本申请各实施例中的各功能模块可以集成在一个处理器中,也可以是单独物理存在,也可以两个或两个以上模块集成在一个模块中。各个模块相互之间的耦合可以是通过一些接口实现,这些接口通常是电性通信接口,但是也不排除可能是机械接口或其它的形式接口。因此,作为分离部件说明的模块可以是或者也可以不是物理上分开的,既可以位于一个地方,也可以分布到同一个或不同设备的不同位置上。上述集成的模块既可以采用硬件的形式实现,也可以采用软件功能模块的形式实现。
[0333] 在介绍了本申请示例性实施方式的攻击检测方法和装置之后,接下来,介绍根据本申请的另一示例性实施方式的电子设备。
[0334] 下面参照图8来描述根据本申请的这种实施方式实现的电子设备800。图8显示的电子设备800仅仅是一个示例,不应对本申请实施例的功能和使用范围带来任何限制。
[0335] 如图8所示,电子设备800以通用电子设备的形式表现。电子设备800的组件可以包括但不限于:上述至少一个处理器801、上述至少一个存储器802、连接不同系统组件(包括存储器802和处理器801)的总线803。
[0336] 其中,至少一个存储器802存储有可被至少一个处理器801执行的计算机程序,计算机程序被至少一个处理器801执行时可使至少一个处理器801执行本申请实施例提供的任一攻击检测方法的步骤。
[0337] 总线803表示几类总线结构中的一种或多种,包括存储器总线或者存储器控制器、外围总线、处理器或者使用多种总线结构中的任意总线结构的局域总线。
[0338] 存储器802可以包括易失性存储器形式的可读介质,例如随机存取存储器(RAM)8021和 / 或高速缓存存储器8022,还可以进一步包括只读存储器(ROM)8023。
[0339] 存储器802还可以包括具有一组(至少一个)程序模块8024的程序 / 实用工具8025,这样的程序模块8024包括但不限于:操作系统、一个或者多个应用程序、其它程序模块以及程序数据,这些示例中的每一个或某种组合中可能包括网络环境的实现。
[0340] 电子设备800也可以与一个或多个外部设备804(例如键盘、指向设备等)通信,还可与一个或者多个使得用户能与电子设备800交互的设备通信,和 / 或与使得该电子设备800能与一个或多个其它电子设备进行通信的任何设备(例如路由器、调制解调器等等)通信。这种通信可以通过输入 / 输出(I / O)接口805进行。并且,电子设备800还可以通过网络适配器806与一个或者多个网络(例如局域网(LAN),广域网(WAN)和 / 或公共网络,例如因特网)通信。如图所示,网络适配器806通过总线803与用于电子设备800的其它模块通信。应当理解,尽管图中未示出,可以结合电子设备800使用其它硬件和 / 或软件模块,包括但不限于:微代码、设备驱动器、冗余处理器、外部磁盘驱动阵列、RAID系统、磁带驱动器以及数据备份存储系统等。
[0341] 在示例性实施例中,还提供了一种存储介质,当存储介质中的计算机程序由电子设备的处理器执行时,电子设备能够执行上述任一攻击检测方法。可选地,存储介质可以是非临时性计算机可读存储介质,例如,所述非临时性计算机可读存储介质可以是ROM、随机存取存储器(RAM)、CD-ROM、磁带、软盘和光数据存储设备等。
[0342] 在示例性实施例中,还提供一种计算机程序产品,当计算机程序产品被电子设备执行时,电子设备能够实现本申请提供的任一攻击检测方法的步骤。
[0343] 并且,计算机程序产品可以采用一个或多个可读介质的任意组合。可读介质可以是可读信号介质或者可读存储介质。可读存储介质例如可以是但不限于电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式盘、硬盘、RAM、ROM、可擦式可编程只读存储器(Erasable Programmable Read-Only Memory,EPROM)、闪存、光纤、光盘只读存储器(Compact Disk Read Only Memory,CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。
[0344] 本申请实施例中用于设备发现的程序产品可以采用CD-ROM并包括程序代码,并可以在计算设备上运行。然而,本申请的程序产品不限于此,在本文件中,可读存储介质可以是任何包含或存储程序的有形介质,该程序可以被指令执行系统、装置或者器件使用或者与其结合使用。
[0345] 可读信号介质可以包括在基带中或者作为载波一部分传播的数据信号,其中承载了可读程序代码。这种传播的数据信号可以采用多种形式,包括但不限于电磁信号、光信号或上述的任意合适的组合。可读信号介质还可以是可读存储介质以外的任何可读介质,该可读介质可以发送、传播或者传输用于由指令执行系统、装置或者器件使用或者与其结合使用的程序。
[0346] 可读介质上包含的程序代码可以用任何适当的介质传输,包括但不限于无线、有线、光缆、射频(Radio Frequency,RF)等等,或者上述的任意合适的组合。
[0347] 可以以一种或多种程序设计语言的任意组合来编写用于执行本申请操作的程序代码,所述程序设计语言包括面向对象的程序设计语言—诸如Java、C++等,还包括常规的过程式程序设计语言诸如"C”语言或类似的程序设计语言。程序代码可以完全地在用户计算设备上执行、部分地在用户设备上执行、作为一个独立的软件包执行、部分在用户计算设备上部分在远程计算设备上执行、或者完全在远程计算设备或服务器上执行。在涉及远程计算设备的情形中,远程计算设备可以通过任意种类的网络如局域网(Local AreaNetwork,LAN)或广域网(Wide Area Network,WAN)连接到用户计算设备,或者,可以连接到外部计算设备(例如利用因特网服务提供商来通过因特网连接)。
[0348] 应当注意,尽管在上文详细描述中提及了装置的若干单元或子单元,但是这种划分仅仅是示例性的并非强制性的。实际上,根据本申请的实施方式,上文描述的两个或更多单元的特征和功能可以在一个单元中具体化。反之,上文描述的一个单元的特征和功能可以进一步划分为由多个单元来具体化。
[0349] 此外,尽管在附图中以特定顺序描述了本申请方法的操作,但是,这并非要求或者暗示必须按照该特定顺序来执行这些操作,或是必须执行全部所示的操作才能实现期望的结果。附加地或备选地,可以省略某些步骤,将多个步骤合并为一个步骤执行,和 / 或将一个步骤分解为多个步骤执行。
[0350] 本领域内的技术人员应明白,本申请的实施例可提供为方法、系统、或计算机程序产品。因此,本申请可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本申请可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。
[0351] 本申请是参照根据本申请实施例的方法、装置(系统)、和计算机程序产品的流程图和 / 或方框图来描述的。应理解可由计算机程序指令实现流程图和 / 或方框图中的每一流程和 / 或方框、以及流程图和 / 或方框图中的流程和 / 或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和 / 或方框图一个方框或多个方框中指定的功能的装置。
[0352] 这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和 / 或方框图一个方框或多个方框中指定的功能。
[0353] 这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和 / 或方框图一个方框或多个方框中指定的功能的步骤。
[0354] 尽管已描述了本申请的优选实施例,但本领域内的技术人员一旦得知了基本创造性概念,则可对这些实施例做出另外的变更和修改。所以,所附权利要求意欲解释为包括优选实施例以及落入本申请范围的所有变更和修改。
[0355] 显然,本领域的技术人员可以对本申请进行各种改动和变型而不脱离本申请的精神和范围。这样,倘若本申请的这些修改和变型属于本申请权利要求及其等同技术的范围之内,则本申请也包含这些改动和变型在内。< / script>
Claims
1. An attack detection method, characterized in that: The method comprises: Perform subword segmentation on the original HTTP request data to obtain a character token sequence of the original HTTP request data; Performing dynamic position coding expansion on the Token sequence according to the initial position coding of the Token sequence and the reference abnormal Token in the Token sequence to obtain the target position coding of the Token sequence; The token sequence and the target position code are input into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token; the multi-task prediction model includes an attack classification head, a boundary detection head, and a localized MLM head; when training the localized MLM head, the tokens in the attack segment of the HTTP request data sample are randomly masked to predict the anomaly score of the tokens in the attack segment; Based on the attack type probability of the Token sequence, the anomaly score of each Token, and the boundary probability of each Token, attack judgment and attack backtracking are performed to determine the attack type and the location of the attack fragment of the original HTTP request data.
2. The method according to claim 1, wherein The step of performing dynamic position coding expansion on the Token sequence according to the initial position coding of the Token sequence and the reference abnormal Token in the Token sequence to obtain the target position coding of the Token sequence includes: For each token, perform the following operations: Determine the local attack density of the token based on the number of reference abnormal tokens in the preset area where the token is located; the reference abnormal token is determined after matching the token sequence with a preset attack fragment library; Input the local attack density of the token into the interpolation factor generation network MLP to obtain the interpolation factor; Based on the interpolation factor and the initial position code of the Token sequence, the Token is dynamically position-coded and expanded to obtain a target position code corresponding to the Token.
3. The method according to claim 1, wherein The multi-task prediction model also includes a BERT encoder; The step of inputting the token sequence and the target position encoding into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token includes: Input the Token sequence and the target position code into the BERT encoder for encoding processing to obtain a hidden state vector corresponding to the Token sequence; Input the classified hidden state in the hidden state vector into the attack classification head, perform linear classification and probability prediction, and obtain the attack type probability of the token sequence; Input the hidden state vector into the localized MLM head, perform linear transformation and probability prediction, and obtain the anomaly score of each token; The hidden state vector is input into the boundary detection head, and linear transformation and probability prediction are performed to obtain the boundary probability of each token.
4. The method according to claim 1 or 3, wherein: The multi-task prediction model is trained in the following way: Obtain a data sample set; the data sample set includes multiple HTTP request data samples and multi-task prediction labels for each HTTP request data sample; the multi-task prediction labels include an attack type probability label of a token sequence of each HTTP request data sample, an anomaly score label of each token in the token sequence, and a boundary probability label; Performing subword segmentation processing and dynamic position coding expansion on the HTTP request data samples in the data sample set to obtain a token sequence and target position coding of each HTTP request data sample; The token sequence and target position of each HTTP request data sample are encoded and input into the multi-task prediction model, and the multi-task prediction model is trained with the multi-task prediction label as the target to obtain a trained multi-task prediction model.
5. The method according to claim 4, wherein Before performing subword segmentation processing and dynamic position coding expansion on the HTTP request data samples in the data sample set, the method further includes: The HTTP request data samples in the data sample set are subjected to semantics-preserving perturbation processing and encoding uniform processing.
6. The method according to claim 4, wherein The data sample set includes a supervised data sample set and a semi-supervised data sample set; the HTTP request data samples in the supervised data sample set have attack segment marks; the HTTP request data samples in the semi-supervised data sample set do not have attack segment marks; The Token sequence and target position encoding of each HTTP request data sample are input into the multi-task prediction model, and the multi-task prediction model is trained with the multi-task prediction label as the target to obtain a trained multi-task prediction model, including: Inputting the token sequence and target position encoding of each HTTP request data sample in the supervised data sample set into the multi-task prediction model, training the multi-task prediction model to obtain a first multi-task prediction model; when training the multi-task prediction model, encoding processing is performed based on the respective tokens marked in the attack fragment in the supervised data set and the target position encoding of each token; The token sequence and target position encoding of each HTTP request data sample in the mixed data set are input into the first multi-task prediction model, and the first multi-task prediction model is trained to obtain a second multi-task prediction model; the mixed data set includes the HTTP request data samples in the supervised data set and the HTTP request data samples in the semi-supervised data sample set; when training the first multi-task prediction model, encoding is performed based on the tokens and target position encoding of each token marked in the attack fragment in the supervised data set, and the token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set; The Token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set are input into the second multi-task prediction model, and the second multi-task prediction model is trained to obtain a trained multi-task prediction model; when training the second multi-task prediction model, encoding processing is performed based on the Token sequence and target position encoding of each HTTP request data sample in the semi-supervised data sample set.
7. The method according to claim 4, wherein The Token sequence and target position encoding of each HTTP request data sample are input into the multi-task prediction model, and the multi-task prediction model is trained with the multi-task prediction label as the target to obtain a trained multi-task prediction model, including: The token sequence and target position of each HTTP request data sample are encoded and input into the multi-task prediction model to obtain the multi-task prediction probability of each HTTP request data sample; the multi-task prediction probability includes the attack type probability of the token sequence of each HTTP request data sample, the anomaly score and boundary probability of each token in the token sequence; Based on the multi-task prediction probability and the multi-task prediction label, a loss function is determined, and the parameters of the multi-task prediction model are adjusted based on the loss function to obtain a trained multi-task prediction model.
8. The method according to claim 1, wherein The attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token are used to perform attack determination and attack backtracking, and determine the attack type and the location of the attack fragment of the original HTTP request data, including: Compare the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token with a preset probability threshold to determine the attack type of the original HTTP request data; Determine the start token and end token of the attack segment in the token sequence based on the boundary probability of each token; Based on the target position code of the start token and the target position code of the end token, the initial position codes of the start token and the end token in the original HTTP request data are traced back to obtain the position of the attack fragment in the original HTTP request data.
9. An attack detection device, characterized in that: The device comprises: A data processing module is used to perform subword segmentation processing on the original HTTP request data to obtain a character token sequence of the original HTTP request data; A coding extension module is used to perform dynamic position coding extension on the Token sequence according to the initial position coding of the Token sequence and a reference abnormal Token in the Token sequence to obtain a target position coding of the Token sequence; A probability prediction module is configured to input the token sequence and the target position code into a multi-task prediction model to obtain the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token; the multi-task prediction model includes an attack classification head, a boundary detection head, and a localized MLM head; when training the localized MLM head, tokens within the attack segment of the HTTP request data sample are randomly masked to predict the anomaly score of the tokens within the attack segment; The attack determination module is used to perform attack determination and attack backtracing based on the attack type probability of the token sequence, the anomaly score of each token, and the boundary probability of each token, and determine the attack type and the location of the attack fragment of the original HTTP request data.
10. An electronic device, characterized in that: include: at least one processor, and a memory communicatively coupled to the at least one processor, wherein: The memory stores a computer program executable by the at least one processor. The computer program is executed by the at least one processor so as to enable the at least one processor to perform the method according to any one of claims 1 to 8.