|  | 
|  | 1 | +# Sphinx Onion Routing in Lightning Network | 
|  | 2 | + | 
|  | 3 | +The Lightning Network uses a Sphinx-based onion message protocol to send | 
|  | 4 | +messages across the Lightning Network. These messages have the property that a | 
|  | 5 | +node which is part of such a message and for example forwards such an onion | 
|  | 6 | +message cannot learn about the destination of this whole packet. It only knows | 
|  | 7 | +the predecessor and the successor of the message. In other words, it only knows | 
|  | 8 | +where the message came from and where it needs to be forwarded. This makes the | 
|  | 9 | +message protocol of the Lightning Network very private. Only the sender | 
|  | 10 | +(creator of the whole onion packet) knows the whole route of the packet. Also, | 
|  | 11 | +the receiver has no idea from which node the message originated. | 
|  | 12 | + | 
|  | 13 | +This diagram illustrates how Sphinx onion routing works in the Lightning | 
|  | 14 | +Network, showing the privacy properties at each hop: | 
|  | 15 | + | 
|  | 16 | +```ascii | 
|  | 17 | +Alice (Sender) | 
|  | 18 | +    | | 
|  | 19 | +    | Knows Full Route: Alice → Bob → Carol → David → Eve | 
|  | 20 | +    | | 
|  | 21 | +    v | 
|  | 22 | +Bob (Hop 1) | 
|  | 23 | +    | | 
|  | 24 | +    | Knows: Alice → Carol | 
|  | 25 | +    | | 
|  | 26 | +    v | 
|  | 27 | +Carol (Hop 2) | 
|  | 28 | +    | | 
|  | 29 | +    | Knows: Bob → David | 
|  | 30 | +    | | 
|  | 31 | +    v | 
|  | 32 | +David (Hop 3) | 
|  | 33 | +    | | 
|  | 34 | +    | Knows: Carol → Eve | 
|  | 35 | +    | | 
|  | 36 | +    v | 
|  | 37 | +Eve (Receiver) | 
|  | 38 | +    | | 
|  | 39 | +    | Knows: From David | 
|  | 40 | +    | | 
|  | 41 | +    v | 
|  | 42 | +
 | 
|  | 43 | +Privacy Properties: | 
|  | 44 | +- Each hop only knows its immediate neighbors | 
|  | 45 | +- Receiver doesn't know the original sender | 
|  | 46 | +- Only sender knows the complete route | 
|  | 47 | +- Each hop peels one layer of the onion | 
|  | 48 | +``` | 
|  | 49 | + | 
|  | 50 | +## Privacy Properties | 
|  | 51 | + | 
|  | 52 | +1. **Hop Privacy**: Each intermediate node only knows: | 
|  | 53 | +   - The previous hop (where the packet came from) | 
|  | 54 | +   - The next hop (where to forward the packet) | 
|  | 55 | +   - Cannot see the full route or final destination | 
|  | 56 | + | 
|  | 57 | +2. **Sender Privacy**: Only the sender (Alice) knows: | 
|  | 58 | +   - The complete route | 
|  | 59 | +   - All intermediate nodes | 
|  | 60 | +   - The final destination | 
|  | 61 | + | 
|  | 62 | +3. **Receiver Privacy**: The receiver (Eve) only knows: | 
|  | 63 | +   - The immediate previous hop (David) | 
|  | 64 | +   - Cannot determine the original sender | 
|  | 65 | + | 
|  | 66 | +4. **Onion Encryption**: Each hop peels one layer of encryption, revealing only | 
|  | 67 | +   the next hop's information | 
|  | 68 | + | 
|  | 69 | + | 
|  | 70 | +The detailed mechanics are described in [BOLT 04](https://github.com/lightning/bolts/blob/master/04-onion-routing.md) | 
|  | 71 | + | 
|  | 72 | + | 
|  | 73 | +## Replaying Onion Packets | 
|  | 74 | + | 
|  | 75 | +The [sphinx protocol](https://cypherpunks.ca/~iang/pubs/Sphinx_Oakland09.pdf) | 
|  | 76 | +clearly states that implementations of the protocol should guard against  | 
|  | 77 | +replay attacks. | 
|  | 78 | +Replaying (resending) onion packets into the network can compromise the | 
|  | 79 | +privacy guarantees promised by the protocol. So it is crucial for node | 
|  | 80 | +participants to not forward replayed onion packets to guard the privacy of all | 
|  | 81 | +network participants. Compared to the original Sphinx protocol, the Lightning | 
|  | 82 | +Network has an improved replay protection in place especially when it comes to | 
|  | 83 | +forwarding HTLCs, which are different from Onion Messages introduced later on | 
|  | 84 | +because HTLCs lock a payment to the particular onion packet. Therefore, sending | 
|  | 85 | +HTLCs packets comes with a cost. Moreover, every HTLC has an expiry date, also | 
|  | 86 | +called CLTV (absolute locktime), which prevents the replay of packets that have | 
|  | 87 | +already expired. In addition to the CLTV expiry of a packet, every HTLC onion | 
|  | 88 | +packet commits to the payment hash in the HMAC of the message (to be precise, | 
|  | 89 | +the associated data), so this prevents an attacker from attaching an old onion | 
|  | 90 | +packet to a new payment hash, which now also comes with the risk for the | 
|  | 91 | +attacker that he does not only have to lock funds when replaying an onion | 
|  | 92 | +packet but he also risks that the next node settles the HTLC because it already | 
|  | 93 | +knows the preimage of the HTLC. Although the attack comes with a high cost, | 
|  | 94 | +Lightning implementations should prevent replayed onion packets from | 
|  | 95 | +propagating through the network to safeguard the privacy for every network | 
|  | 96 | +participant. | 
|  | 97 | + | 
|  | 98 | +## Replay attack | 
|  | 99 | + | 
|  | 100 | +An attacker could execute a re-injection attack by strategically positioning | 
|  | 101 | +well-connected nodes within the network, ensuring they participate in a high | 
|  | 102 | +volume of payment forwarding. In this scenario, the attacker's nodes act as | 
|  | 103 | +forwarding intermediaries. These malicious nodes would collect passing onion | 
|  | 104 | +packets and concurrently monitor network gossip. Upon observing a successful | 
|  | 105 | +payment forward, the attacker could re-inject the captured onion message. By | 
|  | 106 | +then monitoring channel updates or closures from the neighboring node, the | 
|  | 107 | +attacker aims to determine if that channel was part of the payment path. | 
|  | 108 | +Furthermore, if the neighboring node, lacking a suitable outgoing channel, | 
|  | 109 | +instead settles the payment (i.e., acts as the final recipient), it becomes | 
|  | 110 | +highly probable that it was the ultimate receiving node. It's important to note, | 
|  | 111 | +however, that such an attack carries significant costs. | 
|  | 112 | + | 
|  | 113 | +### Replay Protection in LND | 
|  | 114 | + | 
|  | 115 | +In LND there are two interchanging DB names which save information of | 
|  | 116 | +those onion packets to prevent replays from happening. They are called  | 
|  | 117 | +`Sphinx-Replay-DB` or `Decayed-Log-DB`. | 
|  | 118 | +Currently LND only implements onion messages which are tied to payments | 
|  | 119 | +(i.e. HTLCs), and as explained above, HTLCs expire after an absolute lock time | 
|  | 120 | +and therefore can be garbage collected because they will not be forwarded by  | 
|  | 121 | +nodes anyways as that would entail a risk of losing funds. | 
|  | 122 | + | 
|  | 123 | +Sphinx replay protection storage for the different backends: | 
|  | 124 | + | 
|  | 125 | +1. When running LND with the BBolt backend the db is called: `sphinxreplay.db`. | 
|  | 126 | + | 
|  | 127 | +2. For Postgres the table is called: `decayedlogdb_kv`. | 
|  | 128 | + | 
|  | 129 | +3. For SQLite the table is called `decayedlogdb_kv` and is part of the | 
|  | 130 | +   `channel.sqlite` file. | 
|  | 131 | + | 
|  | 132 | +LND employs an internal stores to prevent replay attacks. The internal store | 
|  | 133 | +maintains a record of each onion packet's shared secret alongside its absolute | 
|  | 134 | +timelock (CLTV). This enables LND to efficiently identify and discard replayed | 
|  | 135 | +packets that present an already-seen shared secret. | 
|  | 136 | + | 
|  | 137 | +This store is subject to garbage collection, ensuring it does not impose a | 
|  | 138 | +sustained burden on memory resources. After the CLTV values expiry, the entries | 
|  | 139 | +can be removed as described earlier. | 
|  | 140 | + | 
|  | 141 | +#### What happens if LND encounters a replayed onion HTLC packet? | 
|  | 142 | + | 
|  | 143 | +When LND encounters a replay, it will reject the HTLC and it will not signal a | 
|  | 144 | +specific error that a replay occurred which would reveal that the node was | 
|  | 145 | +indeed part of the route in the onion packet. LND will reject the HTLC and | 
|  | 146 | +respond with the message `invalid_onion_version`. See | 
|  | 147 | +[BOLT 04](https://github.com/lightning/bolts/blob/master/04-onion-routing.md) | 
|  | 148 | + | 
|  | 149 | + | 
|  | 150 | +## Other attacks | 
|  | 151 | + | 
|  | 152 | +In the Lightning Network, the Sphinx protocol effectively mitigates | 
|  | 153 | +`tagging attacks.` This is due to the fact that each onion packet incorporates | 
|  | 154 | +a cryptographic HMAC (Hash-based Message Authentication Code), which is derived | 
|  | 155 | +from the packet's encrypted contents. Consequently, any attempt to tamper with | 
|  | 156 | +the packet's data would render its HMAC invalid, causing the packet to be | 
|  | 157 | +discarded by honest nodes. | 
|  | 158 | + | 
0 commit comments