The Ultimate Smart Contract Auditing Guide

A Guide on How to Start and Common Bugs you can Find, All in 13 Quick Parts

0xPredator
9 min readJun 14, 2022
Photo by Michael Förtsch on Unsplash

Introduction

Blockchain technology is changing the game for businesses in every industry, and smart contracts are a key piece of that puzzle. Smart contracts ensure trust between parties by eliminating third-party intermediaries, helping to lower costs while also increasing transparency. If you’re writing a smart contract, it’s critical that you audit the code to ensure it will work correctly — and won’t have any bugs or security vulnerabilities. This guide will walk you through the basics of auditing your smart contract and some common smart contract vulnerabilities.

Part 1: Why Audit Your Smart Contract?

There are many reasons why smart contracts are important. They can help you automate business processes and make them more efficient, which saves time and money. But the biggest reason why they’re so popular is that they reduce the risk of fraud by removing human error from the equation. However, these benefits come with a cost: it’s easy to make mistakes when developing a smart contract. A flawed codebase could cause your entire system to fall apart or even be vulnerable to hacking attempts! You may have heard about hacks on Ethereum-based tokens. These hacks resulted from vulnerabilities in smart contracts which allowed hackers to steal millions of dollars worth of cryptocurrencies stored within those contracts.

Part 2: How to Start an Audit

Before you start your audit, you should first conduct a security review. This will allow you to identify any issues and vulnerabilities in the smart contract.

The next step is understanding the business logic of the contract. You should understand why it was created, what problem it solves for users, and how it works with other contracts on the blockchain.

After that comes technical architecture analysis: looking at all of the parts involved in writing and deploying this code, as well as understanding how they’re implemented in programming languages like Solidity.

Finally, we have threat modelling where we look at different parties involved such as users/end-users who interact with the system .

Part 3: The Different Phases of a Smart Contract Audit

The different phases of a smart contract audit include:

Identifying the goals and objectives of the audit:

This step you set the goals and objectives of the smart contract audit.

Determining the scope of the audit:

In this step, you collect all assets included in the scope of the audit to ensure nothing is left out.

Planning the audit:

An important phase of an audit is to plan it, this allows the audit to be done efficiently and effectively.

Conducting the audit:

The real work, trying to find vulnerabilities in the code of smart contracts.

Reporting the results of the audit:

Now that all of your hardwork is over, it’s time to write a report of what you have found during the audit.

Part 4: Testing in General

There are many different types of testing, but the most important thing to remember is that you should never trust code you haven’t tested. You can test your smart contract code in many ways, including:

  • Testing in a sandbox environment (such as Remix)
  • Testing on a testnet (such as Rinkeby)
  • Private blockchain networks designed specifically for testing purposes (e.g., Ganache)

Part 5: Basic Security Considerations

As a smart contract developer, you have the responsibility to keep your code secure. This is the only way that you can guarantee that your users’ funds will be safe and secure.

In order to ensure proper security, there are some basic practices that must be followed. These include:

  • Use secure coding practices
  • Use a secure development environment

Part 6: Common Ethereum Vulnerabilities

Ethereum smart contracts are vulnerable to a number of hacks and attacks. The most common vulnerabilities are:

  • Reentrancy: This is where a malicious contract can call another contract multiple times, draining the victim contract’s resources.
  • Integer overflow/underflow: This is where an arithmetic operation results in a value that is outside of the expected range. This can be exploited to send more or less ETH than intended.
  • Timestamp dependence: This is where a contract relies on the current timestamp to determine certain conditions. This can be exploited to manipulate the state of the contract.

We will discuss some common vulnerabilities in the following points. You can also check out my other articles for more information on re-entracy and integer overflow/underflow vulnerabilities.

Part 7: Storage Variable Vulnerabilities

One of the most common security vulnerabilities in Ethereum smart contracts is the use of insecure storage variables. These variables can be used to store sensitive information, such as private keys and passwords, which can be easily compromised if they are not properly protected. There are a few different ways to protect storage variables, but the most effective method is to use a library like SafeMath, which ensures that all maths operations are performed in a secure manner. Other methods include using a hardware security module (HSM) to store the variables, or using multi-signature wallets to protect the private keys. The best way to protect against storage vulnerabilities is to use a combination of these methods, as each has its own strengths and weaknesses. By using multiple layers of security, it is much more difficult for an attacker to compromise the data stored in a smart contract.

Part 8: Misuse of Transfer and Send

When it comes to smart contracts, there are a few key things to keep in mind in order to avoid any misuse. First and foremost, it’s important to remember that smart contracts are immutable. This means that once they’re deployed, they can’t be changed. As such, it’s crucial to double-check and triple-check your code before deploying it.

One common mistake that can lead to misuse of smart contracts is confusion between the transfer and send functions. These two functions may seem similar, but they actually have very different purposes. The transfer function is used to transfer ownership of an asset from one address to another. On the other hand, the send function is used to send an amount of an asset from one address to another.

If you accidentally use the send function when you meant to use the transfer function, it could lead to some serious consequences. For example, let’s say you have a smart contract that allows users to buy and sell tokens. If you use the send function instead of the transfer function when transferring ownership of the tokens, then the users will still be able to buy and sell the tokens, but you will no longer be the owner of the contract. This could lead to all sorts of problems, so it’s important to be careful and use the correct function for the task at hand.

In short, misuse of smart contracts can have serious consequences. Always be sure to double-check your code before deploying it, and make sure you understand the difference between the transfer and send functions. By following these simple tips, you can help avoid any misuse of smart contracts.

