Skip to content

Commit

Permalink
Added challenge 5
Browse files Browse the repository at this point in the history
  • Loading branch information
luksgrin committed May 21, 2022
1 parent 5008bc6 commit 379e9b5
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
50 changes: 50 additions & 0 deletions contracts/N5-BecomeMaster-medium.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Props to band0x for this amazing CTF
// I think we could upload this one as is, unless someone thinks otherwise
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;

contract N5BecomeMaster {
mapping(address => uint256) allocations;
address public admin;
address public master;
address caller;

constructor() payable {
master = msg.sender;
}

modifier onlyMaster() {
require(master == tx.origin, "caller is not the master");
_;
}
modifier onlyContract() {
require(msg.sender != tx.origin, "caller is not the master");
_;
}
modifier onlyAdmin() {
require(admin == tx.origin, "caller is not the Admin");
_;
}

function allocate() public payable {
allocations[caller] = allocations[caller] += (msg.value);
admin = tx.origin;
}

function sendAllocation(address payable allocator) public {
require(allocations[allocator] > 0);
allocator.transfer(allocations[allocator]);
}

function takeMasterRole() public onlyAdmin onlyContract {
master = admin;
}

function collectAllocations() public onlyMaster onlyContract {
payable(msg.sender).transfer(address(this).balance);
}

function allocatorBalance(address allocator) public view returns (uint256) {
return allocations[allocator];
}
}
14 changes: 14 additions & 0 deletions contracts/hacks/N5-BecomeMasterhack.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;

import "../N5-BecomeMaster-medium.sol";

contract N5ExploitHack {
constructor(N5BecomeMaster instance) payable {
// Complete with your own code
}

function finalize() external {
// can be used to finalize the exploit patten
}
}
36 changes: 36 additions & 0 deletions scripts/N5-BecomeMasterExploit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const hre = require("hardhat");
const ethers = hre.ethers;
const CHALLENGES = require("../challenge-addresses.js")

const challengeAddress = CHALLENGES.N5BecomeMaster;
const ETH_UNIT = ethers.utils.parseEther("0.0001");

async function main() {
// command to run against fork
// npx hardhat run scripts/N5-BecomeMasterExploit.js

// command to run against real network
// npx hardhat run scripts/N5-BecomeMasterExploit.js --network mumbai

/*let challengeInstance = await ethers.getContractFactory("N5BecomeMaster");
challengeInstance = await challengeInstance.deploy({ value: ETH_UNIT });
await challengeInstance.deployed();*/
const challengeInstance = await ethers.getContractAt(
"N5BecomeMaster",
challengeAddress
);

/* ------> user solution code starts here */

/* ------> user solution code ends here */

console.log(`Challenge balance `, await ethers.provider.getBalance(challengeInstance.address));

}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
17 changes: 17 additions & 0 deletions test/N5-BecomeMaster-medium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { expect } = require("chai");

const CHALLENGES = require("../challenge-addresses.js")
const challengeAddress = CHALLENGES.N5BecomeMaster;


// run the test
// npx hardhat test ./test/N5-BecomeMaster-medium.js --network mumbai
describe("CTF #5 BecomeMaster", function () {
it("Should recover all funds", async function () {
const challengeInstance = await ethers.getContractAt(
"N5BecomeMaster",
challengeAddress
);
expect(await ethers.provider.getBalance(challengeInstance.address)).to.equal("0");
});
});

0 comments on commit 379e9b5

Please sign in to comment.