Ejemplo n.º 1
0
 def execute(self):
     config = get_config()
     report = config.reporter.get_report(statistics=config.statistics,
                                         mapping=config.mapping)
     config.dispatcher.dispatch(report)
     handler.publish_event(ReportDispatched())
     handler.publish_event(TablesPrinted())
Ejemplo n.º 2
0
 def scan_interfaces(self):
     try:
         logging.debug("HostDiscovery hunter attempting to get external IP address")
         external_ip = requests.get("http://canhazip.com").text # getting external ip, to determine if cloud cluster
     except requests.ConnectionError as e:
         logging.debug("unable to determine local IP address: {0}".format(e))
         logging.info("~ default to 127.0.0.1")
         external_ip = "127.0.0.1"
     cloud = HostDiscoveryHelpers.get_cloud(external_ip)
     for ip in self.generate_interfaces_subnet():
         handler.publish_event(NewHostEvent(host=ip, cloud=cloud))
Ejemplo n.º 3
0
 def scan_interfaces(self):
     try:
         logger.debug(
             "HostDiscovery hunter attempting to get external IP address")
         # getting external ip, to determine if cloud cluster
         external_ip = requests.get("https://canhazip.com",
                                    timeout=config.network_timeout).text
     except requests.ConnectionError:
         logger.warning(
             f"Unable to determine external IP address, using 127.0.0.1",
             exc_info=True)
         external_ip = "127.0.0.1"
     for ip in self.generate_interfaces_subnet():
         handler.publish_event(NewHostEvent(host=ip))
Ejemplo n.º 4
0
def test_subscribe_once_mechanism():
    global counter
    counter = 0

    # testing the multiple subscription mechanism
    handler.publish_event(OnceOnlyEvent())

    time.sleep(0.02)
    assert counter == 1
    counter = 0

    handler.publish_event(OnceOnlyEvent())
    handler.publish_event(OnceOnlyEvent())
    handler.publish_event(OnceOnlyEvent())
    time.sleep(0.02)

    assert counter == 0
Ejemplo n.º 5
0
def test_subscribe_mechanism():
    global counter
    counter = 0

    # first test normal subscribe and publish works
    handler.publish_event(RegularEvent())
    handler.publish_event(RegularEvent())
    handler.publish_event(RegularEvent())

    time.sleep(0.02)
    assert counter == 3
Ejemplo n.º 6
0
def main():
    global hunt_started
    scan_options = [
        config.pod, config.cidr, config.remote, config.interface,
        config.k8s_auto_discover_nodes
    ]
    try:
        if args.list:
            if args.raw_hunter_names:
                list_hunters(class_names=True)
            else:
                list_hunters()
            return

        if not any(scan_options):
            if not interactive_set_config():
                return

        with hunt_started_lock:
            hunt_started = True
        handler.publish_event(HuntStarted())
        if config.pod:
            handler.publish_event(RunningAsPodEvent())
        else:
            handler.publish_event(HostScanEvent())

        # Blocking to see discovery output
        handler.join()
    except KeyboardInterrupt:
        logger.debug("Kube-Hunter stopped by user")
    # happens when running a container without interactive option
    except EOFError:
        logger.error("\033[0;31mPlease run again with -it\033[0m")
    finally:
        hunt_started_lock.acquire()
        if hunt_started:
            hunt_started_lock.release()
            handler.publish_event(HuntFinished())
            handler.join()
            handler.free()
            logger.debug("Cleaned Queue")
        else:
            hunt_started_lock.release()
Ejemplo n.º 7
0
 def scan_interfaces(self):
     for ip in self.generate_interfaces_subnet():
         handler.publish_event(NewHostEvent(host=ip))
Ejemplo n.º 8
0
def test_subscribe_mechanism():
    global counter

    # first test normal subscribe and publish works
    handler.publish_event(RegularEvent())
    handler.publish_event(RegularEvent())
    handler.publish_event(RegularEvent())

    time.sleep(0.02)
    assert counter == 3
    counter = 0

    # testing the subscribe_once mechanism
    handler.publish_event(OnceOnlyEvent())
    handler.publish_event(OnceOnlyEvent())
    handler.publish_event(OnceOnlyEvent())

    time.sleep(0.02)
    # should have been triggered once
    assert counter == 1
Ejemplo n.º 9
0
 def execute(self):
     report = config.reporter.get_report()
     config.dispatcher.dispatch(report)
     handler.publish_event(ReportDispatched())
     handler.publish_event(TablesPrinted())
Ejemplo n.º 10
0
def test_subscribe_many_mechanism():
    global counter
    counter = 0

    # testing the multiple subscription mechanism
    handler.publish_event(DifferentRegularEvent())
    handler.publish_event(DifferentRegularEvent())
    handler.publish_event(DifferentRegularEvent())
    handler.publish_event(DifferentRegularEvent())
    handler.publish_event(DifferentRegularEvent())
    handler.publish_event(AnotherRegularEvent())

    time.sleep(0.02)
    # We expect SmartHunter and SmartHunter2 to be executed once. hence the counter should be 2
    assert counter == 2
    counter = 0

    # Test using most recent event
    newer_version_event = DifferentRegularEvent()
    newer_version_event.new_value = True
    handler.publish_event(newer_version_event)

    assert counter == 2