mononaut Profile picture
Oct 21 20 tweets 6 min read Twitter logo Read on Twitter
How does a lightning replacement cycling attack work?

There's a lot of discussion about this newly discovered vulnerability on the mailing lists, but the actual mechanism is a bit hard to follow.

So here's an illustrated primer...

🧵 1/n


Image
Image
Image
Image
2/ Imagine Bob is routing a lightning payment from Alice to Carol.

While in flight, the payment is protected by HTLC outputs in his pre-signed channel commitments with each peer. Image
3/ An HTLC (Hash/Time Lock Contract) is a conditional payment from sender to receiver.

It can be spent immediately by the receiver by revealing the preimage to a hash H, or reclaimed by the sender after some timeout.

Check out a real HTLC spend here: mempool.space/tx/4ec0f703e08…
Image
4/ By securing the HTLC on each hop with the same hashlock, payments can be routed atomically.

Carol can't claim the outgoing HTLC without revealing the preimage, which Bob can then use to redeem the incoming HTLC from Alice.

At least that's the theory...
5/ To ensure Bob has time to react if something goes wrong, the timelock on the outgoing HTLC expires first at some block height T.

Then the timelock on the incoming HTLC expires at some later height T+Δ, after which Alice can reclaim her money.
6/ OK, so here's the attack:

Remember Bob has HTLCs pending in two channels.

One outgoing HTLC to Carol, which expires at block T, and one incoming HTLC from Alice, which expires at block T+Δ. Image
7/ At block T, Carol still hasn't revealed the preimage to settle the payment, so Bob is forced to time it out on-chain.

He broadcasts the commitment tx to close his channel with Carol, and once it confirms sends an "htlc-timeout" tx which spends the HTLC to reclaim his funds. Image
8/ Unbeknownst to Bob, Alice and Carol are colluding to steal his money.

They have prepared for the attack by broadcasting a chain of two transactions with low fees, apparently unrelated to the lightning channel, which we'll call the "cycle parent" and "cycle child". Image
9/ As soon as the attackers see Bob's htlc-timeout transaction hit the mempool, they broadcast an "htlc-preimage" transaction, which spends both the HTLC output (using Carol's hash preimage) and an output from the cycle parent. Image
10/ Since this htlc-preimage transaction pays a higher fee rate and spends the same inputs, it replaces both the cycle child and Bob's htlc-timeout transaction in the mempool. Image
11/ If Bob sees this, he can take the preimage and use it to immediately redeem the incoming HTLC from Alice.

So the attackers broadcast a new transaction replacing the cycle parent.

The htlc-preimage depends on that for one of its inputs, so is also evicted from the mempool. Image
12/ At the end of this cycle, the HTLC from Bob's channel with Carol ends up unspent, and no trace of the htlc-timeout and htlc-preimage transactions remain in the mempool. Image
13/ The attackers repeat the cycle to eject Bob's htlc-timeout transaction every time he rebroadcasts it.

If they prevent it getting mined for another Δ blocks, Alice can timeout the HTLC on the other channel, and leave Bob out of pocket for the entire value of the payment.
14) This attack isn't easy. Pulling it off involves:
- opening two channels with the victim.
- routing a payment through them.
- successfully replacement-cycling the victim's htlc-timeouts for Δ blocks.
- without the victim discovering the htlc-preimage transaction.
15/ But it's also hard to solve completely.

Increasing the timelock delta or rebroadcasting the htlc-timeout more aggressively make the attack more difficult and more expensive, but still not impossible.
16/ Bob can actively monitor his local mempool to spot the htlc-timeout before it gets replaced.

But a smart attacker could selectively broadcast replacements so that miners receive them while Bob does not.
17/ Perhaps Bob could improve his chances by employing watchtowers connected to other parts of the network to look out for cycled htlc-timeouts and forward him any relevant preimages.
18/ A proper fix probably requires more fundamental changes.

We could redesign the HTLC protocol to prevent adding extra inputs to htlc-preimages (so they can't be replaced).

Or change relay policy to propagate replaced transactions (so the preimage always reaches Bob).
19/ Or have miners keep a cache of recently replaced transactions which may be able to re-enter the mempool later (so that Bob doesn't need to rebroadcast his htlc-timeout).

This could be built into Bitcoin Core, or run as an external service.
20/ Or soft fork in a new opcode which does the opposite of check-locktime-verify (so we can make the htlc-preimage spend path invalid as soon as the timelock expires).

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with mononaut

mononaut Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mononautical

Sep 13
🚨🚨🚨 BREAKING 🚨🚨🚨
The fat fingers belong to PayPal
I had initially discounted that possibility, but after receiving a tip-off I took another look.

The overpaid fee came from a hot wallet reusing the address bc1qr3...zpw3, which started operating in June of this year. Image
The on-chain activity is consistent with automated processing of fiat-denominated withdrawals, and also closely matches the behavior of a now inactive wallet bc1qhs...kx4n, which is labelled as PayPal on .

oxt.me
oxt.me/address/bc1qhs…
Image
Read 8 tweets
May 30
a substack post going around at the moment claims that a single entity owns 64% of all inscriptions created since early March, paying an eye-watering 1056 BTC for the privilege

it's not true.

block21m.substack.com/p/most-bitcoin… Image
I've seen a lot of takes already suggesting this sounds like market manipulation, money laundering, or a well-funded attack on Bitcoin by wealthy adversaries.

but the truth is much less exciting.
inscriptions are created with a two-phase commit/reveal process.

first, a taproot output is created which commits to the inscription data and a public key. Image
Read 8 tweets
Feb 4
While everyone's distracted by jpegs, something else is burning blockspace and bloating the UTXO set.

Over the past month, it may have used up to 2% of block capacity, created 0.4% of the current UTXO set, and fueled the recent spike in P2TR outputs.

mempool.space/block/00000000…
Each of the tiny transactions in that block spends a P2WSH input with a complex witness script.

It pays exactly 606 sats in fees, and creates a single P2TR output with a small round number of satoshis - usually 2500, 5000, or 10000.

mempool.space/tx/512e863a7b2…
The P2WSH inputs are peeled off one by one in precise amounts in long chains of apparently pointless transactions.
Read 9 tweets
Dec 4, 2022
I asked ChatGPT to take a 62-part political compass test.

here's the result.
The AI had strong opinions on only 12 of the propositions. You could probably guess which.

E.g: on racial supremacy:
on abortion
Read 11 tweets
Nov 7, 2022
FTX's main known cold storage balance dropped from 20136 BTC down to zero today, in the first activity on that address since early 2021.

mempool.space/address/bc1qtw…
Their hot wallet avoids address reuse, so it's tricky to estimate a balance, but tracing payouts on-chain suggests they might have about 12685 of that BTC remaining in hot addresses.
Withdrawals were processed in peel-chains of only 30 batched payouts at a time, which might genuinely explain throughput issues.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(