Solidity Automatic Exploit Generation?
try it:
nc aab2596ac4a422a9f803ed317089c399b818bb72.balsnctf.com 30731
Be a King
* Give you a contract bytecode, give me transaction data to be a king.
* Timeout = 10 seconds per challenge.
* You can call isKing() to verify it.
* pragma solidity 0.4.25
This challenge requires Proof-of-Work (PoW). We have already finished the code for you. Please see pow.balsnctf.com .
Author: ysc
The problem is simple. It will give you a contract bytecode, you need to analysis which function call will let isKing()
to return true
.
We only use three contract templates (three level).
The level 1 sample contract is:
pragma solidity 0.4.25;
contract King {
bool king = false;
function setKing_mbruyevcop() public{ // function signature is random
king = true;
}
function isKing() view public returns (bool){
return king;
}
}
In level 2, We have multiple setKing_%(random)s()
functions which like:
pragma solidity 0.4.25;
contract King {
bool king = false;
function setKing_iprjllplvn() public{
king = false;
}
function setKing_duyfevifta() public{
king = false;
}
// ... multiple function to set king to false, only one function to set true.
function setKing_jheajcokms() public{
king = true;
}
function isKing() view public returns (bool){
return king;
}
}
In level 3, you should call a function with an argument to trigger integer overflow. A level 3 sample contract is:
pragma solidity 0.4.25;
contract King {
bool king = false;
uint private sb = 100;
function setKing_utsgesanbm(uint value) public{
sb -= value;
if (sb > 409792) {
if (sb < 409800) {
king = true;
}
else {
king = false;
}
}
}
function isKing() view public returns (bool){
return king;
}
}
Of course, you only get the bytecode from this challenge.
There are two intended solutions to solve:
- Analysis these contract bytecode, parse them and get the function signature (and calculate an argument in level 3).
- Use symbolic execution tools.
You can analysis bytecode by yourself which like PPP's writeup and perfectblue's writeup. They did a great job!
Or you can use symbolic execution tool: manticore. You can get more information from mephi42's writeup. Well done!
My symbolic execution script is below. If you know symbolic execution and learn how2use manticore, you even don't need to parse these bytecode!
It's fun to learn symbolic execution and learn how2hack this challenge. I hope you enjoy ;)