Пример #1
0
    def __disassemble(self, raw_packet, cksum=0):
        # The kernel computes the checksum, even on a raw packet. 
        packet = inet.net2iph(raw_packet)
        b1 = ord(packet[0])
        self.v = (b1 >> 4) & 0x0f
        self.hl = b1 & 0x0f
        if self.v != IPVERSION:
            raise ValueError("cannot handle IPv%d packets" % self.v)
        hl = self.hl * 4

        # verify the checksum
        self.sum = struct.unpack('h', packet[10:12])[0] & 0xffff
        if cksum:
            our_cksum = inet.cksum(packet[:20])
            if our_cksum != 0:
                raise ValueError(packet)

        # unpack the fields
        elts = struct.unpack('cchhhcc', packet[:hl-10])
        # struct didn't do !<> when this was written
        self.tos = ord(elts[1]) 
        self.len = elts[2] & 0xffff
        self.id = elts[3] & 0xffff
        self.off = elts[4] & 0xffff
        self.ttl = ord(elts[5])
        self.p = ord(elts[6])
        self.data = packet[hl:]
        self.src = packet[hl-8:hl-4]
        self.dst = packet[hl-4:hl]
        self.__unparse_addrs()
Пример #2
0
    def assemble(self, cksum=0):
        "Get a packet suitable for sending over an IP socket."
        # make sure all the data is ready
        self.len = self.hl * 4 + len(self.data)
        self.__parse_addrs()
        # create the packet
        header =  struct.pack('cchhhcc',
                              chr((self.v & 0x0f) << 4 
                                  | (self.hl & 0x0f)),    # 4bits each
                              chr(self.tos & 0xff),
                              self.len,
                              self.id,
                              self.off,     # what about flags?
                              chr(self.ttl & 0xff),
                              chr(self.p & 0xff))
        if cksum:
            self.sum = inet.cksum(header + '\000\000' + self.__src +
                                  self.__dst)
            packet = header + struct.pack('h', self.sum) \
                     + self.__src + self.__dst
        else:
            packet = header + '\000\000' + self.__src + self.__dst 
        packet = packet + self.data

        self.__packet = inet.iph2net(packet)
        return self.__packet
Пример #3
0
 def __disassemble(self, raw_packet, cksum=1):
     packet = inet.net2updh(raw_packet)
     if cksum and packet[6:8] != '\000\000':
         our_cksum = inet.cksum(packet)
         if our_cksum != 0:
             raise ValueError(packet)
     elts = map(lambda x:x & 0xffff, struct.unpack('hhhh', packet[:8]))
     [self.sport, self.dport, self.ulen, self.sum] = elts
     self.data = packet[8:]
Пример #4
0
 def assemble(self, cksum=1):
     self.ulen = 8 + len(self.data)
     begin = struct.pack('hhh', self.sport, self.dport, self.ulen)
     packet = begin + '\000\000' + self.data
     if cksum:
         self.sum = inet.cksum(packet)
         packet = begin + struct.pack('h', self.sum) + self.data
     self.__packet = inet.udph2net(packet)
     return self.__packet