<s> from struct import unpack <EOL> import socket <EOL> from ImpactPacket import Header <EOL> from impacket import LOG <EOL> IP_ADDRESS_LENGTH = <NUM_LIT:4> <EOL> class CDPTypes : <EOL> DeviceID_Type = <NUM_LIT:1> <EOL> Address_Type = <NUM_LIT:2> <EOL> PortID_Type = <NUM_LIT:3> <EOL> Capabilities_Type = <NUM_LIT:4> <EOL> SoftVersion_Type = <NUM_LIT:5> <EOL> Platform_Type = <NUM_LIT:6> <EOL> IPPrefix_Type = <NUM_LIT:7> <EOL> ProtocolHello_Type = <NUM_LIT:8> <EOL> MTU_Type = <NUM_LIT> <EOL> SystemName_Type = <NUM_LIT:20> <EOL> SystemObjectId_Type = <NUM_LIT> <EOL> SnmpLocation = <NUM_LIT> <EOL> class CDP ( Header ) : <EOL> Type = <NUM_LIT> <EOL> OUI = <NUM_LIT> <EOL> def __init__ ( self , aBuffer = None ) : <EOL> Header . __init__ ( self , <NUM_LIT:8> ) <EOL> if aBuffer : <EOL> self . load_header ( aBuffer ) <EOL> self . _elements = self . _getElements ( aBuffer ) <EOL> def _getElements ( self , aBuffer ) : <EOL> buff = aBuffer [ <NUM_LIT:4> : ] <EOL> l = [ ] <EOL> finish = False <EOL> while buff : <EOL> elem = CDPElementFactory . create ( buff ) <EOL> data = elem . get_data ( ) <EOL> l . append ( elem ) <EOL> buff = buff [ elem . get_length ( ) : ] <EOL> return l <EOL> def get_header_size ( self ) : <EOL> return <NUM_LIT:8> <EOL> def get_version ( self ) : <EOL> return self . get_byte ( <NUM_LIT:0> ) <EOL> def get_ttl ( self ) : <EOL> return self . get_byte ( <NUM_LIT:1> ) <EOL> def get_checksum ( self ) : <EOL> return self . get_word ( <NUM_LIT:2> ) <EOL> def get_type ( self ) : <EOL> return self . get_word ( <NUM_LIT:4> ) <EOL> def get_lenght ( self ) : <EOL> return self . get_word ( <NUM_LIT:6> ) <EOL> def getElements ( self ) : <EOL> return self . _elements <EOL> def __str__ ( self ) : <EOL> knowcode = <NUM_LIT:0> <EOL> tmp_str = '<STR_LIT>' <EOL> for element in self . _elements : <EOL> tmp_str += "<STR_LIT>" + str ( element . get_type ( ) ) + "<STR_LIT:U+0020>" + str ( element ) + "<STR_LIT:\n>" <EOL> return tmp_str <EOL> def get_byte ( buffer , offset ) : <EOL> return unpack ( "<STR_LIT>" , buffer [ offset : offset + <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> def get_word ( buffer , offset ) : <EOL> return unpack ( "<STR_LIT>" , buffer [ offset : offset + <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] <EOL> def get_long ( buffer , offset ) : <EOL> return unpack ( "<STR_LIT>" , buffer [ offset : offset + <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> def get_bytes ( buffer , offset , bytes ) : <EOL> return buffer [ offset : offset + bytes ] <EOL> def mac_to_string ( mac_bytes ) : <EOL> bytes = unpack ( '<STR_LIT>' , mac_bytes ) <EOL> s = '<STR_LIT>' <EOL> for byte in bytes : <EOL> s += '<STR_LIT>' % byte <EOL> return s [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> class CDPElement ( Header ) : <EOL> def __init__ ( self , aBuffer = None ) : <EOL> Header . __init__ ( self , <NUM_LIT:8> ) <EOL> if aBuffer : <EOL> self . _length = CDPElement . Get_length ( aBuffer ) <EOL> self . load_header ( aBuffer [ : self . _length ] ) <EOL> @ classmethod <EOL> def Get_length ( cls , aBuffer ) : <EOL> return unpack ( '<STR_LIT>' , aBuffer [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] <EOL> def get_header_size ( self ) : <EOL> self . _length <EOL> def get_length ( self ) : <EOL> return self . get_word ( <NUM_LIT:2> ) <EOL> def get_data ( self ) : <EOL> return self . get_bytes ( ) . tostring ( ) [ <NUM_LIT:4> : self . get_length ( ) ] <EOL> def get_ip_address ( self , offset = <NUM_LIT:0> , ip = None ) : <EOL> if not ip : <EOL> ip = self . get_bytes ( ) . tostring ( ) [ offset : offset + IP_ADDRESS_LENGTH ] <EOL> return socket . inet_ntoa ( ip ) <EOL> class CDPDevice ( CDPElement ) : <EOL> Type = <NUM_LIT:1> <EOL> def get_type ( self ) : <EOL> return CDPDevice . Type <EOL> def get_device_id ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . get_device_id ( ) <EOL> class Address ( CDPElement ) : <EOL> Type = <NUM_LIT:2> <EOL> def __init__ ( self , aBuffer = None ) : <EOL> CDPElement . __init__ ( self , aBuffer ) <EOL> if aBuffer : <EOL> data = self . get_bytes ( ) . tostring ( ) [ <NUM_LIT:8> : ] <EOL> self . _generateAddressDetails ( data ) <EOL> def _generateAddressDetails ( self , buff ) : <EOL> self . address_details = [ ] <EOL> while buff : <EOL> address = AddressDetails . create ( buff ) <EOL> self . address_details . append ( address ) <EOL> buff = buff [ address . get_total_length ( ) : ] <EOL> def get_type ( self ) : <EOL> return Address . Type <EOL> def get_number ( self ) : <EOL> return self . get_long ( <NUM_LIT:4> ) <EOL> def get_address_details ( self ) : <EOL> return self . address_details <EOL> def __str__ ( self ) : <EOL> tmp_str = "<STR_LIT>" <EOL> for address_detail in self . address_details : <EOL> tmp_str += "<STR_LIT:\n>" + str ( address_detail ) <EOL> return tmp_str <EOL> class AddressDetails ( ) : <EOL> PROTOCOL_IP = <NUM_LIT> <EOL> @ classmethod <EOL> def create ( cls , buff ) : <EOL> a = AddressDetails ( buff ) <EOL> return a <EOL> def __init__ ( self , aBuffer = None ) : <EOL> if aBuffer : <EOL> addr_length = unpack ( "<STR_LIT>" , aBuffer [ <NUM_LIT:3> : <NUM_LIT:5> ] ) [ <NUM_LIT:0> ] <EOL> self . total_length = addr_length + <NUM_LIT:5> <EOL> self . buffer = aBuffer [ : self . total_length ] <EOL> def get_total_length ( self ) : <EOL> return self . total_length <EOL> def get_protocol_type ( self ) : <EOL> return self . buffer [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> def get_protocol_length ( self ) : <EOL> return get_byte ( self . buffer , <NUM_LIT:1> ) <EOL> def get_protocol ( self ) : <EOL> return get_byte ( self . buffer , <NUM_LIT:2> ) <EOL> def get_address_length ( self ) : <EOL> return get_word ( self . buffer , <NUM_LIT:3> ) <EOL> def get_address ( self ) : <EOL> address = get_bytes ( self . buffer , <NUM_LIT:5> , self . get_address_length ( ) ) <EOL> if self . get_protocol ( ) == AddressDetails . PROTOCOL_IP : <EOL> return socket . inet_ntoa ( address ) <EOL> else : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> return address <EOL> def is_protocol_IP ( self ) : <EOL> return self . get_protocol ( ) == AddressDetails . PROTOCOL_IP <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . get_protocol_type ( ) , self . get_protocol ( ) , self . get_address_length ( ) , self . get_address ( ) ) <EOL> class Port ( CDPElement ) : <EOL> Type = <NUM_LIT:3> <EOL> def get_type ( self ) : <EOL> return Port . Type <EOL> def get_port ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . get_port ( ) <EOL> class Capabilities ( CDPElement ) : <EOL> Type = <NUM_LIT:4> <EOL> def __init__ ( self , aBuffer = None ) : <EOL> CDPElement . __init__ ( self , aBuffer ) <EOL> self . _capabilities_processed = False <EOL> self . _router = False <EOL> self . _transparent_bridge = False <EOL> self . _source_route_bridge = False <EOL> self . _switch = False <EOL> self . _host = False <EOL> self . _igmp_capable = False <EOL> self . _repeater = False <EOL> self . _init_capabilities ( ) <EOL> def get_type ( self ) : <EOL> return Capabilities . Type <EOL> def get_capabilities ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def _init_capabilities ( self ) : <EOL> if self . _capabilities_processed : <EOL> return <EOL> capabilities = unpack ( "<STR_LIT>" , self . get_capabilities ( ) ) [ <NUM_LIT:0> ] <EOL> self . _router = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _transparent_bridge = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _source_route_bridge = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _switch = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _host = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _igmp_capable = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> self . _repeater = ( capabilities & <NUM_LIT> ) > <NUM_LIT:0> <EOL> def is_router ( self ) : <EOL> return self . _router <EOL> def is_transparent_bridge ( self ) : <EOL> return self . _transparent_bridge <EOL> def is_source_route_bridge ( self ) : <EOL> return self . _source_route_bridge <EOL> def is_switch ( self ) : <EOL> return self . _switch <EOL> def is_host ( self ) : <EOL> return self . is_host <EOL> def is_igmp_capable ( self ) : <EOL> return self . _igmp_capable <EOL> def is_repeater ( self ) : <EOL> return self . _repeater <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . get_capabilities ( ) <EOL> class SoftVersion ( CDPElement ) : <EOL> Type = <NUM_LIT:5> <EOL> def get_type ( self ) : <EOL> return SoftVersion . Type <EOL> def get_version ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" + self . get_version ( ) <EOL> class Platform ( CDPElement ) : <EOL> Type = <NUM_LIT:6> <EOL> def get_type ( self ) : <EOL> return Platform . Type <EOL> def get_platform ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . get_platform ( ) <EOL> class IpPrefix ( CDPElement ) : <EOL> Type = <NUM_LIT:7> <EOL> def get_type ( self ) : <EOL> return IpPrefix . Type <EOL> def get_ip_prefix ( self ) : <EOL> return CDPElement . get_ip_address ( self , <NUM_LIT:4> ) <EOL> def get_bits ( self ) : <EOL> return self . get_byte ( <NUM_LIT:8> ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . get_ip_prefix ( ) , self . get_bits ( ) ) <EOL> class ProtocolHello ( CDPElement ) : <EOL> Type = <NUM_LIT:8> <EOL> def get_type ( self ) : <EOL> return ProtocolHello . Type <EOL> def get_master_ip ( self ) : <EOL> return self . get_ip_address ( <NUM_LIT:9> ) <EOL> def get_version ( self ) : <EOL> return self . get_byte ( <NUM_LIT> ) <EOL> def get_sub_version ( self ) : <EOL> return self . get_byte ( <NUM_LIT> ) <EOL> def get_status ( self ) : <EOL> return self . get_byte ( <NUM_LIT> ) <EOL> def get_cluster_command_mac ( self ) : <EOL> return self . get_bytes ( ) . tostring ( ) [ <NUM_LIT:20> : <NUM_LIT:20> + <NUM_LIT:6> ] <EOL> def get_switch_mac ( self ) : <EOL> return self . get_bytes ( ) . tostring ( ) [ <NUM_LIT> : <NUM_LIT> + <NUM_LIT:6> ] <EOL> def get_management_vlan ( self ) : <EOL> return self . get_word ( <NUM_LIT> ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . get_master_ip ( ) , self . get_version ( ) , self . get_sub_version ( ) , self . get_status ( ) , mac_to_string ( self . get_switch_mac ( ) ) , self . get_management_vlan ( ) ) <EOL> class VTPManagementDomain ( CDPElement ) : <EOL> Type = <NUM_LIT:9> <EOL> def get_type ( self ) : <EOL> return VTPManagementDomain . Type <EOL> def get_domain ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> class Duplex ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return Duplex . Type <EOL> def get_duplex ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> def is_full_duplex ( self ) : <EOL> return self . get_duplex ( ) == <NUM_LIT> <EOL> class VLAN ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return VLAN . Type <EOL> def get_vlan_number ( self ) : <EOL> return CDPElement . get_data ( self ) <EOL> class TrustBitmap ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return TrustBitmap . Type <EOL> def get_trust_bitmap ( self ) : <EOL> return self . get_data ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . get_trust_bitmap ( ) <EOL> class UntrustedPortCoS ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return UntrustedPortCoS . Type <EOL> def get_port_CoS ( self ) : <EOL> return self . get_data ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . get_port_CoS ( ) <EOL> class ManagementAddresses ( Address ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return ManagementAddresses . Type <EOL> class MTU ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return MTU . Type <EOL> class SystemName ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return SystemName . Type <EOL> class SystemObjectId ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return SystemObjectId . Type <EOL> class SnmpLocation ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return SnmpLocation . Type <EOL> class DummyCdpElement ( CDPElement ) : <EOL> Type = <NUM_LIT> <EOL> def get_type ( self ) : <EOL> return DummyCdpElement . Type <EOL> class CDPElementFactory ( ) : <EOL> elementTypeMap = { <EOL> CDPDevice . Type : CDPDevice , <EOL> Port . Type : Port , <EOL> Capabilities . Type : Capabilities , <EOL> Address . Type : Address , <EOL> SoftVersion . Type : SoftVersion , <EOL> Platform . Type : Platform , <EOL> IpPrefix . Type : IpPrefix , <EOL> ProtocolHello . Type : ProtocolHello , <EOL> VTPManagementDomain . Type : VTPManagementDomain , <EOL> VLAN . Type : VLAN , <EOL> Duplex . Type : Duplex , <EOL> TrustBitmap . Type : TrustBitmap , <EOL> UntrustedPortCoS . Type : UntrustedPortCoS , <EOL> ManagementAddresses . Type : ManagementAddresses , <EOL> MTU . Type : MTU , <EOL> SystemName . Type : SystemName , <EOL> SystemObjectId . Type : SystemObjectId , <EOL> SnmpLocation . Type : SnmpLocation <EOL> } <EOL> @ classmethod <EOL> def create ( cls , aBuffer ) : <EOL> _type = unpack ( "<STR_LIT>" , aBuffer [ <NUM_LIT:0> : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] <EOL> try : <EOL> class_type = cls . elementTypeMap [ _type ] <EOL> except KeyError : <EOL> class_type = DummyCdpElement <EOL> return class_type ( aBuffer ) </s>
