scapy icon indicating copy to clipboard operation
scapy copied to clipboard

BFD optional_auth ConditionalField condition is always True — phantom auth injected on every parsed packet

Open yue-fred-gao opened this issue 9 months ago • 1 comments

Brief description

The ConditionalField guarding optional_auth in bfd.py uses: lambda pkt: pkt.flags.names[2] == "A"

This condition is always True regardless of the packet's actual flags, because FlagsField.names returns the flag definition string (e.g. "MDACFP"), not the set of currently-set flags.

As a result, every BFD packet parsed by scapy acquires a phantom OptionalAuth with auth_type=1 auth_keyid=1, auth_key=b"password" (11 bytes) — even when the Authentication Present (A) flag is not set and no auth data exists on the wire.

Scapy version

2.7.0

Python version

3.10.12

Operating system

Linux 5.15.0

Additional environment information

No response

How to reproduce

from scapy.contrib.bfd import BFD, OptionalAuth from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP

Build a BFD packet with NO authentication (flags=0, A flag not set)

pkt = (Ether(src='00:11:22:33:44:55', dst='66:77:88:99:aa:bb') / IP(src='10.0.0.1', dst='10.0.0.2', ttl=255) / UDP(sport=49152, dport=4784) / BFD(version=1, diag=0, sta=1, flags=0, detect_mult=3, my_discriminator=1, your_discriminator=0, min_tx_interval=1000000, min_rx_interval=1000000, echo_rx_interval=0))

Verify the constructed packet is clean (24-byte BFD, no auth)

assert len(bytes(pkt[BFD])) == 24 assert b"password" not in bytes(pkt)

Serialize to wire bytes, then parse back (simulates receive path)

wire = bytes(pkt) parsed = Ether(wire)

BUG: parsed packet now has a phantom OptionalAuth

print(parsed[BFD].optional_auth)

→ <OptionalAuth auth_type=Simple auth_len=11 auth_keyid=1 auth_key=b'password' |>

assert parsed[BFD].optional_auth is not None # ← should be None assert parsed[BFD].optional_auth.auth_key == b"password"

Re-serializing inflates the packet by 11 bytes

assert len(bytes(parsed)) == len(wire) + 11 assert b"password" in bytes(parsed)

Actual result

optional_auth appears in parsed BFD packet

Expected result

optional_auth should be None

Related resources

No response

yue-fred-gao avatar Feb 27 '26 16:02 yue-fred-gao

I'll work on this issue. The fix is straightforward: replace pkt.flags.names[2] == "A" with pkt.flags.A to properly check the Authentication Present flag bit.

T3pp31 avatar Apr 12 '26 11:04 T3pp31