-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchecker.py
executable file
·229 lines (183 loc) · 7.96 KB
/
checker.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
import json
import logging
import os
import random
import requests
import string
import sys
import time
import traceback
import urllib3
from requests.adapters import HTTPAdapter, Retry
logging.basicConfig(format="%(asctime)s [%(process)d] %(levelname)-8s %(message)s",
level=logging.DEBUG, handlers=[logging.StreamHandler(sys.stderr)])
OK, CORRUPT, MUMBLE, DOWN, CHECKER_ERROR = 101, 102, 103, 104, 110
TIMEOUT = urllib3.util.Timeout(connect=1.0, read=3.0)
RETRIES = Retry(total=3, backoff_factor=0.3, status_forcelist=[ 500, 502, 503, 504 ])
def verdict(exit_code, public="", private=""):
if public:
print(public)
if private:
print(private, file=sys.stderr)
sys.exit(exit_code)
def random_name(length=10):
return ''.join(random.choices(
string.ascii_lowercase + string.digits, k=length))
def get_base_url(host):
base_url = f"http://{host}/"
try:
r = requests.options(base_url, timeout=TIMEOUT, allow_redirects=False)
if r.status_code > 300 and r.status_code < 400 and r.headers["Location"]:
base_url = r.headers["Location"]
if not base_url[-1] == '/':
base_url += '/'
except:
pass
return base_url
def info():
verdict(OK, "vulns: 1\npublic_flag_description: Flag ID is the document's title and user's org in \"title@org\" format, flag is the content of the document with this title")
def check(host):
verdict(OK)
def put(host, flag_id, flag, vuln):
user1 = {"login": random_name(), "password": random_name(), "org": random_name(length=5)}
logging.info(f"user1 {user1}")
user2 = {"login": random_name(), "password": random_name(), "org": random_name(length=5)}
logging.info(f"user2 {user2}")
state = {"public_flag_id": f"{flag_id}@{user1['org']}", "flag_id": flag_id, "user1": user1, "user2": user2}
base_url = get_base_url(host)
session1 = requests.Session()
session2 = requests.Session()
session1.mount('http://', HTTPAdapter(max_retries=RETRIES))
session1.mount('https://', HTTPAdapter(max_retries=RETRIES))
session2.mount('http://', HTTPAdapter(max_retries=RETRIES))
session2.mount('https://', HTTPAdapter(max_retries=RETRIES))
url = f"{base_url}register"
r = session1.post(url, timeout=TIMEOUT, json=user1)
if r.status_code != 200:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /register")
try:
user1_id = r.json()["user_id"]
logging.info(f"Registered user1 with id: {user1_id}")
state["user1"]["id"] = user1_id
except Exception as e:
logging.debug(traceback.format_exc())
verdict(MUMBLE, public="Invalid answer on /register")
r = session2.post(url, timeout=TIMEOUT, json=user2)
if r.status_code != 200:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /register")
try:
user2_id = r.json()["user_id"]
logging.info(f"Registered user2 with id: {user2_id}")
state["user2"]["id"] = user2_id
except Exception as e:
logging.debug(traceback.format_exc())
verdict(MUMBLE, public="Invalid answer on /register")
url = f"{base_url}login"
r = session1.post(url, timeout=TIMEOUT, json=user1)
if r.status_code != 204:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /login")
r = session2.post(url, timeout=TIMEOUT, json=user2)
if r.status_code != 204:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /login")
url = f"{base_url}docs"
r = session1.post(url, timeout=TIMEOUT, json={"title": flag_id, "shares": []})
if r.status_code != 201:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /docs")
try:
doc_id = r.json()["id"]
url = f"{base_url}contents"
r = session1.post(url, timeout=TIMEOUT, json={"doc_id": doc_id, "data": flag})
if r.status_code != 201:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /contents")
except Exception as e:
logging.debug(traceback.format_exc())
verdict(MUMBLE, public="Invalid answer on /docs")
url = f"{base_url}docs"
r = session1.post(url, timeout=TIMEOUT, json={"title": random_name(), "shares": [user2_id]})
if r.status_code != 201:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /docs")
url = f"{base_url}docs"
r = session2.get(url, timeout=TIMEOUT)
if r.status_code != 200:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /docs")
try:
docs = r.json()["docs"]
if len(docs) != 1:
verdict(MUMBLE, public=f"Invalid answer on /docs")
if docs[0] == state["flag_id"]:
verdict(MUMBLE, public=f"Invalid answer on /docs")
except Exception as e:
logging.debug(traceback.format_exc())
verdict(MUMBLE, public="Invalid answer on /docs")
verdict(OK, json.dumps(state))
def get(host, flag_id, flag, vuln):
state = json.loads(flag_id)
base_url = get_base_url(host)
session1 = requests.Session()
session2 = requests.Session()
session1.mount('http://', HTTPAdapter(max_retries=RETRIES))
session1.mount('https://', HTTPAdapter(max_retries=RETRIES))
session2.mount('http://', HTTPAdapter(max_retries=RETRIES))
session2.mount('https://', HTTPAdapter(max_retries=RETRIES))
url = f"{base_url}login"
r = session1.post(url, timeout=TIMEOUT, json=state["user1"])
if r.status_code != 204:
verdict(CORRUPT, public=f"Wrong HTTP status code ({r.status_code}) on /login")
r = session2.post(url, timeout=TIMEOUT, json=state["user2"])
if r.status_code != 204:
verdict(CORRUPT, public=f"Wrong HTTP status code ({r.status_code}) on /login")
url = f"{base_url}users/{state['user2']['id']}"
logging.info(f"Update org of user2")
r = session2.put(url, timeout=TIMEOUT, json={"org": state["user1"]["org"]})
if r.status_code != 204:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on PUT /users")
url = f"{base_url}docs"
r = session2.get(url, timeout=TIMEOUT)
if r.status_code != 200:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /docs")
try:
docs = r.json()["docs"]
if len(docs) != 2:
verdict(MUMBLE, public=f"Invalid answer on /docs")
doc_id = None
for doc in docs:
if doc["title"] == state["flag_id"]:
doc_id = doc["id"]
break
if not doc_id:
verdict(MUMBLE, public=f"Invalid answer on /docs; document with title {state['flag_id']} not found")
url = f"{base_url}contents/{doc_id}"
r = session1.get(url, timeout=TIMEOUT)
if r.status_code != 200:
verdict(MUMBLE, public=f"Wrong HTTP status code ({r.status_code}) on /contents")
if r.text != flag:
verdict(CORRUPT, public=f"Wrong data in document with title {state['flag_id']}")
except Exception as e:
logging.debug(traceback.format_exc())
verdict(MUMBLE, public="Invalid answer on /docs")
verdict(OK)
def main(args):
CMD_MAPPING = {
"info": (info, 0),
"check": (check, 1),
"put": (put, 4),
"get": (get, 4),
}
if not args:
verdict(CHECKER_ERROR, "No args", "No args")
cmd, args = args[0], args[1:]
if cmd not in CMD_MAPPING:
verdict(CHECKER_ERROR, "Checker error", f"Wrong command {cmd}")
handler, args_count = CMD_MAPPING[cmd]
if len(args) != args_count:
verdict(CHECKER_ERROR, "Checker error",
f"Wrong args count for {cmd}")
try:
handler(*args)
except (requests.Timeout, requests.ConnectionError, requests.exceptions.RetryError):
logging.debug(traceback.format_exc())
verdict(DOWN, public="Can't get HTTP response")
verdict(CHECKER_ERROR, "Checker error", "No verdict")
if __name__ == "__main__":
main(args=sys.argv[1:])