Repository: secdev/scapy

Issue:
Scapy didn't support ASN1 GAUGE32 
When i read snmp packets from scapy_snmp_error_1.pcap, Scapy can't decode ASN1 GAUGE32.
![scap-snmp-error](https://cloud.githubusercontent.com/assets/3477662/19773395/f93937c0-9c9b-11e6-8c52-5ade5c72643a.jpg)

``` python
>>> packets=rdpcap('scapy_snmp_error_1.pcap')
>>> packets[1]
<Ether  dst=40:6c:8f:5a:f3:d4 src=00:1b:1b:a7:2f:6c type=0x800 |<IP  version=4L ihl=5L tos=0x0 len=72 id=19707 flags= frag=0L ttl=30 proto=udp chksum=0x4131 src=192.168.70.210 dst=192.168.70.86 options=[] |<UDP  sport=snmp dport=snmp len=52 chksum=0xbc91 |<Raw  load='0*\x02\x01\x00\x04\x07private\xa2\x1c\x02\x01\x00\x02\x01\x00\x02\x01\x000\x110\x0f\x06\n(\xc4b\x01\x01\x02\x01\x02\x02\x00B\x01\x12' |>>>>
>>>SNMP(packets[1].load)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/scapy/base_classes.py", line 223, in __call__
    i.__init__(*args, **kargs)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 95, in __init__
    self.dissect(_pkt)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 619, in dissect
    s = self.do_dissect(s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1packet.py", line 28, in do_dissect
    return self.ASN1_root.dissect(self, x)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 329, in dissect
    _,x = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 322, in m2i
    s = obj.dissect(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 123, in dissect
    v,s = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 482, in m2i
    return self.extract_packet(choice, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 108, in extract_packet
    c = cls(s)
  File "/usr/local/lib/python2.7/site-packages/scapy/base_classes.py", line 223, in __call__
    i.__init__(*args, **kargs)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 95, in __init__
    self.dissect(_pkt)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 619, in dissect
    s = self.do_dissect(s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1packet.py", line 28, in do_dissect
    return self.ASN1_root.dissect(self, x)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 329, in dissect
    _,x = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 322, in m2i
    s = obj.dissect(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 123, in dissect
    v,s = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 364, in m2i
    c,s = self.extract_packet(self.cls, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 108, in extract_packet
    c = cls(s)
  File "/usr/local/lib/python2.7/site-packages/scapy/base_classes.py", line 223, in __call__
    i.__init__(*args, **kargs)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 95, in __init__
    self.dissect(_pkt)
  File "/usr/local/lib/python2.7/site-packages/scapy/packet.py", line 619, in dissect
    s = self.do_dissect(s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1packet.py", line 28, in do_dissect
    return self.ASN1_root.dissect(self, x)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 329, in dissect
    _,x = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 322, in m2i
    s = obj.dissect(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 123, in dissect
    v,s = self.m2i(pkt, s)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1fields.py", line 89, in m2i
    return codec.dec(s, context=self.context)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1/ber.py", line 237, in dec
    return cls.do_dec(s, context, safe)
  File "/usr/local/lib/python2.7/site-packages/scapy/asn1/ber.py", line 230, in do_dec
    raise BER_Decoding_Error("Unknown prefix [%02x] for [%r]" % (p,t), remaining=s)
BER_Decoding_Error: Unknown prefix [42] for ['B\x01\x12']
### Already decoded ###
None
### Remaining ###
'B\x01\x12'
### Already decoded ###
None
### Remaining ###
'B\x01\x12'
```

[scapy_snmp_error_1.zip](https://github.com/secdev/scapy/files/556231/scapy_snmp_error_1.zip)



Discussion:

Resolving patch:
diff --git a/scapy/asn1/asn1.py b/scapy/asn1/asn1.py
--- a/scapy/asn1/asn1.py
+++ b/scapy/asn1/asn1.py
@@ -172,6 +172,7 @@ class ASN1_Class_UNIVERSAL(ASN1_Class):
     BMP_STRING = 30
     IPADDRESS = 0|0x40          # application-specific encoding
     COUNTER32 = 1|0x40          # application-specific encoding
+    GAUGE32 = 2|0x40            # application-specific encoding
     TIME_TICKS = 3|0x40         # application-specific encoding
 
 
@@ -373,7 +374,10 @@ class ASN1_IPADDRESS(ASN1_STRING):
 
 class ASN1_COUNTER32(ASN1_INTEGER):
     tag = ASN1_Class_UNIVERSAL.COUNTER32
-    
+
+class ASN1_GAUGE32(ASN1_INTEGER):
+    tag = ASN1_Class_UNIVERSAL.GAUGE32
+
 class ASN1_TIME_TICKS(ASN1_INTEGER):
     tag = ASN1_Class_UNIVERSAL.TIME_TICKS
    
diff --git a/scapy/asn1/ber.py b/scapy/asn1/ber.py
--- a/scapy/asn1/ber.py
+++ b/scapy/asn1/ber.py
@@ -449,6 +449,9 @@ def do_dec(cls, s, context=None, safe=False):
 class BERcodec_COUNTER32(BERcodec_INTEGER):
     tag = ASN1_Class_UNIVERSAL.COUNTER32
 
+class BERcodec_GAUGE32(BERcodec_INTEGER):
+    tag = ASN1_Class_UNIVERSAL.GAUGE32
+
 class BERcodec_TIME_TICKS(BERcodec_INTEGER):
     tag = ASN1_Class_UNIVERSAL.TIME_TICKS
 


Test patch: