secdev / scapy

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

Can't decode SNMP v2c packets #4588

Open giovanni-bellini-argo opened 1 week ago

giovanni-bellini-argo commented 1 week ago

Brief description

when scapy tryes to decode an SNMP v2c packet the following error is thrown

  File "C:\Users\User\Desktop\programmi\tryouts\snmp.py", line 122, in parse_snmp
    print(SNMPresponse(bytes(packet)))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\base_classes.py", line 399, in __call__
    i.__init__(*args, **kargs)
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\packet.py", line 165, in __init__
    self.dissect(_pkt)
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\packet.py", line 1029, in dissect
    s = self.do_dissect(s)
        ^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1packet.py", line 55, in do_dissect
    return self.ASN1_root.dissect(self, x)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1fields.py", line 496, in dissect
    _, x = self.m2i(pkt, s)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1fields.py", line 480, in m2i
    i, s, remain = codec.check_type_check_len(s)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1\ber.py", line 330, in check_type_check_len
    l, s3 = cls.check_type_get_len(s)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1\ber.py", line 321, in check_type_get_len
    s2 = cls.check_type(s)
         ^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\programmi\tryouts\env\Lib\site-packages\scapy\asn1\ber.py", line 312, in check_type
    raise BER_BadTag_Decoding_Error(
scapy.asn1.ber.BER_BadTag_Decoding_Error: BERcodec_SNMP_PDU_RESPONSE: Got tag [4/0x4] while expecting <ASN1Tag PDU_RESPONSE[162]>

the following is the packet bytes string:

b"\x04{\xcbf\xf2^\xd8\xec\xe5\x96\xa4d\x08\x00E\x00\x00\x9d\x00\x00@\x00@\x11\xf6\xae\xc0\xa8a\x0e\xc0\xa8aB\x00\xa1\xd7\x16\x00\x89Z'0\x7f\x02\x01\x01\x04\x06public\xa2r\x02\x01\x00\x02\x01\x00\x02\x01\x000g0\x15\x06\x08+\x06\x01\x02\x01\x01\x01\x00\x04\tGS1900-240\x13\x06\x08+\x06\x01\x02\x01\x01\x04\x00\x04\x07Contact0\x12\x06\x08+\x06\x01\x02\x01\x01\x05\x00\x04\x06GS19000\x14\x06\x08+\x06\x01\x02\x01\x01\x06\x00\x04\x08Location0\x0f\x06\x0b+\x06\x01\x02\x01\x19\x03\x02\x01\x02\x01\x80\x00"

Scapy version

2.5.0

Python version

3.12.7

Operating system

Windows 11 / Ubuntu 22.04.4 LTS

Additional environment information

No response

How to reproduce

from scapy.all import *

b_str = b"\x04{\xcbf\xf2^\xd8\xec\xe5\x96\xa4d\x08\x00E\x00\x00\x9d\x00\x00@\x00@\x11\xf6\xae\xc0\xa8a\x0e\xc0\xa8aB\x00\xa1\xd7\x16\x00\x89Z'0\x7f\x02\x01\x01\x04\x06public\xa2r\x02\x01\x00\x02\x01\x00\x02\x01\x000g0\x15\x06\x08+\x06\x01\x02\x01\x01\x01\x00\x04\tGS1900-240\x13\x06\x08+\x06\x01\x02\x01\x01\x04\x00\x04\x07Contact0\x12\x06\x08+\x06\x01\x02\x01\x01\x05\x00\x04\x06GS19000\x14\x06\x08+\x06\x01\x02\x01\x01\x06\x00\x04\x08Location0\x0f\x06\x0b+\x06\x01\x02\x01\x19\x03\x02\x01\x02\x01\x80\x00"

SNMPresponse(b_str).show()

Actual result

No response

Expected result

No response

Related resources

No response