From 998c04cea9fdcb37ca2860407ff1bc8bce86fc75 Mon Sep 17 00:00:00 2001 From: Giulio Date: Fri, 17 Dec 2021 19:49:26 +0100 Subject: [PATCH] Working version! --- example.py | 4 ++-- masstraceroute/__init__.py | 38 +++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/example.py b/example.py index 1ae6192..146a748 100644 --- a/example.py +++ b/example.py @@ -1,8 +1,8 @@ import masstraceroute import logging -logging.basicConfig(level=logging.DEBUG) +logging.basicConfig(level=logging.INFO) targets = ["8.8.8.8", "9.9.9.9", "31.13.64.35"] -t = masstraceroute.MassTraceroute(10, 60, 2, targets, 30) +t = masstraceroute.MassTraceroute(5, 15, 2, targets, 12) t.main() diff --git a/masstraceroute/__init__.py b/masstraceroute/__init__.py index e62e3ed..e60c14d 100644 --- a/masstraceroute/__init__.py +++ b/masstraceroute/__init__.py @@ -7,6 +7,8 @@ from multiprocessing.pool import ThreadPool from multiprocessing import Lock from threading import Thread +from pprint import pprint + class MassTraceroute: def __init__(self, socket_timeout, listener_timeout, threads, targets, maxhops): logging.info("Setting up class") @@ -18,10 +20,12 @@ class MassTraceroute: self.threads = threads self.pool = ThreadPool(processes=self.threads) self.routes = dict() + for target in self.targets: + self.routes[target] = dict() logging.info("Creating listener socket") self.listener = self.init_listener() self.sources = dict() - self.last_send = 0 + self.last_send = time() def main(self): logging.info("Starting listener thread") @@ -40,23 +44,24 @@ class MassTraceroute: return listener def process_listener(self): - while((self.last_send + self.listener_timeout) < time()): + while((self.last_send + self.listener_timeout) > time()): logging.debug("Listening...") try: data, addr = self.listener.recvfrom(1024) except socket.error as e: + logging.debug(e) pass - logging.debug("Got data from ip = {}".format(addr)) - logging.debug(data) - logging.debug(data[48:49]) - srcport = int.from_bytes(data[48:49], byteorder="big") - dstport = int.from_bytes(data[50:51], byteorder="big") + srcport = int.from_bytes(data[48:50], byteorder="big") + dstport = int.from_bytes(data[50:52], byteorder="big") + logging.info("Got data from ip = {}, srcport = {}, dstport = {}, original target = {}, hop = {}".format(addr[0], srcport, dstport, self.sources[srcport]["ip"], self.sources[srcport][dstport])) if srcport in self.sources and dstport in self.sources[srcport]: hop = self.sources[srcport][dstport] ip = self.sources[srcport]["ip"] - self.routes[ip][hop] = addr + self.routes[ip][hop] = addr[0] + logging.info(self.routes[ip][hop]) else: logging.error("Received package with srcport or dstport non existsant?") + pprint(self.routes) def process_sender(self): logging.info("Starting process_sender") @@ -77,22 +82,25 @@ class MassTraceroute: def send(self, dstip): logging.info("Entering send()") sender = self.init_sender() - srcport = getrandbits(16) - while(srcport not in self.sources): - self.sources[srcport] = dict() - self.sources[srcport]["ip"] = dstip + while(True): srcport = getrandbits(16) + if srcport not in self.sources: + self.sources[srcport] = dict() + self.sources[srcport]["ip"] = dstip + break logging.info("Binding sending socket") sender.bind(('', srcport)) for ttl in range(1, self.maxhops): - sender.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) dstport = getrandbits(16) - while(dstport not in self.sources[srcport]): - self.sources[srcport][dstport] = ttl + while(True): dstport = getrandbits(16) + if dstport not in self.sources[srcport]: + self.sources[srcport][dstport] = ttl + logging.debug("Saved ttl = {}, original ttl = {}, target = {}".format(self.sources[srcport][dstport], ttl, dstip)) + break logging.debug("Sending packet srcport = {}, dstport = {}, ttl = {}, dstip = {}".format(srcport, dstport, ttl, dstip)) sender.sendto(b'', (dstip, dstport)) self.last_send = time()