Пример #1
0
 def _get_raw_data(self):
     iphdr = IpHeader()
     iphdr.protocol = self._protocol
     if self.args.srcip:
         iphdr.saddr = self.args.srcip
     iphdr.daddr = self.args.destip
     data = self._get_data()
     iphdr.ihl = iphdr.length >> 2
     return iphdr.dump() + data
Пример #2
0
    def _get_raw_data(self):
        data = self._get_data()

        iphdr = IpHeader()
        iphdr.protocol = socket.IPPROTO_UDP
        if self.args.srcip:
            iphdr.saddr = self.args.srcip
        iphdr.daddr = self.args.destip
        iphdr.ihl = iphdr.length >> 2

        udphdr = UdpHeader()
        udphdr.uh_sport = self.args.srcport if self.args.srcport else random.randint(
            30000, 40000)
        udphdr.uh_dport = self.args.destport
        udphdr.uh_ulen = len(data) + udphdr.length
        udphdr.uh_sum = 0

        return iphdr.dump() + udphdr.dump() + data
Пример #3
0
    def _send(self):
        if self._icmp_echo.sequence > 0:
            time.sleep(1)
        self._icmp_echo.sequence += 1
        addr = (self.args.destip, 0)
        self._icmp_echo.timestamp = int(time.time() * 1000) & 0xffffffff
        self._icmp_echo.header.checksum = 0
        self._icmp_echo.header.checksum = socket.ntohs(
            checksum(self._icmp_echo.dump()))
        self._sock.sendto(self._icmp_echo.dump(), addr)

        try:
            timeout = 2
            start_timestamp = time.time()
            while timeout > 0:
                self._sock.settimeout(timeout)
                data, raddr = self._sock.recvfrom(1024)
                recv_timestamp = int(time.time() * 1000) & 0xffffffff
                if raddr == addr:
                    iphdr = IpHeader()
                    iphdr.load(data)
                    icmp_data = data[iphdr.ihl * 4:]
                    icmp_echo_reply = IcmpEcho()
                    icmp_echo_reply.load(icmp_data)
                    if icmp_echo_reply.header.type == ICMP_ECHOREPLY \
                            and icmp_echo_reply.id == self._icmp_echo.id \
                            and icmp_echo_reply.sequence == self._icmp_echo.sequence:
                        print(
                            '{0} bytes from {1}: icmp_req={2} ttl={3} time={4} ms'
                            .format(
                                len(icmp_data), addr[0],
                                self._icmp_echo.sequence, iphdr.ttl,
                                abs(recv_timestamp -
                                    self._icmp_echo.timestamp)))
                        break
                timeout -= time.time() - start_timestamp
                if timeout < 0:
                    raise socket.timeout()
        except socket.timeout:
            print('timeout')