Exemplo n.º 1
0
class CoLoR_Get(Packet):
    name = "CoLoR_Get"
    fields_desc = [
        BitField("Version", 7, 4, tot_size=1),
        BitField("Package", 2, 4, end_tot_size=1),
        ByteField("TTL", 64),
        LEShortField("pkg_length", None),
        XShortField("checksum", None),
        ShortField("MTU", None),
        FieldLenField("PID_num", None, fmt="B", count_of="PIDs"),
        FlagsField("Flags", 8, 8, "rrrASQKF"),
        ShortField("Minimal_PID_CP", None),
        StrFixedLenField("N_sid", "", 16),
        StrFixedLenField("L_sid", "", 20),
        StrFixedLenField("nid", "", 16),
        ConditionalField(
            FieldLenField("Public_key_len",
                          None,
                          fmt="H",
                          length_of="Public_key"),
            lambda pkt: pkt.Flags.K == True),
        ConditionalField(
            StrLenField("Public_key",
                        "",
                        length_from=lambda pkt: pkt.Public_key_len),
            lambda pkt: pkt.Flags.K == True),
        ConditionalField(
            FieldLenField("QoS_len",
                          None,
                          fmt="B",
                          length_of="QoS_requirements"),
            lambda pkt: pkt.Flags.Q == True),
        ConditionalField(
            StrLenField("QoS_requirements",
                        "",
                        length_from=lambda pkt: pkt.QoS_len),
            lambda pkt: pkt.Flags.Q == True),
        ConditionalField(IntField("Seg_ID", None),
                         lambda pkt: pkt.Flags.S == True),
        FieldListField("PIDs",
                       None,
                       StrFixedLenField("", "", 4),
                       count_from=lambda pkt: pkt.PID_num)
    ]

    def post_build(self, pkt, pay):
        if self.pkg_length is None:
            self.pkg_length = len(pkt)
            pkt = pkt[:2] + int2BytesLE(self.pkg_length, 2) + pkt[4:]
        if self.checksum is None:
            self.checksum = CalcChecksum(pkt)
            pkt = pkt[:4] + int2Bytes(self.checksum, 2) + pkt[6:]
        return pkt + pay
Exemplo n.º 2
0
class VersionInfo(Tag):
    name = 'Tag - VersionInfo'

    fields_desc = [
        LenField('len', None),
        XByteField('tag', 0x0a),
        ByteField('type', None),
        XShortField('unknown', None),
        ByteField('id', None),
        FieldLenField('name_len', None, fmt='B', length_of='name'),
        StrLenField('name', None, length_from=lambda pkt: pkt.name_len),
        FieldLenField('ver_len', None, fmt='B', length_of='version'),
        StrLenField('version', None, length_from=lambda pkt: pkt.ver_len)
    ]
Exemplo n.º 3
0
class VersionInfo(Tag):
    name = 'Tag - VersionInfo'

    fields_desc = [
        LenField("len", None),
        XByteField("tag", 0x0a),

        ByteField("type", None),
        XShortField("unknown", None),
        ByteField("id", None),
        FieldLenField("name_len", None, fmt='B', length_of='name'),
        StrLenField("name", None, length_from=lambda pkt:pkt.name_len),
        FieldLenField("ver_len", None, fmt='B', length_of='version'),
        StrLenField("version", None, length_from=lambda pkt:pkt.ver_len)
    ]
Exemplo n.º 4
0
class DTLSServerHello(PacketNoPayload):
    """Representation of DTLSServerHello message"""

    name = "DTLS Server Hello"
    fields_desc = [
        XShortEnumField("version", ENUM_DTLS_VERSIONS.DTLS_1_1, DTLS_VERSIONS),
        IntField("gmt_unix_time", int(time.time())),
        StrFixedLenField("random_bytes", os.urandom(28), 28),
        XFieldLenField("session_id_length", None, length_of="session_id", fmt="B"),
        StrLenField(
            "session_id", os.urandom(20), length_from=lambda x: x.session_id_length
        ),
        XShortEnumField(
            "cipher_suite", TLSCipherSuite.RSA_WITH_AES_128_CBC_SHA, TLS_CIPHER_SUITES
        ),
        ByteEnumField(
            "compression_method", TLSCompressionMethod.NULL, TLS_COMPRESSION_METHODS
        ),
        StrConditionalField(
            XFieldLenField("extensions_length", None, length_of="extensions", fmt="H"),
            lambda pkt, s, val: True
            if val
            or pkt.extensions
            or (s and struct.unpack("!H", s[:2])[0] == len(s) - 2)
            else False,
        ),
        TypedPacketListField(
            "extensions",
            None,
            TLSExtension,
            length_from=lambda x: x.extensions_length,
            type_="DTLSServerHello",
        ),
    ]
