Exemplo n.º 1
0
def outgoing_packet_handler(src_port, dst_addr, dst_port, data):
	udp_frag = udp.Packet(sport=src_port, dport=dst_port, data=data)
	udp_assembled = udp.assemble(udp_frag, False)
	pseudo_header = socket.inet_aton(local_ip) + socket.inet_aton(dst_addr) + '\x00\x11' + struct.pack('!H', len(udp_assembled))
	cksum = inetutils.cksum(pseudo_header + udp_assembled)
	udp_assembled_with_cksum = udp_assembled[:6] + struct.pack('H', cksum) + udp_assembled[8:]
	out_sk.sendto(udp_assembled_with_cksum, (dst_addr, 0))
Exemplo n.º 2
0
    def _disassemble(self, raw_packet, cksum):
        # The kernel computes the checksum, even on a raw packet. 
        packet = inetutils.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 = inetutils.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()
Exemplo n.º 3
0
    def _disassemble(self, raw_packet, cksum):
        # The kernel computes the checksum, even on a raw packet. 
        packet = inetutils.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 = inetutils.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()
Exemplo n.º 4
0
    def _assemble(self, cksum):
        "Get a packet suitable for sending over an IP socket."
        
        # make sure all the data is ready
        assert self.src, "src needed before assembling."
        assert self.dst, "dst needed before assembling."
        
        self.len = self.hl * 4 + len(self.data)
        self.__parse_addrs()
        # create the packet
        header = struct.pack('!BBHHHBB',
                              ((self.v & 0x0f) << 4
                                  | (self.hl & 0x0f)),    # 4bits each
                              (self.tos & 0xff),
                              self.len,
                              self.id,
                              (self.df & 0x01) << 14 | self.off,
                              (self.ttl & 0xff),
                              (self.p & 0xff))

        # fill checksum field by two null bytes for checksum computing
        pseudo_header = header + b'\000\000' + self.__src + self.__dst
        # compute ip header checksum, checksum will be in network bytes order
        self.sum = inetutils.cksum(pseudo_header)
        # compose ip packet header (checksum already in network bytes order)
        # order conversion doesn't require here
        packet = header + struct.pack('H', self.sum) + self.__src + self.__dst
        # compose ip packet
        packet = packet + self.data
        return packet
Exemplo n.º 5
0
    def _assemble(self, cksum):
        "Get a packet suitable for sending over an IP socket."
        
        # make sure all the data is ready
        assert self.src, "src needed before assembling."
        assert self.dst, "dst needed before assembling."
        
        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.df & 0x01) << 14
                                   | self.off,     # what about flags?
                              chr(self.ttl & 0xff),
                              chr(self.p & 0xff))
        if cksum:
            self.sum = inetutils.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 = inetutils.iph2net(packet)
        return self.__packet
Exemplo n.º 6
0
    def _assemble(self, cksum):
        "Get a packet suitable for sending over an IP socket."
        
        # make sure all the data is ready
        assert self.src, "src needed before assembling."
        assert self.dst, "dst needed before assembling."
        
        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.df & 0x01) << 14
                                   | self.off,     # what about flags?
                              chr(self.ttl & 0xff),
                              chr(self.p & 0xff))
        if cksum:
            self.sum = inetutils.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 = inetutils.iph2net(packet)
        return self.__packet
Exemplo n.º 7
0
 def _disassemble(self, raw_packet, cksum=1):
     packet = inetutils.net2updh(raw_packet)
     if cksum and packet[6:8] != '\000\000':
         our_cksum = inetutils.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:]
Exemplo n.º 8
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 = inetutils.cksum(packet)
         packet = begin + struct.pack('H', self.sum) + self.data
     self.__packet = inetutils.udph2net(packet)
     return self.__packet
Exemplo n.º 9
0
def outgoing_packet_handler(src_port, dst_addr, dst_port, data):
    udp_frag = udp.Packet(sport=src_port, dport=dst_port, data=data)
    udp_assembled = udp.assemble(udp_frag, False)
    pseudo_header = socket.inet_aton(local_ip) + socket.inet_aton(
        dst_addr) + '\x00\x11' + struct.pack('!H', len(udp_assembled))
    cksum = inetutils.cksum(pseudo_header + udp_assembled)
    udp_assembled_with_cksum = udp_assembled[:6] + struct.pack(
        'H', cksum) + udp_assembled[8:]
    out_sk.sendto(udp_assembled_with_cksum, (dst_addr, 0))
Exemplo n.º 10
0
 def _disassemble(self, buffer, cksum):
     type, code = ord(buffer[0]), ord(buffer[1])
     # when this method is called, type and code should be set correctly already
     assert self.get_type() == type, "Mismatched type %d : %d" % (self.get_type(), type)
     assert self.get_code() == code, "Mismatched code %d : %d" % (self.get_code(), code)
     
     #only field let to puplate is cksum
     if cksum and 0 != inetutils.cksum(buffer):
         raise ValueError("CheckSum Error!")
     self.__cksum = buffer[2:4]
     self.__raw_packet = buffer
Exemplo n.º 11
0
def create_ip_packet(src, dst, icmp_id, sequence_number):
    """Create a new echo request packet based on the given id."""
    # Header is type (8), code (8), checksum (16), id (16), sequence (16)
    sent_time = time.time()
    icmp = struct.pack('!BBHHHd', ICMP_ECHO_REQUEST, 0, 0, icmp_id, sequence_number, sent_time)
    # inetutils.cksum return unsigned short in network bytes order
    icmp_checksum = inetutils.cksum(icmp)
    # therefore we must convert it to host endianess than convert all icmp header to bytes array in network endianess
    icmp = struct.pack('!BBHHHd', ICMP_ECHO_REQUEST, 0, socket.ntohs(icmp_checksum), icmp_id, sequence_number, sent_time)
    # Build ip packet
    # Give ipv4 destination and source address and data (icmp packet) as arguments
    packet = ip.Packet(src=src, dst=dst, ttl=64, p=socket.IPPROTO_ICMP, data=icmp)

    return packet._assemble(cksum=True)
Exemplo n.º 12
0
    def _assemble(self, cksum):
        """ method to assemble into a byte sequence that is ready for being sent
             This method will call the virtual method _get_icmp_data from its concrete
             sub-class to get its payload
        """
        
        packet = chr(self.get_type()) + chr(self.get_code()) + '\000\000' + self._get_icmp_data()
        if cksum:
            self.__cksum = struct.pack('H', inetutils.cksum(packet))
        else:
            self.__cksum = '\000\000'
            
        self.__raw_packet = chr(self.get_type()) + chr(self.get_code()) + self.get_cksum() + self._get_icmp_data()

        # Don't need to do any byte-swapping, because idseq is
        # appplication defined and others are single byte values.
        return self.__raw_packet