Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article explains how malicious external contract behavior can be hidden behind apparently harmless Solidity code, and how to detect it.

The mechanism is simple, but overlooking it can result in financial losses. Understanding it is useful for Solidity developers and security researchers participating in bug bounty programs.

If you are unfamiliar with bug bounties, start with this article:

Introduction to Solidity Bug Bounties

How malicious code can be hidden

Illustration of hidden malicious code

Solidity code can conceal which external contract actually executes a call.

More precisely, a reference declared as a seemingly safe contract type can point to a malicious external contract instead.

This can cause behavior that users and developers do not anticipate, potentially resulting in financial losses.

The following example demonstrates the mechanism.

Demonstrating hidden external contract behavior

Illustration of the external contract substitution

The previous section introduced the problem. Here we examine it with a minimal example.

Code walkthrough

CallContract

solidity
contract CallContract {
    SafeContract safeContract;

    constructor(address _safeContract) {
        safeContract = SafeContract(_safeContract);
    }

    function log() public payable {
        safeContract.log();
    }
}

This contract accepts an address for a SafeContract reference and calls its log function.

SafeContract

solidity
contract SafeContract {
    string public message;

    function log() public {
        message = "safe!!!";
    }
}

Calling log stores the string safe!!! in the message variable.

MaliciousContract

solidity
contract MaliciousContract {
    string public message;

    function log() public {
        message = "Malicious!!!";
    }
}

Calling log stores the string Malicious!!! in the message variable.

Execution

The demonstration shows the result of calling the external contract.

Execution demonstration

When deploying CallContract, the demonstration supplies the address of MaliciousContract rather than SafeContract.

Consequently, calling CallContract.log invokes MaliciousContract.log.

Although this behavior may seem obvious, it is easy to overlook when reviewing source code.

A reference typed as SafeContract does not establish that the deployed code at its address is the expected implementation. An external call could therefore execute unexpected behavior, potentially exposing users to financial losses.

Even when the calling contract's source is verified on Etherscan, its source alone does not establish the behavior of the external contract. Investigate the destination address separately.

Complete code

solidity
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.13;

contract CallContract {
    SafeContract safeContract;

    constructor(address _safeContract) {
        safeContract = SafeContract(_safeContract);
    }

    function log() public payable {
        safeContract.log();
    }
}

contract SafeContract {
    string public message;

    function log() public {
        message = "safe!!!";
    }
}

contract MaliciousContract {
    string public message;

    function log() public {
        message = "Malicious!!!";
    }
}

Mitigation

Use a block explorer such as Etherscan to investigate the external contract address and determine whether the destination executes unexpected code.

Users who do not routinely inspect source code should at least check whether the application or service has undergone a security audit. An audit is not a guarantee of safety.

Smart contract audits involve security specialists examining contracts for vulnerabilities. Bug bounty programs can also help identify security issues.

For an introduction to bug bounties, see:

Introduction to Solidity Bug Bounties

Conclusion

Conclusion illustration

This article examined how external contract references can conceal malicious behavior and how to investigate them.

Even familiar applications deserve scrutiny. A seemingly safe contract type is not sufficient evidence that the actual destination is trustworthy.

If you have questions, feel free to contact me on Twitter.

I regularly share information about Solidity, blockchain, and Web3, including articles I find useful.

References