Working version!

This commit is contained in:
Giulio 2021-12-17 19:49:26 +01:00
parent 405b0e863b
commit 998c04cea9
2 changed files with 25 additions and 17 deletions

View File

@ -1,8 +1,8 @@
import masstraceroute import masstraceroute
import logging 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"] 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() t.main()

View File

@ -7,6 +7,8 @@ from multiprocessing.pool import ThreadPool
from multiprocessing import Lock from multiprocessing import Lock
from threading import Thread from threading import Thread
from pprint import pprint
class MassTraceroute: class MassTraceroute:
def __init__(self, socket_timeout, listener_timeout, threads, targets, maxhops): def __init__(self, socket_timeout, listener_timeout, threads, targets, maxhops):
logging.info("Setting up class") logging.info("Setting up class")
@ -18,10 +20,12 @@ class MassTraceroute:
self.threads = threads self.threads = threads
self.pool = ThreadPool(processes=self.threads) self.pool = ThreadPool(processes=self.threads)
self.routes = dict() self.routes = dict()
for target in self.targets:
self.routes[target] = dict()
logging.info("Creating listener socket") logging.info("Creating listener socket")
self.listener = self.init_listener() self.listener = self.init_listener()
self.sources = dict() self.sources = dict()
self.last_send = 0 self.last_send = time()
def main(self): def main(self):
logging.info("Starting listener thread") logging.info("Starting listener thread")
@ -40,23 +44,24 @@ class MassTraceroute:
return listener return listener
def process_listener(self): def process_listener(self):
while((self.last_send + self.listener_timeout) < time()): while((self.last_send + self.listener_timeout) > time()):
logging.debug("Listening...") logging.debug("Listening...")
try: try:
data, addr = self.listener.recvfrom(1024) data, addr = self.listener.recvfrom(1024)
except socket.error as e: except socket.error as e:
logging.debug(e)
pass pass
logging.debug("Got data from ip = {}".format(addr)) srcport = int.from_bytes(data[48:50], byteorder="big")
logging.debug(data) dstport = int.from_bytes(data[50:52], byteorder="big")
logging.debug(data[48:49]) logging.info("Got data from ip = {}, srcport = {}, dstport = {}, original target = {}, hop = {}".format(addr[0], srcport, dstport, self.sources[srcport]["ip"], self.sources[srcport][dstport]))
srcport = int.from_bytes(data[48:49], byteorder="big")
dstport = int.from_bytes(data[50:51], byteorder="big")
if srcport in self.sources and dstport in self.sources[srcport]: if srcport in self.sources and dstport in self.sources[srcport]:
hop = self.sources[srcport][dstport] hop = self.sources[srcport][dstport]
ip = self.sources[srcport]["ip"] ip = self.sources[srcport]["ip"]
self.routes[ip][hop] = addr self.routes[ip][hop] = addr[0]
logging.info(self.routes[ip][hop])
else: else:
logging.error("Received package with srcport or dstport non existsant?") logging.error("Received package with srcport or dstport non existsant?")
pprint(self.routes)
def process_sender(self): def process_sender(self):
logging.info("Starting process_sender") logging.info("Starting process_sender")
@ -77,22 +82,25 @@ class MassTraceroute:
def send(self, dstip): def send(self, dstip):
logging.info("Entering send()") logging.info("Entering send()")
sender = self.init_sender() sender = self.init_sender()
while(True):
srcport = getrandbits(16) srcport = getrandbits(16)
while(srcport not in self.sources): if srcport not in self.sources:
self.sources[srcport] = dict() self.sources[srcport] = dict()
self.sources[srcport]["ip"] = dstip self.sources[srcport]["ip"] = dstip
srcport = getrandbits(16) break
logging.info("Binding sending socket") logging.info("Binding sending socket")
sender.bind(('', srcport)) sender.bind(('', srcport))
for ttl in range(1, self.maxhops): for ttl in range(1, self.maxhops):
sender.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) sender.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
dstport = getrandbits(16) dstport = getrandbits(16)
while(dstport not in self.sources[srcport]): while(True):
self.sources[srcport][dstport] = ttl
dstport = getrandbits(16) 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)) logging.debug("Sending packet srcport = {}, dstport = {}, ttl = {}, dstip = {}".format(srcport, dstport, ttl, dstip))
sender.sendto(b'', (dstip, dstport)) sender.sendto(b'', (dstip, dstport))
self.last_send = time() self.last_send = time()