Skip to content
Smart contracts Write-up Advanced

How Someone Tried to Exploit Our Smart Contract and Steal All of Its Ether

An attacker deployed a malicious smart contract to exploit a reentrancy vulnerability in CityMayor's cancelOfferForCity function, attempting to double their refund. The attack failed because the contract used transfer(), which limits gas and prevented the nested call from succeeding.

blog.citymayor.co

Published on blog.citymayor.co

On March 7th, we noticed some weird transactions happening on CityMayor. First, someone used our contract directly instead of going through the dAPP.

The author, on blog.citymayor.co

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The contract updated state (deleted the offer) after sending the refund, allowing a reentrant call to see the offer still active; however, the use of transfer() limited gas and prevented exploitation.

What the attacker needed

Attacker deploys a malicious contract; attacker makes an offer via the contract; contract has a fallback function

The fix

The contract should follow the checks-effects-interactions pattern by deleting the offer before sending the refund, or use a reentrancy guard.

How it was found

  1. The attacker deployed a malicious smart contract.
  2. The attacker called setContract on the malicious contract with CityMayor's address.
  3. The attacker made an offer to buy a city through the malicious contract, sending 0.25 ETH.
  4. The attacker called getFunds on the malicious contract, which triggered cancelOfferForCity on CityMayor.
  5. CityMayor attempted to refund the attacker's contract, which would execute the contract's fallback function and potentially re-enter cancelOfferForCity.
  6. The transaction failed because transfer() limited the gas available to the fallback function.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A smart contract is a program on the Ethereum blockchain that can hold and send money. When it sends money to another contract, that contract can run code, and if the code calls back into the first contract before it finishes, it might trick it into sending money twice. This is called reentrancy, and here the attempt failed because the money-sending function only allowed a tiny amount of extra work, so the trick could not complete.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.