secdev / scapy

Scapy: the Python-based interactive packet manipulation program & library.
https://scapy.net
GNU General Public License v2.0
10.72k stars 2.02k forks source link

haslayer with conditional packet fields #4354

Closed bsullivan19 closed 6 months ago

bsullivan19 commented 6 months ago

Brief description

haslayer returns true if a Packet has a ConditionalField(PacketField) even when the condition for the conditional field is false.

Scapy version

2.5.0

Python version

3.12.2

Operating system

Windows 10

Additional environment information

No response

How to reproduce

class FizzPacket(Packet):
    fields_desc = [IntField("fizz", 1)]

class BuzzPacket(Packet):
    fields_desc = [IntField("buzz", 1)]

class FooPacket(Packet):
    fields_desc = [IntField("foo", 0),
                   ConditionalField(PacketField("fizzp", FizzPacket(), FizzPacket), lambda p: p.foo == 1),
                   ConditionalField(PacketField("buzzp", BuzzPacket(), BuzzPacket), lambda p: p.foo == 2)]
>>> pkt = FooPacket(foo=1)
>>> pkt
<FooPacket  foo=1 fizzp=<FizzPacket  |> |>
>>> BuzzPacket in pkt
True

Actual result

No response

Expected result

Expected 'BuzzPacket in pkt' to return false because the condition for the field buzzp is false.

Related resources

No response

gpotter2 commented 6 months ago

This isn't a supported behavior.

The supported ways are either: