Skip to content

Commit

Permalink
Make gateway discovery always run when running as pod #471
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsagi authored Jul 23, 2021
1 parent f67f082 commit 473e4fe
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions kube_hunter/modules/discovery/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ def execute(self):
self.publish_event(HostScanEvent())
else:
# Discover cluster subnets, we'll scan all these hosts
cloud = None
cloud, subnets = None, list()
if self.is_azure_pod():
subnets, cloud = self.azure_metadata_discovery()
elif self.is_aws_pod_v1():
subnets, cloud = self.aws_metadata_v1_discovery()
elif self.is_aws_pod_v2():
subnets, cloud = self.aws_metadata_v2_discovery()
else:
subnets = self.gateway_discovery()

subnets += self.gateway_discovery()

should_scan_apiserver = False
if self.event.kubeservicehost:
Expand Down Expand Up @@ -221,19 +221,27 @@ def aws_metadata_v1_discovery(self):
"http://169.254.169.254/latest/meta-data/mac",
timeout=config.network_timeout,
).text
logger.debug(f"Extracted mac from aws's metadata v1: {mac_address}")

cidr = requests.get(
f"http://169.254.169.254/latest/meta-data/network/interfaces/macs/{mac_address}/subnet-ipv4-cidr-block",
timeout=config.network_timeout,
).text.split("/")
).text
logger.debug(f"Trying to extract cidr from aws's metadata v1: {cidr}")

address, subnet = (cidr[0], cidr[1])
subnet = subnet if not config.quick else "24"
cidr = f"{address}/{subnet}"
logger.debug(f"From pod discovered subnet {cidr}")
try:
cidr = cidr.split("/")
address, subnet = (cidr[0], cidr[1])
subnet = subnet if not config.quick else "24"
cidr = f"{address}/{subnet}"
logger.debug(f"From pod discovered subnet {cidr}")

self.publish_event(AWSMetadataApi(cidr=cidr))
self.publish_event(AWSMetadataApi(cidr=cidr))
return [(address, subnet)], "AWS"
except Exception as x:
logger.debug(f"ERROR: could not parse cidr from aws metadata api: {cidr} - {x}")

return [(address, subnet)], "AWS"
return [], "AWS"

# querying AWS's interface metadata api v2 | works only from a pod
def aws_metadata_v2_discovery(self):
Expand All @@ -255,14 +263,19 @@ def aws_metadata_v2_discovery(self):
timeout=config.network_timeout,
).text.split("/")

address, subnet = (cidr[0], cidr[1])
subnet = subnet if not config.quick else "24"
cidr = f"{address}/{subnet}"
logger.debug(f"From pod discovered subnet {cidr}")
try:
address, subnet = (cidr[0], cidr[1])
subnet = subnet if not config.quick else "24"
cidr = f"{address}/{subnet}"
logger.debug(f"From pod discovered subnet {cidr}")

self.publish_event(AWSMetadataApi(cidr=cidr))

self.publish_event(AWSMetadataApi(cidr=cidr))
return [(address, subnet)], "AWS"
except Exception as x:
logger.debug(f"ERROR: could not parse cidr from aws metadata api: {cidr} - {x}")

return [(address, subnet)], "AWS"
return [], "AWS"

# querying azure's interface metadata api | works only from a pod
def azure_metadata_discovery(self):
Expand Down

0 comments on commit 473e4fe

Please sign in to comment.