ERC20 Short Address Attack: Understanding and Prevention

·

What is ERC20?

Tokens have become a fundamental part of the Ethereum ecosystem. Unlike native cryptocurrencies like Bitcoin or Ether, which require extensive infrastructure to maintain blockchain operations, tokens are simply digital records stored within smart contracts on the Ethereum platform.

The ERC20 standard emerged to create uniformity among these tokens, specifying nine mandatory methods and two events that all compliant token contracts must implement. This standardization enables seamless interaction between different tokens and wallets.

👉 Learn more about Ethereum tokens

Understanding Application Binary Interface (ABI)

To comprehend ERC20 attacks, we must first understand how interactions occur with the Ethereum Virtual Machine (EVM). The ABI serves as:

Function selectors comprise the first 4 bytes of call data (derived from Keccak-256 hashing of the function signature). Parameters are then padded to 32 bytes - which becomes crucial in short address attacks.

How the Short Address Attack Works

Consider this simple token contract:

pragma solidity ^0.4.11;

contract MyToken {
    mapping (address => uint) balances;
    
    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    function MyToken() {
        balances[tx.origin] = 10000;
    }

    function sendCoin(address to, uint amount) returns(bool sufficient) {
        if (balances[msg.sender] < amount) return false;
        balances[msg.sender] -= amount;
        balances[to] += amount;
        Transfer(msg.sender, to, amount);
        return true;
    }

    function getBalance(address addr) constant returns(uint) {
        return balances[addr];
    }
}

When calling sendCoin, the ABI might normally look like:

0x90b98a11
00000000000000000000000062bec9abe373123b9aabbcce608f94eb8644163e
0000000000000000000000000000000000000000000000000000000000000004

However, if an attacker truncates the address by one byte, the EVM pads the parameters differently, causing the transfer amount to be shifted left by 8 bits (effectively multiplying by 256).

Attack Execution Steps

  1. Preparation: Attacker creates an address ending with 0x00
  2. Deposit: Attacker deposits 500 tokens to this address
  3. Malicious Transfer: When transferring out, the address is shortened by omitting the final byte
  4. Impact: The EVM interprets the transfer as 500<<8 (128,000 tokens)

Prevention Measures

The solution lies in rigorous parameter validation:

modifier onlyPayloadSize(uint size) {
    assert(msg.data.length == size + 4);
    _;
}

function sendCoin(address to, uint amount) onlyPayloadSize(2 * 32) returns(bool sufficient) {
    // Rest of the function
}

This modifier ensures the complete payload length matches expectations, preventing truncated parameter exploits.

Frequently Asked Questions

Q: Can this attack affect all ERC20 tokens?

A: Only tokens with insufficient parameter validation are vulnerable. Most modern implementations include proper checks.

Q: What's the worst-case impact of this attack?

A: The most significant impact occurs when transferring maximum values, potentially causing token overissuance by 256x the intended amount.

Q: Are there other similar ABI-based attacks?

A: Yes, parameter manipulation attacks can take various forms, making rigorous validation essential for all smart contract functions.

👉 Explore secure token standards

Key Takeaways

  1. Always validate parameter lengths in smart contracts
  2. Understand how ABI encoding affects parameter interpretation
  3. Implement size checks for all external-facing functions
  4. Regularly audit contracts for potential encoding vulnerabilities

By implementing these security measures, developers can effectively prevent short address attacks and maintain the integrity of their token systems.