Reader small image

You're reading from  Attacking and Exploiting Modern Web Applications

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781801816298
Edition1st Edition
Concepts
Right arrow
Authors (2):
Simone Onofri
Simone Onofri
author image
Simone Onofri

Simone Onofri is a cybersecurity director with over two decades of experience in Red and Blue Teaming, vulnerability research, and product management. He has been an instructor at the Joint Intelligence and EW Training Centre and is associated with global companies such as HewlettPackard Enterprise. Simone has discovered various vulnerabilities and holds key certifications such as GXPN, GREM, GWAPT, OSCP, and OPSA. An active participant in organizations such as OWASP and ISECOM, he regularly speaks at major conferences, including TEDx. Simone is committed to inspiring and educating industry professionals and enthusiasts through his work, with a mission to create a positive influence.
Read more about Simone Onofri

Donato Onofri
Donato Onofri
author image
Donato Onofri

Donato Onofri is a seasoned Red Team engineer. He has over a decade of experience in activities including reverse engineering, Red Teaming, threat research, and penetration testing. Passionate about both the offensive and defensive sides of cybersecurity, Donato has worked with industry leaders such as CrowdStrike and Hewlett-Packard Enterprise and as an advisor and engineer for governments and financial institutions. His research delves into state-of-the-art security techniques, malware analysis, and internals. He holds the GREM, GXPN, OSCP, OSCE, and OSWE certifications, and his expertise is underscored by multiple recognitions for vulnerability discovery.
Read more about Donato Onofri

View More author details
Right arrow

Attacking Ethereum Smart Contracts – Reentrancy, Weak Sources of Randomness, and Business Logic

“What Ethereum intends to provide is a blockchain with a built-in fully fledged Turing-complete programming language that can be used to create “contracts” that can be used to encode arbitrary state transition functions [...]. The code in Ethereum contracts is written in a low-level, stack-based bytecode language, referred to as “Ethereum virtual machine code” or “EVM code”. The code consists of a series of bytes, where each byte represents an operation.”

Vitalik Buterin [1]

Welcome to the seventh chapter of this book, where we’ll analyze our vulnerable application with a Capture the Flag (CTF) on Ethereum Smart Contracts.

The epigraph features words from Vitalik Buterin, who, in 2014, examined Bitcoin – the digital currency first introduced through a white paper in 2009 [2]. His analysis expanded the idea...

Technical requirements

You can use the Ubuntu LTS machine configured in w in this chapter.

As with the previous chapters, we will use Visual Studio Code. You can get it from its website if it isn’t installed [5]. After, do the following:

  1. Enable the Shell Command: Install the 'code' command in PATH functionality via the VSCode command palette.
  2. Install the Nomic Foundation extension [6] for Solidity Language Support.

Scenario files

To reproduce the scenario in this chapter, you can use the files in the Chapter07 directory in this book’s GitHub repository.

The scenario comprises a smart contract and other useful files.

LicenseManager smart contract scenario

The following scenario unfolds as a CTF game we created and published on QuillAcademy [7].

We have the LicenseManager smart contract on the blockchain, where each license costs 1 ether.

Our objective? With only 0.01 ether, we need to buy a license and then find a way to collect the ethers in the contract before the owner notices.

Before diving into this scenario, let’s take a high-level look at the significant security incidents of blockchain to understand common vulnerabilities.

Note to chief information security executive officers (CISOs)

As with new technologies, blockchain often brings new opportunities and risks that must be understood and managed correctly.

If you are in an organization, it is possible to implement blockchain technologies on multiple levels:

a. Building your private blockchain with all the issues related to cryptography and programming

b. Having nodes where the blockchain runs, with the various...

How smart contracts work on the Ethereum blockchain and security considerations

The first thing to understand is what contracts are, how applications using smart contracts are structured, and how they get on the blockchain. We’ll understand the most famous vulnerability, named Reentrancy.

What are smart contracts in the Ethereum blockchain?

To define smart contracts, we can refer directly to the documentation on Ethereum, which states that a smart contract “is simply a program that runs on the Ethereum blockchain. It’s a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.” [8]

Contracts have a balance and can use it, as specified in their functions, such as via fund transfer or state change. You can send transactions that call upon these functions to interact with these contracts.

Smart contracts – being Turing-complete – can be used to develop different things:

...

How to find and exploit vulnerabilities in Ethereum smart contracts

The first thing we need to structure in our lab is our local blockchain, along with what we did to set up Burp and Docker earlier in this book.

Of course, we can use public testnets, but on the one hand, it’s not appropriate to leave our traces on these networks and perhaps dedicate them to a later step, and on the other hand, we may not want to give visibility to our tests.

Fortunately – using Solidity’s development environments – we can recreate our blockchain and lab from the comfort of our machine.

For Solidity, excluding Remix, we have the historic Truffle in JavaScript, which we are particularly fond of and which brings along Ganache – a local blockchain server on which to do testing; Hardhat, which also includes Hardhat Network, its local Ethereum node; and the new Foundry framework, written in Rust.

In general, each environment has its pros and cons, and it’...

Summary

In this chapter, we embarked on an insightful journey into smart contracts, exploring the fundamentals of their design and deployment and the structure of web3 applications.

Then, we delved into the critical vulnerabilities that threaten smart contracts on the Ethereum blockchain, offering a thorough understanding of these potential weaknesses.

Furthermore, we probed into various methods for auditing contracts and executing tests using sophisticated tools such as Foundry and various disassemblers. We examined randomness, business logic, and reentrancy vulnerabilities to equip you with comprehensive knowledge about this innovative topic.

Next, we’ll wrap up this book with some concluding thoughts.

Further reading

This chapter covered many topics. If you’d like to dive deeper, we’re happy to share some valuable resources with you:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Attacking and Exploiting Modern Web Applications
Published in: Aug 2023Publisher: PacktISBN-13: 9781801816298
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Simone Onofri

Simone Onofri is a cybersecurity director with over two decades of experience in Red and Blue Teaming, vulnerability research, and product management. He has been an instructor at the Joint Intelligence and EW Training Centre and is associated with global companies such as HewlettPackard Enterprise. Simone has discovered various vulnerabilities and holds key certifications such as GXPN, GREM, GWAPT, OSCP, and OPSA. An active participant in organizations such as OWASP and ISECOM, he regularly speaks at major conferences, including TEDx. Simone is committed to inspiring and educating industry professionals and enthusiasts through his work, with a mission to create a positive influence.
Read more about Simone Onofri

author image
Donato Onofri

Donato Onofri is a seasoned Red Team engineer. He has over a decade of experience in activities including reverse engineering, Red Teaming, threat research, and penetration testing. Passionate about both the offensive and defensive sides of cybersecurity, Donato has worked with industry leaders such as CrowdStrike and Hewlett-Packard Enterprise and as an advisor and engineer for governments and financial institutions. His research delves into state-of-the-art security techniques, malware analysis, and internals. He holds the GREM, GXPN, OSCP, OSCE, and OSWE certifications, and his expertise is underscored by multiple recognitions for vulnerability discovery.
Read more about Donato Onofri