예제 #1
0
    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)
예제 #2
0
 def get_reply(self, seq):
     start = time.time()
     timeout = self.max_wait
     while 1:
         rd, wt, er = select.select([self.icmp_sock], [], [], timeout)
         if rd:
             try:
                 pkt, who = self.icmp_sock.recvfrom(4096)
             except socket.error:
                 pass
             arrived = time.time()
             _reply = ip.Packet(pkt)
             reply = icmp.Packet(_reply.data)
             if (reply.type == icmp.ICMP_TIMXCEED \
                         and reply.code == icmp.ICMP_TIMXCEED_INTRANS) \
                     or reply.type == icmp.ICMP_UNREACH:
                 encap = ip.Packet(reply.data)
                 # what about this checksum?
                 orig = udp.Packet(encap.data, 0)
                 if orig.sport == self.id \
                         and orig.dport == self.def_port+seq:
                     if reply.type == icmp.ICMP_UNREACH:
                         self.got_there = 1
                     return _reply, arrived
         timeout = (start + self.max_wait) - time.time()
         if timeout < 0:
             return None, None
예제 #3
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))
예제 #4
0
    def init_probe_packet(self, src, dest):
        # build the packet, then set its length
        p = ip.Packet()
        p.dst = dest
        p.p = socket.IPPROTO_UDP

        u = udp.Packet()
        u.sport = self.id
        u.data = 'python traceroute' * 4
        # need to set up the lengths
        p.data = u.assemble()
        self.probe_ip = p
        self.probe_udp = u
        self.packlen = len(p.assemble())
예제 #5
0
    def sendPacket(self,bytes,target,sport,port):
	#Build raw UPD packet to avoid duplicate port/socket issues
	udpPacket = udp.Packet()
	udpPacket.sport = sport
	udpPacket.dport = port   
	udpPacket.data = bytes
	packet = udp.assemble(udpPacket, 0)
	sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
	sock.sendto(packet, (target, port))
	if self.debug > 0:
		print "UDP target IP: %s"%target
		print "UDP target port: %s"%port
		print "UDP source port: %s"%sport
		print "Sending: %s"%bytes.encode('hex')
예제 #6
0
    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)
예제 #7
0
    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
예제 #8
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]
예제 #9
0
    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
예제 #10
0
import socket
import sys
import ip
import udp

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]
예제 #11
0
파일: udptests.py 프로젝트: Charriu/PBStats
 def setUp(self):
     self.simple = udp.Packet()
     self.simple.sport = 3213
     self.simple.dport = 1232
     self.simple.data = "ddd"