scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Attempting to craft or modify a STUN packet using the STUN contrib results in AttributeError: tlvlist

Open Fredddi43 opened this issue 2 years ago • 3 comments

Brief description

Whenever I try to modify an imported STUN packet or craft a new one using the recent STUN contribution, the operation fails with an AttributeError: tlvlist

Scapy version

2.5.0

Python version

3.10.6

Operating system

Linux 5.19.0-40-generic

Additional environment information

No response

How to reproduce

  1. Import STUN contribution: load_contrib("stun")
  2. Attempt to craft a stun packet using defaults or any variables, and send it out: sendp(IP(dst="8.8.8.8")/UDP()/STUN())

Actual result

Operation fails with a AttributeError: tlvlist

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 455, in __getattr__
    fld, v = self.getfield_and_val(attr)
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 450, in getfield_and_val
    raise ValueError
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/scapy/sendrecv.py", line 478, in sendp
    return _send(
  File "/usr/local/lib/python3.10/dist-packages/scapy/sendrecv.py", line 415, in _send
    results = __gen_send(socket, x, inter=inter, loop=loop,
  File "/usr/local/lib/python3.10/dist-packages/scapy/sendrecv.py", line 377, in __gen_send
    s.send(p)
  File "/usr/local/lib/python3.10/dist-packages/scapy/arch/linux.py", line 567, in send
    return SuperSocket.send(self, x)
  File "/usr/local/lib/python3.10/dist-packages/scapy/supersocket.py", line 103, in send
    sx = raw(x)
  File "/usr/local/lib/python3.10/dist-packages/scapy/compat.py", line 294, in raw
    return bytes(x)
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 589, in __bytes__
    return self.build()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 730, in build
    p = self.do_build()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 713, in do_build
    pay = self.do_build_payload()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 699, in do_build_payload
    return self.payload.do_build()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 713, in do_build
    pay = self.do_build_payload()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 699, in do_build_payload
    return self.payload.do_build()
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 715, in do_build
    return self.post_build(pkt, pay)
  File "/usr/local/lib/python3.10/dist-packages/scapy/contrib/stun.py", line 256, in post_build
    for attr in self.tlvlist:
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 457, in __getattr__
    return self.payload.__getattr__(attr)
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 455, in __getattr__
    fld, v = self.getfield_and_val(attr)
  File "/usr/local/lib/python3.10/dist-packages/scapy/packet.py", line 1764, in getfield_and_val
    raise AttributeError(attr)
AttributeError: tlvlist

Expected result

Correctly crafted STUN packet is sent out.

Related resources

No response

Fredddi43 avatar Apr 20 '23 00:04 Fredddi43

Thanks for the report. I believe that no one really used this code to build a packet, does this patch works for your use case?

diff --git a/scapy/contrib/stun.py b/scapy/contrib/stun.py
index 92876c4a..9129204a 100644
--- a/scapy/contrib/stun.py
+++ b/scapy/contrib/stun.py
@@ -253,7 +253,7 @@ class STUN(Packet):
         pkt += pay
         if self.length is None:
             pkt = pkt[:2] + struct.pack("!h", len(pkt) - 20) + pkt[4:]
-        for attr in self.tlvlist:
+        for attr in self.attributes:
             if isinstance(attr, STUNMessageIntegrity):
                 pass    # TODO Fill hmac-sha1 in MESSAGE-INTEGRITY attribute
         return pkt

guedou avatar May 12 '23 15:05 guedou

Hey, had the same problem, @guedou's solution did the trick.

paul1278 avatar Jun 09 '23 09:06 paul1278

Finally got around to working on the project I needed this for again, can confirm that this patch does indeed seem to fix the issue. If you could go ahead and merge that fix that would be great, feel free to close as needed.

Thanks for the report. I believe that no one really used this code to build a packet, does this patch works for your use case?

diff --git a/scapy/contrib/stun.py b/scapy/contrib/stun.py
index 92876c4a..9129204a 100644
--- a/scapy/contrib/stun.py
+++ b/scapy/contrib/stun.py
@@ -253,7 +253,7 @@ class STUN(Packet):
         pkt += pay
         if self.length is None:
             pkt = pkt[:2] + struct.pack("!h", len(pkt) - 20) + pkt[4:]
-        for attr in self.tlvlist:
+        for attr in self.attributes:
             if isinstance(attr, STUNMessageIntegrity):
                 pass    # TODO Fill hmac-sha1 in MESSAGE-INTEGRITY attribute
         return pkt

Fredddi43 avatar Oct 24 '23 23:10 Fredddi43