Intelligent contract vulnerability detection method and system based on multi-modal fusion and information entropy
Through the smart contract vulnerability detection method based on multimodal fusion and information entropy, multiple modal data of smart contracts are analyzed and vectorized, and weighted fusion is carried out, the problem of low accuracy of existing detection methods is solved, and more efficient and accurate vulnerability detection is achieved.
Patent Information
- Application Number
- CN202411567489.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2025-03-28
- Estimated Expiration
- 2044-11-05
Smart Images

Figure CN119720209A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of blockchain security technology, and in particular to a smart contract vulnerability detection method and system based on multimodal fusion and information entropy. Background Art
[0002] Blockchain technology is widely used in modern information technology due to its key features such as anti-tampering and decentralization. As one of the core components of blockchain technology, smart contracts are also widely used in finance, games, etc. A smart contract is a predefined logic segment that can automatically execute, record and control related events in the blockchain.
[0003] Although smart contracts are widely used, the lack of execution standards in smart contracts often makes them vulnerable to infiltrating blockchain systems. Permission control vulnerabilities, reentrancy vulnerabilities, and timestamp dependency vulnerabilities are relatively common vulnerabilities in blockchain systems. Permission control vulnerabilities: Smart contracts grant different permissions to different roles. If permissions are configured incorrectly, huge economic losses will be caused. The cross-chain bridge Poly Network was stolen $611 million due to a permission control vulnerability; reentrancy vulnerability: The attacker recursively calls the contract and transfers all the funds inside the contract. 'The DAO' contract was stolen 3,600,000 ETH due to a reentrancy vulnerability; timestamp dependency vulnerability: Miners can modify the value of timestamps to a certain extent, which makes smart contracts that rely on timestamps no longer safe. Fomo3D contract is a Ponzi scheme game, and the game results are strongly dependent on timestamps. Miners increase the probability of winning the game by modifying the value of timestamps, thereby obtaining all the funds in the contract fund pool. Therefore, permission control vulnerabilities, reentrancy vulnerabilities, and timestamp dependency vulnerabilities are serious hazards to blockchain systems.
[0004] In order to detect permission control vulnerabilities, reentrancy vulnerabilities, and timestamp dependency vulnerabilities before deploying smart contracts to the blockchain, an efficient vulnerability detection method must be designed.
[0005] Currently, there are several methods for smart contract vulnerability detection:
[0006] Solution 1: Expert rule-based smart contract vulnerability detection. Vulnerability identification is performed on smart contract codes through predefined rules or manually defined patterns.
[0007] However, this scheme relies heavily on expert rules, which is a very time-consuming and error-prone process. This scheme may produce false positives or false negatives, thus undermining the effectiveness of accurately detecting vulnerabilities.
[0008] Solution 2: Smart contract vulnerability detection based on deep learning. First, collect smart contract data sets and label them. Then, build a deep learning model and input the labeled data sets into the model for model training. Finally, input the smart contract to be tested into the model for vulnerability detection.
[0009] However, existing solutions have certain limitations, namely, they mainly focus on using a single type of feature as the input of the deep learning model. The disadvantage is that the limited perspective makes it difficult for the deep learning model to deeply analyze the essential characteristics of the vulnerability. At the same time, treating the contribution of different types of features to the deep learning model equally will reduce the vulnerability detection performance of the deep learning model to a certain extent. Therefore, due to technical defects such as low accuracy, the existing solutions cannot effectively detect permission control vulnerabilities, reentrancy vulnerabilities, and timestamp dependency vulnerabilities. Summary of the invention
[0010] In view of the above problems, the present invention discloses a smart contract vulnerability detection method and system based on multimodal fusion and information entropy, which can effectively detect permission control vulnerabilities, reentrancy vulnerabilities and timestamp dependency vulnerabilities.
[0011] To achieve the above-mentioned object of the invention, the technical solution of the present invention includes the following contents.
[0012] A smart contract vulnerability detection method based on multimodal fusion and information entropy, the method comprising:
[0013] Parse the source code of the smart contract, obtain bytecode and operation code, and generate source code token sequence, bytecode token sequence and operation code token sequence;
[0014] Vectorizing the source code token sequence, the bytecode token sequence and the operation code token sequence, and performing weighted vector fusion based on information entropy of the source code, bytecode and operation code;
[0015] Based on the weighted fusion vector, the vulnerability detection results of the smart contract are generated.
[0016] Furthermore, generating a source code token sequence includes:
[0017] Performing a first denoising process on the source code, the first denoising process comprising: deleting blank lines, comments, and redundant spaces in the smart contract source code;
[0018] Use Jieba tool to perform word segmentation on the source code after the first denoising process to obtain the original source code token sequence;
[0019] Based on a fixed length, length consistency processing is performed on the original source code token sequence to obtain a source code token sequence.
[0020] Further, generating a bytecode token sequence comprises:
[0021] Performing a second denoising process on the bytecode, the second denoising process comprising: deleting deployment code and auxdata code in the bytecode;
[0022] For the bytecode after the second denoising process, every two bytecodes are grouped together and segmented using a regular expression to obtain the original bytecode token sequence;
[0023] Based on a fixed length, length consistency processing is performed on the original bytecode token sequence to obtain a bytecode token sequence.
[0024] Further, generating an operation code token sequence includes:
[0025] Performing a third denoising process on the operation code, the third denoising process comprising: deleting parameters after the PUSH instruction;
[0026] Generate an original opcode sequence by replacing PUSH instructions with PUSH1-PUSH32 instructions, replacing DUP instructions with DUP1-DUP16 instructions, replacing SWAP instructions with SWAP1-SWAP16 instructions, and replacing LOG instructions with LOG1-LOG4 instructions;
[0027] Based on a fixed length, length consistency processing is performed on the original operation code token sequence to obtain an operation code token sequence.
[0028] Furthermore, the source code token sequence, the bytecode token sequence and the operation code token sequence are vectorized, and weighted vector fusion is performed in combination with information entropy of the source code, bytecode and operation code, including:
[0029] Use the Word2Vec tool to map each token in the source code token sequence, the bytecode token sequence, and the opcode token sequence into a vector, respectively, to obtain a source code token vector sequence, a bytecode token vector sequence, and an opcode token vector sequence;
[0030] Using the source code token vector sequence as an input of a recurrent neural network to obtain a source code vector representation;
[0031] Using the bytecode token vector sequence as an input of a gated recurrent neural network to obtain a bytecode vector representation;
[0032] Using the opcode token vector sequence as an input of a long short-term memory neural network to obtain an opcode vector representation;
[0033] Calculate the average information entropy of source code, bytecode and opcode respectively;
[0034] Based on the average information entropy of the source code, the bytecode and the operation code, weighted vector fusion is performed on the source code vector representation, the bytecode vector representation and the operation code vector representation to obtain a weighted fusion vector.
[0035] Furthermore, the calculating the average information entropy of the source code includes:
[0036] Obtaining the length T of a source code token sequence and the probability p(t) that each token t in the source code token sequence appears in the source code token sequence;
[0037] Based on the length T of the source code token sequence and the probability p(t), the average information entropy of the source code is obtained.
[0038] Furthermore, the generating of the vulnerability detection result of the smart contract based on the weighted fusion vector includes:
[0039] Perform BatchNormalization and Dropout processing on the weighted fusion vector V to obtain a weighted fusion vector V′;
[0040] Use the Dense layer to reduce the dimension of the weighted fusion vector V′ to obtain the weighted fusion vector V″;
[0041] Perform BatchNormalization and Dropout processing on the weighted fusion vector V″ to obtain a weighted fusion vector V″′;
[0042] The Dense layer is used to reduce the dimension of the weighted fusion vector V″′ to obtain the probability that the smart contract has a vulnerability risk.
[0043] A smart contract vulnerability detection system based on multimodal fusion and information entropy, characterized in that the system includes:
[0044] The parsing module is used to parse the source code of the smart contract, obtain the bytecode and operation code, and generate the source code token sequence, the bytecode token sequence and the operation code token sequence;
[0045] A mapping module, used for vectorizing the source code token sequence, the bytecode token sequence and the operation code token sequence, and performing weighted vector fusion in combination with information entropy of the source code, bytecode and operation code;
[0046] The detection module is used to generate vulnerability detection results of smart contracts based on weighted fusion vectors.
[0047] An electronic device, characterized in that the electronic device comprises: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, the smart contract vulnerability detection method based on multimodal fusion and information entropy as described in any one of the above items is implemented.
[0048] A computer-readable storage medium, characterized in that computer program instructions are stored on the computer-readable storage medium, and when the computer program instructions are executed by a processor, the smart contract vulnerability detection method based on multimodal fusion and information entropy as described in any one of claims 1 to 7 is implemented.
[0049] Compared with the prior art, the present invention has at least the following beneficial effects.
[0050] 1. This method has good accuracy. Specifically: denoising reduces the impact of noise data on vulnerability detection accuracy; assigning higher weights to modal features that contain more information is conducive to the artificial neural network learning more vulnerability-related information, thereby improving vulnerability detection accuracy; multi-modal weighted vector fusion enables the artificial neural network to learn the essential characteristics of vulnerabilities from multiple angles and multiple perspectives, thereby improving vulnerability detection accuracy.
[0051] 2. This method has good scalability. Specifically, it is easy to integrate new modal features into the existing model. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 This is a schematic diagram of a smart contract vulnerability detection method based on multimodal fusion and information entropy according to the present invention. DETAILED DESCRIPTION
[0053] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention is further described in detail below through specific implementations and in conjunction with the accompanying drawings.
[0054] like Figure 1 As shown, a smart contract vulnerability detection method based on multimodal fusion and information entropy includes the following steps:
[0055] Step 1: Perform denoising, word segmentation, and length consistency processing on the smart contract source code.
[0056] Specifically, step 1 includes the following steps:
[0057] Step 1.1: Delete blank lines and comments, as well as extra spaces.
[0058] Step 1.2: Use the 'Jieba' tool to segment the smart contract source code and obtain the source code token sequence.
[0059] Step 1.3: Set the fixed length of the source code token sequence. If the source code token sequence length is greater than the fixed length, truncation is performed. Specifically, the excess source code tokens are deleted. If the source code token sequence length is less than the fixed length, padding is performed. Specifically, the ' <pad>' characters to pad the source code token sequence so that its length equals the fixed length.
[0060] Step 2: Parse the smart contract source code into bytecode and perform denoising, segmentation, and length consistency processing on it.
[0061] Specifically, step 2 includes the following steps:
[0062] Step 2.1: Parse the smart contract source code into bytecode using the py-solc package.
[0063] Step 2.2: The bytecode consists of deployment code, running code, and auxdata code. After the deployment code is run, the running code and auxdata code are stored on the blockchain. The deployment code is not stored on the blockchain. The auxdata code contains some auxiliary information of the smart contract, such as compiled version information, etc. This information is irrelevant to the vulnerability characteristics. Therefore, the deployment code and auxdata code are deleted, and only the running code is retained.
[0064] Step 2.3: Use regular expressions to segment the running code into groups of two bytecodes to obtain a bytecode token sequence.
[0065] Step 2.4: Set the fixed length of the bytecode token sequence. If the length of the bytecode token sequence is greater than the fixed length, truncation is performed. Specifically, the excess bytecode tokens are deleted. If the length of the bytecode token sequence is less than the fixed length, padding is performed. Specifically, the ' <pad>' characters pad the bytecode token sequence to make its length equal to the fixed length.
[0066] Step 3: Parse the smart contract source code into opcodes and perform denoising and length consistency processing on them.
[0067] Specifically, step 3 includes the following steps:
[0068] Step 3.1: Parse the smart contract source code into opcodes using the py-solc package.
[0069] Step 3.2: The parameters after the PUSH instruction in the opcode are irrelevant to the vulnerability features. Delete all the parameters after the PUSH instruction in the opcode to obtain the denoised opcode.
[0070] Step 3.3: Simplify the denoised opcodes by replacing 'PUSH1-PUSH32' with 'PUSH', 'DUP' with 'DUP1-DUP16', 'SWAP' with 'SWAP1-SWAP16', and 'LOG' with 'LOG1-LOG4' to obtain the denoised opcode token sequence.
[0071] Step 3.4: Set the fixed length of the opcode token sequence. If the opcode token sequence length is greater than the fixed length, perform truncation. Specifically, delete the excess opcode tokens. If the opcode token sequence length is less than the fixed length, perform padding. Specifically, use ' <pad>' character pads the opcode token sequence to make its length equal to the fixed length.
[0072] Step 4: Map each token in the source code token sequence, bytecode token sequence and operation code token sequence into a digital vector through code embedding technology to obtain a source code token vector sequence, a bytecode token vector sequence and an operation code token vector sequence. The code embedding technology refers to mapping the code into a digital vector.
[0073] Specifically, step 4 includes the following steps:
[0074] Step 4.1: Use the Word2Vec tool to map each token in the smart contract source code token sequence to a 256-dimensional digital vector. That is, code = [C 1 ,C 2 ,C 3 …C n ] is mapped to code_vector = [V 1 ,V 2 ,V 3 …V n ]. Among them, C n represents the nth token in the source code token sequence, V n Represents the 256-dimensional numeric vector corresponding to the n-th token.
[0075] Step 4.2: Use the Word2Vec tool to map each token in the smart contract bytecode token sequence to a 256-dimensional digital vector. That is, bytecode = [B 1 ,B 2 ,B 3 …B n ] is mapped to bytecode_vector = [V 1 ,V 2 ,V 3 …V n ]. Among them, B n Represents the nth token in the bytecode token sequence, V n Represents the 256-dimensional numeric vector corresponding to the n-th token.
[0076] Step 4.3: Use the Word2Vec tool to map each token in the smart contract opcode token sequence to a 256-dimensional digital vector. That is, opcode = [O 1 ,O 2 ,O 3 …O n ] is mapped to opcode_vector = [V 1 ,V 2 ,V 3 …V n ]. Among them, O n Represents the nth token in the opcode token sequence, V n Represents the 256-dimensional numeric vector corresponding to the n-th token.
[0077] Step 5: Smart contract source code vectorization, smart contract bytecode vectorization, and smart contract opcode vectorization.
[0078] Specifically, step 5 includes the following steps:
[0079] Step 5.1: The token vector sequence corresponding to the smart contract source code token sequence is used as the input of the recurrent neural network (RNN), and a 64-dimensional digital vector is output, which represents the smart contract source code vector.
[0080] Step 5.2: The token vector sequence corresponding to the smart contract bytecode token sequence is used as the input of the gated recurrent neural network (Gated Recurrent Unit, GRU) model, and a 64-dimensional digital vector is output, which represents the smart contract bytecode vector.
[0081] Step 5.3: The token vector sequence corresponding to the smart contract opcode token sequence is used as the input of the Long Short-Term Memory (LSTM) neural network, which outputs a 64-dimensional digital vector representing the smart contract opcode vector.
[0082] Step 6: Calculate the average information entropy of the smart contract source code, bytecode, and opcode. The amount of information contained in the information.
[0083] Specifically, step 6 includes the following steps:
[0084] Step 6.1: Calculate the average information entropy of the smart contract source code based on formula (1), where T represents the length of the smart contract source code token sequence, t represents the token in the smart contract source code token sequence, and p(t) represents the probability of token t appearing in the source code token sequence.
[0085] Step 6.2: Calculate the average information entropy of the smart contract bytecode based on formula (1), where T represents the length of the smart contract bytecode token sequence, t represents the token in the smart contract bytecode token sequence, and p(t) represents the probability of token t appearing in the bytecode token sequence.
[0086] Step 6.3: Calculate the average information entropy of the smart contract opcode based on formula (1), where T represents the length of the smart contract opcode token sequence, t represents the token in the smart contract opcode sequence, and p(t) represents the probability of token t appearing in the opcode token sequence.
[0087]
[0088] Step 7: Weighted vector fusion of smart contract source code, bytecode and opcode.
[0089] Specifically, step 7 includes the following steps:
[0090] Step 7.1: Multiply the smart contract source code vector and the average information entropy of the smart contract source code to obtain the source code weighted vector. The dimension of the source code weighted vector is 64.
[0091] Step 7.2: Multiply the smart contract bytecode vector and the average information entropy of the smart contract bytecode to obtain the bytecode weighted vector. The dimension of the bytecode weighted vector is 64.
[0092] Step 7.3: Multiply the smart contract opcode vector and the average information entropy of the smart contract opcode to obtain the opcode weighted vector. The opcode weighted vector dimension is 64.
[0093] Step 7.4: Horizontally concatenate the source code weighted vector, bytecode weighted vector, and opcode weighted vector to obtain a weighted fusion vector. The dimension of the weighted fusion vector is 192.
[0094] Step 8: Learn the latent features of the weighted fusion vector through artificial neural network and perform vulnerability detection on it.
[0095] Specifically, step 8 includes the following steps:
[0096] Step 8.1: Perform BatchNormalization and Dropout processing on the weighted fusion vector.
[0097] Step 8.2: Use the Dense layer to reduce the dimension of the processed weighted fusion vector. The dimension of the vector after dimensionality reduction is 64.
[0098] Step 8.3: Perform BatchNormalization and Dropout processing on the 64-dimensional vector.
[0099] Step 8.4: Use the Dense layer to reduce the dimension of the 64-dimensional vector. The dimension of the vector after dimensionality reduction is 1. This value indicates the probability of vulnerability risk in the smart contract.
[0100] Below, a specific experiment is used to illustrate the smart contract vulnerability detection method based on multimodal fusion and information entropy provided by the present invention.
[0101] Experimental hardware configuration: The experiment was conducted on a server computing node with a CentOS operating system, and the computing node was equipped with Intel Xeon E5-2620 v4 processors. The processor model was 2.40GHz, 6 cores, and the memory was 64GiB.
[0102] Experimental software configuration: The code is written in Python 3.7.3, and the third-party libraries used in the process include Keras2.11.0, Numpy 1.21.6, etc.
[0103] Experimental data:
[0104]
[0105] Among them, document 1 refers to the algorithm proposed by Duy et al. (Duy PT, Khoa NH, Quyen NH, et al (2023) Vulnsense: Efficient vulnerability detection in ethereum smart contracts by multimodal learning with graph neural network and language model. arXiv preprint arXiv: 230908474.).
[0106] The above is one of the embodiments of the present invention, and the present invention should not be limited to the contents disclosed in the embodiment and the drawings. Any equivalent or modification completed without departing from the spirit disclosed in the present invention shall fall within the scope of protection of the present invention.< / pad> < / pad> < / pad>
Claims
1. A smart contract vulnerability detection method based on multimodal fusion and information entropy, characterized in that: The method comprises: Parse the source code of the smart contract, obtain bytecode and operation code, and generate source code token sequence, bytecode token sequence and operation code token sequence; Vectorizing the source code token sequence, the bytecode token sequence and the operation code token sequence, and performing weighted vector fusion based on information entropy of the source code, bytecode and operation code; Based on the weighted fusion vector, the vulnerability detection results of the smart contract are generated.
2. The method according to claim 1, characterized in that The generating source code token sequence comprises: Performing a first denoising process on the source code, the first denoising process comprising: deleting blank lines, comments, and redundant spaces in the smart contract source code; The Jieba tool is used to perform word segmentation processing on the source code after the first denoising processing to obtain an original source code token sequence; based on a fixed length, the original source code token sequence is subjected to length consistency processing to obtain a source code token sequence.
3. The method according to claim 1, characterized in that The generating of the bytecode token sequence comprises: Performing a second denoising process on the bytecode, the second denoising process comprising: deleting deployment code and auxdata code in the bytecode; For the bytecode after the second denoising process, every two bytecodes are grouped together and segmented using a regular expression to obtain the original bytecode token sequence; Based on a fixed length, length consistency processing is performed on the original bytecode token sequence to obtain a bytecode token sequence.
4. The method according to claim 1, characterized in that The generating of the operation code token sequence comprises: Performing a third denoising process on the operation code, the third denoising process comprising: deleting parameters after the PUSH instruction; Generate an original opcode sequence by replacing PUSH instructions with PUSH1-PUSH32 instructions, replacing DUP instructions with DUP1-DUP16 instructions, replacing SWAP instructions with SWAP1-SWAP16 instructions, and replacing LOG instructions with LOG1-LOG4 instructions; Based on a fixed length, length consistency processing is performed on the original operation code token sequence to obtain an operation code token sequence.
5. The method according to claim 1, characterized in that Vectorizing the source code token sequence, the bytecode token sequence, and the operation code token sequence, and performing weighted vector fusion in combination with information entropy of the source code, bytecode, and operation code, including: Use the Word2Vec tool to map each token in the source code token sequence, the bytecode token sequence, and the opcode token sequence into a vector, respectively, to obtain a source code token vector sequence, a bytecode token vector sequence, and an opcode token vector sequence; Using the source code token vector sequence as an input of a recurrent neural network to obtain a source code vector representation; Using the bytecode token vector sequence as an input of a gated recurrent neural network to obtain a bytecode vector representation; Using the opcode token vector sequence as an input of a long short-term memory neural network to obtain an opcode vector representation; Calculate the average information entropy of source code, bytecode and opcode respectively; Based on the average information entropy of the source code, the bytecode and the operation code, weighted vector fusion is performed on the source code vector representation, the bytecode vector representation and the operation code vector representation to obtain a weighted fusion vector.
6. The method according to claim 5, characterized in that The calculating the average information entropy of the source code includes: Obtaining the length T of a source code token sequence and the probability p(t) that each token t in the source code token sequence appears in the source code token sequence; Based on the length T of the source code token sequence and the probability p(t), the average information entropy of the source code is obtained.
7. The method according to claim 1, characterized in that The method of generating the vulnerability detection result of the smart contract based on the weighted fusion vector includes: Perform BatchNormalization and Dropout processing on the weighted fusion vector V to obtain the weighted fusion vector V ′ ; Use the Dense layer to weight the fusion vector V ′ Perform dimensionality reduction processing to obtain the weighted fusion vector V″; Perform BatchNormalization and Dropout processing on the weighted fusion vector V″ to obtain a weighted fusion vector V″′; The Dense layer is used to reduce the dimension of the weighted fusion vector V″′ to obtain the probability that the smart contract has a vulnerability risk.
8. A smart contract vulnerability detection system based on multimodal fusion and information entropy, characterized in that: The system comprises: The parsing module is used to parse the source code of the smart contract, obtain the bytecode and operation code, and generate the source code token sequence, the bytecode token sequence and the operation code token sequence; A mapping module, used for vectorizing the source code token sequence, the bytecode token sequence and the operation code token sequence, and performing weighted vector fusion in combination with information entropy of the source code, bytecode and operation code; The detection module is used to generate vulnerability detection results of smart contracts based on weighted fusion vectors.
9. An electronic device, characterized in that: The electronic device includes: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, it implements the smart contract vulnerability detection method based on multimodal fusion and information entropy as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer program instructions, and when the computer program instructions are executed by the processor, the method for detecting smart contract vulnerabilities based on multimodal fusion and information entropy as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Mobile platform malicious software detection method and device based on multi-modal information fusion
CN116226852A
Method and system for detecting weld defect of small connecting pipe based on multi-modal fusion
CN118503773A
Multi-modal learning-based few-sample intelligent contract vulnerability detection method
CN118734312A
Intelligent contract vulnerability detection method and system based on multiple modes
CN118940272A
Malicious code detection method, device and equipment based on multi-modal feature fusion
CN120470585A