def probe_packet(self): """ Build the outgoing probe packet """ # build the packet, then set its length probe_ip = ip.Packet() probe_ip.src = self.src_addr probe_ip.dst = self.dst_addr probe_ip.p = socket.IPPROTO_UDP probe_ip.ttl = self.ttl probe_ip.df = self.dont_frag # build UPD packet as the payload of IP packet probe_udp = udp.Packet() # Because more than 1 traceroute can be running at the same time, we need to # distinguish the packets responding to this traceroute instance. We do this # by setting source port in UDP header to our process id. As ICMP will include # UDP header that triggers it in the payload, we will be able to compare the # UDP source port embedded in returned ICMP packet with our process id and # determine if the packet is of our interest. probe_udp.sport = self.src_port probe_udp.dport = self.dst_port # calculate the length of UDP data header_len = len(udp.assemble(probe_udp) + ip.assemble(probe_ip)) if self.packet_len <= header_len: raise TraceError, "packet length must be > %d" % (header_len) probe_udp.data = '\000' * (self.packet_len - header_len) probe_ip.data = udp.assemble(probe_udp) return ip.assemble(probe_ip, self.check_sum)
def send(self, data, family, src, dst): packet_udp = udp.Packet() packet_udp.sport = src[1] packet_udp.dport = dst[1] packet_udp.data = data packet_ip = ip.Packet() packet_ip.src = src[0] packet_ip.dst = dst[0] packet_ip.ttl = 1 packet_ip.p = 17 packet_ip.data = udp.assemble(packet_udp, 0) packet = bytearray(ip.assemble(packet_ip)) #Restore byte order for OSX repack(packet, "H", 2) repack(packet, "H", 6) print ':'.join('{:02x}'.format(c) for c in packet) if family == socket.AF_INET: self.socket_v4.sendto(packet, dst) #elif family == socket.AF_INET6: # self.socket_v6.sendto(packet, dst) else: raise "Socket family not implemented"
def send(self, service_descriptor, response_data, REQUEST): import ip, udp response_packet = udp.Packet(dport=REQUEST[1], sport=self.port, data=response_data) ip_packet = ip.Packet(dst=REQUEST[0], src=service_descriptor.params['ip'], data=udp.assemble(response_packet, 0), p=socket.IPPROTO_UDP, ttl=15) self.logger.debug(response_packet) data = udp.assemble(response_packet, 0) self.logger.debug(response_data) self.logger.debug(ip_packet) self.raw_sock.sendto(ip.assemble(ip_packet, 0), REQUEST.REMOTE)
def disconnect(self, payload): # TODO Throttle disconnects! # Send fake packet to stop upload # Structure of content: # 254 254 06 B (A+1) (7 bytes) # # First 2 bytes marks it as udp paket(?!) # Thrid bytes is command (close connection to client) # B and A+1 are to 16 bit numbers where A and B # are content of 'payload' aHi, aLow = payload[1], payload[2] bHi, bLow = payload[3], payload[4] a_plus_1 = (aHi * 256 + aLow + 1) % 65536 data = bytes( [254, 254, 6, bHi, bLow, int(a_plus_1 / 256), (a_plus_1 % 256)]) logging.info('Disconnecting client at {!r}'.format(self)) upacket = udp2.Packet() upacket.sport = self.client_port upacket.dport = self.server_port upacket.data = data ipacket = ip2.Packet() ipacket.src = self.client_ip ipacket.dst = self.server_ip ipacket.df = 1 ipacket.ttl = 64 ipacket.p = 17 ipacket.data = udp2.assemble(upacket, False) raw_ip = ip2.assemble(ipacket, 1) # Send fake packet to the PB server that looks like its coming from the client try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) except socket.error as e: logging.error('Socket could not be created: {}'.format(e)) sock.sendto(raw_ip, (ipacket.dst, 0)) self.time_disconnected = time.time() self.number_unanswered_outgoing_packets = 0
def sendUdpReply(src,dst,data): upacket = udp2.Packet() upacket.sport = src[1] upacket.dport = dst[1] upacket.data = data ipacket = ip2.Packet() ipacket.src = src[0] ipacket.dst = dst[0] ipacket.df = 1 ipacket.ttl = 64 ipacket.p = 17 ipacket.data = udp2.assemble(upacket, False) raw_ip = ip2.assemble(ipacket, 1) try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) except socket.error , msg: print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
def disconnect(self, payload): # TODO Throttle disconnects! # Send fake packet to stop upload # Structure of content: # 254 254 06 B (A+1) (7 bytes) A = payload[3:5] # String! B = payload[5:7] a1 = ord(A[0]) * 256 + ord(A[1]) + 1 A1 = chr(a1 / 256) + chr(a1 % 256) data = chr(254) + chr(254) + chr(06) + B + A1 logging.info('Disconnecting client at {!r}'.format(self)) upacket = udp2.Packet() upacket.sport = self.client_port upacket.dport = self.server_port upacket.data = data ipacket = ip2.Packet() ipacket.src = self.client_ip ipacket.dst = self.server_ip ipacket.df = 1 ipacket.ttl = 64 ipacket.p = 17 ipacket.data = udp2.assemble(upacket, False) raw_ip = ip2.assemble(ipacket, 1) # Send fake packet to the PB server that looks like its coming from the client try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) except socket.error as err: logging.error('Socket could not be created: {}'.format(err)) sock.sendto(raw_ip, (ipacket.dst, 0)) self.time_disconnected = time.time() self.number_unanswered_outgoing_packets = 0
ClientHost = '192.168.0.22' ServerHost = '192.168.0.22' ClientPort = 2057 ServerPort = 2056 upacket = udp.Packet() upacket.sport = ClientPort upacket.dport = ServerPort upacket.data = "\xfe\xfe\x00\x06" #upacket.data = "Hello Server" ipacket = ip.Packet() ipacket.src = ClientHost ipacket.dst = ServerHost ipacket.df = 1 ipacket.ttl = 64 ipacket.p = 17 ipacket.data = udp.assemble(upacket, False) raw_ip = ip.assemble(ipacket, 1) try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) except socket.error , msg: print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() sock.sendto(raw_ip, (ipacket.dst, 0))
def _get_icmp_data(self): assert self.__embedded_ip, "Embedded IP should not be None" return self.__unused + ip.assemble(self.get_embedded_ip())
ClientHost = '192.168.0.22' ServerHost = '192.168.0.22' ClientPort = 2057 ServerPort = 2056 upacket = udp.Packet() upacket.sport = ClientPort upacket.dport = ServerPort upacket.data = "\xfe\xfe\x00\x06" #upacket.data = "Hello Server" ipacket = ip.Packet() ipacket.src = ClientHost ipacket.dst = ServerHost ipacket.df = 1 ipacket.ttl = 64 ipacket.p = 17 ipacket.data = udp.assemble(upacket, False) raw_ip = ip.assemble(ipacket, 1) try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) except socket.error, msg: print 'Socket could not be created. Error Code : ' + str( msg[0]) + ' Message ' + msg[1] sys.exit() sock.sendto(raw_ip, (ipacket.dst, 0))
def testSimplePacket(self): buf = ip.assemble(self.simple, 1) new = ip.disassemble(buf, 1) self.assertEqual(self.simple, new)