CN-122020661-A - Intelligent contract security vulnerability identification method and system based on multi-mode feature learning
Abstract
The invention discloses an intelligent contract security vulnerability recognition method and system based on multi-mode feature learning, which comprises the steps of preprocessing an intelligent contract source code and constructing a code structure diagram, extracting sequence semantic features by utilizing a CodeBERT model, extracting graph structure features by adopting an extended graph convolution network, wherein the extended graph convolution network comprises a multi-level feature retaining mechanism, an enhanced side attention mechanism, an adaptive graph convolution operation and graph level feature aggregation, and is respectively used for relieving deep network degradation, improving the characterization capability of key dependency relationships, capturing indirect dependency relationships and aggregating graph structure features with different scales, then fusing the sequence semantic features and the graph structure features by an adaptive weight learning mechanism, carrying out vulnerability classification and recognition based on the fused features, finally calculating various evaluation indexes, evaluating recognition effects and performances, displaying recognition results and performance evaluation indexes in a visual mode, and greatly improving the accuracy of intelligent contract recognition.
Inventors
- LIU DUANYANG
- HU XINGGUANG
Assignees
- 浙江外国语学院
Dates
- Publication Date
- 20260512
- Application Date
- 20260126
Claims (10)
- 1. An intelligent contract security vulnerability recognition method based on multi-mode feature learning is characterized by comprising the following steps: (1) The construction of a code structure diagram, namely, carrying out standardized processing on the intelligent contract source code, and constructing the processed code into the code structure diagram; (2) Extracting sequence semantic features, namely segmenting a standardized source code to obtain a token sequence, adding position codes, inputting CodeBERT models, and using the hidden state of the [ CLS ] position as the sequence semantic features; (3) The method comprises the steps of drawing structural feature extraction, namely carrying out feature extraction on a code structure diagram by adopting an extended drawing convolution network to obtain drawing structural features, wherein the extended drawing convolution network comprises a multi-level feature holding mechanism, an enhanced edge attention mechanism, self-adaptive drawing convolution operation and drawing level feature aggregation; (4) Multi-modal feature fusion, namely fusing sequence semantic features and graph structural features through a self-adaptive weight learning mechanism to obtain fusion features; (5) Vulnerability identification, namely inputting the fusion characteristics into a multi-layer perceptron classifier and outputting a vulnerability identification result; (6) Calculating corresponding accuracy, precision, recall rate and F1 value according to the vulnerability identification result in the step (5), and further evaluating the identification effect and performance to obtain an evaluation index result; (7) And the result display is carried out by visually displaying the vulnerability identification result and the evaluation index result through a confusion matrix or an ROC curve.
- 2. The method of claim 1, wherein the normalizing of step (1) comprises removing a single line of annotations and multiple lines of annotations, deleting redundant blank lines and blank characters, and employing a unified indentation rule.
- 3. The method of claim 1, wherein the code structure diagram in the step (1) is g= (V, E), and defines two classes of graph nodes and three classes of edge relationships, wherein V is a node set and E is an edge set, the code structure diagram is used to model control flows and data dependencies in the smart contract code, wherein the two classes of graph nodes include a general node and a core node, and the three classes of edge relationships include a control flow edge, a data flow edge and a Fallback edge.
- 4. The method according to claim 1, characterized in that the sequence semantic feature extraction in step (2) comprises the following sub-steps: (2.1) word segmentation is carried out on the standardized codes according to a vocabulary, and a token sequence is obtained: ; Wherein, the Represent the first The number of the token to be used in the process, Is the sequence length; (2.2) adding Special tags at the beginning and end of the token sequence, respectively And And then obtaining the complete sequence of its input [ CLS ] Generating a position code for each position in the sequence and embedding the position code in the token; (2.3) inputting the sequence obtained in the step (2.2) into a CodeBERT model, and outputting a hidden state matrix by the model: ; Wherein, the In order to hide the state matrix, Is the dimension of the hidden state; (2.4) from the hidden State matrix Extracting a vector corresponding to the first position, namely a vector corresponding to the [ CLS ] mark: ; the vector is used as the semantic feature of the whole code sequence, and finally the sequence semantic feature is output : 。
- 5. The method of claim 1, wherein the total layer number l=3 of the extended graph convolution network in the step (3) specifically includes a multi-level feature preserving mechanism, an enhanced edge attention mechanism, an adaptive graph convolution operation and a graph-level feature aggregation, and the input/output change process of the multi-level feature preserving mechanism is as follows: ; ; Wherein, the Is a node In the first place The intermediate state of the layer(s), Is a node In the first place The hidden state of the layer(s), Is a node Is used for the original characteristics of the (c), Is a node In the first place The hidden state of the layer(s), Is the first The matrix of learnable weights of the layer, Is a node Is defined by a set of neighboring nodes of the network, Is an aggregation function, used to integrate neighbor information, The mechanism balances local structure learning and original information reservation through residual connection and original feature injection, and is used for enhancing the feature stability of a deep network; The input and output change process of the enhanced edge attention mechanism of the extended graph rolling network is as follows: ; ; Wherein, the Is a side Is used for the feature vector of (a), And Respectively nodes Sum node In the first place The hidden state of the layer(s), For an embedded representation of the edge type, In the case of the type of edge, For the time-series position coding, The timing information of the edges, ||represents the feature stitching operation, Is a side Is used for the concentration weight of the person, As a learnable weight matrix for the attention mechanism, In order to activate the function, The mechanism comprehensively considers the edge type and time sequence information and is used for improving the distinguishing capability of the edge relationship of different types; The input and output change process of the self-adaptive graph rolling operation of the extended graph rolling network is as follows: ; Wherein, the Is the first A hidden state matrix of all nodes of the layer, Is the first A hidden state matrix of all nodes of the layer, In order to be a contiguous matrix, For the second order adjacency matrix to model the effect of two-hop neighbors, Is a matrix of units which is a matrix of units, In order to be able to operate in an attentive manner, Is the first The weight matrix of the layer is used to determine, The connectivity among the nodes is enhanced by introducing a second-order adjacency matrix for capturing indirect dependency; The graph level characteristic aggregation of the extended graph convolution network adopts a multi-scale pooling strategy, and the input and output change process is as follows: ; ; ; ; Wherein, the A hidden state matrix for all nodes of layer L, L being the total number of layers of the extended graph rolling network, In order to maximize the result of the pooling, For maximum pooling operations, for capturing the most salient features, For the purpose of averaging the results of the pooling, For the averaging pooling operation, for providing overall statistics, In order to focus on the pooling of the results, For the attention pooling operation, a weighted representation for learning important nodes, In order to be a structural feature of the figure, Is a feature stitching operation.
- 6. The method according to claim 1, wherein the multi-modal feature fusion in step (4) specifically comprises the following sub-steps: (4.1) adaptive weight learning based on input sequence features And picture features The self-adaptive weight learning mechanism is utilized to learn the self-adaptive weights of two modal characteristics of input, and the input and output change process is as follows: ; ; Wherein, the In order to gate the weight matrix, As a result of the offset vector, The function is activated for Sigmoid, For the weights of the characteristics of the sequence, The mechanism can dynamically adjust the weights of two modal features according to different vulnerability types; (4.2) Multi-level feature fusion, namely, the weight learned according to the step (4.1) 、 Original features 、 Carrying out multi-level feature fusion to generate final fusion features The input and output change process is as follows: ; ; ; Wherein, the For linear fusion of features, by learned weights And The two features are combined in a weighted manner, For the interactive fusion feature, as would be indicated by the element-wise multiplication operation, Is a multi-layer perceptron, is used for modeling the interaction relation among the features, For final fusion of features, the multi-level fusion strategy not only reserves independent information of single modes, but also fuses cross-mode interaction features.
- 7. The method according to claim 1, wherein the input/output change process of the multi-layer perceptron classifier in step (5) is: ; Wherein the MLP is a multi-layer perceptron comprising two hidden layers, regularized using a ReLU activation function and Dropout, For the probability of belonging to class y given contract C, y e {0,1} represents the vulnerability recognition result, y=1 represents the presence of a vulnerability, and y=0 represents no vulnerability.
- 8. The method of claim 1, wherein the method has a class imbalance problem for intelligent contract vulnerability data, and the contribution of a sample easy to classify to total loss is dynamically reduced by introducing a focus loss function, so that the training process is focused on a sample difficult to classify, and the vulnerability recognition capability is improved, wherein the specific expression of the focus loss function is as follows: ; Wherein, the In order for the focal point to be lost, For the probability of identification, α is a class weight coefficient, and is used for controlling the importance of positive and negative samples, and γ is a focusing parameter, and is used for controlling the importance of difficult samples.
- 9. The method of claim 1, wherein the method is capable of identifying vulnerability types of intelligent contracts including reentry vulnerabilities, timestamp dependency vulnerabilities, tx.
- 10. The system of intelligent contract security vulnerability identification method based on multi-modal feature learning of any one of claims 1-9, characterized in that the system specifically comprises the following modules: The code structure diagram construction module is used for carrying out standardized processing on the intelligent contract source code and constructing the processed code into a code structure diagram; The sequence semantic feature extraction module is used for segmenting the standardized source code to obtain a token sequence, adding position codes, inputting CodeBERT models, and using the hidden state of the [ CLS ] position as the sequence semantic feature; The diagram structure feature extraction module is used for extracting features of the code structure diagram by adopting an extended diagram convolution network, wherein the extended diagram convolution network comprises a multi-level feature holding mechanism, an enhanced edge attention mechanism, a self-adaptive diagram convolution operation and diagram level feature aggregation; the multi-mode feature fusion module is used for fusing the sequence semantic features and the graph structural features through a self-adaptive weight learning mechanism to obtain fusion features; The vulnerability identification module inputs the fusion characteristics into a multi-layer perceptron classifier and outputs a vulnerability identification result; The performance evaluation module is used for calculating corresponding accuracy, precision, recall rate and F1 value according to the vulnerability identification result so as to measure the identification effect and performance; and the result display module is used for visually displaying the vulnerability identification result obtained by the vulnerability identification module and the evaluation index result obtained by the performance evaluation module through the confusion matrix or the ROC curve.
Description
Intelligent contract security vulnerability identification method and system based on multi-mode feature learning Technical Field The invention relates to the technical field of blockchain security, in particular to an intelligent contract security vulnerability identification method and system based on multi-mode feature learning. Background Intelligent contract vulnerability identification is an important component of blockchain security systems, and identifying security vulnerabilities in intelligent contracts can solve blockchain related field problems, including digital asset protection, financial security, and user trust. For example, when a developer deploys a smart contract, reasonable security suggestions and repair schemes may be provided based on vulnerability identification results. DeFi protocols and NFT transactions play a critical role in blockchain ecology, and understanding the security status is critical to asset protection and sustainable development. At present, a plurality of students propose methods for detecting intelligent contract vulnerabilities based on various technologies, such as symbol execution, rule matching, deep learning and the like, but the method for mining the vulnerability pattern from the source code and accurately identifying the vulnerability pattern is more effective, not only comprises rich code semantics and structural information, but also can find problems before contract deployment, and does not need to depend on a runtime environment. The method for realizing intelligent contract vulnerability recognition based on deep learning mainly comprises three types. The first type of method is a method based on a sequence model, such as a two-way long-short-term memory network, a transducer and the like. The method regards the code as linear text, ignores inherent structural information of the code, and is difficult to capture complex data flow and control flow dependency. Meanwhile, the method has the problem of gradient disappearance when processing long sequences, and has limited fitting effect. The second type of method is a method based on a graph neural network, such as a graph roll-up network, a graph annotation network, and the like. The method models program dependency relations by constructing a code structure diagram, but has the problem that semantic information is lost due to node clipping, and deep semantics of codes cannot be fully utilized. The third class of methods is based on pre-training models, and mainly comprises CodeBERT and other codes. The method is used for pre-training on a large-scale code corpus, learns rich code semantic knowledge, is suitable for being applied to a large data set, and has good semantic understanding capability. However, there are also a number of problems in smart contract vulnerability detection applications. Some deep learning models cannot achieve self-adaptive detection on different types of loopholes, such as reentrant loopholes and timestamp dependency loopholes, and have different dependency degrees on sequence information and structural information, so that unified modeling is not easy. Meanwhile, most deep learning models do not fully fuse sequence semantic features and graph structural features of codes, only single-mode information is considered, and although few models try to fuse features, the models adopt a simple splicing or fixed weight mode and do not adaptively fuse according to different vulnerability types, so that the detection effect is limited. At present, the existing intelligent contract vulnerability identification method has the main problems that 1) the traditional method relies on manual definition detection rules, vulnerability patterns cannot be automatically learned, expert dependence is strong, rule maintenance cost is high, 2) a sequence method ignores code structure information, a graph method has the problem of semantic information loss, sequence semantic features and graph structure features are not fully fused, 3) the existing fusion method adopts a fixed weight strategy, dependency differences of different vulnerability types on features are not considered, and self-adaptive fusion cannot be achieved. Disclosure of Invention Aiming at the defects of the prior art, the invention provides the intelligent contract security vulnerability identification method and system based on multi-mode feature learning, which can accurately identify security vulnerabilities in contracts by inputting the intelligent contract source codes into the method, and have good accuracy and robustness. The invention is realized by the following technical scheme that the intelligent contract security vulnerability identification method based on multi-mode feature learning comprises the following steps: (1) The construction of a code structure diagram, namely, carrying out standardized processing on the intelligent contract source code, and constructing the processed code into the code structure diagram; (2) E