The objective of this Solidity code is to demonstrate a vulnerability known as the "Metamorphic Contract Attack."
A metamorphic contract refers to a smart contract that can change its code while maintaining the same contract address. This is typically achieved by deploying a new contract with updated code at the same address as the old contract. Metamorphic contracts can be exploited in attacks such as the Metamorphic Contract Attack, where malicious code is deployed in place of previously destroyed contract code.
-
ContractA:
- Contains a function
die()
that triggersselfdestruct
, destroying the contract and sending any remaining funds to address0
.
- Contains a function
-
ContractB:
- Contains a private variable
b
and a constructor that sets the value ofb
.
- Contains a private variable
-
Factory:
- Contains functions
createContractA()
andcreateContractB()
that deploy instances of contracts ContractA and ContractB, respectively. - Also contains a
die()
function that destructs the contract.
- Contains functions
-
MetamorphicContract:
- This contract is a test contract that inherits from
Test
. - Defines a setup function
setUp()
that deploys a Factory contract and an instance of ContractA, then triggers thedie()
function of ContractA and Factory. - Defines a test function
testMorphingContract()
that verifies if the code of contracts ContractA and Factory were destroyed and then attempts to redeploy Factory and deploy another instance of ContractB. This test aims to demonstrate the vulnerability known as the Metamorphic Contract Attack.
- This contract is a test contract that inherits from
The code exploits the vulnerability during the setup phase by destroying the code of ContractA and Factory using the selfdestruct
function. It then attempts to redeploy Factory and deploy another instance of ContractB. If successful, this demonstrates how an attacker could replace previously destroyed contract code with malicious code, potentially leading to unexpected behavior or security vulnerabilities in the system.
The code includes a test function testMorphingContract()
that verifies the behavior described above, demonstrating the vulnerability in action.