forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncheckedCall.sol
More file actions
56 lines (46 loc) · 1.52 KB
/
UncheckedCall.sol
File metadata and controls
56 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: MIT
// por 0xAA
pragma solidity ^0.8.21;
contract UncheckedBank {
// Mapeamento de saldo
// Depositar ether e atualizar o saldo
function deposit() external payable {
balanceOf[msg.sender] += msg.value;
}
// Extrair todos os ether do msg.sender
function withdraw() external {
// Obter saldo
uint256 balance = balanceOf[msg.sender];
require(balance > 0, "Insufficient balance");
balanceOf[msg.sender] = 0;
// Chamada de baixo nível não verificada
bool success = payable(msg.sender).send(balance);
}
// Obter o saldo do contrato bancário
function getBalance() external view returns (uint256) {
return address(this).balance;
}
}
contract Attack {
// Endereço do contrato Bank
// Inicializando o endereço do contrato Bank
constructor(UncheckedBank _bank) {
bank = _bank;
}
// Função de retorno, a transferência de ETH falhará
receive() external payable {
revert();
}
// Função de depósito, quando chamada, defina msg.value como a quantidade a ser depositada
function deposit() external payable {
bank.deposit{value: msg.value}();
}
// Função de saque, embora a chamada tenha sido bem-sucedida, na realidade o saque falhou
function withdraw() external payable {
bank.withdraw();
}
// Obter o saldo deste contrato
function getBalance() external view returns (uint256) {
return address(this).balance;
}
}