Lightning loan attack identification method and storage medium
By listening to blockchain events and building token traffic maps, combining smart contracts and market price models, identifying lightning loan attacks, the problem of low accuracy in the existing technology is solved and the accuracy of attack recognition is improved.
Patent Information
- Application Number
- CN202411737203.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-11-29
AI Technical Summary
The existing technology has low accuracy when identifying lightning loan attacks and is prone to misjudgment of secure transactions.
By presetting Ethereum full nodes to monitor blockchain events, building an event token traffic chart, determining whether there is a flash loan transaction, and fusion of the price model and market price model of smart contracts are estimated to estimate the probability of the transaction attack.
It improves the accuracy of flash loan attack identification, reduces misjudgment of secure transactions, and enhances the security of blockchain transactions.
Smart Images

Figure CN119599786A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of blockchain technology, and in particular to a flash loan attack identification method and storage medium. Background Art
[0002] A smart contract is a computer program that runs on the blockchain and has the characteristics of decentralization, openness, transparency and immutability based on the blockchain. In practical applications, smart contracts on the blockchain are not easily tampered with once deployed. If there are loopholes in the deployed contract, they cannot be fixed by modifying the code, making the smart contract an easy target for attack.
[0003] At present, the detection method of flash loan attacks mainly analyzes the transactions on the chain, extracts the contract address, events and other information of the transaction and uses it to establish a feature library, analyzes from many transactions whether the source of funds for the transaction is from the flash loan platform and intercepts it. This kind of scheme will misjudge many safe flash loan transactions, and the accuracy rate is not high. Therefore, how to improve the accuracy of flash loan attack identification needs to be solved urgently. Summary of the invention
[0004] The embodiments of the present application provide a flash loan attack identification method and storage medium, which improve the accuracy of flash loan attack identification.
[0005] In a first aspect, an embodiment of the present application provides a flash loan attack identification method, the method comprising:
[0006] By presetting the Ethereum full node to monitor the target blockchain, the first event log is obtained;
[0007] Construct a first event token flow graph according to the first event log; the first event token flow graph includes n account addresses in the target blockchain for receiving or forwarding tokens; n is a positive integer;
[0008] Determining whether there is a flash loan transaction in the first event log according to the first event token flow graph;
[0009] When there is a flash loan transaction in the first event log, determine the target AMM node with a flash loan attack risk among the n account addresses;
[0010] Obtain the target smart contract bytecode and historical transaction data corresponding to the target AMM node;
[0011] Determine a first price model corresponding to the target smart contract bytecode; determine a second price model corresponding to the historical transaction data; the first price model and the second price model are both mathematical expressions describing the price relationship of a preset transaction token pair; the preset transaction token pair includes a first token and a second token;
[0012] Determine a comprehensive price model according to the first price model and the second price model;
[0013] Estimate the probability that the flash loan transaction is a flash loan attack based on the comprehensive price model to obtain a target flash loan attack probability;
[0014] When the target flash loan attack probability is greater than a preset attack probability threshold, the flash loan transaction is determined to be a flash loan attack.
[0015] In a second aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for electronic data exchange, wherein the computer program enables a computer to execute some or all of the steps described in the first aspect of the embodiment of the present application.
[0016] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor, a memory, a communication interface, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the processor, and the program includes instructions for executing the steps in the first aspect of the embodiment of the present application.
[0017] In a fourth aspect, an embodiment of the present application provides a computer program product, wherein the computer program product includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to cause a computer to execute some or all of the steps described in the first aspect of the embodiment of the present application. The computer program product may be a software installation package.
[0018] It can be seen that the flash loan attack identification method provided in the embodiment of the present application obtains a comprehensive price model by integrating the first price model of the internal logic of the comprehensive price model smart contract and the second price model that reflects the actual market situation. The comprehensive price model obtained in this way can more accurately characterize the overall performance of the token price under normal and abnormal conditions, avoiding the problem of one-sided judgment due to relying solely on a single model, making the evaluation of whether the token price is abnormal more scientific and comprehensive, thereby improving the accuracy of the judgment on flash loan attacks. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the drawings required for use in the embodiments of the present application or the background technology will be described below.
[0020] Figure 1 This is a flow chart of a flash loan attack identification method provided in an embodiment of the present application;
[0021] Figure 2It is a structural schematic diagram of a first event token flow diagram provided in an embodiment of the present application;
[0022] Figure 3 It is a structural schematic diagram of a second event token flow diagram provided in an embodiment of the present application;
[0023] Figure 4 It is a structural diagram of a target variable propagation tree provided in an embodiment of the present application;
[0024] Figure 5 It is a flow chart of a control flow subgraph provided in an embodiment of the present application;
[0025] Figure 6 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0026] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0027] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0028] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0029] The electronic devices described in the embodiments of the present application may include smart phones (such as Android phones, iOS phones, Windows Phone phones, etc.), tablet computers, PDAs, laptops, video matrices, monitoring platforms, mobile Internet devices (mobile internet devices, MID) or wearable devices, etc. The above are only examples and not exhaustive, including but not limited to the above devices. Of course, the above electronic devices can also be servers, for example, cloud servers.
[0030] The following is an explanation of some professional terms involved in this application:
[0031] Blockchain: It is a distributed, tamper-proof recording technology that enables decentralized trusted transactions and information storage by linking data together in the form of blocks.
[0032] Ethereum full node: refers to a node that completely stores all data of the Ethereum blockchain (including block headers, transaction data, smart contract code, etc.). It maintains the complete ledger of the blockchain and can independently verify the validity of transactions and blocks.
[0033] Decentralized Finance (DeFi) platform: a financial platform based on blockchain technology. It realizes financial transactions and services without intermediaries through means such as smart contracts and cryptocurrencies, giving users greater autonomy and transparency.
[0034] Automatic Market Maker (AMM): is a trading protocol mechanism widely used in DeFi platforms. It automatically provides liquidity through smart contracts, replacing the way market makers manually provide liquidity in traditional financial markets.
[0035] Smart Contract: A self-executing contract clause deployed on the blockchain in the form of code. It is a computer protocol designed to disseminate, verify or execute contracts in an information-based manner. Smart contracts allow for trusted transactions without a third party, which are traceable and irreversible.
[0036] Smart contract bytecode: It is the machine-readable code form of the compiled smart contract. A smart contract is an automatically executed contract clause deployed on the blockchain in the form of code, and the bytecode is the intermediate or final form generated by the compiler after the contract code is compiled, which is used to execute in the blockchain virtual machine (such as the Ethereum virtual machine).
[0037] Flash loan attack: a malicious attack using the flash loan mechanism in DeFi platforms. Flash loans allow users to borrow funds (usually cryptocurrencies) without collateral, and only need to repay the principal and interest at the end of the same blockchain transaction (within a block). Attackers use this mechanism to obtain large amounts of funds, manipulate market prices, or exploit smart contract vulnerabilities to obtain improper benefits.
[0038] See also Figure 1 , Figure 1 : is a flow chart of a flash loan attack identification method provided in an embodiment of the present application, the method comprising:
[0039] S101. Monitor the target blockchain by presetting the Ethereum full node to obtain a first event log.
[0040] In the embodiment of the present application, the preset Ethereum full node can be preset in advance or defaulted.
[0041] In a specific embodiment, the event type to be monitored can be determined according to the application scenario of the target blockchain and the content that needs to be paid attention to. Then, the events occurring in the target blockchain are monitored by presetting the Ethereum full node to obtain a first event log. The first event log may include transaction events in one or more DeFi platforms in the target blockchain. For example, when paying attention to flash loan-related events, it is necessary to monitor smart contract events related to lending operations, such as "LoanRequested", "LoanApproved", "LoanRepaid", "TransactionSent", "TransactionReceived" and other event types. The target blockchain network is continuously monitored through the monitoring program in the preset Ethereum full node. Once a flash loan-related event occurs, the corresponding processing mechanism will be triggered. This processing mechanism can be to record the event information to a local file, send it to a remote server, or perform real-time analysis, thereby obtaining the first event log.
[0042] S102. Construct a first event token flow graph according to the first event log; the first event token flow graph includes n account addresses in the target blockchain for receiving or forwarding tokens; n is a positive integer.
[0043] In the embodiment of the present application, the first event log can be traversed, and the logs related to the token transaction can be screened out to obtain the second event log. According to the second event log, a token flow graph with the transaction address (i.e., the above-mentioned n account addresses) as a node and the transaction relationship as a directed edge is established to obtain the first event token flow graph. Generally speaking, the token transfer will trigger an event with the theme of "Transfer" on the target blockchain, and the event log of the event contains information such as the sender, the receiver, and the number of transferred tokens. In the event token flow graph established by the embodiment of the present application, the sender's address is the starting point of the directed edge, and the receiver's address is the end point of the directed edge. The flow of flash loan funds on each DeFi platform can be tracked through the first event token flow graph, and then, the flash loan attacks on the DeFi platform can be accurately identified.
[0044] For an example, see Figure 2 , Figure 2 is a schematic diagram of the structure of a first event token flow diagram provided in an embodiment of the present application, comprising Figure 2 It can be seen that node 1, as the sender, sent tokens to nodes 2 and 3 respectively; node 2, as the sender, sent tokens to nodes 4 and 6 respectively; node 4, as the sender, sent tokens to node 5; node 5, as the sender, sent tokens to node 6; node 6, as the sender, sent tokens to nodes 7 and 8 respectively; node 7, as the sender, sent tokens to node 2. Among them, nodes 1 to 8 are account addresses in the target blockchain, and each node represents an account address.
[0045] S103. Determine whether there is a flash loan transaction in the first event log according to the first event token flow graph.
[0046] In the embodiment of the present application, it is possible to determine whether there is a flash loan transaction in the first event log based on the first event token flow graph. Specifically, it is possible to identify in turn whether each node on the first event token flow graph is a flash loan node. The identification of a flash loan node is generally achieved by analyzing the historical transaction features of the account address corresponding to the node, or searching for the account address in the blockchain Internet community to locate whether it provides flash loan lending services to the outside world. If a node provides flash loan lending services to the outside world, it can be determined that the node is a flash loan node, and further, it is determined that there is a flash loan transaction in the first event log. Conversely, if each node on the first event token flow graph is not a flash loan node, it is determined that there is no flash loan transaction in the first event log, that is, the probability of a target flash loan attack is 0, and no subsequent steps are required.
[0047] S104. When there is a flash loan transaction in the first event log, determine the target AMM node with flash loan attack risk among the n account addresses.
[0048] In an embodiment of the present application, when there is a flash loan transaction in the first event log, a risk analysis can be performed on n account addresses to determine the target AMM nodes that may be at risk of flash loan attacks.
[0049] Optionally, step S104, determining the target AMM node with a flash loan attack risk among the n account addresses may include the following steps:
[0050] A1. Obtain historical transaction features of each of the n account addresses to obtain n historical transaction features;
[0051] A2. Determine the flash loan nodes and AMM nodes in the n account addresses according to the n historical transaction features, and obtain i flash loan nodes and j AMM nodes; i and j are both natural numbers less than or equal to n, and the sum of i and j is less than or equal to n;
[0052] A3. Convert the first event token flow graph into a directed acyclic graph to obtain a second event token flow graph;
[0053] A4. Initialize the first dictionary; the key in the first dictionary is the node ID in the second event token flow graph, and the value represents the node set that controls the flow of its tokens to the current node upstream of the node; during initialization, the value corresponding to the entry node of the second event token flow graph is initialized to a node set containing only its own node ID, and the values of the remaining nodes are set to the initial set V; the initial set V stores the node IDs of all nodes in the second event token flow graph;
[0054] A5. Perform node identification on the second event token flow graph according to the first dictionary to obtain the target AMM node.
[0055] In an embodiment of the present application, historical transaction characteristics may include at least one of the following: transaction frequency characteristics, transaction amount characteristics, transaction object characteristics, transaction type characteristics, etc., which are not limited here; node types include one of the following: flash loan node, AMM node, normal node, etc., which are not limited here.
[0056] In a specific embodiment, the historical transaction characteristics of each of the n account addresses can be obtained first to obtain n historical transaction characteristics. Specifically, the Ethereum full node can be preset to obtain the historical transaction data of the n account addresses to obtain n historical transaction data. Then, the features of the n historical transaction data can be extracted to obtain n feature data, that is, n historical transaction characteristics. For example, the historical transaction characteristics can be transaction frequency characteristics. The number of transactions of the node in a specific time period (such as daily, weekly, and monthly) is calculated, and the distribution of transaction frequency is analyzed, such as whether there is a peak or trough period for transactions, thereby obtaining the transaction frequency characteristics; then, the flash loan nodes and AMM nodes in the n account addresses can be determined according to the n historical transaction characteristics to obtain i flash loan nodes and j AMM nodes. Specifically, the mapping relationship between the preset historical transaction characteristics and the node types can be pre-stored, and the n node types corresponding to the n historical transaction characteristics are determined based on the mapping relationship, and the nodes that are flash loan nodes and AMM nodes among the n node types are determined to obtain i flash loan nodes and j AMM nodes.
[0057] Next, the first event token flow graph can be converted into a directed acyclic graph to obtain a second event token flow graph. Specifically, the rings contained in the first event token flow graph can be determined to obtain f rings, where f is a natural number. Then, the edges in which the receiver is a flash loan node in these f rings are determined to obtain at least one edge. The at least one edge can be removed to obtain the second event token flow graph.
[0058] It should be explained that in the embodiment of the present application, the ring of the directed acyclic graph refers to the ring of the flash loan nodes included. Figure 2 As an example, assuming that node 2 is a flash loan node, it is necessary to determine the ring of flash loan nodes contained in the first event token flow graph (for example, node 2→node 6→node 7→node 2), and then determine the edge in the ring where the receiver is a flash loan node, and obtain edge 72 (i.e., node 7→node 2). Remove edge 72 to obtain the second event token flow graph. The second event token flow graph is as follows: Figure 3 As shown, Figure 3 It is a structural schematic diagram of a second event token flow diagram provided in an embodiment of the present application.
[0059] Next, the first dictionary can be initialized; the key in the first dictionary is the node ID in the second event token flow graph (for example, Figure 2 The node ID of node 1 in is 1), value represents the set of nodes upstream of the node that control the flow of its tokens to the current node, Figure 2Taking node 6 in as an example, the value corresponding to node 6 is [1, 2, 6], which does not include node 4. Although node 4 is upstream of node 6, node 4 cannot control the flow of tokens of node 6, because node 2 can directly trade tokens with node 2 without going through node 4. value can also be understood as the set of nodes that all paths from the entry node to this node must pass through; during initialization, the value corresponding to the entry node of the second event token flow graph is initialized to a node set containing only its own node ID, and the values of the remaining nodes are set to the initial set V; the initial set V stores the node IDs of all nodes in the second event token flow graph; finally, the node identification of the second event token flow graph can be performed according to the first dictionary to obtain the target AMM node.
[0060] In this way, by obtaining the historical transaction characteristics of n account addresses to determine i flash loan nodes and j AMM nodes, more monitoring resources and security protection resources can be concentrated on i flash loan nodes and j AMM nodes, instead of indiscriminately investing resources in all n nodes, thereby improving resource utilization efficiency.
[0061] Optionally, A5, performing node identification on the second event token flow graph according to the first dictionary to obtain the target AMM node may include the following steps:
[0062] Traversing the nodes in the second event token flow graph using a preset traversal method, performing a preset risk identification operation on each traversed node to obtain the target AMM node; the target AMM node includes at least one node;
[0063] In the embodiment of the present application, the preset traversal method and the preset risk identification operation can be preset or defaulted in advance.
[0064] In a specific embodiment, the preset traversal method can be a reverse post-order traversal of DFS. The second event token flow graph is traversed according to the preset traversal method. Each time a node is traversed, a preset risk identification operation is performed on it, thereby obtaining the target AMM node.
[0065] Specifically, the preset risk identification operation specifically includes:
[0066] B1. Obtain the first node v currently traversed; obtain the first predecessor node set W corresponding to the first node v in the second event token flow graph; initialize the first set R to an empty set, and the first set R is used to store the node set;
[0067] B2. performing a preset dictionary update operation on the first predecessor node set W based on the first set R to update the first dictionary and obtain a target dictionary;
[0068] B3. Take out the value corresponding to each of the j AMM nodes from the target dictionary to obtain j values;
[0069] B4. Determine the value of the flash loan node included in the j values, and obtain m values; m is a natural number less than or equal to j;
[0070] B5. Determine that the nodes corresponding to the m values are nodes in the target AMM nodes.
[0071] In the embodiment of the present application, the preset dictionary update operation may be preset or defaulted in advance.
[0072] In a specific embodiment, the first node v currently traversed may be obtained first, and then the first predecessor node set W corresponding to the first node v in the second event token flow graph may be obtained. Specifically, the predecessor node is a node having an edge pointing from the node to the first node v, for example, Figure 3 The predecessor node set of node 6 in is [2, 5]; then, a preset dictionary update operation can be performed on the first predecessor node set W based on the first set R to update the first dictionary and obtain a target dictionary, for example, Figure 3 The target dictionary corresponding to the second event token flow graph in is {1: [1], 2: [1, 2], 3: [1, 3], 4: [1, 2, 4], 5: [1, 2, 4, 5], 6: [1, 2, 6], 7: [1, 2, 6, 7], 8: [1, 2, 6, 8]}; then, the value corresponding to each AMM node in the j AMM nodes can be taken out from the target dictionary to obtain j values; find out the value that contains the flash loan node in the j values to obtain m values. Specifically, the nodes in each of the j values can be checked for matching with the i flash loan nodes. If any flash loan node is found in a certain value, then this value is the value that contains the flash loan node, thus, obtaining m values; finally, the nodes corresponding to these m values can be determined according to the target dictionary to obtain m nodes, that is, the target AMM node includes these m nodes.
[0073] In this way, the relationship between nodes is recorded in a structured way through the first dictionary. By updating the dictionary, the record of such node relationships can be continuously improved and optimized. In complex blockchain networks and transaction scenarios, accurate node relationship data can help to conduct subsequent analysis more efficiently, thereby improving data processing efficiency. In addition, once the target AMM node is determined, it can be monitored and managed in a targeted manner, and the transaction review of these nodes can be strengthened, and risk warning mechanisms can be set up, thereby ensuring the security and stability of blockchain transactions.
[0074] Optionally, the preset dictionary update operation may include the following steps:
[0075] Extract nodes from the first predecessor node set W in a preset order, and perform a first dictionary update operation on each extracted node to obtain a second dictionary;
[0076] Checking whether the second dictionary is consistent with the first dictionary;
[0077] If they are inconsistent, obtaining the next node of the first node v, and performing the preset dictionary update operation on the next node;
[0078] If they are consistent, determining the target dictionary according to the second dictionary, and stopping the preset dictionary update operation;
[0079] The first dictionary update operation includes the following steps:
[0080] C1. Obtain the first node ID corresponding to the first predecessor node w; the first predecessor node w is a node taken out from the first predecessor node set W at the current moment;
[0081] C2. Extract the value corresponding to the first node ID from the first dictionary, and record it as a first set DV;
[0082] C3. When the first set R is an empty set, R = DV;
[0083] C4. When the first set R is not an empty set, R = R∩DV;
[0084] C5. Add the first node v to the first set R to obtain a target set;
[0085] C6. Update the value of the first node v in the first dictionary with the target set.
[0086] In the embodiment of the present application, the preset order can be preset in advance or defaulted.
[0087] In a specific embodiment, nodes can be taken out from the first precursor node set W in a preset order, and a first dictionary update operation is performed on each node taken out. The first dictionary update operation includes: first, the first node ID of the first predecessor node w can be queried from the preset database, or the first predecessor node w can be monitored by the preset Ethereum full node to obtain the first node ID, and then the value corresponding to the first node ID can be extracted from the first dictionary, which is recorded as the first set DV; when the first set R is an empty set, that is, when the preset dictionary update operation is performed for the first time, let the set R = DV. When the first set R is not an empty set, that is, it is not the first time to perform the preset dictionary update operation, let the set R = R∩DV; then, the first node v can also be added to the first set R to obtain the target set; replace the value of the first node v in the first dictionary with the target set.
[0088] After executing the first dictionary update operation once for all nodes in the first predecessor node set W, the second dictionary can be obtained; check whether the second dictionary is consistent with the first dictionary. Specifically, the first key set of the first dictionary and the second key set of the second dictionary can be obtained respectively, and check whether the first key set and the second key set are equal. If they are equal, further check whether the value corresponding to each key is equal. If both are equal, it means that the dictionary update is completed, that is, the second dictionary is consistent with the first dictionary. Otherwise, it means that the dictionary update is not completed and the second dictionary is inconsistent with the first dictionary.
[0089] If they are inconsistent, the next node of the first node v in the preset traversal method is obtained, and the preset dictionary update operation is performed on the next node;
[0090] If they are consistent, the second dictionary is directly determined as the target dictionary, and the preset dictionary update operation is stopped.
[0091] In this way, by obtaining the first node ID corresponding to each predecessor node w, and then extracting the corresponding value (i.e., the first set DV) from the first dictionary, it is possible to accurately obtain other node information associated with the current predecessor node, thereby constructing a complete association relationship network between nodes and obtaining the target dictionary, providing data support for subsequent in-depth analysis of the dependency, control, and influence relationships between nodes.
[0092] S105. Obtain the target smart contract bytecode and historical transaction data corresponding to the target AMM node.
[0093] In an embodiment of the present application, the Ethereum full node can be preset to obtain the target smart contract bytecode corresponding to the target AMM node. Specifically, the smart contract address of the target AMM node can be obtained first, and the target smart contract bytecode can be obtained through the RPC (remote procedure call) method. The AMM node ID corresponding to the target AMM node can also be obtained. The transaction data associated with the AMM node ID in the preset database can be queried through the preset Ethereum full node, thereby obtaining historical transaction data.
[0094] S106. Determine the first price model corresponding to the target smart contract bytecode; determine the second price model corresponding to the historical transaction data; the first price model and the second price model are both mathematical expressions that describe the price relationship of a preset transaction token pair; the preset transaction token pair includes a first token and a second token.
[0095] In the embodiment of the present application, the first price model can be obtained by analyzing the target smart contract bytecode; then, the second price model corresponding to the historical transaction data can also be determined. Specifically, the transaction records involving the first token and the second token can be screened out from the historical transaction data to obtain the currency pair transaction data, and the currency pair transaction data can be preprocessed (for example, removing outliers, filling missing values, etc.) to obtain the processed currency pair transaction data, and a preset fitting method is selected to fit the currency pair transaction data to obtain the fitted parameter value, and then, the nonlinear fitting curve equation of the preset transaction token pair is obtained, which is used as the second price model. The preset fitting method may include at least one of the following: linear regression fitting method, polynomial regression method, support vector regression fitting method, etc., which are not limited here. For example, assuming that the currency pair transaction data is fitted by the polynomial regression method, the fitting curve equation (that is, the second price model) is obtained as follows:
[0096] u(x)=y=0.138634+x*1.519267+x*x*0.003977;
[0097] Among them, u(x) represents the second price model, y represents the second token, and x represents the first token.
[0098] In this way, the first price model and the second price model describe the price relationship of the preset trading token pair from the perspectives of the inherent logic of the contract and the actual market behavior respectively. Taking these two models into consideration, the rationality and stability of the price relationship can be more comprehensively evaluated. At the same time, potential transaction risks, such as price manipulation risk and contract execution risk, can be more accurately identified.
[0099] Optionally, step S106, determining the first price model corresponding to the target smart contract bytecode may include the following steps:
[0100] D1. Decompile the target smart contract bytecode to obtain the target control flow graph;
[0101] D2. Obtain the external function name and exchange parameters for token exchange from the target ABI file corresponding to the target smart contract bytecode;
[0102] D3. Calculate the function signature according to the preset hash function, the external function name and the exchange parameter to obtain the target function signature;
[0103] D4, determining an entry node in the target control flow graph according to the target function signature;
[0104] D5. Starting from the entry node, traverse the target control flow graph to obtain a traversed control flow subgraph;
[0105] D6. Reversely infer the first price model according to the control flow subgraph.
[0106] In the embodiment of the present application, the preset hash function can be preset in advance or defaulted.
[0107] In a specific embodiment, the target smart contract bytecode can be decompiled to obtain a target control flow graph. Specifically, the Ethereum smart contract decompilation technology can be used to construct a target control flow graph from the target smart contract bytecode (a string sequence composed of hexadecimal numbers).
[0108] It should be explained that decompilation technology is to reverse analyze the executable program of other people's software and deduce its execution process. The control flow graph is a directed graph composed of basic blocks (the longest instruction sequence with a single entry and a single exit) as nodes and jump relationships between basic blocks as edges. By traversing the target control flow graph, all execution paths of the smart contract can be analyzed.
[0109] Next, the external function name and exchange parameters of the exchange tokens can be obtained from the target ABI file corresponding to the target smart contract bytecode. Specifically, the target smart contract corresponding to the target smart contract bytecode can be determined first, the contract address of the target smart contract can be obtained, and the target ABI file can be obtained from the preset database according to the contract address through the preset Ethereum full node or Ethereum blockchain browser. Then, a suitable parsing tool (for example, Python language) can be selected to parse the target ABI file to obtain the external function name and exchange parameters. Specifically, the external function name of the exchange token often contains some specific keywords, for example, "swap", "exchange", "convert", etc. The external function name related to the exchange token can be screened out by checking whether the name of the external function (name field) contains these keywords. Then, the input parameters of the function corresponding to the external function name, that is, the exchange parameters, can be obtained.
[0110] Furthermore, a function signature is calculated according to a preset hash function (for example, a SHA3 hash function), an external function name, and an exchange parameter to obtain a target function signature. Specifically, the external function name and the exchange parameter may be concatenated together and then input into a preset hash function for calculation to obtain the target function signature. For example, assuming that the external function name is x11, and the exchange parameters are uint a and uint b, the concatenation results in "x11(uint, uint)", Signature = H("x11(uint, uint)"), wherein "x11(uint, uint)" represents input, H() represents a preset hash function, and Signature represents the target function signature; then, the entry node in the target control flow graph may be determined according to the target function signature. Specifically, the function signature of each node in the target control flow graph may be compared to see if they are consistent with the target function signature. If they are consistent, it represents that the node is an entry node; then, starting from the entry node, the target control flow graph may be traversed to obtain a traversed control flow subgraph; finally, the first price model may be inferred based on the control flow subgraph.
[0111] In this way, by traversing the control flow graph starting from the entry node, the control flow subgraph obtained is a more detailed graphical representation focusing on the execution part of the token exchange function. It filters out other contract logic that is not related to the execution of this function, so that the code execution path, data flow, etc. related to the token exchange function can be analyzed more carefully and deeply, thereby improving the efficiency of data analysis.
[0112] Optionally, the inverse deducing the first price model according to the control flow subgraph may include the following steps:
[0113] E1. Initialize the initial variable model VTable-init; the initial variable model VTable-init is a table for storing tuples (var, val), where var is a unique identifier of a variable and val is a possible value of the variable;
[0114] E2. Initialize the stack S to be empty, and then push the first element (VTable-init, entry) into the stack S; wherein entry is the node address of the entry node;
[0115] E3. Execute the preset variable propagation tree construction operation to obtain the target variable propagation tree;
[0116] E4. Starting from the leaf node of the target variable propagation tree, the parent node is expressed by using the child node, and the root node y of the target variable propagation tree is iterated, and the variable y corresponding to the root node y is expanded into the expression of the leaf nodes in the target variable propagation tree to obtain the target expression;
[0117] E5. Replace the variables in the target output amount_out based on the target expression to obtain the first price model.
[0118] In the embodiment of the present application, the preset variable propagation tree construction operation can be preset or defaulted in advance; val has four types: symbols of other variables, expressions composed of other variables, constant values, and unknown values.
[0119] It needs to be explained that the present application determines the target output amount_out by identifying the LOG instruction in the control flow subgraph, thereby obtaining the variable y. Therefore, it is necessary to perform the above steps of inferring the first price model based on the control flow subgraph after determining that there is a node containing the LOG instruction in the control flow subgraph. Conversely, if there is no node containing the LOG instruction in the control flow subgraph, it means that the price model cannot be derived based on the control flow subgraph, that is, there is no need to perform the above steps of inferring the first price model based on the control flow subgraph.
[0120] In a specific embodiment, the initial variable model VTable-init can be initialized to be empty or a preset variable model, and then, the stack S can be initialized to be empty, and then, the first element (VTable-init, entry) can be pushed into the stack S; then, the preset variable propagation tree construction operation can be performed to obtain the target variable propagation tree; then, starting from the leaf node of the target variable propagation tree, the parent node can be expressed by using the child node, and the iterative execution can be performed to the root node y of the target variable propagation tree, and the variable y corresponding to the root node y can be expanded into the expression of the leaf nodes in the target variable propagation tree to obtain the target expression.
[0121] For example, assume that the initial variable model VTable-init is a preset variable model, as shown in Table 1:
[0122] Table 1
[0123] var val x null r_in null r_out null
[0124] By updating the initial variable model VTable-init according to the node content in the control flow subgraph, the final variable model can be obtained, as shown in Table 2:
[0125] Table 2
[0126]
[0127]
[0128] According to Table 2, the target variable propagation tree can be constructed. For details, please refer to Figure 4 , Figure 4 is a schematic diagram of a target variable propagation tree provided in an embodiment of the present application, which can be obtained from Figure 4 Starting from the leaf node of the target variable propagation tree, the parent node is expressed by using the child node. For example, the leaf node of node v1 is 997, then v1 can be represented by 997. For another example, the child nodes of node v2 are node x and node v1, then v2=x*v1=x*997, and iterate to the root node y of the target variable propagation tree, and expand the variable y corresponding to the root node y into the expression of the leaf nodes in the target variable propagation tree to obtain the target expression. Specifically, the root node y is node v7, and the target expression is v7=(RY*x*997) / (RX*1000+x*997); wherein RY represents the number of the second token in the target blockchain at the current moment, and RX represents the number of the first token in the target blockchain at the current moment.
[0129] Finally, the variables in the target output amount_out can be replaced based on the target expression to obtain the first price model. It should be explained that the target output amount_out is the root node of the target variable propagation tree, that is, the target output amount_out is v7, then the first price model is amount_out = f(x) = (RY*x*997) / (RX*1000+x*997).
[0130] In this way, by starting from the leaf node, using the child node to express the parent node, and gradually iterating to the root node, it is equivalent to starting from the most basic and simplest elements, and gradually building a complex variable relationship expression. It can clearly show how the variables are nested and interact with each other, making the relationship between variables more transparent, intuitive, and easy to understand.
[0131] Optionally, the preset variable propagation tree construction operation may include the following steps:
[0132] S11, determining whether the stack S is empty, if it is empty, stopping the execution of the preset variable propagation tree construction operation, and taking the latest generated variable propagation tree as the target variable propagation tree; if it is not empty, jumping to step S12;
[0133] S12, taking out the top element from the stack S, obtaining the target element (VTable, node), determining the target node according to the node address node, updating the variable model VTable based on the content in the target node, and obtaining an updated variable model VTable-new; VTable represents the variable model before entering the target node;
[0134] S13, determining a node in the control flow subgraph that contains a LOG instruction, obtaining a first control flow graph node, acquiring LOG parameter data of the LOG instruction in the first control flow graph node, and identifying the target output amount_out from the LOG parameter data;
[0135] S14, determining the variable y corresponding to the target output amount_out in the control flow subgraph;
[0136] S15, constructing a variable propagation tree with the variable y as the root node according to the variable model stored in the stack S, to obtain a first variable propagation tree;
[0137] S16. Push the second element (VTable-new, node-next) into the stack S, and jump to step S11; wherein node-next is the node address of the successor node of node.
[0138] In an embodiment of the present application, it is possible to first determine whether the stack S is empty. If it is empty, it means that the variable propagation tree has been constructed, then the preset variable propagation tree construction operation is stopped, and the latest generated variable propagation tree is used as the target variable propagation tree; if it is not empty, jump to the above step S12.
[0139] If step S12 is executed, the top element of the stack is taken out from the stack S to obtain the target element (VTable, node). Then, the node corresponding to the node address node in the control flow subgraph can be determined to obtain the target node. The variable model VTable is updated according to the content in the target node to obtain the updated variable model VTable-new. Further, the node containing the LOG instruction in the control flow subgraph can be queried to obtain the first control flow graph node, the LOG parameter data of the LOG instruction in the first control flow graph node can be obtained, and the target output amount_out can be identified from the LOG parameter data. For example, assuming that the LOG instruction is "LOG SWAP(……, v7, …)”, from which the LOG parameter data “(……, v7, …)” is extracted, and it can be known that the target output amount_out is v7; then, it can be determined that the variable y corresponding to the target output amount_out in the control flow subgraph is v7; then, a variable propagation tree with variable y as the root node can be constructed according to the variable model stored in the stack S to obtain the first variable propagation tree; the second element (VTable-new, node-next) is pushed into the stack S, and jump to step S11.
[0140] It should be explained that there may be more than one successor node of node, so there may be more than one second element pushed into stack S in step S16; see Figure 5 , Figure 5 is a flow chart of a control flow subgraph provided in an embodiment of the present application, Figure 5 Take the control flow subgraph in as an example to illustrate S16:
[0141] At the beginning, the control flow graph node ① (i.e., the entry node) is pushed into the stack, and the first element (VTable-init, entry) is pushed into the stack S. Then, the first element is popped out of the stack, and the variable model VTable-init is updated according to the content in the control flow graph node ① to obtain the variable model VTable1. Then, the subsequent nodes of the control flow graph node ①, the control flow graph nodes ② and ③, can be pushed into the stack, that is, the elements (VTable1, n1) and (VTable1, n2) are pushed into the stack S in sequence, where n1 is the address of the control flow graph node ②, and n2 is the address of the control flow graph node ③. Then, the element (VTable1, n2) is popped out of the stack, and the variable model VTable1 is updated according to the content in the control flow graph node ③ to obtain the variable model VT able2, since control flow graph node ③ has no subsequent node, no new elements enter stack S, then, pop element (VTable1, n1) from stack, update variable model VTable1 according to the content in control flow graph node ②, get variable model VTable3, push its subsequent node ④ into stack, that is, push element (VTable3, n3) into stack S, where n3 is the address of control flow graph node ④, then, pop element (VTable3, n3) from stack, update variable model VTable3 according to the content in control flow graph node ④, get variable model VTable4, since control flow graph node ④ has no subsequent node, no new elements enter stack S, at this time stack S is empty, indicating that the control flow subgraph has been traversed.
[0142] Optionally, updating the variable model VTable based on the content in the target node to obtain an updated variable model VTable-new may include the following steps:
[0143] F1. Obtain the target statement type of the target node; the target statement type includes one of the following: a declaration statement, an assignment statement;
[0144] F2, determining the target variable model update operation corresponding to the target statement type;
[0145] F3. Update the variable model VTable according to the target variable model update operation to obtain the updated variable model VTable-new.
[0146] In the embodiment of the present application, the target statement type may include one of the following: a declaration statement, an assignment statement, a judgment statement, a loop statement, etc., which are not limited here.
[0147] In a specific embodiment, the target statement type of the target node can be obtained; then the target variable model update operation corresponding to the target statement type is determined. Specifically, a mapping relationship between a preset statement type and a variable model update operation can be pre-stored, and the target variable model update operation corresponding to the target statement type is determined based on the mapping relationship; then, the variable model VTable can be updated according to the target variable model update operation to obtain an updated variable model VTable-new, for example:
[0148] (a) If the target statement type is a declaration statement, declaring variable v1, then add (v1, unknown value) to the variable model VTable, where v1 is the variable name.
[0149] (b) If the target statement type is an assignment statement, and a value is assigned to the variable v1, then find out the value val corresponding to the left value of the assignment statement where var is the left value of the assignment statement from the variable model VTable, and update it to the right value of the assignment statement. For example, suppose there is an assignment statement "v1=b+1", v1 is var, b+1 is val, where var represents the symbol on the left side of the equal sign in the assignment statement, and val represents the symbol on the right side of the equal sign; then, find out the row where var is v1 from the variable model VTable, and then update b+1 to the val column.
[0150] Optionally, in a certain embodiment, step S15, constructing a variable propagation tree with the variable y as a root node according to the variable model stored in the stack S to obtain a first variable propagation tree, may include the following steps:
[0151] The target variable model corresponding to the stack S can be obtained. Specifically, the variable models stored in the stack S can be obtained. Then, the duplicate data in these variable models can be removed, and the remaining data can be combined into a variable model, that is, the target variable model. Then, the two-tuple set TreeE is initialized to an empty set; the variable stack VarS is initialized to be empty, and then the variable y is pushed into the variable stack VarS.
[0152] S21, taking out the variable v' from the variable stack VarS, and reading the val value of var=v' from the target variable model;
[0153] S22. If the value of val is a constant value, the first group of elements (v', val) is added to the two-tuple set TreeE, indicating that there is an edge from v' to val in the variable propagation tree;
[0154] S23. If the value of val is a function expression, scan the function expression from left to right, collect the variables v_e and constant c contained therein, add the second group of elements (v', v_e) and the third group of elements (v', c) to the two-tuple set TreeE in turn, and push the variable v_e into the variable stack VarS;
[0155] S23. Determine whether the variable stack VarS is empty. If it is empty, determine the first variable propagation tree according to the two-tuple set TreeE. If it is not empty, jump to step S21.
[0156] In the embodiment of the present application, the top element of the variable stack VarS, that is, the variable v', can be taken out, and then the val value of var=v' can be read from the target variable model, thereby obtaining the value of the variable v'.
[0157] If the value of val is a constant, the first set of elements (v', val) is added to the two-tuple set TreeE, indicating that there is an edge from v' to val in the variable propagation tree;
[0158] If the value of val is a function expression, the function expression is scanned from left to right to collect the variables v_e and constants c it contains, and the second group of elements (v', v_e) and the third group of elements (v', c) are added to the two-tuple set TreeE in turn, and the variable v_e is pushed into the variable stack VarS. For example, assuming that a function expression is v2=x*v1, there are no constants in the function expression, and there are two variables x and v1, then the elements (v', x) and (v', v1) can be added to the two-tuple set TreeE, and the two variables x and v1 can also be pushed into the variable stack VarS.
[0159] Determine whether the variable stack VarS is empty. If it is empty, determine the first variable propagation tree according to the two-tuple set TreeE. Specifically, each element in the two-tuple set TreeE represents an edge in the first variable propagation tree. Specifically, in the element (v', x), v' is the parent node and x is the child node. For example, see Figure 4 , Figure 4 is a schematic diagram of the structure of a target variable propagation tree provided in an embodiment of the present application, Figure 4 The set of two-tuples corresponding to the target variable propagation tree in is {(v7, v3), (v7, v6), (v3, v2), (v3, r_out), (v2, x), (v2, v1), (v1, 997), (v6, v5), (v6, v2), (v5, r_in), (v5, v4), (v4, 1000), (v2, x), (v2, v1), (v1, 997)}.
[0160] If it is not empty, jump to step S21.
[0161] In this way, by constructing a variable propagation tree, the dependency relationship between variables can be clearly represented, that is, how the value of a variable depends on other variables or constants, thereby providing strong data support for the derivation of the price model.
[0162] S107: Determine a comprehensive price model according to the first price model and the second price model.
[0163] In the embodiment of the present application, the preset weight pair can be preset in advance or defaulted.
[0164] In a specific embodiment, the first price model and the second price model may be combined using a preset weight pair to obtain a comprehensive price model, as follows:
[0165] v(x)=a*f(x)+b*u(x),0 <a<1,0<b<1,a+b=1;
[0166] Among them, the preset weight pair is (a, b), a is the weight corresponding to the first price model, b is the weight corresponding to the second price model, f(x) represents the first price model, u(x) represents the second price model, and v(x) represents the comprehensive price model.
[0167] S108. Estimate the probability that the flash loan transaction is a flash loan attack based on the comprehensive price model to obtain a target flash loan attack probability.
[0168] In the embodiment of the present application, the token stock RX_b and RY_b of the preset transaction token pair before the flash loan transaction and the stock RX_a and RY_a after the flash loan transaction can be obtained through the preset Ethereum full node, and the exchange rate change k of the trading pool stock can be calculated. The calculation formula is as follows:
[0169] k=abs(RX_a / RY_a–RX_b / RY_b);
[0170] Among them, the abs() function represents taking the absolute value; then, the comprehensive price model v(x) can be used to estimate the exchange rate trend change value of the flash loan transaction exchange rate. Specifically, the comprehensive price model v(x) can be derived to obtain the derivative v'(x), and RX_a and RY_a can be brought into the derivative v'(x) to calculate the exchange rate trend change value:
[0171] s = w(x) = abs(v'(x));
[0172] Among them, s represents the exchange rate trend change value, which can be used to measure the impact of the current transaction on the exchange rate between the first token and the second token. Then, the target flash loan attack probability can be determined according to the exchange rate change k of the trading pool stock and the exchange rate trend change value s. The specific calculation formula is as follows:
[0173] p = w_s*s+w_k*k;
[0174] Among them, p represents the target flash loan attack probability, w_s is the first preset weight, and w_k is the second preset weight; both the first preset weight and the second preset weight can be preset or defaulted in advance.
[0175] Let's take an example to illustrate. Assuming a = 0.8 and b = 0.2, the comprehensive price model is:
[0176] v(x)=0.8*((RY*x*997) / (RX*1000+x*997))+0.2*(0.138634+x*1.519267+x*x*0.003977).
[0177] Before the transaction, RX_b=60000, RY_b=100000, RX_a=60078.763, RY_a=99868.900; According to the above calculation formula of k, k=0.001576; Assuming x=79, the derivative of the above comprehensive price model v(x) is v'(x): 0.8*(-994009*RY*x / (1000*RX+997*x)^2+997*RY / (1000*RX+997*x))+0.2*(1.519267+2*x*0.003977);
[0178] Substitute RX_a and RY_a into v'(x) to calculate s=1.634733; assuming w_s=0.7, w_k=0.3, the target flash loan attack probability p can be calculated based on k and s. The specific calculation formula is as follows:
[0179] p=w_s*s+w_k*k=1.1447859.
[0180] S109: When the target flash loan attack probability is greater than a preset attack probability threshold, determine that the flash loan transaction is a flash loan attack.
[0181] In the embodiment of the present application, the preset attack probability threshold can be preset in advance or defaulted.
[0182] In a specific embodiment, when the target flash loan attack probability is greater than a preset attack probability threshold, the flash loan transaction is judged to be a flash loan attack, and a corresponding target alarm operation is performed. For example, a mapping relationship between a preset flash loan attack probability and an alarm operation can be pre-stored, and the target alarm operation corresponding to the target flash loan attack probability is determined based on the mapping relationship. For example, assuming that the preset attack probability threshold is 0.5, the above target flash loan attack probability p is 1.1447859, and 1.1447859 is less than 0.5, indicating that the preset transaction token pair has not changed much, and it can be judged that this flash loan transaction is not a flash loan attack.
[0183] It can be seen that the flash loan attack identification method provided in the embodiment of the present application obtains a comprehensive price model by integrating the first price model of the internal logic of the comprehensive price model smart contract and the second price model that reflects the actual market situation. The comprehensive price model obtained in this way can more accurately characterize the overall performance of the token price under normal and abnormal conditions, avoiding the problem of one-sided judgment due to relying solely on a single model, making the evaluation of whether the token price is abnormal more scientific and comprehensive, thereby improving the accuracy of the judgment on flash loan attacks.
[0184] See also Figure 6 , Figure 6 1 is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application, the electronic device includes a processor, a memory, a communication interface and one or more programs, the processor, the memory and the communication interface are interconnected via a bus. The one or more programs are stored in the memory and are configured to be executed by the processor. In the embodiment of the present application, the program includes instructions for executing the following steps:
[0185] By presetting the Ethereum full node to monitor the target blockchain, the first event log is obtained;
[0186] Construct a first event token flow graph according to the first event log; the first event token flow graph includes n account addresses in the target blockchain for receiving or forwarding tokens; n is a positive integer;
[0187] Determining whether there is a flash loan transaction in the first event log according to the first event token flow graph;
[0188] When there is a flash loan transaction in the first event log, determine the target AMM node with a flash loan attack risk among the n account addresses;
[0189] Obtain the target smart contract bytecode and historical transaction data corresponding to the target AMM node;
[0190] Determine a first price model corresponding to the target smart contract bytecode; determine a second price model corresponding to the historical transaction data; the first price model and the second price model are both mathematical expressions describing the price relationship of a preset transaction token pair; the preset transaction token pair includes a first token and a second token;
[0191] Determine a comprehensive price model according to the first price model and the second price model;
[0192] Estimate the probability that the flash loan transaction is a flash loan attack based on the comprehensive price model to obtain a target flash loan attack probability;
[0193] When the target flash loan attack probability is greater than a preset attack probability threshold, the flash loan transaction is determined to be a flash loan attack.
[0194] Optionally, in determining the target AMM node with a flash loan attack risk among the n account addresses, the program further includes instructions for executing the following steps:
[0195] Obtaining historical transaction features of each of the n account addresses to obtain n historical transaction features;
[0196] Determine the flash loan nodes and AMM nodes in the n account addresses according to the n historical transaction features, and obtain i flash loan nodes and j AMM nodes; i and j are both natural numbers less than or equal to n, and the sum of i and j is less than or equal to n;
[0197] Convert the first event token flow graph into a directed acyclic graph to obtain a second event token flow graph;
[0198] Initialize the first dictionary; the key in the first dictionary is the node ID in the second event token flow graph, and the value represents the node set that controls the flow of its tokens to the current node upstream of the node; during initialization, the value corresponding to the entry node of the second event token flow graph is initialized to a node set containing only its own node ID, and the values of the remaining nodes are set to the initial set V; the initial set V stores the node IDs of all nodes in the second event token flow graph;
[0199] The second event token flow graph is subjected to node identification according to the first dictionary to obtain the target AMM node.
[0200] Optionally, in terms of performing node identification on the second event token flow graph according to the first dictionary to obtain the target AMM node, the program further includes instructions for performing the following steps:
[0201] Traversing the nodes in the second event token flow graph using a preset traversal method, performing a preset risk identification operation on each traversed node to obtain the target AMM node; the target AMM node includes at least one node;
[0202] The preset risk identification operation specifically includes:
[0203] Get the first node v currently traversed; get the first predecessor node set W corresponding to the first node v in the second event token flow graph; initialize the first set R to an empty set, and the first set R is used to store the node set;
[0204] Based on the first set R, a preset dictionary update operation is performed on the first predecessor node set W to update the first dictionary to obtain a target dictionary;
[0205] Taking out the value corresponding to each AMM node in the j AMM nodes from the target dictionary to obtain j values;
[0206] Determine that the value of the flash loan node is included in the j values, and obtain m values; m is a natural number less than or equal to j;
[0207] Determine that the nodes corresponding to the m values are nodes in the target AMM node.
[0208] Optionally, the preset dictionary update operation includes:
[0209] Extract nodes from the first predecessor node set W in a preset order, and perform a first dictionary update operation on each extracted node to obtain a second dictionary;
[0210] Checking whether the second dictionary is consistent with the first dictionary;
[0211] If they are inconsistent, obtaining the next node of the first node v, and performing the preset dictionary update operation on the next node;
[0212] If they are consistent, determining the target dictionary according to the second dictionary, and stopping the preset dictionary update operation;
[0213] The first dictionary update operation includes:
[0214] Obtaining a first node ID corresponding to a first predecessor node w; the first predecessor node w is a node taken out from the first predecessor node set W at the current moment;
[0215] Extract the value corresponding to the first node ID from the first dictionary, and record it as a first set DV;
[0216] When the first set R is an empty set, R=DV;
[0217] When the first set R is not an empty set, R=R∩DV;
[0218] Add the first node v to the first set R to obtain a target set;
[0219] Update the value of the first node v in the first dictionary with the target set.
[0220] Optionally, in determining the first price model corresponding to the target smart contract bytecode, the program further includes instructions for executing the following steps:
[0221] Decompile the target smart contract bytecode to obtain a target control flow graph;
[0222] Obtain the external function name and exchange parameters for exchanging tokens from the target ABI file corresponding to the target smart contract bytecode;
[0223] Calculate the function signature according to the preset hash function, the external function name and the exchange parameter to obtain the target function signature;
[0224] Determine an entry node in the target control flow graph according to the target function signature;
[0225] Starting from the entry node, traverse the target control flow graph to obtain a traversed control flow subgraph;
[0226] The first price model is inferred based on the control flow subgraph.
[0227] Optionally, in the aspect of inferring the first price model according to the control flow subgraph, the program further includes instructions for executing the following steps:
[0228] Initialize the initial variable model VTable-init; the initial variable model VTable-init is a table for storing tuples (var, val), var is the unique label of the variable, and val is the possible value of the variable;
[0229] Initialize the stack S to be empty, and then push the first element (VTable-init, entry) into the stack S; wherein entry is the node address of the entry node;
[0230] Execute the preset variable propagation tree construction operation to obtain the target variable propagation tree;
[0231] Starting from the leaf node of the target variable propagation tree, by using the child node to express the parent node, iteratively executing to the root node y of the target variable propagation tree, expanding the variable y corresponding to the root node y into the expression of the leaf nodes in the target variable propagation tree, and obtaining the target expression;
[0232] The first price model is obtained by replacing the variables in the target output amount_out based on the target expression.
[0233] Optionally, the preset variable propagation tree construction operation includes:
[0234] S11, determining whether the stack S is empty, if it is empty, stopping the execution of the preset variable propagation tree construction operation, and taking the latest generated variable propagation tree as the target variable propagation tree; if it is not empty, jumping to step S12;
[0235] S12, taking out the top element from the stack S, obtaining the target element (VTable, node), determining the target node according to the node address node, updating the variable model VTable based on the content in the target node, and obtaining an updated variable model VTable-new; VTable represents the variable model before entering the target node;
[0236] S13, determining a node in the control flow subgraph that contains a LOG instruction, obtaining a first control flow graph node, acquiring LOG parameter data of the LOG instruction in the first control flow graph node, and identifying the target output amount_out from the LOG parameter data;
[0237] S14, determining the variable y corresponding to the target output amount_out in the control flow subgraph;
[0238] S15, constructing a variable propagation tree with the variable y as the root node according to the variable model stored in the stack S, to obtain a first variable propagation tree;
[0239] S16. Push the second element (VTable-new, node-next) into the stack S, and jump to step S11; wherein node-next is the node address of the successor node of node.
[0240] Optionally, in the aspect of updating the variable model VTable based on the content in the target node to obtain an updated variable model VTable-new, the program further includes instructions for executing the following steps:
[0241] Acquire a target statement type of the target node; the target statement type includes one of the following: a declaration statement, an assignment statement;
[0242] Determine a target variable model update operation corresponding to the target statement type;
[0243] The variable model VTable is updated according to the target variable model update operation to obtain the updated variable model VTable-new.
[0244] Optionally, in the aspect of constructing a variable propagation tree with the variable y as a root node according to the variable model stored in the stack S to obtain a first variable propagation tree, the program further includes instructions for executing the following steps:
[0245] Obtaining a target variable model corresponding to the stack S;
[0246] Initialize the two-tuple set TreeE to be an empty set; the two-tuple set TreeE is used to store the variable tree;
[0247] Initialize the variable stack VarS to be empty, then push the variable y into the variable stack VarS; the variable stack VarS is used to store the variables to be processed;
[0248] S21, taking out the variable v' from the variable stack VarS, and reading the val value of var=v' from the target variable model;
[0249] S22. If the value of val is a constant value, the first group of elements (v', val) is added to the two-tuple set TreeE, indicating that there is an edge from v' to val in the variable propagation tree;
[0250] S23. If the value of val is a function expression, scan the function expression from left to right, collect the variables v_e and constant c contained therein, add the second group of elements (v', v_e) and the third group of elements (v', c) to the two-tuple set TreeE in turn, and push the variable v_e into the variable stack VarS;
[0251] S23. Determine whether the variable stack VarS is empty. If it is empty, determine the first variable propagation tree according to the two-tuple set TreeE. If it is not empty, jump to step S21.
[0252] In a specific implementation, the electronic device described in the embodiment of the present invention may also execute part or all of the steps of any method recorded in the above method embodiment, which will not be described in detail here.
[0253] An embodiment of the present application also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for electronic data exchange, wherein the computer program enables a computer to execute part or all of the steps of any method described in the above method embodiments, and the above computer includes an electronic device.
[0254] The embodiment of the present application also provides a computer program product, the computer program product includes a non-transitory computer-readable storage medium storing a computer program, the computer program is operable to cause a computer to execute some or all of the steps of any method described in the method embodiment. The computer program product may be a software installation package, and the computer includes an electronic device.
[0255] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0256] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0257] In the several embodiments provided in the present application, it should be understood that the disclosed device can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of the above-mentioned units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0258] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0259] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0260] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a memory, including a number of instructions to enable a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of the above-mentioned methods of each embodiment of the present application. The aforementioned memory includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, RandomAccess Memory), mobile hard disk, disk or CD-ROM and other media that can store program codes.
[0261] The embodiments of the present application are introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A flash loan attack identification method, characterized in that: The method comprises: By presetting the Ethereum full node to monitor the target blockchain, the first event log is obtained; Construct a first event token flow graph according to the first event log; the first event token flow graph includes n account addresses in the target blockchain for receiving or forwarding tokens; n is a positive integer; Determining whether there is a flash loan transaction in the first event log according to the first event token flow graph; When there is a flash loan transaction in the first event log, determine the target AMM node with a flash loan attack risk among the n account addresses; Obtain the target smart contract bytecode and historical transaction data corresponding to the target AMM node; Determine a first price model corresponding to the target smart contract bytecode; determine a second price model corresponding to the historical transaction data; the first price model and the second price model are both mathematical expressions describing the price relationship of a preset transaction token pair; the preset transaction token pair includes a first token and a second token; Determine a comprehensive price model according to the first price model and the second price model; Estimate the probability that the flash loan transaction is a flash loan attack based on the comprehensive price model to obtain a target flash loan attack probability; When the target flash loan attack probability is greater than a preset attack probability threshold, the flash loan transaction is determined to be a flash loan attack.
2. The method according to claim 1, characterized in that The step of determining the target AMM node with a flash loan attack risk among the n account addresses includes: Obtaining historical transaction features of each of the n account addresses to obtain n historical transaction features; Determine the flash loan nodes and AMM nodes in the n account addresses according to the n historical transaction features, and obtain i flash loan nodes and j AMM nodes; i and j are both natural numbers less than or equal to n, and the sum of i and j is less than or equal to n; Convert the first event token flow graph into a directed acyclic graph to obtain a second event token flow graph; Initialize the first dictionary; the key in the first dictionary is the node ID in the second event token flow graph, and the value represents the node set that controls the flow of its tokens to the current node upstream of the node; during initialization, the value corresponding to the entry node of the second event token flow graph is initialized to a node set containing only its own node ID, and the values of the remaining nodes are set to the initial set V; the initial set V stores the node IDs of all nodes in the second event token flow graph; The second event token flow graph is subjected to node identification according to the first dictionary to obtain the target AMM node.
3. The method according to claim 2, characterized in that The performing node identification on the second event token flow graph according to the first dictionary to obtain the target AMM node includes: Traversing the nodes in the second event token flow graph using a preset traversal method, performing a preset risk identification operation on each traversed node to obtain the target AMM node; the target AMM node includes at least one node; The preset risk identification operation specifically includes: Get the first node v currently traversed; get the first predecessor node set W corresponding to the first node v in the second event token flow graph; initialize the first set R to an empty set, and the first set R is used to store the node set; Based on the first set R, a preset dictionary update operation is performed on the first predecessor node set W to update the first dictionary to obtain a target dictionary; Taking out the value corresponding to each AMM node in the j AMM nodes from the target dictionary to obtain j values; Determine that the value of the flash loan node is included in the j values, and obtain m values; m is a natural number less than or equal to j; Determine that the nodes corresponding to the m values are nodes in the target AMM node.
4. The method according to claim 3, characterized in that The preset dictionary update operation includes: Extract nodes from the first predecessor node set W in a preset order, and perform a first dictionary update operation on each extracted node to obtain a second dictionary; Checking whether the second dictionary is consistent with the first dictionary; If they are inconsistent, obtaining the next node of the first node v, and performing the preset dictionary update operation on the next node; If they are consistent, determining the target dictionary according to the second dictionary, and stopping the preset dictionary update operation; The first dictionary update operation includes: Obtaining a first node ID corresponding to a first predecessor node w; the first predecessor node w is a node taken out from the first predecessor node set W at the current moment; Extract the value corresponding to the first node ID from the first dictionary, and record it as a first set DV; When the first set R is an empty set, R=DV; When the first set R is not an empty set, R=R∩DV; Add the first node v to the first set R to obtain a target set; Update the value of the first node v in the first dictionary with the target set.
5. The method according to any one of claims 1 to 4, characterized in that: The determining a first price model corresponding to the target smart contract bytecode includes: Decompile the target smart contract bytecode to obtain a target control flow graph; Obtain the external function name and exchange parameters for exchanging tokens from the target ABI file corresponding to the target smart contract bytecode; Calculate the function signature according to the preset hash function, the external function name and the exchange parameter to obtain the target function signature; Determine an entry node in the target control flow graph according to the target function signature; Starting from the entry node, traverse the target control flow graph to obtain a traversed control flow subgraph; The first price model is inferred based on the control flow subgraph.
6. The method according to claim 5, characterized in that The step of inferring the first price model according to the control flow subgraph includes: Initialize the initial variable model VTable-init; the initial variable model VTable-init is a table for storing tuples (var, val), var is the unique label of the variable, and val is the possible value of the variable; Initialize the stack S to be empty, and then push the first element (VTable-init, entry) into the stack S; wherein entry is the node address of the entry node; Execute the preset variable propagation tree construction operation to obtain the target variable propagation tree; Starting from the leaf node of the target variable propagation tree, by using the child node to express the parent node, iteratively executing to the root node y of the target variable propagation tree, expanding the variable y corresponding to the root node y into the expression of the leaf nodes in the target variable propagation tree, and obtaining the target expression; The first price model is obtained by replacing the variables in the target output amount_out based on the target expression.
7. The method according to claim 6, characterized in that The preset variable propagation tree construction operation includes: S11, determining whether the stack S is empty, if it is empty, stopping the execution of the preset variable propagation tree construction operation, and taking the latest generated variable propagation tree as the target variable propagation tree; if it is not empty, jumping to step S12; S12, taking out the top element from the stack S, obtaining the target element (VTable, node), determining the target node according to the node address node, updating the variable model VTable based on the content in the target node, and obtaining an updated variable model VTable-new; VTable represents the variable model before entering the target node; S13, determining a node in the control flow subgraph that contains a LOG instruction, obtaining a first control flow graph node, acquiring LOG parameter data of the LOG instruction in the first control flow graph node, and identifying the target output amount_out from the LOG parameter data; S14, determining the variable y corresponding to the target output amount_out in the control flow subgraph; S15, constructing a variable propagation tree with the variable y as the root node according to the variable model stored in the stack S, to obtain a first variable propagation tree; S16. Push the second element (VTable-new, node-next) into the stack S, and jump to step S11; wherein node-next is the node address of the successor node of node.
8. The method according to claim 7, characterized in that The updating of the variable model VTable based on the content in the target node to obtain an updated variable model VTable-new includes: Acquire a target statement type of the target node; the target statement type includes one of the following: a declaration statement, an assignment statement; Determine a target variable model update operation corresponding to the target statement type; The variable model VTable is updated according to the target variable model update operation to obtain the updated variable model VTable-new.
9. The method according to claim 7, characterized in that The step of constructing a variable propagation tree with the variable y as a root node according to the variable model stored in the stack S to obtain a first variable propagation tree includes: Obtaining a target variable model corresponding to the stack S; Initialize the two-tuple set TreeE to be an empty set; the two-tuple set TreeE is used to store the variable tree; Initialize the variable stack VarS to be empty, then push the variable y into the variable stack VarS; the variable stack VarS is used to store the variables to be processed; S21, taking out the variable v' from the variable stack VarS, and reading the val value of var=v' from the target variable model; S22. If the value of val is a constant value, the first group of elements (v', val) is added to the two-tuple set TreeE, indicating that there is an edge from v' to val in the variable propagation tree; S23. If the value of val is a function expression, scan the function expression from left to right, collect the variables v_e and constant c contained therein, add the second group of elements (v', v_e) and the third group of elements (v', c) to the two-tuple set TreeE in turn, and push the variable v_e into the variable stack VarS; S23. Determine whether the variable stack VarS is empty. If it is empty, determine the first variable propagation tree according to the two-tuple set TreeE. If it is not empty, jump to step S21.
10. A computer-readable storage medium, characterized in that: A computer program for electronic data exchange is stored, wherein the computer program enables a computer to execute the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Lightning loan identification method and device based on smart contract
CN115187366A
Intelligent contract lightning loan attack early warning system and method based on byte code analysis
CN117522582A
System and method for swapping token using decentralized finace
KR102564770B1
Graph-based analysis and visualization of digital tokens
US20230070625A1