SWC-117_Signature Malleability
Signature Malleability
Description: The implementation of a cryptographic signature system in Ethereum contracts often assumes that the signature is unique, but signatures can be altered without the possession of the private key and still be valid. The EVM specification defines several so-called ‘precompiled’ contracts one of them being
ecrecover
which executes the elliptic curve public key recovery. A malicious user can slightly modify the three values v, r and s to create other valid signatures. A system that performs signature verification on contract level might be susceptible to attacks if the signature is part of the signed message hash. Valid signatures could be created by a malicious user to replay previously signed messages.Remediation: A signature should never be included into a signed message hash to check if previously messages have been processed by the contract.
vulnerability contract 1:
我的理解
(1)这个合约txid如果包含sig,那么sig可以最多被修改成四个不同的,这四个不同的sig对应的消息m是相同的,也就是说value,to,gasprice,nonce是相同的。
(2)然后这四个sig都可以执行transfer方法,因为算出来的txid不一样,但是解析出来的签名者是一样的,因此该签名者会被最多扣除4次余额。
(3)照这么说,以太坊的消息为啥不会被重放呢?因为以太坊会拒绝相同的nonce,无法重放。但是在这里不存在这个问题
(4)然后这个SWC把sig从txid中的hash移除了,但是这会造成DoS(因为同一个value,to,gasprice,nonce被执行之前,不能被其他人再次执行),在GitHub讨论的修改方法如链接:https://github.com/SmartContractSecurity/SWC-registry/issues/173ECDSA链接:
4篇连载:https://coders-errand.com/malleability-ecdsa-signatures/
https://eklitzke.org/bitcoin-transaction-malleability
https://hackernoon.com/what-is-the-math-behind-elliptic-curve-cryptography-f61b25253da3
https://www.derpturkey.com/inherent-malleability-of-ecdsa-signatures/
然后我去stackoverflow问了,答案很清晰
This is how someone can exploit it:
- Alice sends some tokens to Bob using the
transfer
function. All normal so far, sincetxid
wasn’t seen before thensignatureUsed[txid] == false
and the payment goes through.- Bob is our exploiter. He picks the
signature = (r, s, v)
used by Alice and creates a new onesignature2 = (r2, s2, v2)
like this:
1
2
3 r2 = r
s2 = s
v2 = v<27 ? v+27 : v-27
- Bob calls
transfer
with the same parameters used by Alice but usingsignature2
.txid
will be different than before sosignatureUsed[txid] == false
. The signature is also recognized as a valid Alice signature (see howecrecoverFromSig
handlesv
…). So the payment goes through.- At then end Bob stole an extra payment from Alice.
The root of the problem is that it’s possible to use a signature to make another valid one. This opens up to “replay attacks”.
This particular code tries to prevent replay attacks by checking if
txid
was seen before. Howevertxid
was calculated using the signature, thus failing to prevent this exploit. The fix is just to remove the signature fromtxid
.
1 | pragma solidity ^0.4.24; |
fix
1 | // instead of: mapping(bytes32 => bool) signatureUsed; |
or
1 | // SPDX-License-Identifier: MIT |
vulnerability contract 2:
1 | pragma solidity 0.4.24; |