The Chain logo

Introduction to Smart Contract Security Auditing

smart contract audit services

By cypher shieldPublished 2 years ago 5 min read
Like

Background overview

In the last article, we learned several common ways to obtain random numbers in smart contracts , as well as their advantages and disadvantages, and also introduced the harm caused by pseudo-random numbers in smart contracts. Traditional network security also exists in the problem of smart contract audit services— denial of service.

Pre-knowledge

Traditional network security denial of service attack (DoS) : DoS is the abbreviation of Denial of service, that is, denial of service. Any interference to a service that reduces or loses its availability is called a denial of service. Common denial-of-service attacks against network protocols are roughly as follows: SYN Flood, IP spoofing attack, UDP flood attack, Ping flood attack, Teardrop attack, Land attack, Smurf attack, Fraggle attack, etc.

Smart contract denial-of-service attacks : code logic errors, compatibility errors, or excessive call depth (a feature of blockchain virtual machines ) that can cause smart contracts to fail to function properly . The denial of service attack methods in smart contracts are relatively simple, including but not limited to the following three:

1. Denial of service attack based on code logic: This type of denial of service attack is generally caused by the imprecision of the contract code logic. The most typical example is when there is logic in the contract to loop through the incoming mapping or array And when there is no limit to the length of the incoming map or array, an attacker can consume a large amount of Gas by passing in a super-long map or array for loop traversal, so that the Gas of the transaction overflows, and finally makes the smart contract audit temporarily or permanently inoperable.

2. Denial of service attack based on external call: This denial of service attack is caused by improper handling of external calls in the contract. For example, there is a knot in a smart contract that changes the contract state based on the execution of an external function and does not deal with the fact that the transaction has been failed. The attacker will use this feature to intentionally fail the transaction, and the smart contract will keep repeating the failed transaction. As a result, the smart contract logic card cannot continue to be executed here, and finally the smart contract is temporarily or permanently inoperable.

Vulnerability Analysis

We can see that the purpose of the above contract is to select the “King of Ether”. Players can compete for the title of “King of Ether” by entering any amount of Ether that is greater than the previous user in the claimThrone() contract. When the coin is higher than that of the previous player, the ether that the player has entered remains in the contract and gets the title of “King of Ether”, and the ether of the previous player will be returned in the same way.

We can see that the logic of generating the new king and returning the old king is completed in the same function, and the return value sent of the refund is also checked in claimThrone() . Let’s combine this feature to complete the attack.

Attack contract

Note: The following contract code logic and attack scenarios are only for demonstration examples, please do not make random associations.

3. Operation management-based denial of service attack: This type of denial of service attack is based on post-operational situations. For example, in smart contracts, the Owner account is usually used as the administrator role, and this role usually holds very high permissions. For example, enabling or suspending the transfer function may be subject to a non-subjective denial of service attack when the Owner role fails to operate or the private key is lost.

Vulnerability example

Through the pre-knowledge, I believe that everyone has a certain understanding of denial of service attacks. The most typical of the three methods to trigger denial of service attacks is the denial of service attack based on external calls. Let’s take a look at a typical code example below:

Vulnerability Analysis

We can see that the purpose of the above contract is to select the “King of Ether”. Players can compete for the title of “King of Ether” by entering any amount of Ether that is greater than the previous user in the claimThrone() contract. When the coin is higher than that of the previous player, the ether that the player has entered remains in the contract and gets the title of “King of Ether”, and the ether of the previous player will be returned in the same way.

We can see that the logic of generating the new king and returning the old king is completed in the same function, and the return value sent of the refund is also checked in claimThrone() . Let’s combine this feature to complete the attack.

Attack contract

Note: The following contract code logic and attack scenarios are only for demonstration examples, please do not make random associations.

Repair suggestion bsc smart contract audit.

As a developer

1. In the development of smart contracts, attention should be paid to dealing with continuous failures, such as asynchronous processing of external call logic that may fail.

2. Pay attention to Gas consumption when using call to make external calls and use loops and traversals.

3. Avoid over-authorization of a single role. When dealing with contract permissions, a reasonable division of permissions should be implemented. Multi-signature wallet management should be used for roles with permissions to prevent loss of permissions due to private key leakage.

smart contractblockchain
Like

About the Creator

cypher shield

Get your smart contracts audited and certified by leading smart contract security experts. Our smart contract audit services cover functionality, vulnerabilities, and gas efficiency. Talk to a consultant now to get started.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.