Part 9: Function Visibility Vulnerabilities

When it comes to visibility of functions in smart contracts, there are two main types of vulnerabilities that can occur. The first type of vulnerability is when a function is made public when it should not be. This can happen if the function is not properly marked as private or if the visibility is accidentally set to public. This type of vulnerability can be exploited by anyone who has access to the contract, and can lead to the contract being compromised.

The second type of vulnerability is when a function is made private when it should not be. This can happen if the visibility is accidentally set to private or if the function is not properly marked as public. This type of vulnerability can be exploited by anyone who has access to the contract, and can lead to the contract being compromised.

Part 10: Integer Arithmetic Errors

When you’re writing a smart contract, it’s important to be careful of integer arithmetic errors. These can occur when you’re doing calculations with integers that are too large or too small.

For example, let’s say you’re trying to calculate the total value of all the tokens in a smart contract. The value of each token is stored as an integer. If there are more than 2,147,483,647 tokens in the contract, then the total value will be too large to fit into an integer variable. This will cause an error.

To avoid this, you can use a library like SafeMath which will help you do integer arithmetic safely.

Part 11: Loops and Recursion Vulnerabilities

As we’ve seen, loops and recursion can be used to great effect in programming. However, they can also be abused to create vulnerabilities.

One example of this is the so-called “infinite loop”. This is when a program gets stuck in a loop and can never break out of it. This can cause the program to crash or, worse, consume all the resources of the system it’s running on, causing a Denial of Service attack. This can be detrimental for smart contracts as it can cause users to lose money.

Another example is when a program recurses too deeply. This can also lead to a crash, or to the program running out of memory and being forced to terminate.

Both of these vulnerabilities can be exploited by an attacker to cause problems for the system or its users. Therefore, it’s important to be aware of them and to take steps to prevent them from being exploited.

Part 12: Prevention, Detection, Mitigation, and Remediation

Prevention: The best way to prevent issues with smart contracts is to have a thorough and complete testing process. This should include both unit testing of the contract code as well as functional testing of the contract in its intended environment. It is also important to have a clear and well-defined specification for the contract so that all parties involved understand exactly what the contract is supposed to do.

Detection: Issues with smart contracts can often be detected by monitoring the contract’s performance and comparing it to the expected behaviour. If there are any discrepancies, this can indicate that there is a problem with the contract. It is also possible to use static analysis tools to check the contract code for errors or potential vulnerabilities.

Mitigation: If an issue is detected in a smart contract, it is important to take steps to mitigate the problem. This may involve changing the contract code to fix the issue, or it may involve changing the way the contract is used. For example, if a contract is found to be vulnerable to a particular attack, it may be possible to mitigate the problem by changing the way the contract is used so that the attack is no longer possible.

Remediation: If an issue cannot be mitigated, then it may be necessary to take steps to remediate the problem. This may involve replacing the contract with a new version that does not have the same issue, or it may involve completely removing the contract from use.

Part 13: Automated Tools for Vulnerability Detection

As the popularity of smart contracts continues to grow, so does the need for automated tools that can scan these contracts for vulnerabilities.

There are a number of different automated vulnerability scanners available, each with its own strengths and weaknesses.

Mythril is one popular option that uses symbolic execution to analyze contracts and find potential vulnerabilities.

Oyente is another popular choice that uses static analysis to find potential issues.

Securify is a newer tool that uses a combination of static and dynamic analysis to find vulnerabilities.

These scanners are not always 100% accurate so it’s important to do your own testing and manual reviews.

Resources for Smart Contract Hacking/Auditing

Videos:

https://www.youtube.com/watch?v=LLiJK_VeAvQ
Auditing Smart Contracts — Security Review of Ethereum Applications
https://www.youtube.com/watch?v=DkVpUqzU8SI
Learn How to Audit Ethereum Smart Contracts — presented by bloctrax
https://www.youtube.com/watch?v=WchXkMlKj9w
BHIS | Getting Started in Blockchain Security and Smart Contract Auditing
https://www.youtube.com/watch?v=qRznPHUFCLA
Smart Contracts 101 — How to Audit an NFT Project
https://www.youtube.com/watch?v=PTE1Hcqup_M
Advanced Smart Contract Security and Auditing Tools
https://www.youtube.com/watch?v=0aJfCug1zTM
Smart Contract Security and Auditing 101
https://www.youtube.com/watch?v=R1eZCmR91vQ
How To Find Solidity Vulnerabilities
https://www.youtube.com/watch?v=IOUnhCTw6tE
Advanced Smart Contract Hacking

Learning attacks:
https://consensys.github.io/smart-contract-best-practices/known_attacks/
https://www.youtube.com/watch?v=apCGPh7tKhw
https://www.youtube.com/watch?v=R1eZCmR91vQ — Some practical thoughts on hunting for specific common issues
https://swcregistry.io/

For Practice:
https://www.damnvulnerabledefi.xyz/index.html
https://ethernaut.openzeppelin.com/

A Roadmap:
https://github.com/razzorsec/AuditorsRoadmap

A bunch of stuff:
https://github.com/crytic/awesome-ethereum-security

Conclusion

This article provides a comprehensive guide to auditing smart contracts. By understanding the auditing process, you can be better prepared to identify and correct potential vulnerabilities in your contracts. To learn more about vulnerabilities in smart contracts, you can view my other articles. You can also drop me a follow on Twitter too: https://twitter.com/0xPredator

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

0xPredator

I write about Crypto Currencies and Hacking Smart Contracts. Twitter: twitter.com/0xPredator Buy me a coffee: buymeacoffee.com/0xPredator