Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

Potential vulnerability in Lightning backends: BOLT-11 "payment hash" does not commit to payment!

9 messages, Jun 19 to Jul 16, 2023. Started by callebtc.

callebtc (2), Martin Habovštiak (2), Antoine Riard, Rusty Russell, David A. Harding, fiatjaf, Olaoluwa Osuntokun

Permalink First message in the Wayback Machine

callebtc permalink original

Dear list,

earlier last month, our team at LNbits discovered a rather interesting exploit which wich would enable an attacker to create balances out of thin air by abusing a quirk in how invoices are handled internally. We've patched this in LNbits version 0.10.5 and urge anyone to update ASAP if you haven't done so already. I want to describe the attack here, since my gut feeling is that carrying out the same exploit is possible in other Lightning applications. If you're working on custodial wallets, payment processors, account management software, etc. you probably want to read this.

In short, the attacker was able to insert a bolt-11 payment hash of payment A into a different payment, creating a malicious invoice B that can trick the backend into believing that B == A.

Here is how it goes:

- Attacker creates invoice A of amount 1000 sat in LNbits
- Attacker creates invoice B' of amount 1 sat on her own node
- Attacker deserializes B', inserts payment_hash(A) into payment_hash(B), re-signs the invoice, and serializes it again, producing malicious invoice B
- Attacker creates a new account in LNbits and pays B
- LNbits backend uses payment_hash(B) to check whether this is an internal payment or a payment via LN
- Backend finds A in its database since we implicitly assume that payment_hash(A) commits to A

** This is the critical part! Payment hashes do *NOT* commit to any payment details (like amount) but only to the preimage! **

- Backend settles payment internally by crediting A debiting B
- Attacker has "created" 999 sats

Mitigation:

The mitigation is quite simple. Backends should either use self-generated unique "checking id's" for looking up internal payments or use additional checks to make sure that the invoice details have not been messed around with (e.g., asserting amount(A) == amount(B)).

Lessons:

I think there are two lessons here. First, it's good to realize the level of sophistication of LN-savvy attackers. This attack clearly involves a fundamental understanding of bolt-11 and requires custom tooling to produce the malicious invoice.

The second lesson is more valuable: The "payment hash" of an invoice is not a "payment" hash but merely a "preimage" hash – and nothing else. Naming this field as such increases the chance of developers implicitly assuming that the hash commits to payment details like amount, pubkey, etc. I will from now on call this simply the "preimage hash" and invite you to do so too.

Best

Calle

Antoine Riard in reply to callebtc permalink original

Hi calle,

Thanks for the report.

Quoted text (1 line)
>From my understanding of what you're describing, the attack is possible

because LNBits backend does not check that an external received HTLC `amount_msat` satisfies the invoice amount for both matching preimage and payment secret. This sounds plausible to me.

If you're a custodial wallet, payment processor or account management software based on LDK, and you respect the API recommendations of using `create_inbound_payment`, you should not be affected as amount equivalence checks are handled by the implementation: https://github.com/lightningdevkit/rust-lightning/blob/c3c105075aeb8128699e043f777b4c89c452e54d/lightning/src/ln/channelmanager.rs#L4469

The "checking ids" sounds a workable mitigation to firewall internal invoice state from external ones, though beware issues with id collision, if it can be determined by the attacker.

Potential safety issues with invoices have been known since CVE-2020-26896.

Good disclosure security practice suggests having previously warned the Lightning implementation maintainers on their respective security communication channels to ease patch coordination if needed with second-line vendors like wallets and processors.

Best, Antoine

Le lun. 19 juin 2023 à 17:19, callebtc via Lightning-dev < lightning-dev at lists.linuxfoundation.org> a écrit :

