1

I have found a python project that implements Traceroute, however it doesn't work. Here is the project:

import socket
import sys

class TraceRoute(object):

    BADDR = "0.0.0.0" # default bind address - (all IPs)
    PORT = 33434 # default port
    ICMP = socket.getprotobyname('icmp')
    UDP = socket.getprotobyname('udp')

    destination = ""
    ttl = 0 # increase ttl by one each time. 

    # sockets
    reciever = None
    sender = None

    # finished?
    finished = False

    def __init__(self, addr):
        self.destination = socket.gethostbyname(addr)

        self.reciever = socket.socket(socket.AF_INET, socket.SOCK_RAW, self.ICMP)
        self.sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, self.UDP)

        # bind to reciever so we can listen for replies
        self.reciever.bind(('', self.PORT))

    def next_server(self):
        """ Connects to next server 1 hop away from current server (i.e. server[ttl + 1]) """
        if self.finished:
            # we have nothing to do, just return
            return

        # first job increment the ttl on the socket
        self.ttl += 1
        self.sender.setsockopt(socket.SOL_IP, socket.IP_TTL, self.ttl)

        self.sender.sendto("", (self.destination, self.PORT))

        current_server = self.reciever.recvfrom(512)[1][0] # get 512 bytes from the reciever
        print current_server
        self.display(current_server)

        if current_server == self.destination:
            self.finished = True

    def display(self, address):
        """ Gets the hostname (if we can) and displays """
        try:
            name = socket.gethostbyaddr(address)[0]
            print "%s) %s (%s)" % (self.ttl, name, address)
        except socket.error:
            # we couldn't - we'll just tell them the IP address
            print "%s) %s" % (self.ttl, address)

    def __del__(self):
        """ close our sockets """
        try:
            self.reciever.close()
        except socket.error:
            # already closed
            pass

        try:
            self.sender.close()
        except socket.error:
            # already closed
            pass

if __name__ == "__main__":
    # lets get the address from the commandline args
    if len(sys.argv) <= 1:
        # nothing been specified
        print "You need to give an address"
        print "%s <server>" % sys.argv[0]
        sys.exit() # we can't do anything.

    tracert = TraceRoute(sys.argv[1])
    while not tracert.finished:
        tracert.next_server()

I run the project and it gets stuck on this line:

current_server = self.reciever.recvfrom(512)[1][0]

After looking at Wireshark I figured that I'm recieving the ICMP response but my python project doesn't catch and recieve it for some reason, therefore it cannot continue and pass that line.

Help?

user3016694
  • 307
  • 2
  • 4
  • 11

1 Answers1

0

I'm writing a traceroute project on Windows currently, and I have faced the exact same problem as you did, socket.recvfrom doesn't receive anything on my computer, however, it works fine on other computers.

I found out that it was actually Windows Defender blocking the reply message the whole time. After I shut down Windows Defender, I started to receive all those blocked messages. Hopefully this helps you(though it might be a bit too late).

SaKaTetsu
  • 11
  • 3