From 0a1e61fd8b89b2e6916cb3d405c5ee4bd924eae8 Mon Sep 17 00:00:00 2001 From: Daniel Sagi Date: Thu, 25 Aug 2022 19:29:46 +0300 Subject: [PATCH] changed to using pyroute2 instead of manually parsing /proc/net/route and instead of psutil for interface enum --- kube_hunter/modules/discovery/hosts.py | 55 ++++++++++++++------------ 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/kube_hunter/modules/discovery/hosts.py b/kube_hunter/modules/discovery/hosts.py index 89cfbbb..70640f0 100644 --- a/kube_hunter/modules/discovery/hosts.py +++ b/kube_hunter/modules/discovery/hosts.py @@ -1,14 +1,13 @@ import os -import struct +import sys import socket import logging import itertools import requests -from pathlib import Path from enum import Enum from netaddr import IPNetwork, IPAddress, AddrFormatError -from psutil import net_if_addrs +from pyroute2 import IPDB, IPRoute from kube_hunter.conf import get_config from kube_hunter.modules.discovery.kubernetes_client import list_all_k8s_cluster_nodes @@ -226,21 +225,20 @@ class FromPodHostDiscovery(Discovery): # netifaces currently does not have a maintainer. so we backported to linux support only for this cause. # TODO: implement WMI queries for windows support # https://stackoverflow.com/a/6556951 - if not Path("/proc/net/route").exists(): - logging.debug("Error getting default gateway from /proc/net/route. not runnning in linux environment") + if sys.platform in ["linux", "linux2"]: + try: + ip = IPDB() + gateway_ip = ip.routes["default"]["gateway"] + ip.release() + return [gateway_ip, "24"] + except Exception as x: + logging.debug(f"Exception while fetching default gateway from container - {x}") + finally: + ip.release() + else: + logging.debug("Not running in a linux env, will not scan default subnet") return False - try: - with open("/proc/net/route") as fh: - for line in fh: - fields = line.strip().split() - if fields[1] != "00000000" or not int(fields[3], 16) & 2: - # If not default route or not RTF_GATEWAY, skip it - continue - return [socket.inet_ntoa(struct.pack("