-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpoint.py
104 lines (78 loc) · 2.25 KB
/
checkpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from flask import Flask, session, send_file, redirect, url_for, request
import os
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from captcha import generate_captcha
import uuid
import io
CAPTCHA_LENGTH = 10
CAPTCHA_LIFETIME = timedelta(seconds=5)
CAPTCHAS_REQUIRED = 100
app = Flask(__name__)
app.secret_key = os.urandom(32)
@dataclass
class Captcha:
text: str
audio: bytes
generated_at: datetime
captchas: dict[str, Captcha] = {}
def session_captcha_stale() -> bool:
if "captcha_id" not in session:
return True
captcha_id = session["captcha_id"]
captcha = captchas.get(captcha_id)
if captcha is None:
return True
elif datetime.now(timezone.utc) >= captcha.generated_at + CAPTCHA_LIFETIME:
del captchas[captcha_id]
return True
return False
def new_session_captcha():
text, audio = generate_captcha(CAPTCHA_LENGTH)
captcha = Captcha(
text=text,
audio=audio,
generated_at=datetime.now(timezone.utc),
)
captcha_id = str(uuid.uuid4())
captchas[captcha_id] = captcha
session["captcha_id"] = captcha_id
@app.post("/captcha/generate")
def generate():
if session.get("captcha_passed") == True:
new_session_captcha()
del session["captcha_passed"]
elif session_captcha_stale():
new_session_captcha()
session["captchas_solved"] = 0
captcha = captchas[session["captcha_id"]]
return send_file(
io.BytesIO(captcha.audio),
mimetype="audio/mpeg",
as_attachment=False,
)
@app.post("/captcha/submit")
def submit():
if session_captcha_stale():
return "", 403
captcha_id = session.pop("captcha_id")
captcha = captchas.pop(captcha_id)
text = request.form.get("text")
if text != captcha.text:
del session["captchas_solved"]
return "", 403
session["captcha_passed"] = True
session["captchas_solved"] += 1
if session["captchas_solved"] >= CAPTCHAS_REQUIRED:
return os.getenv("FLAG")
return "", 200
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=24242,
debug=False,
use_debugger=False,
use_evalex=False,
threaded=False,
processes=1,
)