DEV Community

Cover image for Hack Solidity: Block Timestamp Manipulation
Kamil Polak
Kamil Polak

Posted on

3 2

Hack Solidity: Block Timestamp Manipulation

Timestamp provides information about the date and time in which the block is mined. When you go e.g. to the etherscan you can find a timestamp for each block that was mined.

timestamp solidity

Block timestamp can be manipulated by miners a then used to their advantage to attack a smart contract.

Let's go into details of how this could happen using the contract below.

contract Roulette {
    uint public pastBlockTime;

    constructor() payable {}

    function spin() external payable {
        require(msg.value == 5 ether); // player must send 5 ether to play
        require(block.timestamp != pastBlockTime); // only 1 transaction per block

        pastBlockTime = block.timestamp;
 // if the block.timestamp is divisible by 7 you win the Ether in the contract
        if (block.timestamp % 7 == 0) {
            (bool sent, ) = msg.sender.call{value: address(this).balance}("");
            require(sent, "Failed to send Ether");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

I created a simple game where a player can win all of the Ether in the contract if he submits a transaction at a specific time.

To join the game player need to send 5 ether. To play the game player call the spin function. If the block.timestamp is divisible by 7 player win all the Ether in the contract.

The miner that wants to win all of the Ethers can manipulate the contract. To do so he can:

  • call the spin function and submit 5 Ether to enter the game

  • submit a block.timestamp for the next block that is divisible by 7

How to prevent block timestamp attacks

There are two ways to prevent block timestamp attacks:

  • do not use block.timestamp in your contract

  • apply the 15-second rule which says that

If the scale of your time-dependent event can vary by 15 seconds and maintain integrity, it is safe to use a block.timestamp.

Sources

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
xdlaughemoter profile image
revert nunu rework

at least make the modulo a bigger number than 7. it can literally be solved by just running it until u win which is like 10 times MAX

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay