コード例 #1
0
 def getfield(self, pkt, s):
     chunks = sorted([(getattr(pkt, fld), cls) for fld, cls in self.offsets
                      if getattr(pkt, fld) != 0])
     lst = []
     last = 0
     for off, cls in chunks:
         off -= self.shift
         if last > off:  # data overlaps, cannot add structure to packet list
             continue
         elif last < off:  # extra data before offset
             lst.append(conf.raw_layer(s[last:off]))
         try:
             p = cls(s[off:])
         except Exception:  # will be added as extra data instead
             if conf.debug_dissector:
                 raise
         else:
             if 'Padding' in p:
                 del (p['Padding'].underlayer.payload)
             lst.append(p)
             off += len(p)
         last = off
     if s[last:]:  # extra data at end
         lst.append(conf.raw_layer(s[last:]))
     return "", lst
コード例 #2
0
 def any2i(self, pkt, x):
     if isinstance(x, BasePacket):
         return [x]
     elif type(x) in (list, tuple):
         return [
             (p if isinstance(p, BasePacket) else conf.raw_layer(str(p)))
             for p in x
         ]
     else:
         return [conf.raw_layer(str(x))]
コード例 #3
0
 def getfield(self, pkt, s):
     l = self.length_from(pkt)
     if l <= 0:
         return s, conf.raw_layer()
     try:
         i = self.m2i(pkt, s[:l])
     except Exception:
         if conf.debug_dissector:
             raise
         i = conf.raw_layer(load=s[:l])
     return s[l:], i
コード例 #4
0
ファイル: fields.py プロジェクト: Marcus1911/mptcp-scapy
 def getfield(self, pkt, s):
     c = l = None
     if self.length_from is not None:
         l = self.length_from(pkt)
     elif self.count_from is not None:
         c = self.count_from(pkt)
         
     lst = []
     ret = ""
     remain = s
     if l is not None:
         remain,ret = s[:l],s[l:]
     while remain:
         if c is not None:
             if c <= 0:
                 break
             c -= 1
         try:
             p = self.m2i(pkt,remain)
         except Exception:
             if conf.debug_dissector:
                 raise
             p = conf.raw_layer(load=remain)
             remain = ""
         else:
             if 'Padding' in p:
                 pad = p['Padding']
                 remain = pad.load
                 del(pad.underlayer.payload)
             else:
                 remain = ""
         lst.append(p)
     return remain+ret,lst
コード例 #5
0
    def recv(self, x=MTU):
        pkt, sa_ll = self.ins.recvfrom(x)
        if sa_ll[2] == socket.PACKET_OUTGOING:
            return None
        if sa_ll[3] in conf.l2types:
            cls = conf.l2types[sa_ll[3]]
            lvl = 2
        elif sa_ll[1] in conf.l3types:
            cls = conf.l3types[sa_ll[1]]
            lvl = 3
        else:
            cls = conf.default_l2
            warning(
                "Unable to guess type (interface=%s protocol=%#x family=%i). Using %s"
                % (sa_ll[0], sa_ll[1], sa_ll[3], cls.name))
            lvl = 3

        try:
            pkt = cls(pkt)
        except KeyboardInterrupt:
            raise
        except:
            if conf.debug_dissector:
                raise
            pkt = conf.raw_layer(pkt)
        if lvl == 2:
            pkt = pkt.payload

        if pkt is not None:
            from arch import get_last_packet_timestamp
            pkt.time = get_last_packet_timestamp(self.ins)
        return pkt
コード例 #6
0
    def getfield(self, pkt, s):
        c = l = None
        if self.length_from is not None:
            l = self.length_from(pkt)
        elif self.count_from is not None:
            c = self.count_from(pkt)

        lst = []
        ret = ""
        remain = s
        if l is not None:
            remain, ret = s[:l], s[l:]
        while remain:
            if c is not None:
                if c <= 0:
                    break
                c -= 1
            try:
                p = self.m2i(pkt, remain)
            except Exception:
                if conf.debug_dissector:
                    raise
                p = conf.raw_layer(load=remain)
                remain = ""
            else:
                if conf.padding_layer in p:
                    pad = p[conf.padding_layer]
                    remain = pad.load
                    del (pad.underlayer.payload)
                else:
                    remain = ""
            lst.append(p)
        return remain + ret, lst
コード例 #7
0
ファイル: sendrecv.py プロジェクト: tass-belgium/scapy
def gen_send(s, x, inter=0, loop=0, count=None, verbose=None, realtime=None, *args, **kargs):
    if type(x) is str:
        x = conf.raw_layer(load=x)
    if not isinstance(x, Gen):
        x = SetGen(x)
    if verbose is None:
        verbose = conf.verb
    n = 0
    if count is not None:
        loop = -count
    elif not loop:
        loop=-1
    dt0 = None
    try:
        while loop:
            for p in x:
                if realtime:
                    ct = time.time()
                    if dt0:
                        st = dt0+p.time-ct
                        if st > 0:
                            time.sleep(st)
                    else:
                        dt0 = ct-p.time 
                s.send(p)
                n += 1
                if verbose:
                    os.write(1,".")
                time.sleep(inter)
            if loop < 0:
                loop += 1
    except KeyboardInterrupt:
        pass
    if verbose:
        print "\nSent %i packets." % n
コード例 #8
0
    def recv(self, x=MTU):
        pkt, sa_ll = self.ins.recvfrom(x)
        if sa_ll[2] == socket.PACKET_OUTGOING:
            return None
        if sa_ll[3] in conf.l2types:
            cls = conf.l2types[sa_ll[3]]
            lvl = 2
        elif sa_ll[1] in conf.l3types:
            cls = conf.l3types[sa_ll[1]]
            lvl = 3
        else:
            cls = conf.default_l2
            warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s" % (sa_ll[0],sa_ll[1],sa_ll[3],cls.name))
            lvl = 3

        try:
            pkt = cls(pkt)
        except KeyboardInterrupt:
            raise
        except:
            if conf.debug_dissector:
                raise
            pkt = conf.raw_layer(pkt)
        if lvl == 2:
            pkt = pkt.payload
            
        if pkt is not None:
            from arch import get_last_packet_timestamp
            pkt.time = get_last_packet_timestamp(self.ins)
        return pkt
コード例 #9
0
ファイル: fields.py プロジェクト: Marcus1911/mptcp-scapy
 def getfield(self, pkt, s):
     l = self.length_from(pkt)
     try:
         i = self.m2i(pkt, s[:l])
     except Exception:
         if conf.debug_dissector:
             raise
         i = conf.raw_layer(load=s[:l])
     return s[l:],i
コード例 #10
0
 def __div__(self, other):
     if isinstance(other, Packet):
         cloneA = self.copy()
         cloneB = other.copy()
         cloneA.add_payload(cloneB)
         return cloneA
     elif type(other) is str:
         return self / conf.raw_layer(load=other)
     else:
         return other.__rdiv__(self)
コード例 #11
0
ファイル: packet.py プロジェクト: 7924102/scapy
 def __div__(self, other):
     if isinstance(other, Packet):
         cloneA = self.copy()
         cloneB = other.copy()
         cloneA.add_payload(cloneB)
         return cloneA
     elif type(other) is str:
         return self/conf.raw_layer(load=other)
     else:
         return other.__rdiv__(self)
コード例 #12
0
def __gen_send(s,
               x,
               inter=0,
               loop=0,
               count=None,
               verbose=None,
               realtime=None,
               return_packets=False,
               *args,
               **kargs):
    if type(x) is str:
        x = conf.raw_layer(load=x)
    if not isinstance(x, Gen):
        x = SetGen(x)
    if verbose is None:
        verbose = conf.verb
    n = 0
    if count is not None:
        loop = -count
    elif not loop:
        loop = -1
    if return_packets:
        sent_packets = plist.PacketList()
    try:
        while loop:
            dt0 = None
            for p in x:
                if realtime:
                    ct = time.time()
                    if dt0:
                        st = dt0 + p.time - ct
                        if st > 0:
                            time.sleep(st)
                    else:
                        dt0 = ct - p.time
                s.send(p)
                if return_packets:
                    sent_packets.append(p)
                n += 1
                if verbose:
                    os.write(1, ".")
                time.sleep(inter)
            if loop < 0:
                loop += 1
    except KeyboardInterrupt:
        pass
    s.close()
    if verbose:
        print "\nSent %i packets." % n
    if return_packets:
        return sent_packets
コード例 #13
0
ファイル: utils.py プロジェクト: H1ghl4nd3r/scapy
 def read_packet(self, size=MTU):
     rp = RawPcapNgReader.read_packet(self, size=size)
     if rp is None:
         return None
     s, (linktype, sec, usec, wirelen) = rp
     try:
         p = conf.l2types[linktype](s)
     except KeyboardInterrupt:
         raise
     except:
         if conf.debug_dissector:
             raise
         p = conf.raw_layer(s)
     p.time = sec+0.000001*usec
     return p
コード例 #14
0
 def read_packet(self, size=MTU):
     rp = RawPcapNgReader.read_packet(self, size=size)
     if rp is None:
         return None
     s, (linktype, sec, usec, wirelen) = rp
     try:
         p = conf.l2types[linktype](s)
     except KeyboardInterrupt:
         raise
     except:
         if conf.debug_dissector:
             raise
         p = conf.raw_layer(s)
     p.time = sec + 0.000001 * usec
     return p
