Tags: web3py
Rating: 3.0
The challenge gives a TCP port connected with netcat to get connection information and that checks the Setup contract and gives the flag if the isSolved() function returns true. The other adress given is a gateway to connect to the blockchain.
```
1 - Connection information
2 - Restart Instance
3 - Get flag
action?
```
This challenge gives these two contracts:
```solidity
# Setup.sol
pragma solidity ^0.8.18;
import {Unknown} from "./Unknown.sol";
contract Setup {
Unknown public immutable TARGET;
constructor() {
TARGET = new Unknown();
}
function isSolved() public view returns (bool) {
return TARGET.updated();
}
}
```
```solidity
# Unknown.sol
pragma solidity ^0.8.18;
contract Unknown {
bool public updated;
function updateSensors(uint256 version) external {
if (version == 10) {
updated = true;
}
}
}
```
So the solution is simple, call the function `updateSensors(10)`. The problem is how to interact with the blockchain. The following script using web3py does that.
```python
from web3 import Web3
from solcx import compile_source, install_solc
# installs solc compiler, necessary only for first execution
install_solc("0.8.18")
# compiles source code to get the contract abi
with open("Unknown.sol", "rt") as fp:
src = fp.read()
compiled = compile_source(src, output_values=["abi"])
contract_id, contract_interface = compiled.popitem()
abi = contract_interface['abi']
# contract adress (given in the tcp interface)
target = '0x612DC3AebddE1E9CBc1a608a0E126F54c5988376'
w3 = Web3(Web3.HTTPProvider("http://104.248.169.177:32181")) # blockchain gateway from tcp interface
contract = w3.eth.contract(address=target, abi=abi)
contract.functions.updateSensors(10).transact() # calls update sensor with value of 10
```
The tcp inteface then gives the flag: `FLAG=HTB{9P5_50FtW4R3_UPd4t3D}`