Disclosure of Invention
The purpose of the invention is as follows: considering that data on blockchains has the property of being non-modifiable, the severity of smart contract problems is much more severe than in other areas. The invention provides a regular expression and program instrumentation based Ethenhouse intelligent contract problem detection and prevention method and device, aiming at the Ethenhouse intelligent contract developed by using a solid language, and achieving the purpose of detecting and preventing the generation of problems by using a defined regular expression, detection rule and prevention code construction method for describing different problem characteristics.
The technical scheme is as follows: in order to achieve the purpose, the invention adopts the following technical scheme:
a method for detecting and preventing problems of an Ether intelligent contract comprises the following steps:
step 1: acquiring an Ether house intelligent contract to be detected, and formatting a source code;
step 2: determining corresponding regular expressions and detection rules according to the characteristics of different problems, and determining a construction method of a code statement for preventing reentry vulnerabilities and integer overflow vulnerabilities;
and step 3: sending the formatted codes to different problem detection programs, and detecting different kinds of intelligent contract problems by the problem detection programs according to the regular expressions and the detection rules defined in the step 2;
and 4, step 4: for reentry vulnerabilities, sending the formatted codes to a reentry vulnerability prevention program, positioning code sentences possibly introduced with reentry vulnerabilities by the reentry vulnerability prevention program according to the regular expression defined in the step 2, constructing prevention codes to be inserted according to contract contents, and finally inserting the prevention codes into the intelligent contracts;
and 5: for the integer overflow vulnerability, sending the formatted codes to an integer overflow vulnerability prevention program, positioning code statements possibly introducing the integer overflow vulnerability by the integer overflow vulnerability prevention program according to the regular expression defined in the step 2, constructing a prevention code to be inserted according to contract contents, and finally inserting the prevention code into an intelligent contract;
step 6: and outputting the detection or prevention result of at least one step from the step 3 to the step 5 to the user according to the selected function of the user.
Preferably, the ethernet intelligent contract source code used as input in step 1 is written using ethernet intelligent contract programming language Solidity.
Preferably, the types of intelligent contract problems in step 2 include at least one of strictly comparing contract deposits, unprocessed exceptions, denial of service by external addresses, identity verification using tx. origin, missing constructors, locked money, unsafe type inference, use of Byte [ ], expensive cycles, timestamp dependencies, non-standard token interfaces, use of floating point numbers, private visibility, redundant denial of payment, compiler version problems, non-standard programming styles, integer division, non-explicit visibility; each problem is characterized by at least one regular expression.
Preferably, the step 3 comprises the following steps:
step 31: reading an intelligent contract file storing a formatting code;
step 32: sending the formatted codes to different kinds of problem detection programs;
step 33: each problem detection program saves the formatting codes as a character string array, traverses line by line, and matches the line of codes by using a regular expression and a detection rule defined for the problem; if the match is successful, the code of the line is determined to contain the problem; if the matching fails, the code of the line is determined not to contain the problem;
step 34: and (4) counting the problem types and the number according to the detection results of all the problem detection programs in the step (33), and determining the number of lines of each problem.
Preferably, the inserting the code in the step 4 prevents the generation of a re-entry vulnerability, and specifically includes:
step 41: reading a file storing a formatted intelligent contract source code;
step 42: sending the formatted codes to a reentry vulnerability generation preventing program;
step 43: the method comprises the steps that a reentry vulnerability generation program is prevented from saving a formatting code into a character string array;
step 44: traversing the array row by the program for preventing the reentry vulnerability generation, positioning code sentences which possibly introduce the reentry vulnerability according to the regular expression and the detection rule defined in the step 2, if the sentences exist in the codes, turning to the step 45, otherwise, turning to the step 5;
step 45: for each line of code statements which can introduce reentry holes, firstly, traversing formatted codes line by line from the beginning, searching a first account variable recording account addresses and address holding token quantity relations, and acquiring the name of the account variable; then, obtaining the address of the received Ethernet currency from a code statement which possibly introduces a reentry vulnerability; finally, according to the address of the received Ethernet currency and the name of the ledger variable, a preventive code to be inserted into the contract is constructed;
step 46: firstly, constructing a function calling chain according to a function calling relation in a contract; the precautionary code constructed in step 45 is then inserted into a different location in the call chain according to the function call chain.
Preferably, the step 5 comprises the following steps:
step 51: reading a file storing a formatted intelligent contract source code;
step 52: storing the formatted code as a character string array;
step 53: traversing the character string array line by line, positioning the integer arithmetic statement by using the regular expression and the detection rule defined in the step 2, if the integer arithmetic statement is matched with the character string array, constructing a prevention code according to the matched integer arithmetic statement, and then inserting the prevention code in front of and behind the integer arithmetic statement; if not, the scan matches the next row.
Preferably, in step 6, the detection report generated in step 3, or the intelligent contract generated in step 4 for preventing reentry of vulnerabilities, or the intelligent contract generated in step 5 for preventing integer overflow vulnerabilities, is output to the user according to the function selected by the user.
The invention relates to an intelligent Ethernet room contract problem detection and prevention device which comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein the computer program realizes the intelligent Ethernet room contract problem detection and prevention method when being loaded to the processor.
Has the advantages that: the method for detecting and preventing the problems of the Ether house intelligent contracts provided by the invention can be suitable for intelligent contracts written by using the Ether house intelligent contract high-level programming language Solidity. In the problem detection part, detecting the intelligent contract problem through the defined regular expressions and detection rules for describing different problem code statement characteristics; in the problem prevention part, code sentences which possibly introduce problems are positioned through defined regular expressions and detection rules which describe different problem code sentence characteristics, and then prevention codes are constructed and inserted into contracts to achieve the purpose of preventing the problems from being generated. Compared with the prior art, the method has higher problem coverage rate and better detection efficiency when detecting or preventing the problem of the intelligent house contract, and also has better detection accuracy. The invention can be used for rapidly detecting the problem of the intelligent contracts of the Ether workshop, or when the intelligent contracts of the Ether workshop are manually audited, the invention can be used for providing guidance for manual auditing after the contracts are scanned and audited.
Detailed Description
The present invention is further illustrated by the following examples, which are intended to be purely exemplary and are not intended to limit the scope of the invention, as various equivalent modifications of the invention will occur to those skilled in the art upon reading the present disclosure and fall within the scope of the appended claims.
As shown in fig. 1, the method for detecting and preventing problems of an intelligent ethernet contract based on regular expressions and program instrumentation, disclosed in the embodiments of the present invention, mainly includes 6 steps:
step 1: acquiring an Ether house intelligent contract to be detected, and formatting a source code;
step 2: determining corresponding regular expressions and detection rules according to the characteristics of different problems, and determining a construction method of a code statement for preventing reentry vulnerabilities and integer overflow vulnerabilities;
and step 3: sending the formatted codes to different problem detection programs, and detecting different kinds of intelligent contract problems by the problem detection programs according to the regular expressions and the detection rules defined in the step 2;
and 4, step 4: for reentrant holes, the method aims to prevent reentrant holes from being generated. Sending the formatted codes to a program for preventing reentry vulnerabilities, positioning code sentences possibly introduced with reentry vulnerabilities by the program for preventing reentry vulnerabilities according to the regular expression defined in the step 2, constructing preventive codes to be inserted according to contract contents, and finally inserting the preventive codes into an intelligent contract;
and 5: for integer overflow holes, the method aims to prevent the integer overflow holes from being generated. Sending the formatted codes to an integer overflow vulnerability prevention program, positioning code sentences possibly introducing integer overflow vulnerabilities by the integer overflow vulnerability prevention program according to the regular expressions defined in the step 2, constructing prevention codes to be inserted according to contract contents, and finally inserting the prevention codes into an intelligent contract;
step 6: and outputting the detection or prevention result of at least one step from the step 3 to the step 5 to the user according to the selected function of the user.
The detailed steps of the method for detecting and preventing the problem of the intelligent Ethern contracts based on the regular expressions and the program instrumentation, disclosed by the embodiment of the invention, are described by taking the example of the intelligent Ethern contracts written in the Solidity language and disclosed by any part of the Ethern browsers, and specifically comprise the following steps:
step 1: acquiring an Ethern intelligent contract written by using a Solidity language, and formatting a source code, wherein the specific process is as follows:
step 11: any piece of EtherFang intelligent contract written by using the Solidiy language is selected from an EtherFang browser official network (https:// etherscan. io/verifiedContacts /), the source code of the contract is copied and saved locally as a file in the sol format.
Step 12: reading in the intelligent contract source code line by line, and storing the source code in a character string array. The array is traversed row by row, and the code lines with only line feed symbols in one line are discarded. And traversing the character string array after discarding the empty row line by line, and replacing the annotation content with a space character if the annotation content is contained in a certain row. And traversing the character string array with the filtered annotations and the empty lines line by line, and deleting redundant spaces of each line. Traversing the character string array with the empty lines, the comments and the redundant spaces filtered out line by line, splicing the contents of each line into a character string, and then discarding the character string array. Traversing character strings character by character, and replacing all line feed characters with blank spaces;
step 13: the character string after replacing the line break is traversed character by character, and when a semicolon (;), left curly brace ({) or right curly brace (}) is encountered, a line break is inserted after the semicolon (;), left curly brace ({) or right curly brace (}). And 14, outputting the character string processed in the step 23 into a sol format file (Ether intelligent contract file) with a naming rule of 'original file name _ format.sol', and finishing code formatting after the step
Step 2: according to the characteristics of different problems, determining corresponding regular expressions and detection rules, and determining a construction method of code sentences for preventing reentry vulnerabilities and integer overflow vulnerabilities, wherein the specific process is as follows:
step 21: according to the types and the characteristics of the problems of the existing Ether house intelligent contracts, regular expressions for describing different problem statement characteristics are compiled, and a detection rule for detecting each problem is formulated.
For example, an ethernet house may force the transmission of ethernet currency to any address, so the function of the contract should not depend on the ethernet currency balance of the contract being at some fixed value. Because an attacker can force to send ethernet coins to the contract, stating the problem as "strictly comparing contract deposits", a code statement that compares the deposits in an if statement or a requre statement (which terminates execution when the condition determination portion is false) is described using the regular expression "Λ (\ s) ((if) | (white) | (velocity) | (d)) + (ether)) | (\\ d) + (\ s) (\\ s) (} s) (\ s) (-) - (which is strictly comparing deposits" which results in a "strictly comparing deposit" problem.
For another example, in an ethernet intelligent contract, a constructor runs when the contract is deployed, and a value is usually assigned to key information of the contract in the constructor. The function with the same name as the contract can be used as the constructor in the ether house, and the constructor key word can also be used for declaring the constructor, and the damage of the constructor or the wrong spelling of the constructor name can cause the key information of the contract to be falsified. Therefore, the detection rule formulated according to the problem feature is: after acquiring the contract name, detecting whether a function declared by using a constructor or a function with the same name as the contract is contained in the contract. If yes, the problem is not caused; if not, this problem exists.
The problem types and corresponding problem descriptions detected by the embodiment of the invention are shown in table 1, and the problem types, the problem detection rules and the corresponding regular expressions are shown in table 2 (including reentry vulnerabilities and integer overflow vulnerabilities).
TABLE 1 problem categories and descriptions
TABLE 2 problem detection rules and regular expressions
Step 22: and determining a construction method of an inserted code for preventing the reentry vulnerability and the integer overflow vulnerability according to the problem characteristics of the Ethernet intelligent contract reentry vulnerability and the integer overflow vulnerability. For example, an integer overflow hole exists in an ethernet, and in order to prevent the integer overflow hole from being generated, the operation result of each sentence of integer operation code should be checked. Therefore, the invention captures each sentence of integer operation code through the regular expression, constructs the check code by intercepting the variable name in the code, and inserts the check code into the proper position in the contract to achieve the effect of preventing the integer overflow vulnerability from being generated. Specifically, the construction method of the inserted code for preventing the re-entry vulnerability and the integer overflow vulnerability is respectively shown in step 4 and step 5.
And step 3: if the function selected by the user is to detect the problem in the intelligent contract, matching the problem statement by using the regular expressions which are written in the step 2 and describe different problem characteristics and the formulated detection rule, and specifically comprising the following steps:
step 31: reading an intelligent contract file storing a formatting code;
step 32: sending the formatted codes to different kinds of problem detection programs;
step 33: each problem detection program saves the formatted code as an array of strings, traverses line by line, matches this line of code using the regular expression (see table 2) and detection rules defined for this problem. If the match is successful, the code of the line is determined to contain the problem; if the matching fails, the code of the line is determined not to contain the problem;
step 34: and (4) counting the problem types and the number according to the detection results of all the problem detection programs in the step (33), and determining the number of lines of each problem.
And 4, step 4: if the function selected by the user is to prevent the reentry vulnerability in the intelligent contract, the sentence which is possibly introduced with the reentry vulnerability is positioned by using a regular expression which describes the characteristics of the sentence which is possibly introduced with the reentry vulnerability and a formulated detection rule, and then the goal of preventing the reentry vulnerability is achieved by inserting a prevention code, and the specific steps are as follows:
step 41: reading a file storing a formatted intelligent contract source code;
step 42: sending the formatted codes to a reentry vulnerability generation preventing program;
step 43: the method comprises the steps that a reentry vulnerability generation program is prevented from saving a formatting code into a character string array;
step 44: and (3) traversing the array row by the program for preventing the reentrant vulnerability generation, positioning code statements possibly introducing the reentrant vulnerability according to the regular expression written in the step (2) and the formulated detection rule, and calling the statements possibly introducing the reentrant vulnerability as dangerous statements. If the code has a sentence with the characteristic described by the regular expression, turning to step 45, otherwise, turning to step 5;
step 45: a code statement that re-enters a bug may be introduced for each row. Firstly, traversing formatted codes line by line from the beginning, searching a first variable recording the relation between account addresses and the number of tokens held by the addresses, calling the variable as an account book variable, and acquiring a variable name of the account book variable; then, obtaining the address of the received Ethernet currency from a code statement which possibly introduces a reentry vulnerability; finally, a preventive code to be inserted into the contract is constructed based on the address of the received ethernet currency and the name of the ledger variable. The following four types of codes were constructed in total, see table 3:
TABLE 3 constructing code to prevent reentry vulnerabilities
Step 46: firstly, according to the function call relation in the contract, a function call chain is constructed, and the construction method of the call chain is illustrated, if the following codes exist in one contract:
that is, function B is called in function a, and function C is called in function B, and function C contains a dangerous statement. Thus, a chain-like function call relationship is generated, and for convenience of description, the following definitions are defined: the function at the head of the function call chain (function a in this example) is the chain head function and the function at the tail of the function call chain (function C in this example) is the chain tail function; in the function calling chain, a function which does not contain a dangerous statement in the function body is an indirect calling function, and a function which contains the dangerous statement in the function body is a direct calling function; the prevention code constructed in step 45 is then inserted into a different location in the call chain, according to the above definition. The location of the various types of preventive code insertions is described in table 4.
TABLE 4 insertion location for reentrant vulnerability code prevention
| Code type
|
Insertion position
|
| A
|
Function body first line for directly calling function or chain head function
|
| B
|
Directly calling the previous line of dangerous statement in function
|
| C
|
Directly calling one line behind dangerous statement in function
|
| D
|
First line of contract body |
And 5: if the function selected by the user is to prevent integer overflow vulnerability in the intelligent contract, the sentence possibly introducing integer overflow vulnerability is positioned through the regular expression and the detection rule written in the step 2 and describing the characteristics of the sentence possibly introducing integer overflow vulnerability, and then the purpose of preventing the integer overflow vulnerability is achieved by inserting a prevention code, and the specific steps are as follows:
step 51: reading a file storing a formatted intelligent contract source code;
step 52: storing the formatted code as a character string array;
step 53: and traversing the character string array line by line, and positioning the integer operation statement by using the regular expression and the detection rule written in the step 2. If the integer arithmetic statement is matched with the integer arithmetic statement, constructing a prevention code according to the matched integer arithmetic statement, constructing the prevention code according to the difference of the acquired integer arithmetic code, inserting the prevention code before and after the integer arithmetic code, and showing the insertion positions of the acquired integer arithmetic code, the constructed prevention code and the prevention code in a table 5; if not, the scan matches the next row.
Table 5 correspondence of integer arithmetic codes, constructed preventive codes, and insertion positions of preventive codes captured
Step 6: and summarizing the detection result into a detection report according to the function selected by the user, or returning an intelligent contract for preventing the reentry vulnerability after the prevention code is inserted, or an intelligent contract for preventing the integer overflow vulnerability after the prevention code is inserted to the user.
Based on the same inventive concept, the device for detecting and preventing the problem of the intelligent Ethernet house contract disclosed by the embodiment of the invention comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein the computer program realizes the method for detecting and preventing the problem of the intelligent Ethernet house contract when being loaded to the processor.