コード例 #15
0
 def read_packet(self):
     rp = RawPcapReader.read_packet(self)
     if rp is None:
         return None
     s,(sec,usec,wirelen) = rp
     
     try:
         p = self.LLcls(s)
     except KeyboardInterrupt:
         raise
     except:
         if conf.debug_dissector:
             raise
         p = conf.raw_layer(s)
     p.time = sec+0.000001*usec
     return p
コード例 #16
0
 def add_payload(self, payload):
     if payload is None:
         return
     elif not isinstance(self.payload, NoPayload):
         self.payload.add_payload(payload)
     else:
         if isinstance(payload, Packet):
             self.__dict__["payload"] = payload
             payload.add_underlayer(self)
             for t in self.aliastypes:
                 if payload.overload_fields.has_key(t):
                     self.overloaded_fields = payload.overload_fields[t]
                     break
         elif type(payload) is str:
             self.__dict__["payload"] = conf.raw_layer(load=payload)
         else:
             raise TypeError("payload must be either 'Packet' or 'str', not [%s]" % repr(payload))
コード例 #17
0
ファイル: packet.py プロジェクト: 7924102/scapy
 def do_dissect_payload(self, s):
     if s:
         cls = self.guess_payload_class(s)
         try:
             p = cls(s, _internal=1, _underlayer=self)
         except KeyboardInterrupt:
             raise
         except:
             if conf.debug_dissector:
                 if isinstance(cls,type) and issubclass(cls,Packet):
                     log_runtime.error("%s dissector failed" % cls.name)
                 else:
                     log_runtime.error("%s.guess_payload_class() returned [%s]" % (self.__class__.__name__,repr(cls)))
                 if cls is not None:
                     raise
             p = conf.raw_layer(s, _internal=1, _underlayer=self)
         self.add_payload(p)
コード例 #18
0
 def do_dissect_payload(self, s):
     if s:
         cls = self.guess_payload_class(s)
         try:
             p = cls(s, _internal=1, _underlayer=self)
         except KeyboardInterrupt:
             raise
         except:
             if conf.debug_dissector:
                 if isinstance(cls,type) and issubclass(cls,Packet):
                     log_runtime.error("%s dissector failed" % cls.name)
                 else:
                     log_runtime.error("%s.guess_payload_class() returned [%s]" % (self.__class__.__name__,repr(cls)))
                 if cls is not None:
                     raise
             p = conf.raw_layer(s, _internal=1, _underlayer=self)
         self.add_payload(p)
コード例 #19
0
ファイル: packet.py プロジェクト: 7924102/scapy
 def add_payload(self, payload):
     if payload is None:
         return
     elif not isinstance(self.payload, NoPayload):
         self.payload.add_payload(payload)
     else:
         if isinstance(payload, Packet):
             self.__dict__["payload"] = payload
             payload.add_underlayer(self)
             for t in self.aliastypes:
                 if payload.overload_fields.has_key(t):
                     self.overloaded_fields = payload.overload_fields[t]
                     break
         elif type(payload) is str:
             self.__dict__["payload"] = conf.raw_layer(load=payload)
         else:
             raise TypeError("payload must be either 'Packet' or 'str', not [%s]" % repr(payload))
コード例 #20
0
    def getfield(self, pkt, s):
        c = l = None
        if self.length_from is not None:
            l = self.length_from(pkt)
        elif self.count_from is not None:
            c = self.count_from(pkt)

        lst = []
        ret = ""
        remain = s
        if l is not None:
            if l <= 0:
                return s, []
            remain, ret = s[:l], s[l:]
        while remain:
            if c is not None:
                if c <= 0:
                    break
                c -= 1
            try:
                p = self.m2i(pkt, remain)
            except Exception:
                if conf.debug_dissector:
                    raise
                p = conf.raw_layer(load=remain)
                remain = ""
            else:
                pay = None
                if 'Padding' in p:
                    pay = p['Padding']
                elif 'Raw' in p:
                    pay = p['Raw']
                if pay and pay.underlayer:
                    remain = str(pay)
                    pay.underlayer.remove_payload()
                else:
                    remain = ""
            lst.append(p)
        return remain + ret, lst
コード例 #21
0
 def __rdiv__(self, other):
     if type(other) is str:
         return conf.raw_layer(load=other) / self
     else:
         raise TypeError
コード例 #22
0
 def recv(self, x=MTU):
     return conf.raw_layer(self.ins.recv(x))
コード例 #23
0
 def recv(self, x=MTU):
     return conf.raw_layer(self.ins.recv(x))
コード例 #24
0
ファイル: packet.py プロジェクト: 7924102/scapy
 def __rdiv__(self, other):
     if type(other) is str:
         return conf.raw_layer(load=other)/self
     else:
         raise TypeError