嘿,伙计们,你们好吗,有人能帮我吗?
我想在这个Dex上做一个REENTRANCY,如果有人想通过sell函数出售他们的令牌,我如何将sell函数上的代码更改为stell ETH?https://ethereum.org/en/developers/tutorials/transfers-and-approval-of-erc-20-tokens-from-a-solidity-smart-contract/
其职能是:
function sell(uint256 amount) public {
require(amount > 0, "You need to sell at least some tokens");
uint256 allowance = token.allowance(msg.sender, address(this));
require(allowance >= amount, "Check the token allowance");
token.transferFrom(msg.sender, address(this), amount);
msg.sender.transfer(amount);
emit Sold(amount);
}如何修改此函数使其易重入?#PLEASEHELP
发布于 2021-07-23 17:19:03
您提到的函数不使用任何契约状态,因此它对重入攻击有很好的抵抗能力。通常,要通过重入攻击使函数被滥用,您需要在外部调用之后使其更改契约的本地状态。您的函数可能会更改为如下所示:
mapping(address => uint256) allowances;
function sell(uint256 amount) public {
require(amount > 0, "You need to sell at least some tokens");
require(allowances[msg.sender] >= amount, "Check the token allowance");
msg.sender.transfer(amount);
token.transferFrom(msg.sender, address(this), amount);
allowances[msg.sender].sub(amount)
emit Sold(amount);
}正如您所看到的,这里我们可以回忆起相同的sell函数,在第一个函数从补贴中减去金额之后,即使发送方没有足够的折扣,也能够再次出售。
注意:最初的函数可以通过跨合同的重入攻击来利用,因为它不检查token.transferFrom是否成功,尽管这似乎是一个标准错误。
https://ethereum.stackexchange.com/questions/103966
复制相似问题