-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
212 lines (158 loc) · 4.97 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
import re
import os
import uuid
import json
import random
import subprocess
import tempfile
from flask_session import Session
from flask import (
Flask,
render_template,
session,
request,
abort,
redirect,
url_for,
flash,
)
ADMIN_KEY = "{{seed}}"
NODE_PATH = "/usr/bin/node"
NODE_PATH = "/usr/local/bin/node"
app = Flask(__name__)
app.config["SECRET_KEY"] = "{{seed}}"
app.secret_key = "{{seed}}"
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_COOKIE_HTTPONLY"] = False
app.config["TEMPLATES_AUTO_RELOAD"] = True
Session(app)
@app.route("/")
def index():
return redirect(url_for("cookie"))
# Cookie index
@app.route("/cookie")
def cookie():
return render_template("new_cookie.html")
# Display a given cookie
@app.route("/cookie/<string:uid>")
def get_cookie(uid):
cookies = session.get("cookies", {})
if uid not in cookies:
return abort(404)
cookie = cookies[uid]
if cookie.get("flag", False) and not session.get("admin", False):
return abort(403)
return render_template("cookie.html", cookie=cookies[uid], uid=uid)
# View cookies to review
@app.route("/admin")
def admin():
if not session.get("admin", False):
return abort(403)
cookies = session.get("cookies", {})
cookies = [
(x, y) for x, y in cookies.items() if y["submitted"] and not y["rejected"]
]
return render_template("admin.html", cookies=cookies)
# Request a cookie to be approved
@app.route("/approve", methods=["POST"])
def approve():
uid = request.form["cookie"]
cookies = session.get("cookies", {})
if uid not in cookies:
return abort(400)
sid = get_session_id()
if sid in procs:
return redirect(url_for("get_cookie", uid=uid))
session["cookies"][uid]["submitted"] = True
start_admin(uid)
return redirect(url_for("get_cookie", uid=uid))
# Create a new cookie
@app.route("/cookie/new", methods=["POST"])
def new_cookie():
ingredients = request.form.getlist("ingredients")
if not "cookies" in session:
# Store the flag into the session
session["cookies"] = {
str(uuid.uuid4()): {
"name": "Flag Cookie",
"recipe": "<p>1. Preheat the oven to 350</p><p>2. Add one {{flag}}</p><p>3. Bake for 1337 minutes and allow to cool</p>",
"flag": True,
"submitted": True,
"rejected": False,
}
}
# Build XSS location
recipe = "<p>1. Preheat the oven to 350</p>"
count = 1
for i, ing in enumerate(ingredients):
if ing == "Add ingredient":
continue
count += 1
recipe += f"""
<p>{count}. {random.choice(['Mix together','Add in','Stir in'])} {random.randint(1,5)} {random.choice(['cups','teaspoons','tablespoons','ounces'])} of {ing}</p>"""
recipe += f"\n<p>{count+1}. Bake for {random.randint(30,60)} minutes and allow to cool</p>"
uid = str(uuid.uuid4())
session["cookies"][uid] = {
"name": request.form["name"],
"recipe": recipe,
"submitted": False,
"rejected": False,
}
return redirect(url_for("get_cookie", uid=uid))
procs = {}
# Clean up any child procs and update the session
@app.before_request
def before_request():
sid = get_session_id()
if sid is None or sid not in procs:
return
p, uid = procs[sid]
if p.poll() is None:
return
del procs[sid]
session["cookies"][uid]["rejected"] = True
def start_admin(uid):
tmpf = save_session_to_file()
url = (
f"http://localhost:5000/admin/landing?key={ADMIN_KEY}&session={tmpf}&uid={uid}"
)
os.environ["NODE_PATH"] = "/usr/lib/node_modules"
p = subprocess.Popen(["timeout", "10s", NODE_PATH, "./chrome.js", url])
procs[get_session_id()] = (p, uid)
@app.route("/admin/landing")
def admin_landing():
if not "key" in request.args or request.args["key"] != ADMIN_KEY:
abort(404)
uid = request.args["uid"]
load_session_from_file(request.args["session"])
return redirect(url_for("get_cookie", uid=uid))
class FakeRequest:
def __init__(self, session):
self.cookies = {app.session_cookie_name: session}
class FakeResponse:
def set_cookie(*args, **kwargs):
pass
def get_session(sid):
return app.session_interface.open_session(app, FakeRequest(sid))
def save_session(ses):
app.session_interface.save_session(app, ses, FakeResponse())
def get_session_id():
return request.cookies.get(app.session_cookie_name, None)
def save_session_to_file():
sid = get_session_id()
ses = get_session(sid)
ses["user_session"] = sid
fd, tmpf = tempfile.mkstemp()
os.close(fd)
with open(tmpf, "w") as f:
json.dump(ses, f)
return tmpf
def load_session_from_file(tmpf):
with open(tmpf, "r") as f:
ses = json.load(f)
os.unlink(tmpf)
ses["admin"] = True
session.update(ses)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5001)