Exemplo n.º 5
0
class Data(Packet):
    name = "Data"
    fields_desc = [
        StrLenField("data", "", length_from=lambda p: p.underlayer.length - 4)
    ]

    def extract_padding(self, s):
        return "", s
Exemplo n.º 6
0
class RadioEvent(Tag):
    name = 'Tag - RadioEvent'

    fields_desc = [
        LenField('len', None),
        XByteField('tag', 0x00),
        StrLenField('message', None, length_from=lambda pkt: pkt.len - 1)
    ]
Exemplo n.º 7
0
class CoLoR_Data(Packet):
    name = 'CoLoR_Data'
    fields_desc = [
        BitField("Version", 7, 4, tot_size=1),
        BitField("Package", 3, 4, end_tot_size=1),
        ByteField("TTL", 64),
        LEShortField("pkg_length", None),
        XShortField("checksum", None),
        ByteField("header_length", None),
        ByteField("PID_pt", None),
        FieldLenField("PID_num",
                      None,
                      fmt="B",
                      count_of="PIDs",
                      adjust=lambda pkt, x: x - (pkt.Flags.R == True)),
        FlagsField("Flags", 0, 8, "rSCQMRBF"),
        ConditionalField(ShortField("Minimal_PID_CP", None),
                         lambda pkt: pkt.Flags.M),
        StrFixedLenField("N_sid", "", 16),
        StrFixedLenField("L_sid", "", 20),
        StrFixedLenField("nid_cus", "", 16),
        ConditionalField(
            StrFixedLenField("nid_pro", "", 16),
            lambda pkt: pkt.Flags.B == False and pkt.Flags.R == True),
        ConditionalField(
            FieldLenField("QoS_len",
                          None,
                          fmt="B",
                          length_of="QoS_requirements"),
            lambda pkt: pkt.Flags.Q == True),
        ConditionalField(
            StrLenField("QoS_requirements",
                        "",
                        length_from=lambda pkt: pkt.QoS_len),
            lambda pkt: pkt.Flags.Q == True),
        ConditionalField(IntField("HMAC", None),
                         lambda pkt: pkt.Flags.C == True),
        ConditionalField(IntField("Seg_ID", None),
                         lambda pkt: pkt.Flags.S == True),
        FieldListField("PIDs", [""],
                       StrFixedLenField("", "", 4),
                       count_from=lambda pkt: pkt.PID_num +
                       (pkt.Flags.R == True))
    ]

    def post_build(self, pkt, pay):
        if self.header_length is None:
            self.header_length = len(pkt)
            pkt = pkt[:6] + int2Bytes(self.header_length, 1) + pkt[7:]
        if self.pkg_length is None:
            self.pkg_length = len(pkt) + len(pay)
            pkt = pkt[:2] + int2BytesLE(self.pkg_length, 2) + pkt[4:]
        if self.checksum is None:
            self.checksum = CalcChecksum(pkt)
            pkt = pkt[:4] + int2Bytes(self.checksum, 2) + pkt[6:]
        # print(self.pkg_length, self.header_length, self.checksum)
        return pkt + pay
Exemplo n.º 8
0
class UsageReport(Tag):
    name = 'Tag - UsageReport'

    fields_desc = [
        LenField('len', None),
        XByteField('tag', 0x01),
        XByteField('unknown', None),
        StrLenField('data', None, length_from=lambda pkt: pkt.len - 2)
    ]
