def test_traceroute(monkeypatch):
    monkeypatch.setattr(dublintraceroute._dublintraceroute, 'DublinTraceroute',
                        MockDublinTraceroute)
    d = dublintraceroute.DublinTraceroute('127.0.0.1')
    result = d.traceroute()
    assert type(result) == dublintraceroute.TracerouteResults
    assert list(result.keys()) == ['flows']
def test_str():
    d = dublintraceroute.DublinTraceroute('127.0.0.1',
                                          sport=12345,
                                          dport=33434,
                                          npaths=10,
                                          min_ttl=3,
                                          max_ttl=15,
                                          delay=100,
                                          broken_nat=False)
    assert str(
        d
    ) == '''<DublinTraceroute (target='127.0.0.1', sport=12345, dport=33434, npaths=10, min_ttl=3, max_ttl=15, delay=100, broken_nat=False, use_srcport_for_path_generation=False)>'''
def test_init_attrs():
    d = dublintraceroute.DublinTraceroute('127.0.0.1', sport=12345,
            dport=33434, npaths=10, min_ttl=3, max_ttl=15, delay=100,
            broken_nat=False)
    assert d.target == '127.0.0.1'
    assert d.sport == 12345
    assert d.dport == 33434
    assert d.npaths == 10
    assert d.min_ttl == 3
    assert d.max_ttl == 15
    assert d.delay == 100
    assert d.broken_nat == False
예제 #4
0
    def perform_tracert(self):
        print("Performing tracert...")
        for t in self.target_l:

            dublin = dublintraceroute.DublinTraceroute(t.ip,
                                                       npaths=1,
                                                       delay=25,
                                                       dport=t.dst_port,
                                                       sport=t.src_port,
                                                       broken_nat=False,
                                                       iterate_sport=False)
            results = dublin.traceroute()

            local_path = []
            for sport in results['flows']:

                port_res = results['flows'][sport]

                for flow in port_res:
                    flow_name = flow['name']
                    if flow_name is not "":
                        local_path.append(flow['received']['ip']['src'])

                    if flow['is_last'] or (len(local_path) > 0
                                           and local_path[-1] == t.ip):
                        if len(local_path) > 0:
                            if local_path[-1] == t.ip:
                                del local_path[-1]
                            break

                print("ip:{} sport:{} dport:{} path:{}", t.ip, t.src_port,
                      t.dst_port, local_path)
                if len(local_path) <= 0:
                    local_path = [t.ip]
                    print("ERROR ip:{} has path with len 0, adding "
                          "ip to path:{}".format(t.ip, local_path))

                t.route = local_path
        pass
def test_init():
    d = dublintraceroute.DublinTraceroute('127.0.0.1')
    assert d.__class__.__name__ == 'DublinTraceroute'
    assert d.sport == 12345
예제 #6
0
def do_dublin_tracert(tracert_id):
    """
    Perform a dublin traceroute against a remote host on a remote port
    The parameters for this traceroute execution are defined on the db
    :param tracert_id: the ID of the iteration to get the tracerts
    :return:
    """
    current_f_name = inspect.currentframe().f_code.co_name

    logger.info("{}: do_dublin_tracert tracert_id:{}".format(
        current_f_name, tracert_id))

    tracert_t = db.session.query(models.Tracert).filter_by(
        id=tracert_id, status='PENDING').first()
    if tracert_t is None:
        logger.error("{}: Tracert not found with tracert_id: {}".format(
            current_f_name, tracert_id))
        return None

    try:
        tracert_t.status = "STARTED"
        db.session.commit()

        npaths = tracert_t.ponger_port.src_port_max - \
            tracert_t.ponger_port.src_port_min
        logger.info("{}: do_dublin_tracert started address:{} "
                    "srcport:{} dstport:{} npaths:{}".format(
                        current_f_name,
                        str(tracert_t.ponger_port.ponger.address),
                        tracert_t.ponger_port.src_port_min,
                        tracert_t.ponger_port.dst_port, npaths))

        dublin = dublintraceroute.DublinTraceroute(
            str(tracert_t.ponger_port.ponger.address),
            sport=tracert_t.ponger_port.src_port_min,
            dport=tracert_t.ponger_port.dst_port,
            use_srcport_for_path_generation=True,
            delay=25,
            npaths=npaths)

        success = False
        tracert_results = ""
        try:
            tracert_results = dublin.traceroute()

            if "flows" not in tracert_results.keys() or len(
                    tracert_results['flows']) <= 0:
                logger.debug("{}: Invalid tracert result:{}".format(
                    current_f_name, tracert_results))
            else:
                success = True
        except Exception:
            logger.debug("{}: Tracert client error: {}".format(
                current_f_name, traceback.format_exc()))

        if not success:
            tracert_t.status = "FAILURE"
            db.session.commit()

            logger.error(
                "{}: Tracert client cannot generate trace for server at:{}".
                format(current_f_name, tracert_t.ponger_port.ponger.address))

        tracert_t.status = "SUCCESS"
        tracert_t.result = json.dumps(tracert_results)
        db.session.commit()

        logger.debug("{}: result:{}".format(current_f_name, tracert_results))
    except SoftTimeLimitExceeded:
        tracert_t.status = "FAILED"
        db.session.commit()
        return None
    except Exception as e:
        logger.error("{}: Error calling do_dublin_tracert {} "
                     "src port:{}~{} dst port:{} error:{}".format(
                         current_f_name,
                         str(tracert_t.ponger_port.ponger.address),
                         str(tracert_t.ponger_port.src_port_min),
                         str(tracert_t.ponger_port.src_port_max),
                         str(tracert_t.ponger_port.dst_port), str(e)))
        return None

    return True