-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
133 lines (109 loc) · 3.25 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
import bottle, os, traceback
from bottle import request, response
from playwright.sync_api import sync_playwright
from urllib.parse import quote_plus
import string
flag = os.environ.get(
"FLAG", "jctf{red_flags_and_fake_flags_form_an_equivalence_class}"
)
public_host = os.environ.get("PUBLIC_HOST", "http://localhost:3000/")
visit_flag_url = public_host + "flag?flag=" + quote_plus(flag)
print(f"{visit_flag_url = }")
def sanitize(s):
bad = "<>\"'{}\0\n\r"
return "".join(c for c in s if c not in bad)
def secure(callback):
def wrapper(*args, **kwargs):
response.headers["Content-Security-Policy"] = "default-src 'none';"
response.headers["Cross-Origin-Resource-Policy"] = "same-origin"
return callback(*args, **kwargs)
return wrapper
bottle.install(secure)
@bottle.post("/visit")
def visit():
url = sanitize(request.forms.get("url"))
if not url.startswith("http://") and not url.startswith("https://"):
return "Invalid URL"
print(f"Visiting {url}")
with sync_playwright() as playwright:
browser = playwright.chromium.launch(
headless=True,
args=[
"--js-flags=--jitless,--no-expose-wasm",
"--disable-gpu",
"--disable-dev-shm-usage",
],
)
context = browser.new_context()
try:
page = context.new_page()
page.goto(
visit_flag_url, wait_until="networkidle", timeout=5000
) # this is essentially localhost, so it should be fast
page.close()
except:
traceback.print_exc()
return "Failed to visit the flag URL, please report this to the admin"
try:
page = context.new_page()
page.goto(url, wait_until="networkidle")
page.close()
except:
traceback.print_exc()
return "Failed to visit the requested URL"
return "Visited!"
@bottle.get("/visit")
def index():
return f"""<!DOCTYPE html>
<html>
<head>
<title>Admin Bot</title>
</head>
<body>
<pre>{public_host = }</pre>
<form action="/visit" method="post">
<label for="url">URL:</label>
<input type="url" name="url" required>
<input type="submit">
</form>
</body>
</html>
"""
@bottle.get("/source")
def source():
with open(__file__) as f:
response.content_type = "text/plain"
return f.read()
@bottle.get("/Dockerfile")
def dockerfile():
with open("Dockerfile") as f:
response.content_type = "text/plain"
return f.read()
@bottle.get("/")
def index():
return f"""<!DOCTYPE html>
<html>
<head>
<title>Flag Server</title>
</head>
<body>
<h1>Flag Server</h1>
<form action="/flag">
<label for="flag">Flag:</label>
<input type="text" name="flag" required>
<input type="submit">
</form>
<a href="/source">Source</a>
</body>
</html>
"""
@bottle.get("/<_:re:.+>")
def flag(_):
flag = request.get_cookie("flag", "No flag found...")
if request.query.flag:
flag = sanitize(request.query.flag)
response.set_cookie("flag", flag, httponly=True)
return flag
application = bottle.default_app()
if __name__ == "__main__":
bottle.run(host="localhost", port=3000)