Quoted text (64 lines)
> Dear list,
>
> earlier last month, our team at LNbits discovered a rather interesting
> exploit which wich would enable an attacker to create balances out of thin
> air by abusing a quirk in how invoices are handled internally. We've
> patched this in LNbits version 0.10.5 and urge anyone to update ASAP if you
> haven't done so already. I want to describe the attack here, since my gut
> feeling is that carrying out the same exploit is possible in other
> Lightning applications. If you're working on custodial wallets, payment
> processors, account management software, etc. you probably want to read
> this.
>
> In short, the attacker was able to insert a bolt-11 payment hash of
> payment A into a different payment, creating a malicious invoice B that can
> trick the backend into believing that B == A.
>
> Here is how it goes:
>
> - Attacker creates invoice A of amount 1000 sat in LNbits
> - Attacker creates invoice B' of amount 1 sat on her own node
> - Attacker deserializes B', inserts payment_hash(A) into payment_hash(B),
> re-signs the invoice, and serializes it again, producing malicious invoice B
> - Attacker creates a new account in LNbits and pays B
>
> - LNbits backend uses payment_hash(B) to check whether this is an internal
> payment or a payment via LN
> - Backend finds A in its database since we implicitly assume that
> payment_hash(A) commits to A
>
> ** This is the critical part! Payment hashes do *NOT* commit to any
> payment details (like amount) but only to the preimage! **
>
> - Backend settles payment internally by crediting A debiting B
> - Attacker has "created" 999 sats
>
> Mitigation:
>
> The mitigation is quite simple. Backends should either use self-generated
> unique "checking id's" for looking up internal payments or use additional
> checks to make sure that the invoice details have not been messed around
> with (e.g., asserting amount(A) == amount(B)).
>
> Lessons:
>
> I think there are two lessons here. First, it's good to realize the level
> of sophistication of LN-savvy attackers. This attack clearly involves a
> fundamental understanding of bolt-11 and requires custom tooling to produce
> the malicious invoice.
>
> The second lesson is more valuable: The "payment hash" of an invoice is
> not a "payment" hash but merely a "preimage" hash – and nothing else.
> Naming this field as such increases the chance of developers implicitly
> assuming that the hash commits to payment details like amount, pubkey, etc.
> I will from now on call this simply the "preimage hash" and invite you to
> do so too.
>
> Best
>
> Calle
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Rusty Russell in reply to callebtc permalink original

Um, this is a super-weird setup AFAICT.

You generate an invoice, but you don't remember anything about it? Um, OK, stateless invoices are a thing, but there's a metadata field for that?

Someone pays an invoice, you don't check the place it's going (which, y'know, signed it), you check some other field?

I've never used lnbits and I'm clearly missing something...

Thanks! Rusty.

callebtc via Lightning-dev <lightning-dev at lists.linuxfoundation.org> writes:

Quoted text (38 lines)
> Dear list,
>
> earlier last month, our team at LNbits discovered a rather interesting exploit which wich would enable an attacker to create balances out of thin air by abusing a quirk in how invoices are handled internally. We've patched this in LNbits version 0.10.5 and urge anyone to update ASAP if you haven't done so already. I want to describe the attack here, since my gut feeling is that carrying out the same exploit is possible in other Lightning applications. If you're working on custodial wallets, payment processors, account management software, etc. you probably want to read this.
>
> In short, the attacker was able to insert a bolt-11 payment hash of payment A into a different payment, creating a malicious invoice B that can trick the backend into believing that B == A.
>
> Here is how it goes:
>
> - Attacker creates invoice A of amount 1000 sat in LNbits
> - Attacker creates invoice B' of amount 1 sat on her own node
> - Attacker deserializes B', inserts payment_hash(A) into payment_hash(B), re-signs the invoice, and serializes it again, producing malicious invoice B
> - Attacker creates a new account in LNbits and pays B
>
> - LNbits backend uses payment_hash(B) to check whether this is an internal payment or a payment via LN
> - Backend finds A in its database since we implicitly assume that payment_hash(A) commits to A
>
> ** This is the critical part! Payment hashes do *NOT* commit to any payment details (like amount) but only to the preimage! **
>
> - Backend settles payment internally by crediting A debiting B
> - Attacker has "created" 999 sats
>
> Mitigation:
>
> The mitigation is quite simple. Backends should either use self-generated unique "checking id's" for looking up internal payments or use additional checks to make sure that the invoice details have not been messed around with (e.g., asserting amount(A) == amount(B)).
>
> Lessons:
>
> I think there are two lessons here. First, it's good to realize the level of sophistication of LN-savvy attackers. This attack clearly involves a fundamental understanding of bolt-11 and requires custom tooling to produce the malicious invoice.
>
> The second lesson is more valuable: The "payment hash" of an invoice is not a "payment" hash but merely a "preimage" hash – and nothing else. Naming this field as such increases the chance of developers implicitly assuming that the hash commits to payment details like amount, pubkey, etc. I will from now on call this simply the "preimage hash" and invite you to do so too.
>
> Best
>
> Calle
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
callebtc in reply to Rusty Russell permalink original

Hi Rusty,

I actually think I answered most of these questions in my previous post.

- We do remember everything about the invoice in the db.
- We check where it is going (internal or external).
- We can't meaningfully check the signature (we don't know "our" pubkey, LNbits supports many different LN backends, some without that info).
- The problem is we index invoices by preimage_hash and assume it commits to payment details.

