Back to Blog

当“SafeMint”不再安全:HypeBears安全事件的教训

Code Auditing
February 3, 2022
2 min read

在东八区时间 2 月 3 日上午,我们的系统报告了一笔针对 HypeBears NFT 合约的攻击交易 0xfa97c3476aa8aeac662dae0cc3f0d3da48472ff4e7c55d0e305901ec37a2f704。经过调查,我们发现这是由 ERC721 的 _safeMint 函数引起的重入攻击。

根本原因

该项目对一个账户可以铸造的 NFT 数量有限制。基本上,它有一个 addressMinted 的映射,用于记录一个账户是否已经铸造过 NFT。

在铸造 NFT 时,代码使用了 OZ 参考实现中的 _safeMint 函数。这个函数是 safe 的,因为它检查接收方是否可以接收 ERC721 代币。这可以防止 NFT 被铸造到无法处理 ERC721 代币的合约中。根据 文档:

如果 to 指的是一个智能合约,它必须实现 IERC721Receiver.onERC721Received,该函数在安全转账时被调用。以下代码显示了 OZ 对 _safeMint 函数的实现。

然而,这个外部函数调用会产生一个安全漏洞。具体来说,攻击者可以在 onERC721Received 回调中执行重入调用。例如,在 有漏洞的 HypeBears 合约 中,攻击者可以在 onERC721Received 回调中再次调用 mintNFT 函数(因为 addressMinted 尚未更新)。

攻击

下图显示了 攻击交易

教训

安全研究人员讨论过 SafeMint 带来的风险 链接1 链接2。然而,我们仍然可以看到有漏洞的代码和实际发生的攻击。正如在 QBridge 安全事件 中的 safeTransfer 中所示,使用一个 安全 的函数并不能保证一个 安全 的合约 😃。

Sign up for the latest updates
~$15.9M Lost: Trusted Volumes & More | BlockSec Weekly
Security Insights

~$15.9M Lost: Trusted Volumes & More | BlockSec Weekly

This BlockSec bi-weekly security report covers 11 notable attack incidents identified between April 27 and May 10, 2026, across Sui, Ethereum, BNB Chain, Base, Blast, and Berachain, with total estimated losses of approximately $15.9M. Three incidents are analyzed in detail: the highlighted $1.14M Aftermath Finance exploit on Sui, where a signed/unsigned semantic mismatch in the builder-fee validation allowed an attacker to inject a negative fee that was converted into positive collateral during settlement; the $5.87M Trusted Volumes RFQ authorization mismatch on Ethereum; and the $5.7M Wasabi Protocol infrastructure-to-contract-control compromise across multiple EVM chains.

Newsletter - April 2026
Security Insights

Newsletter - April 2026

In April 2026, the DeFi ecosystem experienced three major security incidents. KelpDAO lost ~$290M due to an insecure 1-of-1 DVN bridge configuration exploited via RPC infrastructure compromise, Drift Protocol suffered ~$285M from a multisig governance takeover leveraging Solana's durable nonce mechanism, and Rhea Finance incurred ~$18.4M following a business logic flaw in its margin-trading module that allowed circular swap path manipulatio

~$7.04M Lost: GiddyDefi, Volo Vault & More | BlockSec Weekly
Security Insights

~$7.04M Lost: GiddyDefi, Volo Vault & More | BlockSec Weekly

This BlockSec weekly security report covers eight attack incidents detected between April 20 and April 26, 2026, across Ethereum, Avalanche, Sui, Base, HyperLiquid, and MegaETH, with total estimated losses of approximately $7.04M. The highlighted incident is the $1.3M GiddyDefi exploit, where the attacker did not break any cryptography or use a flash loan but simply replayed an existing on-chain EIP-712 signature with the unsigned `aggregator` and `fromToken` fields swapped out for a malicious contract, demonstrating how partial signature coverage turns any historical signature into a generic permit. Other incidents include a $3.5M Volo Vault operator key compromise on Sui, a $1.5M Purrlend privileged-role takeover, a $413K SingularityFinance oracle misconfiguration, a $142.7K Scallop cross-pool index injection, a $72.35K Kipseli Router decimal mismatch, a $50.7K REVLoans (Juicebox) accounting pollution, and a $64K Custom Rebalancer arbitrary-call exploit.

Best Security Auditor for Web3

Validate design, code, and business logic before launch. Aligned with the highest industry security standards.

BlockSec Audit