scapy icon indicating copy to clipboard operation
scapy copied to clipboard

FieldListFields used non-exclusively in a MultipleTypeField don't build correctly

Open nununoisy opened this issue 3 years ago • 1 comments

Brief description

I am using a MultipleTypeField to switch between a complex field (itself a MultipleTypeField) and a FieldListField of that field for array types. The definition works while dissecting, but it does not work when trying to build the packet unless that packet was constructed from bytes.

I tried doing some debugging to see if I could come up with a workaround, and I did notice something: during building, Packet.getfieldval returns the first element of the value array instead of the entire array as I would expect. This may mean that the outer MultipleTypeField is returning dflt when it's not supposed to, though strangely the inner MultipleTypeField is working correctly.

Scapy version

scapy-2.4.5rc1.dev293 (commit ba9fff2)

Python version

3.10.4

Operating system

Ubuntu 22.04 LTS on WSL2

Additional environment information

No response

How to reproduce

Gist with a script to reproduce the issue

Actual result

Traceback (most recent call last):
  File "/<project>/scapybug.py", line 64, in <module>
    test_pkt_arr_filled = DataPacket(raw(test_pkt_arr))
  File "/<project>/venv/lib/python3.10/site-packages/scapy/compat.py", line 294, in raw
    return bytes(x)
  File "/<project>/venv/lib/python3.10/site-packages/scapy/packet.py", line 586, in __bytes__
    return self.build()
  File "/<project>/venv/lib/python3.10/site-packages/scapy/packet.py", line 727, in build
    p = self.do_build()
  File "/<project>/venv/lib/python3.10/site-packages/scapy/packet.py", line 707, in do_build
    pkt = self.self_build()
  File "/<project>/venv/lib/python3.10/site-packages/scapy/packet.py", line 686, in self_build
    raise ex
  File "/<project>/venv/lib/python3.10/site-packages/scapy/packet.py", line 677, in self_build
    p = f.addfield(self, p, val)
  File "/<project>/venv/lib/python3.10/site-packages/scapy/fields.py", line 537, in addfield
    return fld.addfield(pkt, s, val)
  File "/<project>/venv/lib/python3.10/site-packages/scapy/fields.py", line 2002, in addfield
    for v in val:
TypeError: While dissecting field 'value': 'int' object is not iterable

Expected result

The program should not crash, and test_pkt_arr_filled == test_pkt_arr_raw should be True.

Related resources

No response

nununoisy avatar Jun 28 '22 22:06 nununoisy

Quick update: if I set explicit on the packet instance to 1, I get the expected behavior. This could be the same issue as #1143 - is this intended behavior then?

nununoisy avatar Jun 29 '22 21:06 nununoisy