Full Report
The Merkle root construction of Bitcoin takes a double SHA256 hash of each transaction as a leaf. Then, each hash is concatenated and hashed until we reach the top. If there are an odd number of leaves at a level in the tree then the final element is duplicated. CVE-2012-2459 exposes an issue with the odd number part of this. This is best done by example: let's take a tree with [1,2,3,4,5,6]. After [5,6] is hashed, it will need to be duplicated then hashed. Functionality, this is the same as [1,2,3,4,5,6,5,6]! There is a really good image of this in the paper. Another issue is the lack of domain separators between nodes and leaves. A non-leaf node in the Merkle tree is a hash of a 64 byte input, which is the same as two hashes of its children. Meaningwhile, a leaf node is the has of a transaction. If the serialization of the transaction is 64 bytes, it can be impossible to distinguish between a leaf node and a non-leaf node. With this knowledge, it begs the question: how much work is required to produce a 2 transaction block such that the merkle root is the hash of a 64 byte input? After showing which bytes are controlled and which are not, this requires about 8 bits of work in the first 32 bytes and 22 bits of work on the final 32 bytes, which is doable! An SPV proof provides the client with a path through the Merkle tree; from the root down to the transaction. The SPV client also does not know how many TXs are in the block. So, if we can construct a transaction such that the second 32 bytes collide with the hash of a fake transaction, we can include this fake TX in the block. Although this seems crazy initially, it's not. It's reasonable to have the second 32 bytes of a TX (with much control over it) be the output with a pre-calculated hash that was made. As a result, the TX we WANT to include will be included unintentionally. This requires 81 bits of work, which is much less than the 128 bits expected.
Analysis Summary
# Vulnerability: Bitcoin Merkle Tree Path Ambiguity and Collisions
## CVE Details
- **CVE ID:** CVE-2012-2459
- **CVSS Score:** 7.5 (High) - *Estimated based on protocol impact*
- **CWE:** CWE-345 (Insufficient Verification of Data Authenticity), CWE-20 (Improper Input Validation)
## Affected Systems
- **Products:** Bitcoin Core (formerly Bitcoin-Qt), various Bitcoin implementation libraries, and Simplified Payment Verification (SPV) clients.
- **Versions:** Bitcoin Core versions prior to 0.6.1.
- **Configurations:** Systems utilizing Merkle tree verification without checking for duplicated leaves or lack of domain separation between internal nodes and leaves.
## Vulnerability Description
The vulnerability stems from two primary flaws in the Bitcoin Merkle root construction:
1. **Leaf Duplication:** When a level in the Merkle tree has an odd number of nodes, the final node is duplicated to complete the pair. This allows an attacker to create a block with a different set of transactions that results in the same Merkle root (e.g., a tree of 6 transactions appears identical to a tree of 8 transactions where the last two are duplicates).
2. **Lack of Domain Separation:** There is no distinction (domain separation) between a leaf node (a hashed transaction) and an internal node (a hash of two combined hashes). Since both are double-SHA256 hashes of 64-byte inputs, a 64-byte serialized transaction can be misinterpreted as an internal node.
By combining these, an attacker can craft a transaction where the second 32 bytes collide with the hash of a "fake" transaction. This allows the inclusion of unintended transactions that SPV clients will accept as valid due to path ambiguity.
## Exploitation
- **Status:** PoC available / Historically exploited to cause network splits (DoS).
- **Complexity:** Medium to High (Requires approximately 81 bits of work to find a collision, significantly lower than the theoretical 128 bits).
- **Attack Vector:** Network (Blockchain)
## Impact
- **Confidentiality:** None
- **Integrity:** High (Transactions can be spoofed or misrepresented to SPV clients).
- **Availability:** High (Can be used to create "mutated" blocks that cause nodes to reject valid blocks, leading to potential chain splits or Denial of Service).
## Remediation
### Patches
- **Bitcoin Core 0.6.1:** Implemented checks to reject blocks with duplicate transactions in the Merkle tree construction.
### Workarounds
- **Strict Verification:** Implementations should explicitly check for and reject blocks where the Merkle tree construction involves duplicated nodes at any level.
- **SPV Hardening:** SPV clients should be updated to verify the total number of transactions and ensure the tree structure matches the expected leaf count.
## Detection
- **Indicators of Compromise:** Detection of blocks containing duplicate transaction hashes or transactions exactly 64 bytes in length that could be interpreted as internal tree nodes.
- **Detection Methods:** Static analysis of block structures to identify "mutated" Merkle trees where `L == R` (Left hash equals Right hash) at any branch.
## References
- **Vendor Advisory:** [https://bitcoin.org/en/alert/2012-05-15-asumu-bitcoin-qt-0.6.1](https://bitcoin.org/en/alert/2012-05-15-asumu-bitcoin-qt-0.6.1)
- **NVD Entry:** [https://nvd.nist.gov/vuln/detail/CVE-2012-2459](https://nvd.nist.gov/vuln/detail/CVE-2012-2459)
- **Technical Discussion:** [https://github.com/bitcoin/bitcoin/pull/1341](https://github.com/bitcoin/bitcoin/pull/1341)