Example #1
0
def test_FromPodHostDiscovery():

    with requests_mock.Mocker() as m:
        e = RunningAsPodEvent()

        config.azure = False
        config.remote = None
        config.cidr = None
        m.get("http://169.254.169.254/metadata/instance?api-version=2017-08-01", status_code=404)
        f = FromPodHostDiscovery(e)
        assert not f.is_azure_pod()
        # TODO For now we don't test the traceroute discovery version
        # f.execute()

        # Test that we generate NewHostEvent for the addresses reported by the Azure Metadata API
        config.azure = True
        m.get("http://169.254.169.254/metadata/instance?api-version=2017-08-01", \
            text='{"network":{"interface":[{"ipv4":{"subnet":[{"address": "3.4.5.6", "prefix": "255.255.255.252"}]}}]}}')
        assert f.is_azure_pod()
        f.execute()

        # Test that we don't trigger a HostScanEvent unless either config.remote or config.cidr are configured
        config.remote = "1.2.3.4"
        f.execute()

        config.azure = False
        config.remote = None 
        config.cidr = "1.2.3.4/24"
        f.execute()
def main():
    global hunt_started
    scan_options = [
        config.pod, 
        config.cidr,
        config.remote, 
        config.internal
    ]
    try:
        if config.list:
            list_hunters()
            return

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

        hunt_started_lock.acquire()
        hunt_started = True
        hunt_started_lock.release()
        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:
        logging.debug("Kube-Hunter stopped by user")
    # happens when running a container without interactive option
    except EOFError:
        logging.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()
            logging.debug("Cleaned Queue")
        else:
            hunt_started_lock.release()