本文へ移動
Contact

TECHFUND
Media

Hi AUDIT
2025/6/3

The Threat of Denial-of-Service :  A Deep Dive into Smart Contract Vulnerability

A DoS (Denial of Service) attack occurs when someone is unable to access a service because of excessive or malicious traffic caused by someone else. This can affect websites, networks, or even smart contracts. Sometimes, we experience situations where a website keeps loading for a long time but eventually shows an error message.

Have you ever faced a situation where a university result is announced and every student tries to check their result at the same time? This creates heavy load on the website, causing the server to crash. As a result, anyone trying to access the site at that time won’t be able to load it.

What Is a DoS Attack in Smart Contracts?

A DoS (Denial of Service) attack in smart contracts refers to making a service or function unavailable — either temporarily or permanently — by intentionally disrupting its normal operation. It prevents others from using the contract properly and can break the function or contract logic.

I hope you understand what a DoS attack is now.

Types of DoS attacks in smart contracts

1. Denial of Service — Caused by an Unbounded for Loop 

An unbounded for loop is one that has no fixed endpoint. This means a user could potentially pass a very long array to perform certain operations. Since each iteration consumes gas, the loop may consume too much gas and revert before completing its execution. This can lead to a Denial of Service (DoS) vulnerability.

In this example, if the recipients array contains a large number of addresses, it can cause a Denial of Service (DoS) because the loop consumes a lot of gas and may revert the entire transaction before completion.

So how do we solve this issue?

An alternative approach is to use a pull-over-push methodology. Instead of the contract owner pushing rewards to all winners at once (which can cause gas and DoS issues due to unbounded loops), the contract stores each winner’s reward entitlement. Then, winners can individually claim their rewards by calling a claim function and paying the transaction gas costs themselves. This avoids costly loops and reduces the risk of failed transactions.

2. Denial of Service — Caused by External Data

A DoS attack caused by external data occurs in functions that rely on external sources. In this type of attack, the attacker ensures that the external call always fails. By manipulating the external data, the attacker causes the function to revert every time it’s executed, leading to a Denial of Service.

In this example, the mintNFT and burnNFT functions both rely on the totalSupply variable, which is updated based on external input from the user. The contract allows users to mint any number of NFTs, with totalSupply incrementing each time.

Let’s consider the following scenario:

  • A user mints 6 NFTs. At this point, totalSupply = 6.

  • Now, the user burns the NFT with token ID 5. As a result, totalSupply is manually reduced to 5.

  • The next time someone calls mintNFT, the function will attempt to mint using totalSupply + 1 as the next token ID — which would be ID 6 again.

  • Since NFT token IDs must be unique, and token ID 6 already exists, the _safeMint call will revert. This causes the entire transaction to fail and effectively locks the minting function, resulting in a Denial of Service.

So how do we solve this issue?

To fix this issue, introduce a separate counter variable that increments each time mint() is called. This counter is then passed to _safeMint() as the unique token ID. By doing this, the burn() operation won’t affect the minting sequence or token IDs, ensuring that each minted token ID remains unique and increasing, regardless of any burns. This prevents mint failures and potential DoS issues caused by reused or conflicting token IDs.

3. Denial of Service — Caused by External Call

A Denial of Service (DoS) caused by external calls happens when a contract relies on an external call to update or validate its internal state, but fails to properly handle the case where that call fails.

This typically occurs when:

  • The contract’s logic depends on the result of an external contract call .

  • The external call fails, but the contract doesn’t correctly check or respond to that failure.

  • As a result, the contract continues to try and process invalid or stale data again and again.

  • An attacker can exploit this by intentionally causing the external call to fail, leading to repeated transaction failures and effectively causing a Denial of Service for legitimate users.

Working of the TopDonor Contract :

Normal Behavior

  • When a user donates more Ether than the current highestDonation,
     they become the new topDonor.

  • Their donation is stored as highestDonation .

  • Before updating the donor, the contract tries to refund the previous topDonor.

Attacker’s Strategy

  • The attacker donates a large amount to become the topDonor .

  • They use a smart contract as their address.

  • This contract contains a receive() function that always reverts when Ether is sent.

Triggering the DoS

  • When a new donor sends a higher donation, the contract tries to refund the attacker.

  • The refund fails because the attacker’s fallback function reverts the call.

  • The donate() function then reverts the whole transaction due to require(sent).

Result: Permanent DoS

  • No one can donate a higher amount anymore.

  • The attacker locks themselves as the top donor permanently.

  • The contract becomes stuck and unusable for future donations.

So how do we solve this issue?

To avoid the DoS vulnerability caused by directly refunding the previous top donor during a new donation, separate the bidding logic from the refund logic. Instead of sending Ether back immediately, the contract should record the amount owed to the previous top donor in a mapping. This way, users can withdraw their refundable amounts later at their convenience by calling a dedicated withdraw() function.

Hoping that you learned something from this blog. See you again next time in another awesome blog. Good Luck & WAGMI :)

Author: Mohini Vasisth

Intern, Security Researcher at TECHFUND

Hi AUDIT

Hi AUDIT is a high speed, high quality, best price security audit service, providing automated audit SaaS and consulting for web3 startups.
View More