{"content":{"title":"如何比较虚拟机","body":"作者: Alexey Naberezhniy - MixBytes 的安全研究员\r\n\r\n![](https://img.learnblockchain.cn/2025/03/09/27.jpg)\r\n\r\n## 在具有类似以太坊虚拟机的区块链中存在哪些潜在的漏洞？\r\n\r\n由于发送恶意交易而导致的区块链完全关闭\r\n\r\n区块链是一个开放系统，允许用户发送将通过虚拟机处理的任何交易。区块链中可能存在的一个潜在漏洞是缺乏对用户发送的有效负载的验证，这可能导致网络完全停止，因为区块生产者无法处理此操作。\r\n\r\nAptos 虚拟机中的错误示例\r\n\r\n以下命令会导致 Aptos 虚拟机中潜在的问题：\r\n\r\n```cpp\r\nBytecode::VecPack(si, num)\r\nBytecode::VecUnpack(si, num)\r\n```\r\n\r\n这些操作码与堆栈的交互，导致用户能够将 num (u64) 元素添加到堆栈中（在以太坊虚拟机中是不可行的，因为 PUSH 操作码是有等限制的）。\r\n\r\n在代码的这个地方，造成溢出的情况是可能的，因为 num_pushes 没有经过任何验证：\r\n\r\n[https://github.com/aptos-labs/move/blob/97a771cb6a2414ef3a68d71aef8f0bc6aac61ef2/language/move-bytecode-verifier/src/stack_usage_verifier.rs#L60](https://github.com/aptos-labs/move/blob/97a771cb6a2414ef3a68d71aef8f0bc6aac61ef2/language/move-bytecode-verifier/src/stack_usage_verifier.rs#L60)\r\n\r\n在这个地方调用溢出不会导致资金被盗，但会导致节点出现严重错误，因此整个区块链都会出现故障。由于 Aptos 是一个新网络，这可能会使整个网络在此时停机。为了做到这一点，一个黑客只需发送一笔恶意交易即可。以太坊不易受到此类漏洞的影响，因为存在多种替代链节点实现。\r\n\r\n来源: [https://learnblockchain.cn/article/12427](https://learnblockchain.cn/article/12427)\r\n\r\n## 预编译的不正确实现\r\n\r\n为了优化智能合约的执行，节点链开发员添加了可以直接从虚拟机利用的附加功能，以加快某些复杂功能的速度（例如 sha3，ecrecover 等）。最容易理解的例子是 [JNI，用于直接与本机代码通信的 Java 字节码](https://en.wikipedia.org/wiki/Java_Native_Interface)。\r\n\r\n预编译实现的示例：\r\n\r\n- [https://github.com/ava-labs/coreth/tree/master/precompile](https://github.com/ava-labs/coreth/tree/master/precompile)\r\n- [https://github.com/PureStake/moonbeam/tree/master/precompiles](https://github.com/PureStake/moonbeam/tree/master/precompiles)\r\n\r\n某些预编译的实现可能包含重大错误，这通常会导致严重错误（例如 DoS、访问控制等）。\r\n\r\nAvalanche 中预编译的错误示例\r\n\r\n导致一些智能合约无法正常工作的预编译错误如下所示：\r\n\r\n[https://github.com/ava-labs/coreth/blob/8543a81bfd8e7812c181ff4fade6405396b0a1d6/core/vm/evm.go#L747](https://github.com/ava-labs/coreth/blob/8543a81bfd8e7812c181ff4fade6405396b0a1d6/core/vm/evm.go#L747)\r\n\r\n```cs\r\n// https://snowtrace.io/address/0x0100000000000000000000000000000000000000\r\n\r\nfunc (evm *EVM) NativeAssetCall(caller common.Address, input []byte, suppliedGas uint64, gasCost uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {\r\n\tif suppliedGas < gasCost {\r\n\t\treturn nil, 0, vmerrs.ErrOutOfGas\r\n\t}\r\n\tremainingGas = suppliedGas - gasCost\r\n\r\n    ...\r\n\r\n    // 将 [assetAmount] 的 [assetID] 发送到 [to] 地址\r\n\tevm.Context.TransferMultiCoin(evm.StateDB, caller, to, assetID, assetAmount)\r\n\tret, remainingGas, err = evm.Call(AccountRef(caller), to, callData, remainingGas, new(big.Int))\r\n\r\n    ...\r\n}\r\n```\r\n\r\n由于预编译中的错误而易受到攻击的智能合约代码：\r\n\r\n[https://github.com/Abracadabra-money/magic-internet-money/blob/23266d17969a95e69199670cba9d0060bff33340/contracts/CauldronV3\\_1.sol#L410](https://github.com/Abracadabra-money/magic-internet-money/blob/23266d17969a95e69199670cba9d0060bff33340/contracts/CauldronV3_1.sol#L410)\r\n\r\n```cs\r\n...\r\nrequire(callee != address(bentoBox) && callee != address(this), \"Cauldron: can't call\");\r\n\r\n(bool success, bytes memory returnData) = callee.call{value: value}(callData);\r\n...\r\n```\r\n\r\n在调用 callee.call 之前，有一个对 callee != address(bentoBox) 的检查，这限制了对 bentoBox 的调用。但是，如果我们调用合约 0x0100000000000000000000000000000000000000，并将调用传递给 bentoBox，则可以绕过 require 检查。\r\n\r\n在披露此漏洞后，此预编译被排除在节点之外。\r\n\r\n来源: [https://learnblockchain.cn/article/12426](https://learnblockchain.cn/article/12426)\r\n\r\n## 网络攻击\r\n\r\n每个区块链节点以某种方式与外部世界交互。有很多接口，例如：RPC、管理接口等。攻击者也可以通过这些接口对网络发起攻击。\r\n\r\n示例：\r\n\r\n- [https://github.com/ethereum/go-ethereum/security/advisories/GHSA-59hh-656j-3p7v](https://github.com/ethereum/go-ethereum/security/advisories/GHSA-59hh-656j-3p7v)\r\n- [https://github.com/openethereum/parity-ethereum/pull/11356/files](https://github.com/openethereum/parity-ethereum/pull/11356/files)\r\n- [https://github.com/ethereum/ethereumj/issues/1161](https://github.com/ethereum/ethereumj/issues/1161)\r\n\r\n## 如何检查这些类型的漏洞？\r\n\r\n有几种方法可以发现类似以太坊虚拟机的漏洞：\r\n\r\n- 手动审查。例如，手动分析指令（例如，对于 Avalanche，你应该检查此链接的代码 [https://github.com/ava-labs/coreth/blob/master/core/vm/instructions.go](https://github.com/ava-labs/coreth/blob/master/core/vm/instructions.go)）。\r\n- 使用工具进行半自动化研究。例如，以太坊提供了用于此目的的 EVM lab 工具（[https://github.com/ethereum/evmlab](https://github.com/ethereum/evmlab)）\r\n- 自动研究。例如，一个工具可以使用模糊生成的交易比较所有流行的虚拟机。（[https://github.com/MariusVanDerWijden/FuzzyVM](https://github.com/MariusVanDerWijden/FuzzyVM)）。由于所有类似以太坊的虚拟机环境应该以类似的方式工作，我们可以传递相同的环境并通过跟踪找到所有不同之处（每个这样的不同将是潜在的漏洞）。\r\n- 有一个非常好的分析预编译的工具，可以找到漏洞（[https://github.com/holiman/goevmlab/tree/master/cmd/precompile\\_fuzzer](https://github.com/holiman/goevmlab/tree/master/cmd/precompile_fuzzer)）\r\n\r\n\r\n> MixBytes 是谁？ \r\n> [MixBytes](https://mixbytes.io/) 是一支专家区块链审计师和安全研究员团队，专注于为 EVM 兼容和 Substrate 基础的项目提供全面的智能合约审计和技术咨询服务。加入我们在 [X](https://twitter.com/MixBytes) 上，与时俱进，获取最新的行业趋势和见解。\r\n\r\n>- 原文链接： [mixbytes.io/blog/how-to-...](https://mixbytes.io/blog/how-to-compare-vms)\r\n>- 登链社区 AI 助手，为大家转译优秀英文文章，如有翻译不通的地方，还请包涵～"},"author":{"user":"https://learnblockchain.cn/people/24680","address":null},"history":null,"timestamp":1741495478,"version":1}