Attack contract detection method based on semantics and large model uncertainty
Patent Information
- Application Number
- CN202610760066.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-29
- Publication Date
- 2026-10-27
AI Technical Summary
然而直接将大模型应用于攻击合约检测的过程中,仍面临三大技术瓶颈:首先,低级中间表示的理解能力有限
[0039]有益效果:本发明的基于语义与大模型不确定性的攻击合约检测方法通过构建基于语义增强与大语言模型不确定性感知的攻击合约检测框架,实现了从智能合约字节码到高层行为语义表示的自动转换,并结合资金流可达性分析识别潜在攻击路径,从而能够在无需大量训练数据的情况下识别复杂的对抗性攻击行为,有利于提高区块链系统的安全检测能力和防御效率。
Smart Images

Figure CN122993977A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of attack contract detection technology, and relates to an attack contract detection method based on semantics and large model uncertainty. Background Technology
[0002] Blockchain provides a decentralized and verifiable infrastructure for decentralized finance (DeFi), with smart contracts as a core component responsible for defining asset ownership and automatically executing transaction logic. Based on this architecture, DeFi can offer permissionless, composable financial services. However, the openness and anonymity of blockchain also allow attackers to deploy malicious contracts and commit fraud or attacks. In 2024, losses due to blockchain security incidents exceeded $2.01 billion.
[0003] Vulnerability detection is a traditional means of ensuring blockchain security, encompassing static analysis, symbolic execution, fuzzing, and machine learning-based methods. Recently, large language models (hereinafter referred to as large models) have also been introduced into automated vulnerability identification due to their powerful code understanding capabilities. However, existing methods still have the following core limitations in practical applications: (1) Extremely low actual exploitability: Many detected vulnerabilities are not exploitable in practice. Studies have shown that only 2.68% of contracts with reentrancy defects are exploitable, and only 3% of contracts hold non-zero balances. This results in a large amount of resources being occupied by code that poses no real threat. (2) High false positive rate and semantic gaps: Existing tools mostly rely on coarse-grained detection rules. Due to the lack of deep semantic understanding capabilities, they often misjudge secure contracts that adopt complex defense patterns (such as mutexes, check-influence-interaction patterns) as vulnerable contracts. (3) Defense asymmetry: Defenders must defend against all potential threats, while attackers only need to successfully exploit one vulnerability. This asymmetry highlights the urgency of developing proactive detection technologies that focus on real attack behaviors rather than theoretical defects.
[0004] Another type of work focuses on attack detection based on transaction behavior, primarily by monitoring unconfirmed transactions in a public mempool and combining this with heuristic rules or machine learning models for real-time identification. However, attackers can exploit private mempools to submit malicious transactions directly to miners or validators, thereby bypassing the public pool and evading oversight. Furthermore, reliance on fixed patterns or pre-trained models limits its ability to detect novel or evolving attacks.
[0005] Recent research has begun to focus on the detection of adversarial smart contracts. Adversarial smart contracts are smart contracts deployed by attackers specifically designed to exploit system vulnerabilities or manipulate protocol logic for illicit gains. This type of research focuses on contracts that are both exploitable and likely to be deployed in real-world attacks, thereby identifying potential threats before the attack is executed. Existing methods can be broadly categorized into two types. One type is rule-based detection methods, which identify contracts with attack characteristics by constructing static analysis rules and combining them with program structure information (such as function call relationships or asset flow relationships). These methods are typically designed for specific attack scenarios, such as identifying contracts that can trigger reentrancy attacks or implement price manipulation, thus offering advantages in detection efficiency, but their applicability is often limited to specific types of attacks.
[0006] Another type is the machine learning-based detection method. This method extracts features from contract bytecode or program structure and uses a learning model to classify contracts, thereby automatically identifying different types of attack contracts. These methods typically construct a unified contract representation and improve the detection capability against various attack patterns by training the model. While these methods improve the ability to identify attack contracts to some extent, they still have several limitations. First, these methods usually rely on labeled training data, and their detection capability is often limited when new attack types emerge. Second, many methods are mainly based on low-level opcode features or static code features, making it difficult to accurately capture the high-level semantic intent behind the attack behavior. Finally, the interpretability of these models is relatively weak. In high-risk scenarios such as blockchain security, security analysts often need to understand the basis for the model's judgments; a lack of interpretability reduces the model's usability in practical security analysis.
[0007] In summary, existing methods still have significant shortcomings in terms of practical usability, generalization ability, and high-level semantic understanding. Defenders struggle to effectively identify potential attacks before contract deployment and have limited detection capabilities when facing new or evolving attacks.
[0008] Large-scale models have shown great potential in understanding high-level code representations (such as source code) and demonstrated strong capabilities in capturing behavioral semantics and interpreting smart contract logic. However, directly applying large-scale models to attack contract detection still faces three major technical bottlenecks: First, their ability to understand low-level intermediate representations is limited. Existing large-scale models are mainly pre-trained in high-level languages, resulting in a weak understanding of low-level representations such as EVM bytecode. Furthermore, the decompiled three-address code (TAC) sequences are lengthy (approximately 87.02% of the sequences exceed the model context limits), and inputting long sequences in full can easily cause the model to lose global semantic focus, making it difficult to capture the overall behavioral intent of the contract. Second, the diversification and evolution of attack strategies pose challenges to generalization. Traditional methods based on fixed features struggle to identify zero-day attacks. Empirical analysis of a large number of samples reveals that the profit logic of attack contracts can essentially be abstracted into a three-stage lifecycle of "initial preparation, vulnerability exploitation, and fund withdrawal," such as... Figure 1 As shown, capturing malicious evidence chains centered on fund flow trajectories is key to improving the universality of detection. Finally, model illusions and uncertainty metrics limit the reliability of detection. Large models exhibit conservative reasoning biases when handling fuzzy patterns such as redundant access control, easily generating illusions and leading to high false positive rates. Furthermore, due to their black-box nature, they lack a quantitative evaluation mechanism for the confidence level of detection results. Therefore, introducing deterministic scoring mechanisms such as information entropy to transform model output into graded confidence signals is a necessary path to reduce false positives and improve usability in high-risk blockchain scenarios.
[0009] Therefore, a new attack contract detection method based on semantics and large model uncertainty is needed to solve the above problems. Summary of the Invention
[0010] The purpose of this invention is to provide an attack contract detection method based on semantics and large model uncertainty, so as to solve the problems of the prior art.
[0011] The technical solution of the present invention is as follows:
[0012] An attack contract detection method based on semantics and large model uncertainty includes the following steps:
[0013] 1) High-level behavioral semantic analysis phase, including the following steps:
[0014] Step 1.1: Elevate the EVM bytecode of the contract to a semantic unit, and convert the semantic unit into a contract-level natural language description based on the template;
[0015] Step 1.2: Utilize a large language model to perform general purpose analysis and attack characteristic analysis on the natural language description obtained in Step 1.1. The general purpose analysis is used to understand the contract's intent and core functions from a holistic perspective. The attack characteristic analysis includes function summary analysis and fund flow reachability analysis.
[0016] The function summary analysis is used to analyze the intent, suspicious behavior and related evidence of each function in the contract; the fund flow reachability analysis is used to track fund-related operations and potential exploitation paths controlled by attackers.
[0017] 2) The uncertainty quantification stage based on detection and fusion includes the following steps:
[0018] Step 2.1: Expand the binary classification task into a four-level labeling system, expressed by the following formula:
[0019]
[0020] Six types of prompts were designed to detect the confidence level of the model from different perspectives, as expressed by the following formula:
[0021]
[0022] ;
[0023] By prompting the large language model on the tag set The four labels are sorted by probability and assigned a total confidence score of 100%, expressed by the following formula: In the formula, For a set of tags, including , , , Four categories of labels are used to characterize the aggressiveness, suspiciousness, uncertainty, and normality of smart contracts, respectively. The set of prompts includes the following six categories: Indicates general purpose information, Indicates an attack on specific information. This indicates an adversarial misleading suggestion based on general purpose information. This indicates a normal misleading message based on general purpose information. This indicates an adversarial misleading message based on attacking specific information. This indicates a normal, misleading message based on attacking specific information. Indicates a prompt Next tag Confidence ranking For prompts Next tag The confidence scores are summed to 100%.
[0024] Step 2.2: Calculate the binary label score using the following formula. and ,if If the condition is met, the current contract is considered an attack contract; otherwise, it is considered a normal contract.
[0025] , In the formula, and Scores for aggressive tendencies and benign tendencies. For a normalized probability distribution, Weighted score for each label, For prompts Next tag The score, For prompts The weight, For prompts Output the entropy of the probability distribution. For prompts Next tag The confidence scores are summed to 100%.
[0026] Furthermore, the general purpose analysis specifically involves using a large language model to synthesize information from all functions and generate a contract-level overall purpose analysis summary.
[0027] Furthermore, the general purpose analysis extracts contract-level metrics from the natural language description using a rule-based approach. These contract-level metrics include the number and proportion of external calls, the number of unknown functions, and whether there is any fund transfer behavior.
[0028] Furthermore, the general purpose analysis includes contract summary analysis and contract behavior indicator analysis.
[0029] Furthermore, the accessibility analysis of cash flow includes the following steps:
[0030] Step 1.2.1, Segmentation: The contract-level natural language description is segmented. Based on the function identification information, the overall description is divided into several function-level descriptions to obtain the function-level natural language description.
[0031] Step 1.2.2, Contract Forest Construction: Construct a control dependency structure for each function-level natural language description, representing it as a function dependency tree, where the function node is the root node, and the statements under it are organized into different types of nodes according to semantic relationships, with each sentence being regarded as a node;
[0032] The entire contract is represented as a control dependency forest consisting of multiple functional dependency trees;
[0033] Step 1.2.3, Behavioral Node Entity Resolution: Refine each node in the functional dependency tree into composite entities with different semantic roles, and generate propagation tuples: ,in, Represents the set of input source entities. Represents the target entity to be output;
[0034] Step 1.2.4, Graph Transformation: Perform a depth-first search traversal on each dependency tree, and when visiting a behavior node... At that time, extract the source entity set. and target entity If the source node If an edge has not yet been visited, an edge with conditional annotations is created, and the conditions on the path accumulate along the traversal path: ,in For the conditions on this path, the target node is then marked as visited; each node, except the root node, represents an entity, and the condition nodes are encoded as edge annotations.
[0035] Step 1.2.5, Entry Anchor Point Identification and Exit Anchor Point Identification: Entry anchor point identification identifies data that is controllable by the attacker or has valuable triggering attributes as entry anchor points, thus obtaining a set of entry points. Export anchor point identification identifies operations involving key financial changes or changes in permissions as export anchor points, resulting in a set of export points. ; Step 1.2.6, Accessibility Analysis of Fund Flow: Forward Propagation: Based on the set of entry points Starting from the entry anchor point, a depth-first search is performed. Whenever a new variable is encountered, it is added to the reachable set until no new nodes are found. After the forward propagation is complete, the node exits from the exit set. Starting from the exit anchor point, a reverse depth-first traversal search is performed. Through bidirectional path verification, only valid semantic paths triggered by the entry anchor point and ending at the exit anchor point are retained. A set of variable-level dependency chains is obtained, each chain with its condition constraints, which are expressed in natural language.
[0036] Furthermore, the semantic roles in step 1.2.3 include operations, conditions, and variables.
[0037] Furthermore, the entry anchor includes the function caller, the amount of attached tokens, the transaction initiator, the current contract balance, and function parameters; the exit anchor includes standard business logic behavior and underlying sensitive behavior. The standard business logic behavior includes ERC-20 asset transfer, authorization control, and deposit and withdrawal operations in DeFi protocols, while the underlying sensitive behavior includes arbitrary code execution and contract destruction.
[0038] Furthermore, the large language model is DeepSeek-V3.
[0039] Beneficial effects: The attack contract detection method based on semantics and large model uncertainty of the present invention realizes the automatic conversion from smart contract bytecode to high-level behavioral semantic representation by constructing an attack contract detection framework based on semantic enhancement and large language model uncertainty perception. Combined with fund flow reachability analysis, it identifies potential attack paths, thereby enabling the identification of complex adversarial attack behaviors without the need for a large amount of training data, which is conducive to improving the security detection capability and defense efficiency of blockchain systems. Attached Figure Description
[0040] Figure 1 This is a summary of the three-stage lifecycle of adversarial attacks through case studies;
[0041] Figure 2 This is an overall flowchart of an attack contract detection method based on semantics and large model uncertainty.
[0042] Figure 3 This is a flowchart of the accessibility analysis of cash flow in Phase 1. Detailed Implementation
[0043] 1. Overall Design
[0044] This invention proposes a general detection framework leveraging the powerful semantic understanding capabilities of large-scale models, aiming to directly identify attack contracts from EVM bytecode. Its core motivation lies in the fact that adversarial intent is essentially manifested through the active control flow and fund flow behaviors encoded in the contract logic. Therefore, the method described in this invention can overcome the limitations of traditional rules and achieve generalized detection of unknown attack patterns. Figure 2 As shown, the present invention consists of two main stages: a high-level behavioral semantic analysis stage one and an uncertainty quantification stage two based on detection and fusion.
[0045] In Phase One, the original EVM bytecode is first elevated to semantic units. These units are then converted into semi-structured natural language descriptions using template-based rules and used as input for subsequent design modules. This alleviates the problem of the large model's limited ability to understand low-level intermediate representations. Subsequently, this invention performs general-purpose analysis based on the large model to understand the overall purpose of the contract and extract key semantic information. Simultaneously, this invention also performs attack characteristic analysis, analyzing the intent, potential suspicious behaviors, and corresponding evidence of each function within the contract.
[0046] In attack characteristic analysis, to further capture diverse and constantly evolving attack strategies, this invention is also based on... Figure 1The three-stage attack pattern shown is used to perform fund flow reachability analysis to track the fund-related operations and potential exploitation paths controlled by the attacker.
[0047] In Phase Two, in response to the inherent illusion tendency and the difficulty in quantifying decision confidence in current mainstream large models (especially high-performance closed-source black-box models), this invention proposes a detection and fusion-based method to extend traditional binary classification detection into fine-grained risk assessment.
[0048] By constructing normal detection and misleading detection, and combining them with an entropy-based fusion mechanism, this invention can quantify, sort, and effectively fuse the uncertainties of large models, thereby achieving more reliable detection.
[0049] This invention will first introduce a summary of functions for natural language description, general purpose analysis, and attack characteristic analysis. The fund flow reachability analysis submodule in attack characteristic analysis, and the uncertainty quantification module based on detection and fusion in Phase Two, will be described in detail in the next section.
[0050] (1) Improved Natural Language Description. This invention first performs structured parsing of the EVM bytecode of smart contracts, converting it into a more easily analyzable intermediate representation to explicitly express the control relationships and data dependencies of the contract. However, such intermediate representations are often verbose and difficult to directly input into large language models for effective analysis. Therefore, this invention further abstracts contract behavior, dividing complex code logic into several semantic units with clear triggering conditions and execution behaviors, and organizing them in a structured form. Subsequently, through predefined transformation rules (i.e., templates), these semantic units are mapped to pseudocode descriptions close to natural language, thus forming a more compact and semantically clear contract representation. Through this approach, the representation complexity can be significantly reduced while preserving the semantics of key behaviors, thereby improving the ability of large language models to understand smart contract logic.
[0051] (2) General Purpose Analysis. General purpose analysis first understands the contract's intent and core functions at a holistic level. This high-level perspective allows for a more effective explanation of the contract's design goals and an assessment of its potential security risks. Specifically, this invention utilizes a large model to synthesize information from all functions, generating a holistic purpose analysis summary at the contract level. Furthermore, this invention extracts several contract-level indicators from the natural language description using a rule-based approach, including the number and proportion of external calls, the number of unknown functions, and whether there are any fund transfer activities (e.g., attempts to withdraw contract balances). These indicators help to understand the contract's characteristics at the overall structural level and assess its potential security impact.
[0052] (3) Attack Characteristic Analysis. The core component of attack characteristic analysis is fund flow reachability analysis, used to track how attacker-controlled input propagates to sensitive or high-risk operations. Due to its importance, this invention will be described in detail in the next subsection. In addition to fund flow analysis, this invention also performs fine-grained function-level behavioral analysis, guiding the large model to analyze the intended use, suspicious behavior, and related evidence of each function one by one, revealing potential malicious logic that may be overlooked in the overall analysis. Typically, benign contracts consist of modular functions with clear objectives and reasonable access control; while attack contracts often embed sensitive operations or privilege escalation logic in seemingly normal functions. Therefore, accurately identifying the intent of each function is crucial for distinguishing malicious behavior from normal functionality. Furthermore, by detecting suspicious patterns (such as insecure external calls or attacker-controlled input) and extracting specific evidence, key attack clues can be preserved.
[0053] 2. Cash Flow Accessibility Analysis
[0054] This invention is based on Figure 1 The module illustrates the three-stage lifecycle of an adversarial attack, analyzing potential attack behaviors in smart contracts from the perspective of fund flow. Attacked contracts typically aim to obtain illicit economic gains, thus manipulable fund transfer paths often exist during contract execution. In contrast, the fund flow of normal contracts is usually subject to explicit permission controls or conditional restrictions, such as allowing only specific roles to withdraw funds or executing transfers only when specific conditions are met. Therefore, analyzing the fund flow paths within a contract can effectively identify potential abnormal fund flow patterns.
[0055] This invention first uses the natural language semantic representation obtained in the preceding steps to perform structured modeling of the contract logic and constructs a control dependency forest, thereby transforming complex contract logic into an analyzable structured representation. Based on this, starting from the input locations that an attacker might control, reachability analysis is performed on fund-related operations to identify execution paths that may lead to abnormal fund outflows. The overall process of fund flow reachability analysis is as follows: Figure 3 As shown, the main steps include the following six steps: ① Blocking; ② Contract forest construction; ③ Behavioral node entity parsing; ④ Graph transformation and entry / exit identification; ⑤ Fund flow reachability analysis; ⑥ Reverse pruning and result output.
[0056] (1) Controlling Dependency Forest Construction
[0057] During the control dependency forest construction phase, such as Figure 3 As shown in sections (a)-(d), the present invention first performs structuring processing on the semantic description of the contract and constructs a control dependency forest that can represent the functional logic and behavioral dependencies.
[0058] First, in step ①, the contract-level natural language description is segmented. Specifically, the overall description is divided into several function-level descriptions based on function identifier information, enabling the logic of each function to be represented and analyzed independently.
[0059] Subsequently, in step ❷, contract forest construction, a control dependency structure is built for each function-level description, representing it as a control dependency tree. The function node serves as the root node, and the statements under it are organized into different types of nodes based on semantic relationships. Each sentence (e.g., behavior nodes representing actions and condition nodes representing control logic) is considered a node. By organizing the dependencies between statements within a function, the execution logic and control structure within the function can be explicitly represented. After repeating this process for all functions, the entire smart contract is represented as a control dependency forest composed of multiple function dependency trees, such as... Figure 3 As shown in (c).
[0060] Building upon the established control dependencies, this invention further performs deep entity parsing on behavior nodes to extract key entities and data interaction information related to specific operations. This step refines each node in the function tree into a composite entity containing semantic roles such as operations, conditions, and variables, and generates propagation tuples: .in Represents the set of input source entities. This represents the target entity of the output. This solution automatically identifies key entities from semantic units through predefined rules and establishes input-output mapping relationships between entities, thereby accurately depicting the data flow path between different functional modules. This process achieves fine-grained feature representation of contract behavior, laying a crucial data foundation for subsequent analysis of fund flows throughout the entire attack lifecycle.
[0061] (2) Graph transformation
[0062] Figure 3 (d)-(e) illustrate the graph transformation process. In this process, the invention transforms the control dependency forest into a structure suitable for fund flow reachability analysis, and performs a depth-first traversal on each function tree, maintaining the set of visited entities and their condition sets. Specifically, when a node is accessed... At that time, extract the source entity set. and target entity If the source node If an edge has not yet been visited, an edge with conditional annotations is created, and the conditions on the path accumulate along the traversal path: The target node is then marked as visited. To ensure cross-function consistency, this invention adds a function name prefix to local variables to achieve scope isolation, and establishes cross-function edges to interconnect entities with the same normalized name in different functions (such as global variables). Figure 3(e) Bidirectional dashed edges). In this way, each node (except the root node) represents an entity, and condition nodes are encoded as edge annotations. Through this transformation process, this invention maps discrete functional logic into a unified global reachability graph, where nodes represent variables or operations, and edges encode logical decision conditions, thereby achieving a complete characterization of the contract's fund flow path and providing topological support for subsequent identification of complex adversarial profit-making behaviors. Each node (except the root node) represents an entity, and condition nodes are encoded as edge annotations.
[0063] (3) Identification of entrance and exit nodes
[0064] During the entry and exit identification phase ( Figure 3 (e) This invention identifies key input / output points in contracts related to access control after fund transfers. The specific process is as follows:
[0065] Entry point anchor identification: This invention identifies data that is controllable by the attacker or has value-triggered attributes as entry points. These entry points include, but are not limited to: function caller (msg.sender), attached token quantity (msg.value), transaction initiator (tx.origin), and the current contract balance (address(this).balance); simultaneously, this invention treats function parameters as contextual entry points to capture user input that affects the internal logic of the contract. Semantically, entry point anchors correspond to the preparation phase of an attack, i.e., the attacker constructs the subsequent exploitation environment by injecting funds, triggering flash loans, or manipulating input.
[0066] Exit Anchor Identification: This invention identifies operations involving critical financial changes or permission alterations as exit points, encompassing both standard business logic and underlying sensitive behaviors. Standard behaviors include ERC-20 asset transfers (transfer, transferFrom), authorization control (approve), and deposit / withdrawal operations in DeFi protocols; underlying behaviors include arbitrary code execution (delegatecall) and contract destruction (selfdestruct). Semantically, exit anchors correspond to the exploitation phase (manipulating internal state for profit) and the exit phase (extracting or refining rewards) of an attack.
[0067] This step yields the set of entry points. Set of exit points This provides support for subsequent accessibility analysis and intent determination.
[0068] (4) Accessibility analysis
[0069] Finally, this invention performs reachability analysis ( Figure 3 (e)-(g)), with the entry set Starting from the entry anchor point, a depth-first search strategy is executed. Whenever a new variable is encountered, it is added to the reachable set until no new nodes are found. After forward propagation, this invention further extracts data from the exit set... A reverse depth-first traversal is performed starting from the exit anchor point. Through bidirectional path validation, only valid semantic paths triggered by the entry anchor point and ending at the exit anchor point are retained, thus precisely pruning irrelevant branch logic. Because... Not identified as an exit anchor point, with it as the endpoint The path was removed. Conversely, due to Identified as the exit anchor point, with it as the endpoint The path is fully preserved, and its contents are ensured. and Key conditions and constraints are not lost.
[0070] Finally, this section yields a set of variable-level dependency chains, each with its own conditional constraints. These constraints are represented in natural language, providing semantic support for subsequent reasoning based on a large language model. At the same time, no forced parsing is required during traversal, thus achieving a balance between interpretability and scalability.
[0071] 3. Uncertainty Quantification Based on Detection and Fusion
[0072] To better quantify uncertainty, this invention explores the model from a general view capturing global semantics and an attack-specific view focusing on adversarial behavior. Furthermore, this invention introduces misleading cues as controlled perturbations to examine confidence stability. Figure 3 Phase Two).
[0073] In boundary scenarios, a single direct query often misclassifies some normal contracts as attack contracts, mainly due to the tendency of large models to overconfident. In black-box scenarios, the internal confidence signal is inaccessible, making it impossible to directly calibrate the model's confidence. When a large model outputs textual confidence scores, their distribution reflects the model's uncertainty. A uniform distribution (all n labels are 1 / n) indicates high uncertainty; a highly skewed distribution (e.g., 95% and 5%) indicates strong model confidence. This aligns with the concept of entropy in information theory, where lower entropy indicates more reliable predictions. By integrating multidimensional information about contracts and combining it with entropy-based uncertainty quantification, this invention fuses various prediction results into a more stable and robust decision. All queries are executed in parallel, and the overall cost of the fusion step remains within an acceptable range of approximately $0.003 per contract.
[0074] (1) Binary tag expansion
[0075] In binary classification adversarial detection tasks, large models often exhibit overconfidence in boundary cases, especially when the contract only displays partially suspicious behavior. This overconfidence masks the model's uncertainty, leading to unreliable decisions. To capture more granular confidence information, this invention extends the binary classification task to a four-level labeling system:
[0076] .
[0077] Directly using textual confidence scores has proven unreliable; in contrast, comparative judgments based on relative ranking typically produce more consistent and credible results. Therefore, this invention prompts a large model to rank the four labels by probability and assign a total confidence score of 100%, ensuring the output is both interpretable and comparable. This structured output not only accurately quantifies the degree of susceptibility but also provides the necessary data foundation for subsequent entropy-based multi-perspective fusion.
[0078] (2) Confidence detection
[0079] In Phase One, this invention acquires general-purpose information and attack-specific information about the contract. Because general-purpose information is abstract, it often fails to capture specific behavioral patterns, potentially leading to a bias in classifying the contract as normal. Attack-specific information, on the other hand, provides detailed contextual clues related to malicious behavior, but due to its specificity, it may, in some cases, lead to a bias in classifying the contract as malicious. Therefore, combining these two perspectives is crucial for a comprehensive analysis of contract behavior. Furthermore, existing research indicates that even if the final prediction remains unchanged, misleading or weak cues can still affect the confidence level of the model's expression. For example, when a large model is highly confident that the contract is adversarial, introducing misleading information suggesting it is a normal contract has little impact; conversely, when the model itself is uncertain, cues suggesting it is adversarial may increase its confidence level. Based on this, this invention designs six types of cues to detect the model's confidence level from different perspectives, defined as:
[0080]
[0081] ,
[0082] in and These correspond to general-purpose information and attack-specific information, respectively. and This indicates both adversarial and normal misleading prompts based on general-purpose information; and This corresponds to similar misleading prompts that attack specific information. Each misleading prompt contains a soft hint, such as: Hint: I think the answer should be (A) adversarial / (D) benign.
[0083] (3) Entropy-based fusion and decision making
[0084] The confidence entropy-based fusion strategy provides a theoretical basis for aggregating multi-view outputs. Confidence entropy measures the uncertainty of a prediction; higher entropy indicates greater uncertainty in the output, while lower entropy indicates a more stable and reliable prediction, consistent with Shannon's information theory. Each viewpoint generates an ordered confidence distribution for four labels and calculates its entropy. During fusion, views with lower entropy are assigned higher weights to reflect their higher reliability. This method effectively utilizes the consistency of multiple prompts to improve classification robustness while having extremely low computational cost. Specifically, the labels and confidence distributions generated for each prompt are as follows:
[0085] (1)
[0086] in Indicates a prompt Next tag Confidence ranking The corresponding confidence scores must sum to 100%. The entropy of each prompt output is calculated as follows:
[0087] (2)
[0088] Lower entropy indicates greater confidence in the prediction, which is a good indicator. The weights are defined as follows:
[0089] (3)
[0090] Each prompt also outputs a sorted list of labels and assigns discrete scores based on the sorting, with the highest-ranking label receiving 3 points, followed by 2, 1, and 0. Let... For prompts Next tag The score is then calculated as follows:
[0091] (4)
[0092] The scores were then normalized to obtain the probability distribution:
[0093] (5)
[0094] Finally, the four-level labels are merged into two-category labels, and the following calculations are performed:
[0095] (6)
[0096] (7)
[0097] like If the condition is met, the contract is considered adversarial; otherwise, it is considered normal.
[0098] Through the above method, this invention achieves fine-grained uncertainty quantification of large model outputs and significantly improves the stability and robustness of binary classification by utilizing multi-view fusion.
[0099] This invention constructs an attack contract detection framework based on semantic enhancement and uncertainty awareness of a large language model. It achieves automatic conversion from smart contract bytecode to high-level behavioral semantic representation and, combined with fund flow reachability analysis, identifies potential attack paths. This enables the identification of complex adversarial attack behaviors without requiring extensive training data, thus improving the security detection capabilities and defense efficiency of blockchain systems. Specifically, this invention has the following significant advantages:
[0100] (1) Achieved generalized attack contract detection: This invention integrates fund flow reachability analysis and multidimensional semantic reasoning to construct a detection model capable of capturing the three core stages of attack preparation, vulnerability exploitation, and fund withdrawal. Compared with traditional rule-based methods, this invention does not rely on fixed feature signatures and can effectively identify continuously evolving new attack variants, solving the limitations of existing technologies that have poor versatility and can only target a single known attack type.
[0101] (2) Significantly improves the reliability and robustness of detection results: This invention constructs a refined four-level risk assessment quantification system by designing multiple detection prompts and a fusion mechanism based on information entropy, thus achieving effective measurement of the uncertainty of large models in black-box environments. Compared with the judgment scheme that directly uses large models, this invention can effectively suppress the "illusion" phenomenon of large models and significantly reduce the risk of false alarms caused by conservative inference of models when facing fuzzy code logic.
[0102] (3) Possesses extremely strong zero-shot generalization and anti-interference capabilities: This invention constructs a fine-tuning-free framework that does not require training on specific labeled data by analyzing the behavioral essence of contracts rather than their surface features. Compared to existing machine learning-based methods, it has the following significant advantages:
[0103] When faced with unknown attack patterns, this invention can maintain optimal detection accuracy, while the performance of existing methods will drop significantly due to a lack of training data;
[0104] In scenarios with small training data, the performance of this invention remains constant, outperforming existing methods that experience significant performance degradation as data decreases.
[0105] In the case of feature obfuscation, this invention relies solely on bytecode semantics and is unaffected by on-chain fraudulent transaction features, demonstrating strong adaptability to production environments.
[0106] (4) Extremely high operating efficiency and economy: This invention achieves an extremely low cost of only about US$0.003 per detection by using hierarchical summarization and prompt word parallelization technology. Compared with machine learning technology that requires tens of minutes or even 24 hours for model training, this invention requires no training time and can meet the urgent needs of the blockchain ecosystem for large-scale contract deployment detection.
[0107] Example 1:
[0108] To conduct a comprehensive evaluation, this invention collected and labeled 455 real attack contracts from mainstream chains such as Ethereum and BSC, and collected 20,000 benign contracts. The attack contracts and benign contracts together constitute the dataset to be tested in this invention. This invention uses Balanced Accuracy (BAC) as the core evaluation metric. At the same time, it reports multi-dimensional metrics such as False Negative Rate (FNR), False Positive Rate (FPR), and True Positive Rate (TPR) for comprehensive evaluation. This invention selects the most advanced models for detecting attack contracts directly from bytecode as the baseline: (1) Lookahead: extracts 40 handcrafted features and uses a machine learning classifier for identification. (2) Skyeye: performs semantic modeling through control flow graph slicing and feature extraction. (3) Large model direct detection: directly inputs natural language descriptions into the large model for inference to verify the necessity of enhancing semantic logic in this invention. This invention uses DeepSeek-V3 as the underlying large model inference engine by default.
[0109] To ensure fair comparison and reliable results, this invention uses the same training and testing split in all experiments. Specifically, this invention randomly selects 20% of the attacking and benign contracts from the dataset as the test set, and uses the remaining 80% for training. This process is repeated five times, and the average performance is taken as the final result. All baseline methods are trained and evaluated based on these five splits. Only in the "Unknown Mode Evaluation" experiment, due to special considerations for the attacking contract type, the evaluation settings are different. Since this invention is a training-free method, it is directly evaluated on the five test sets.
[0110] 1. Overall performance
[0111] Table 1 summarizes the detection performance of different methods. This invention achieved a highest BAC of 0.9374, outperforming Lookahead (0.8235) and Skyeye (0.9049). Lookahead and Skyeye had false negative rates (FNR) of 0.3516 and 0.1868, respectively, indicating high false negative rates in identifying attack contracts. In contrast, this invention significantly reduced the FNR to 0.0769, demonstrating its superior ability to identify attack behavior. The "Training Time (minutes)" column in Table 1 represents the total time required to train the model after completing all necessary data preparation. Specifically, Lookahead required up to 16.58 minutes, while Skyeye required approximately 1440 minutes (about 24 hours). In contrast, this invention is a training-free method, completely eliminating training time and making it significantly more efficient.
[0112] Table 1: Overall performance comparison of different methods.
[0113]
[0114] 2. Evaluation of Unknown Patterns
[0115] In this experiment, the present invention uses a subset of 200 attack contracts with normalized type labels from 455 attack contracts in the dataset, covering nine types of attack contracts. In each experimental group, one type is selected. As a test set. To ensure fairness, anything marked as containing Many types of contracts, including those mentioned above, will be excluded from the test set. If If there are more than 10 contracts, 10 are randomly selected for testing; otherwise, all contracts are used for testing to ensure representative coverage while maintaining stable evaluation across different random seeds. The training set is drawn from the remaining types, with a maximum of 100 contracts if available. This setup maintains a training-to-test ratio of approximately 100:10. This invention reports specific results for each type, as well as micro / macro average metrics, to provide a comprehensive evaluation.
[0116] Table 2 summarizes the results of the baseline model and the present invention under the evaluation of new attack types. Lookahead's micro / macro FNR surged to 0.6610 and 0.7043, causing the BAC to drop from 0.8235 to 0.6692 and 0.6476. Skyeye also showed a similar decline, with the micro / macro BAC dropping to 0.7447 and 0.7529, and the FNR rising above 0.49. In contrast, the present invention achieved the best BAC of 0.9233, with an FNR of only 0.1050, demonstrating extremely strong generalization ability to unknown attack types. For example, when testing type 5 (reentrancy attack), Lookahead detected only 1 out of 10 cases, Skyeye detected 3, while the present invention significantly outperformed both.
[0117] Table 2: Performance under different unknown attack contract types. “Micro-average” and “Macro-average” represent different aggregation methods for the nine attack categories.
[0118]
[0119] 3. Performance under obfuscated settings
[0120] Lookahead and Skyeye rely on on-chain transaction features such as Nonce, Value, GasUsed, and Verified status, which are easily manipulated through simple obfuscation. Existing research has revealed that the bytecode of verified Ethereum contracts may not match the source code, allowing potentially exploitable vulnerabilities to evade verification checks. Inspired by this, this invention simulates an attacker submitting seemingly benign source code (which does not match the actual deployed bytecode) and sets the Verified status of the attack contract to "True" to evade detection. Table 3 shows that Lookahead and Skyeye's FNR increased (from 0.3516 to 0.5956 and from 0.1868 to 0.2132), respectively, while their BAC decreased to 0.7017 and 0.8917, respectively. In contrast, because this invention does not rely on on-chain features, its performance remains unaffected.
[0121] Table 3: Performance under obfuscation settings.
[0122]
[0123] 4. Performance under trained vs. untrained setup
[0124] To evaluate the impact of training set size on method performance, this invention performs stratified sampling on the pre-partitioned training dataset and maintains a fixed training-to-test ratio for fair comparison. Table 4 shows the results under different sampling ratios. As the training data decreases from 100% to 10%, Lookahead's BAC decreases from 0.8235 to 0.7604, Skyeye's from 0.9049 to 0.8488, while this invention maintains a BAC of 0.9374, demonstrating its robustness as a training-free method. It is worth noting that Lookahead and Skyeye require 40 and 12 handcrafted features, respectively, and acquiring these features incurs significant additional costs (not included in the training time in Table 3). In contrast, this invention only requires contract bytecode as input, without any additional data preparation.
[0125] Table 4: Performance under training vs. no training settings.
[0126]
[0127] 5. Evaluation of backbone models for different large-scale models
[0128] As shown in Table 7, this invention demonstrates stable and robust detection capabilities across multiple mainstream large-scale models. Experimental results show that the balanced accuracy (BAC) of this invention on Claude 3.5 and GPT-4o reaches 0.9064 and 0.8461, respectively; by optimizing the threshold, its false negative rate (FNR) is reduced to 0.1429 and 0.1231, respectively, outperforming the baseline models by 59.36% and 64.99% in FNR. Notably, this invention achieves the best performance on DeepSeek-V3, with a BAC as high as 0.9374 and an FNR of only 0.0769. Furthermore, Claude 3.5 comprehensively surpasses all comparison baselines in both BAC and FNR, and GPT-4o also demonstrates a considerable detection level. These results prove that this invention does not depend on a specific model architecture and can maintain high-precision detection capabilities across multiple mainstream large-scale model environments, exhibiting excellent generalization ability.
[0129] Table 5: Performance under different large-scale backbone models.
[0130]
[0131] 6. Efficiency Analysis
[0132] To evaluate the feasibility of this invention in real-world deployment scenarios, the average token consumption and time cost under different models were compared. Taking the default DeepSeek-V3 model as an example, the average token usage for detecting a single contract was 17,085.48, corresponding to a cost of only $0.0030 (of which, Phase I cost $0.0018 and Phase II cost $0.0012). In contrast, the cost of detecting a single contract using Claude 3.5 was $0.0021, while the cost of GPT-4o was as high as $0.0664. Since this invention relies solely on contract bytecode as input, it enables real-time analysis during the contract deployment phase. This not only achieves very early risk perception but also significantly reduces the long-term maintenance overhead required by traditional dynamic monitoring methods.
Claims
1. An attack contract detection method based on semantics and large model uncertainty, characterized in that, Includes the following steps: 1) High-level behavioral semantic analysis phase, including the following steps: Step 1.1: Elevate the EVM bytecode of the contract to a semantic unit, and convert the semantic unit into a contract-level natural language description based on the template; Step 1.2: Utilize a large language model to perform general purpose analysis and attack characteristic analysis on the natural language description obtained in Step 1.
1. The general purpose analysis is used to understand the contract's intent and core functions from a holistic perspective. The attack characteristic analysis includes function summary analysis and fund flow reachability analysis. The function summary analysis is used to analyze the intent, suspicious behavior and related evidence of each function in the contract; the fund flow reachability analysis is used to track fund-related operations and potential exploitation paths controlled by attackers. 2) The uncertainty quantification stage based on detection and fusion includes the following steps: Step 2.1: Expand the binary classification task into a four-level labeling system, expressed by the following formula: , Six types of prompts were designed to detect the confidence level of the model from different perspectives, as expressed by the following formula: , ; By prompting the large language model on the tag set The four labels are sorted by probability and assigned a total confidence score of 100%, expressed by the following formula: In the formula, For a set of tags, including , , , Four categories of labels are used to characterize the aggressiveness, suspiciousness, uncertainty, and normality of smart contracts, respectively. The set of prompts includes the following six categories: Indicates general purpose information, Indicates an attack on specific information. This indicates an adversarial misleading suggestion based on general purpose information. This indicates a normal misleading message based on general purpose information. This indicates an adversarial misleading message based on attacking specific information. This indicates a normal, misleading message based on attacking specific information. Indicates a prompt Next tag Confidence ranking For prompts Next tag The confidence scores are summed to 100%. Step 2.2: Calculate the binary label score using the following formula. and ,if If the condition is met, the current contract is considered an attack contract; otherwise, it is considered a normal contract. , In the formula, and Scores for aggressive tendencies and benign tendencies. For a normalized probability distribution, Weighted score for each label, For prompts Next tag The score, For prompts The weight, For prompts Output the entropy of the probability distribution. For prompts Next tag The confidence scores are summed to 100%.
2. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The general purpose analysis specifically involves using a large language model to synthesize information from all functions and generate a contract-level overall purpose analysis summary.
3. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The general purpose analysis extracts contract-level metrics from the natural language description using a rule-based approach. These contract-level metrics include the number and proportion of external calls, the number of unknown functions, and whether there is any fund transfer behavior.
4. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The general purpose analysis includes contract summary analysis and contract behavior indicator analysis.
5. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The accessibility analysis of cash flow includes the following steps: Step 1.2.1, Segmentation: The contract-level natural language description is segmented. Based on the function identification information, the overall description is divided into several function-level descriptions to obtain the function-level natural language description. Step 1.2.2, Contract Forest Construction: Construct a control dependency structure for each function-level natural language description, representing it as a function dependency tree, where the function node is the root node, and the statements under it are organized into different types of nodes according to semantic relationships, with each sentence being regarded as a node; The entire contract is represented as a control dependency forest consisting of multiple functional dependency trees; Step 1.2.3, Behavioral Node Entity Resolution: Refine each node in the functional dependency tree into composite entities with different semantic roles, and generate propagation tuples: ,in, Represents the set of input source entities. Represents the target entity to be output; Step 1.2.4, Graph Transformation: Perform a depth-first search traversal on each dependency tree, and when visiting a behavior node... At that time, extract the source entity set. and target entity If the source node If an edge has not yet been visited, an edge with conditional annotations is created, and the conditions on the path accumulate along the traversal path: ,in For the conditions on this path, the target node is then marked as visited; each node, except the root node, represents an entity, and the condition nodes are encoded as edge annotations. Step 1.2.5, Entry Anchor Point Identification and Exit Anchor Point Identification: Entry anchor point identification identifies data that is controllable by the attacker or has valuable triggering attributes as entry anchor points, thus obtaining a set of entry points. Export anchor point identification identifies operations involving key financial changes or changes in permissions as export anchor points, resulting in a set of export points. ; Step 1.2.6, Accessibility Analysis of Fund Flow: Forward Propagation: Based on the set of entry points Starting from the entry anchor point, a depth-first search is performed. Whenever a new variable is encountered, it is added to the reachable set until no new nodes are found. After the forward propagation is complete, the node exits from the exit set. Starting from the exit anchor point, a reverse depth-first traversal search is performed. Through bidirectional path verification, only valid semantic paths triggered by the entry anchor point and ending at the exit anchor point are retained. A set of variable-level dependency chains is obtained, each chain with its condition constraints, which are expressed in natural language.
6. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, In step 1.2.3, the semantic roles include operation, condition, and variable.
7. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The entry anchor includes the function caller, the amount of attached tokens, the transaction initiator, the current contract balance, and the function parameters; the exit anchor includes standard business logic behaviors and underlying sensitive behaviors. The standard business logic behaviors include ERC-20 asset transfers, authorization control, and deposit and withdrawal operations in DeFi protocols. The underlying sensitive behaviors include arbitrary code execution and contract destruction.
8. The attack contract detection method based on semantics and large model uncertainty as described in claim 1, characterized in that, The large language model is DeepSeek-V3.