Exemplo n.º 9
0
class ErrorMessage(Tag):
    name = 'Tag - ErrorMessage'

    fields_desc = [
        LenField('len', None),
        XByteField('tag', 0x0b),
        IEEEFloatField('timestamp', 0.0),
        ShortField('seqnum', 0),
        ShortField('unknown', 1),
        SignedIntField('code', 0),
        FlagsField('flag', 0, 8,
                   ['error', 'isLVCode', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7']),
        FieldLenField('details_len', None, fmt='H', length_of='details'),
        StrLenField('details', '', length_from=lambda pkt: pkt.details_len),
        FieldLenField('location_len', None, fmt='H', length_of='location'),
        StrLenField('location', '', length_from=lambda pkt: pkt.location_len),
        FieldLenField('callstack_len', None, fmt='H', length_of='callstack'),
        StrLenField('callstack', '', length_from=lambda pkt: pkt.callstack_len)
    ]
Exemplo n.º 10
0
class Strategy_unit(Packet):
    name = "CoLoR_Ann_Strategy_unit"
    fields_desc = [
        ByteField("tag", 0),
        FieldLenField("length", None, fmt="B", length_of="value"),
        StrLenField("value", "", length_from=lambda pkt: pkt.length)
    ]

    def extract_padding(self, s):
        return "", s
Exemplo n.º 11
0
class StdOut(Tag):
    name = 'Tag - StdOut'

    fields_desc = [
        LenField('len', None),
        XByteField('tag', 0x0b),
        IEEEFloatField('timestamp', 0.0),
        ShortField('seqnum', 0),
        StrLenField('message', '', length_from=lambda pkt: pkt.len - 7),
    ]
Exemplo n.º 12
0
class CoLoR_Ann(Packet):
    name = "CoLoR_Ann"
    fields_desc = [
        BitField("Version", 7, 4, tot_size=1),
        BitField("Package", 1, 4, end_tot_size=1),
        ByteField("TTL", 64),
        LEShortField("pkg_length", None),
        XShortField("checksum", None),
        FlagsField("Flags", 0, 8, "rrrrrPKF"),
        BitField("unit_num", None, 4, tot_size=1),
        BitField("PX_num", None, 4, end_tot_size=1),
        PacketListField("Announce_unit_list",
                        None,
                        Ann_unit,
                        count_from=lambda pkt: pkt.unit_num),
        ConditionalField(
            FieldLenField("Public_key_len",
                          None,
                          fmt="H",
                          length_of="Public_key"),
            lambda pkt: pkt.Flags.K == True),
        ConditionalField(
            StrLenField("Public_key",
                        "",
                        length_from=lambda pkt: pkt.Public_key_len),
            lambda pkt: pkt.Flags.K == True),
        ConditionalField(
            FieldLenField("AS_PATH_len", None, fmt="B", count_of="AID_list"),
            lambda pkt: pkt.Flags.P == True),
        ConditionalField(
            FieldListField("AID_list",
                           None,
                           StrFixedLenField("", "", 1),
                           count_from=lambda pkt: pkt.AS_PATH_len),
            lambda pkt: pkt.Flags.P == True),
        FieldListField("PX_list",
                       None,
                       StrFixedLenField("", "", 2),
                       count_from=lambda pkt: pkt.PX_num)
    ]

    def post_build(self, pkt, pay):
        if self.pkg_length is None:
            self.pkg_length = len(pkt)
            pkt = pkt[:2] + int2BytesLE(self.pkg_length, 2) + pkt[4:]
        if self.checksum is None:
            self.checksum = CalcChecksum(pkt)
            pkt = pkt[:4] + int2Bytes(self.checksum, 2) + pkt[6:]
        if self.unit_num is None and self.PX_num is None:
            self.unit_num = len(self.Announce_unit_list)
            self.PX_num = len(self.PX_list)
            pkt = pkt[:7] + int2Bytes(self.unit_num << 4 | self.PX_num,
                                      1) + pkt[8:]
        return pkt + pay
Exemplo n.º 13
0
class Tag(Packet):
    fields_desc = [
        LenField('len', None),
        XByteField('tag', None),
        StrLenField('data', '', length_from=lambda pkt: pkt.len - 1)
    ]

    @classmethod
    def dispatch_hook(cls, s=None, *_args, **_kwargs):
        if s is None:
            return conf.raw_layer
        tag_id = orb(s[2])

        if tag_id == 0x00:
            return RadioEvent
        elif tag_id == 0x01:
            return UsageReport
        elif tag_id == 0x04:
            return DisableFaults
        elif tag_id == 0x05:
            return RailFaults
        elif tag_id == 0x0a:
            return VersionInfo
        elif tag_id == 0x0b:
            return ErrorMessage
        elif tag_id == 0x0c:
            return StdOut
        else:
            return Tag

    def guess_payload_class(self, payload):
        return conf.padding_layer

    def post_build(self, p, pay):
        if self.len is None:
            p = struct.pack('>H', len(p) - 2) + p[2:]
        return p + pay
Exemplo n.º 14
0
class L2Payload(Packet):
    """L2 dummy payload."""
    name = 'l2payload'
    fields_desc = [
        StrLenField('_l2payload', '\x01' * DEFAULT_LEN, lambda pkt: pkt.len)
    ]
Exemplo n.º 15
0
class OpenFlowBody_v1_0(Packet):
    """
    OpenFlow Packet v1.0
    """

    name = 'OpenFlowBody_v1_0'

    fields_desc = [
        # Header
        ByteEnumField('Type', None, ofp_v1_0_message_type),
        ShortField('Length', None),
        IntField('ID', None),

        # Error message body
        ConditionalField(ShortField('ErrorType', None),
                         lambda pkt: pkt.Type == 1),
        ConditionalField(ShortField('ErrorCode', None),
                         lambda pkt: pkt.Type == 1),

        # Echo Request / Echo Reply message body
        ConditionalField(StrField('Data', None),
                         lambda pkt: pkt.Type in [2, 3]),

        # Vendor message body
        ConditionalField(IntField('VendorID', None),
                         lambda pkt: pkt.Type == 4),

        # Features Reply message body
        ConditionalField(XLongField('DatapathID', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(IntField('AvailableNumberOfPacketsCanBeHeld', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(ByteField('NumberOfFlowTabs', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(X3BytesField('FeaturesReplyReserved', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(XIntField('<SwitchCapabilityFlags>', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(XIntField('ActionCapabilityFlags', None),
                         lambda pkt: pkt.Type == 6),
        ConditionalField(
            _PacketField('FeaturesReply<PortDescriptors>', None,
                         PortDescriptorField_v1_0, 48),
            lambda pkt: pkt.Type == 6),

        # Get Config Reply / Set Config message body
        ConditionalField(XShortField('SwitchConfigurationFlags', None),
                         lambda pkt: pkt.Type in [8, 9]),
        ConditionalField(ShortField('MissSendLength', None),
                         lambda pkt: pkt.Type in [8, 9]),

        # Packet Input Notification message body
        ConditionalField(
            IntField('PacketInputNotificationPacketBufferID', None),
            lambda pkt: pkt.Type == 10),
        ConditionalField(ShortField('EthernetFrameLength', None),
                         lambda pkt: pkt.Type == 10),
        ConditionalField(
            ShortField('PacketInputNotificationIngresss<PortNumber>', None),
            lambda pkt: pkt.Type == 10),
        ConditionalField(ByteField('PacketInputNotificationReason', None),
                         lambda pkt: pkt.Type == 10),
        ConditionalField(XByteField('PacketInputNotificationReserved', None),
                         lambda pkt: pkt.Type == 10),
        ConditionalField(
            StrLenField('EthenretFrame',
                        None,
                        length_from=lambda pkt: pkt.EthernetFrameLength),
            lambda pkt: pkt.Type == 10),

        # Flow Removed Notification message body
        ConditionalField(
            _PacketField('FlowRemovedNotification<FlowMatchDescriptor>', None,
                         FlowMatchDescriptorField_v1_0, 40),
            lambda pkt: pkt.Type == 11),
        ConditionalField(ShortField('FlowRemovedNotificationPriority', None),
                         lambda pkt: pkt.Type == 11),
        ConditionalField(ByteField('FlowRemovedNotificationReason', None),
                         lambda pkt: pkt.Type == 11),
        ConditionalField(XByteField('FlowRemovedNotificationReserved', None),
                         lambda pkt: pkt.Type == 11),
        ConditionalField(IntField('LifetimeDuration', None),
                         lambda pkt: pkt.Type == 11),
        ConditionalField(
            ShortField('FlowRemovedNotificationSoftLifetime', None),
            lambda pkt: pkt.Type == 11),
        ConditionalField(
            StrFixedLenField('FlowRemovedNotification_Reserved', None, 48),
            lambda pkt: pkt.Type == 11),
        ConditionalField(LongField('NumberOfPacketsTransferred', None),
                         lambda pkt: pkt.Type == 11),
        ConditionalField(LongField('NumberOfOctetsTransferred', None),
                         lambda pkt: pkt.Type == 11),

        # Port Status Notification message body
        ConditionalField(ByteField('PortStatusNotificationReason', None),
                         lambda pkt: pkt.Type == 12),
        ConditionalField(
            StrFixedLenField('PortStatusNotificationReserved', None, 64),
            lambda pkt: pkt.Type == 12),
        ConditionalField(
            _PacketField('PortStatusNotification<PortDescriptors>', None,
                         PortDescriptorField_v1_0, 48),
            lambda pkt: pkt.Type == 12),

        # Packet Output message body
        ConditionalField(IntField('PacketOutputPacketBufferID', None),
                         lambda pkt: pkt.Type == 13),
        ConditionalField(ShortField('PacketOutputIngress<PortNumber>', None),
                         lambda pkt: pkt.Type == 13),
        ConditionalField(ShortField('LengthOfActionDescriptors', None),
                         lambda pkt: pkt.Type == 13),
        ConditionalField(
            _PacketField('PacketOutput<ActionDescriptors>', None,
                         ActionDescriptorField_v1_0),  ## ???? bytes  ##
            lambda pkt: pkt.Type == 13),
        ConditionalField(StrField('<PacketData>', None),
                         lambda pkt: pkt.Type == 13),

        # Flow Modification message body
        ConditionalField(
            _PacketField('FlowModification<FlowMatchDescriptor>', None,
                         FlowMatchDescriptorField_v1_0, 40),
            lambda pkt: pkt.Type == 14),
        ConditionalField(ShortField('Command', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(ShortField('FlowModificationSoftLifetime', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(ShortField('HardLifetime', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(ShortField('FlowModificationPriority', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(IntField('FlowModificationPacketBufferID', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(
            ShortField('FlowModificationEgress<PortNumber>', None),
            lambda pkt: pkt.Type == 14),
        ConditionalField(XShortField('FlowModificationCU', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(XIntField('FlowModificationReserved', None),
                         lambda pkt: pkt.Type == 14),
        ConditionalField(
            _PacketField('FlowModification<ActionDescriptors>', None,
                         ActionDescriptorField_v1_0),  ## ???? bytes
            lambda pkt: pkt.Type == 14),

        # Port Modification message body
        ConditionalField(ShortField('PortModification<PortNumber>', None),
                         lambda pkt: pkt.Type == 15),
        ConditionalField(MACField('Ethernet Address', None),
                         lambda pkt: pkt.Type == 15),
        ConditionalField(XIntField('<PortConfigurationFlags>', None),
                         lambda pkt: pkt.Type == 15),
        ConditionalField(XIntField('<PortConfigurationFlags>Mask', None),
                         lambda pkt: pkt.Type == 15),
        ConditionalField(XIntField('<PortFeatureFlags>', None),
                         lambda pkt: pkt.Type == 15),

        # Stats Request message body
        ConditionalField(
            _PacketField('StatsRequest', None, StatsRequestBody_v1_0, 48),
            lambda pkt: pkt.Type == 16),

        # Stats Reply message body
        ConditionalField(
            _PacketField('StatsReply', None,
                         StatsReplyBody_v1_0),  ## ???? bytes
            lambda pkt: pkt.Type == 17),
        StrField('Payload', None),
    ]
Exemplo n.º 16
0
class UnknownTag(Tag):
    fields_desc = [
        LenField('len', None, fmt='B'),
        XByteField('tag', None),
        StrLenField('data', None, length_from=lambda pkt: pkt.len - 1)
    ]
Exemplo n.º 17
0
class Timezone(Tag):  # 0x10
    fields_desc = [
        LenField('len', None, fmt='B'),
        XByteField('tag', None),
        StrLenField('tz', None, length_from=lambda pkt: pkt.len - 1)
    ]