Changeset 876:d89fb130c10d
- Timestamp:
- 08/17/08 01:23:07
(3 months ago)
- Author:
- Phil <phil@secdev.org>
- Message:
Fixed SNMPv1 traps (D. Loss, ticket #99)
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r862 |
r876 |
|
| 160 | 160 | CHAR_STRING = 29 |
|---|
| 161 | 161 | BMP_STRING = 30 |
|---|
| | 162 | IPADDRESS = 0x40 |
|---|
| 162 | 163 | COUNTER32 = 0x41 |
|---|
| 163 | 164 | TIME_TICKS = 0x43 |
|---|
| … | … | |
| 240 | 241 | tag = ASN1_Class_UNIVERSAL.VIDEOTEX_STRING |
|---|
| 241 | 242 | |
|---|
| | 243 | class ASN1_IPADDRESS(ASN1_STRING): |
|---|
| | 244 | tag = ASN1_Class_UNIVERSAL.IPADDRESS |
|---|
| | 245 | |
|---|
| 242 | 246 | class ASN1_UTC_TIME(ASN1_STRING): |
|---|
| 243 | 247 | tag = ASN1_Class_UNIVERSAL.UTC_TIME |
|---|
| r862 |
r876 |
|
| 5 | 5 | |
|---|
| 6 | 6 | from scapy.error import warning |
|---|
| 7 | | from asn1 import ASN1_Decoding_Error,ASN1_BadTag_Decoding_Error,ASN1_Codecs,ASN1_Class_UNIVERSAL,ASN1_Error,ASN1_DECODING_ERROR,ASN1_BADTAG |
|---|
| | 7 | from asn1 import ASN1_Decoding_Error,ASN1_Encoding_Error,ASN1_BadTag_Decoding_Error,ASN1_Codecs,ASN1_Class_UNIVERSAL,ASN1_Error,ASN1_DECODING_ERROR,ASN1_BADTAG |
|---|
| 8 | 8 | |
|---|
| 9 | 9 | ################## |
|---|
| … | … | |
| 18 | 18 | class BER_Exception(Exception): |
|---|
| 19 | 19 | pass |
|---|
| | 20 | |
|---|
| | 21 | class BER_Encoding_Error(ASN1_Encoding_Error): |
|---|
| | 22 | def __init__(self, msg, encoded=None, remaining=None): |
|---|
| | 23 | Exception.__init__(self, msg) |
|---|
| | 24 | self.remaining = remaining |
|---|
| | 25 | self.encoded = encoded |
|---|
| | 26 | def __str__(self): |
|---|
| | 27 | s = Exception.__str__(self) |
|---|
| | 28 | if isinstance(self.encoded, BERcodec_Object): |
|---|
| | 29 | s+="\n### Already encoded ###\n%s" % self.encoded.strshow() |
|---|
| | 30 | else: |
|---|
| | 31 | s+="\n### Already encoded ###\n%r" % self.encoded |
|---|
| | 32 | s+="\n### Remaining ###\n%r" % self.remaining |
|---|
| | 33 | return s |
|---|
| 20 | 34 | |
|---|
| 21 | 35 | class BER_Decoding_Error(ASN1_Decoding_Error): |
|---|
| … | … | |
| 240 | 254 | tag = ASN1_Class_UNIVERSAL.IA5_STRING |
|---|
| 241 | 255 | |
|---|
| | 256 | class BERcodec_IPADDRESS(BERcodec_STRING): |
|---|
| | 257 | tag = ASN1_Class_UNIVERSAL.IPADDRESS |
|---|
| | 258 | |
|---|
| | 259 | @classmethod |
|---|
| | 260 | def enc(cls, ipaddr_ascii): |
|---|
| | 261 | try: |
|---|
| | 262 | s = inet_aton(ipaddr_ascii) |
|---|
| | 263 | except Exception: |
|---|
| | 264 | raise BER_Encoding_Error("IPv4 address could not be encoded") |
|---|
| | 265 | return chr(cls.tag)+BER_len_enc(len(s))+s |
|---|
| | 266 | |
|---|
| | 267 | @classmethod |
|---|
| | 268 | def do_dec(cls, s, context=None, safe=False): |
|---|
| | 269 | l,s,t = cls.check_type_check_len(s) |
|---|
| | 270 | try: |
|---|
| | 271 | ipaddr_ascii = inet_ntoa(s) |
|---|
| | 272 | except Exception: |
|---|
| | 273 | raise BER_Decoding_Error("IP address could not be decoded", decoded=obj) |
|---|
| | 274 | return cls.asn1_object(ipaddr_ascii), t |
|---|
| | 275 | |
|---|
| 242 | 276 | class BERcodec_UTC_TIME(BERcodec_STRING): |
|---|
| 243 | 277 | tag = ASN1_Class_UNIVERSAL.UTC_TIME |
|---|
| r862 |
r876 |
|
| 161 | 161 | class ASN1F_BIT_STRING(ASN1F_STRING): |
|---|
| 162 | 162 | ASN1_tag = ASN1_Class_UNIVERSAL.BIT_STRING |
|---|
| | 163 | |
|---|
| | 164 | class ASN1F_IPADDRESS(ASN1F_STRING): |
|---|
| | 165 | ASN1_tag = ASN1_Class_UNIVERSAL.IPADDRESS |
|---|
| | 166 | |
|---|
| | 167 | class ASN1F_TIME_TICKS(ASN1F_INTEGER): |
|---|
| | 168 | ASN1_tag = ASN1_Class_UNIVERSAL.TIME_TICKS |
|---|
| 163 | 169 | |
|---|
| 164 | 170 | class ASN1F_UTC_TIME(ASN1F_STRING): |
|---|
| r862 |
r876 |
|
| 180 | 180 | class SNMPtrapv1(ASN1_Packet): |
|---|
| 181 | 181 | ASN1_codec = ASN1_Codecs.BER |
|---|
| 182 | | ASN1_root = ASN1F_SNMP_PDU_TRAPv1( ASN1F_INTEGER("id",0), |
|---|
| 183 | | ASN1F_OID("enterprise", "1.3"), |
|---|
| 184 | | ASN1F_STRING("agent_addr",""), |
|---|
| | 182 | ASN1_root = ASN1F_SNMP_PDU_TRAPv1( ASN1F_OID("enterprise", "1.3"), |
|---|
| | 183 | ASN1F_IPADDRESS("agent_addr","0.0.0.0"), |
|---|
| 185 | 184 | ASN1F_enum_INTEGER("generic_trap", 0, SNMP_trap_types), |
|---|
| 186 | 185 | ASN1F_INTEGER("specific_trap", 0), |
|---|
| 187 | | ASN1F_INTEGER("time_stamp", IntAutoTime()), |
|---|
| | 186 | ASN1F_TIME_TICKS("time_stamp", IntAutoTime()), |
|---|
| 188 | 187 | ASN1F_SEQUENCE_OF("varbindlist", [], SNMPvarbind) |
|---|
| 189 | 188 | ) |
|---|
| … | … | |
| 232 | 231 | bind_layers( UDP, SNMP, sport=161) |
|---|
| 233 | 232 | bind_layers( UDP, SNMP, dport=161) |
|---|
| | 233 | bind_layers( UDP, SNMP, sport=162) |
|---|
| | 234 | bind_layers( UDP, SNMP, dport=162) |
|---|
| | 235 | |
|---|
| 234 | 236 | def snmpwalk(dst, oid="1", community="public"): |
|---|
| 235 | 237 | try: |
|---|