This is not a super-weird setup, I've contacted multiple service providers before posting and most do the same thing. Those that were safe were using the mitigation steps I outlined in my OP.

Happy to expand on this in more detail if needed.

Best Calle

------- Original Message ------- On Thursday, July 6th, 2023 at 09:10, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (56 lines)
> Um, this is a super-weird setup AFAICT.
>
> You generate an invoice, but you don't remember anything about it? Um,
> OK, stateless invoices are a thing, but there's a metadata field for
> that?
>
> Someone pays an invoice, you don't check the place it's going (which,
> y'know, signed it), you check some other field?
>
> I've never used lnbits and I'm clearly missing something...
>
> Thanks!
> Rusty.
>
> callebtc via Lightning-dev lightning-dev at lists.linuxfoundation.org
>
> writes:
>
> > Dear list,
> >
> > earlier last month, our team at LNbits discovered a rather interesting exploit which wich would enable an attacker to create balances out of thin air by abusing a quirk in how invoices are handled internally. We've patched this in LNbits version 0.10.5 and urge anyone to update ASAP if you haven't done so already. I want to describe the attack here, since my gut feeling is that carrying out the same exploit is possible in other Lightning applications. If you're working on custodial wallets, payment processors, account management software, etc. you probably want to read this.
> >
> > In short, the attacker was able to insert a bolt-11 payment hash of payment A into a different payment, creating a malicious invoice B that can trick the backend into believing that B == A.
> >
> > Here is how it goes:
> >
> > - Attacker creates invoice A of amount 1000 sat in LNbits
> > - Attacker creates invoice B' of amount 1 sat on her own node
> > - Attacker deserializes B', inserts payment_hash(A) into payment_hash(B), re-signs the invoice, and serializes it again, producing malicious invoice B
> > - Attacker creates a new account in LNbits and pays B
> >
> > - LNbits backend uses payment_hash(B) to check whether this is an internal payment or a payment via LN
> > - Backend finds A in its database since we implicitly assume that payment_hash(A) commits to A
> >
> > ** This is the critical part! Payment hashes do NOT commit to any payment details (like amount) but only to the preimage! **
> >
> > - Backend settles payment internally by crediting A debiting B
> > - Attacker has "created" 999 sats
> >
> > Mitigation:
> >
> > The mitigation is quite simple. Backends should either use self-generated unique "checking id's" for looking up internal payments or use additional checks to make sure that the invoice details have not been messed around with (e.g., asserting amount(A) == amount(B)).
> >
> > Lessons:
> >
> > I think there are two lessons here. First, it's good to realize the level of sophistication of LN-savvy attackers. This attack clearly involves a fundamental understanding of bolt-11 and requires custom tooling to produce the malicious invoice.
> >
> > The second lesson is more valuable: The "payment hash" of an invoice is not a "payment" hash but merely a "preimage" hash – and nothing else. Naming this field as such increases the chance of developers implicitly assuming that the hash commits to payment details like amount, pubkey, etc. I will from now on call this simply the "preimage hash" and invite you to do so too.
> >
> > Best
> >
> > Calle
> > _______________________________________________
> > Lightning-dev mailing list
> > Lightning-dev at lists.linuxfoundation.org
> > https://www.learnbitcoin.com/archive/lists/lightning-dev
David A. Harding in reply to callebtc permalink original

On 2023-06-19 05:26, callebtc via Lightning-dev wrote:

Quoted text (3 lines)
> Our team at LNbits discovered a rather interesting
> exploit which which would enable an attacker to create balances out of
> thin air by abusing a quirk in how invoices are handled internally.

Hi Calle,

If I understand correctly from some quick research, LNBits allows its users to request payments from both external users and other internal users of the same instance via the same interface, BOLT11 invoices. Users may also generate payments to both external users and other internal users via the same interface where they agree to the details in a BOLT11 invoice.

To make this possible, LNBits essentially short circuits payment attempts it believes are being made from one internal user to another internal user. Instead of passing those through an underlying LN implementation, LNBits attempts to handle them on its own.

The problem here is that the the short-circuit version didn't implement all the checks specified in BOLT4, e.g. "[...] the _final node_ [...] if the amount paid is less than the amount expected: MUST fail the HTLC"

My question is whether you think it would be worthwhile to ask developers of the underlying LN node implementations you use to support self-payment of their own invoices (if they don't already). I see this having several advantages:

1. LNBits and similar systems no longer need any short circuit logic.
    When user Alice requests a payment, LNBits simply calls the
    underlying LN implementation's standard `requestpayment` API.  When
    user Bob wants to review and pay the invoice, LNBits simply call the
    underlying implementation's `pay` API.  Whatever the result of the
    payment (success or failure), the underlying API returns that result
    to LNBits so it can make the appropriate account updates.
2. As the LN protocol grows and changes, fewer changes will be needed to
    LNBits and similar systems as they can continue to simply call the
    appropriate APIs.  For example, support for BOLT12 offers could
    become trivial to add.
3. Even for people not using LNBits, being able to pay yourself is a
    nice tool for experimentation with an API, especially if it doesn't
    cost any fees.

The downside is that it might be a bit more complex for LN implementations. However, if there are lots of services using LNBits and similar systems, and I can see how that might come to be the case, I think there's a strong argument to avoid repetition of security essential payment processing logic in both an underlying implementation and account management software.

Thanks for your security disclosure!,

-Dave

fiatjaf in reply to David A. Harding permalink original

On Thu, Jul 13, 2023 at 3:47 AM David A. Harding <dave at dtrt.org> wrote:

Quoted text (3 lines)
> My question is whether you think it would be worthwhile to ask
> developers of the underlying LN node implementations you use to support
> self-payment of their own invoices (if they don't already).

As far as I know no Lightning node has this ability, which is very unfortunate. If possible this should definitely be implemented. It would be the biggest feature for custodial Lightning service providers of all kinds since always.

Martin Habovštiak in reply to fiatjaf permalink original

Hi folks,

I would also like to point out this can help testing a lot. I do integration testing of payment flow by spawning a secondary node, setting up a channel etc which takes considerable time even automated on regtest. Also it sometimes fails for silly unrelated reasons.

Dňa so 15. 7. 2023, 20:22 fiatjaf <fiatjaf at gmail.com> napísal(a):

Quoted text (15 lines)
> On Thu, Jul 13, 2023 at 3:47 AM David A. Harding <dave at dtrt.org> wrote:
> > My question is whether you think it would be worthwhile to ask
> > developers of the underlying LN node implementations you use to support
> > self-payment of their own invoices (if they don't already).
>
> As far as I know no Lightning node has this ability, which is very
> unfortunate.
> If possible this should definitely be implemented. It would be the
> biggest feature for custodial Lightning service providers of all kinds
> since always.
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Olaoluwa Osuntokun in reply to fiatjaf permalink original

lnd supports paying invoices it generates, you just need to set the `allow_self_payment` field using this API: https://lightning.engineering/api-docs/api/lnd/router/send-payment-v2.

This _does_ end up actually finding a circular route through the network though, it's most commonly used to implement circular rebalancing.

However if you set up another node, and then fund bi-lateral "trusted channels" (so zero conf channel that will never actually confirm as the funding point will never exist on chain), then you gain the ability to pay invoices without doing the actual network route. This doesn't need any other external software, and also gives you all the normal payment/invoice records you'd expect for normal payments.

Another way to accomplish the same thing would be to use the `"allow-circular-route` flag, which'll let you double back on the same channel (incoming+outgoing channel is the same for the route).

-- Laolu

On Sat, Jul 15, 2023 at 8:22 PM fiatjaf <fiatjaf at gmail.com> wrote:

Quoted text (15 lines)
> On Thu, Jul 13, 2023 at 3:47 AM David A. Harding <dave at dtrt.org> wrote:
> > My question is whether you think it would be worthwhile to ask
> > developers of the underlying LN node implementations you use to support
> > self-payment of their own invoices (if they don't already).
>
> As far as I know no Lightning node has this ability, which is very
> unfortunate.
> If possible this should definitely be implemented. It would be the
> biggest feature for custodial Lightning service providers of all kinds
> since always.
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Martin Habovštiak in reply to Olaoluwa Osuntokun permalink original

Yeah, my point was to not have to setup any other instance of LND. I'm just testing LND <-> app integration - whether the app receives payments from LND. So another similar flag to do self-payment without going through network would be ideal.

Dňa ne 16. 7. 2023, 19:32 Olaoluwa Osuntokun <laolu32 at gmail.com> napísal(a):

Quoted text (45 lines)
> lnd supports paying invoices it generates, you just need to set the
> `allow_self_payment` field using this
> API: https://lightning.engineering/api-docs/api/lnd/router/send-payment-v2
> .
>
> This _does_ end up actually finding a circular route through the network
> though, it's most commonly used to implement circular rebalancing.
>
> However if you set up another node, and then fund bi-lateral "trusted
> channels" (so zero conf channel that will never actually confirm as the
> funding point will never exist on chain), then you gain the ability to pay
> invoices without doing the actual network route. This doesn't need any
> other
> external software, and also gives you all the normal payment/invoice
> records
> you'd expect for normal payments.
>
> Another way to accomplish the same thing would be to use the
> `"allow-circular-route` flag, which'll let you double back on the same
> channel (incoming+outgoing channel is the same for the route).
>
> -- Laolu
>
> On Sat, Jul 15, 2023 at 8:22 PM fiatjaf <fiatjaf at gmail.com> wrote:
>
>> On Thu, Jul 13, 2023 at 3:47 AM David A. Harding <dave at dtrt.org> wrote:
>> > My question is whether you think it would be worthwhile to ask
>> > developers of the underlying LN node implementations you use to support
>> > self-payment of their own invoices (if they don't already).
>>
>> As far as I know no Lightning node has this ability, which is very
>> unfortunate.
>> If possible this should definitely be implemented. It would be the
>> biggest feature for custodial Lightning service providers of all kinds
>> since always.
>> _______________________________________________
>> Lightning-dev mailing list
>> Lightning-dev at lists.linuxfoundation.org
>> https://www.learnbitcoin.com/archive/lists/lightning-dev
>>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>