-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchecker.py
executable file
·520 lines (396 loc) · 19.9 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
import sys
import traceback
import requests
import random
import string
import http
import json
import ssl
import socket
import os
from urllib.parse import urljoin
from lxml import etree
from random import randint
from faker import Faker
requests.packages.urllib3.disable_warnings()
from checker_helper import *
VERIFY = False
TIMEOUT = 30
ADMIN_CERT = 'admin.crt'
ADMIN_KEY = 'admin.key'
fake = Faker()
def info():
verdict(OK, "vulns: 1\npublic_flag_description: Flag ID is the ID of the contact containing the flag")
def get_random_string(min_len, max_len):
letters = string.ascii_lowercase + string.digits
return ''.join(random.choice(letters) for i in range(random.randint(min_len, max_len)))
def get_url_path(path, use_client_cert):
if not use_client_cert:
return path
if not os.getenv('DEV'):
return f"/private{path}"
return path
def register_user(host, user, password, use_client_cert=False):
base_url = f"https://{host}/"
session = requests.Session()
trace(f"Trying to register using {user}:{password} (use_client_cert={use_client_cert})")
if use_client_cert:
cert=(ADMIN_CERT, ADMIN_KEY)
else:
cert=None
if random.choice([0, 1]):
url = urljoin(base_url, get_url_path('/', use_client_cert))
trace(f"Going to '{url}' and waiting for a redirect to '/login'")
try:
r = session.get(url, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during requesting home page: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during requesting home page: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Unexpected result", "Unexpected HTTP status code when requesting home page without session cookie: '%d'" % r.status_code, None, None)
if r.request.url != urljoin(base_url, get_url_path('/login', use_client_cert)):
return (MUMBLE, "Unexpected result", f"Unexpected login url '{r.request.url}' when requesting home page without session cookie. Expected url: '{urljoin(base_url, get_url_path('/login', use_client_cert))}'", None, None)
headers = {'Referer': urljoin(base_url, '/login')}
url = urljoin(base_url, get_url_path('/register', use_client_cert))
trace(f"Going to '{url}' from login page")
try:
r = session.get(url, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during requesting registration page: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during requesting registration page: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Unexpected result", "Unexpected HTTP status code when requesting registration page: '%d'" % r.status_code, None, None)
headers = {'Referer': urljoin(base_url, '/register')}
else:
headers = {}
url = urljoin(base_url, get_url_path('/register', use_client_cert))
trace(f"Sending the registration data to '{url}'")
data = {"username": user, "password": password}
try:
r = session.post(url, data=data, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during registration: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during registration: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Can't register", "Unexpected registration result: '%d'" % r.status_code, None, None)
try:
parser = etree.HTMLParser()
parser.feed(r.text)
doc = parser.close()
except Exception as e:
return (MUMBLE, "Unexpected registration result", "Can't parse result html: '%s'" % e, None, None)
user_element = doc.xpath("//span[@id='username']")
if len(user_element) != 1:
return (MUMBLE, "Unexpected registration result", "Can't find username HTML element in '%s'" % r.text, None, None)
actual_user = user_element[0].text.removesuffix(' | ').strip()
if actual_user != user:
return (MUMBLE, "Unexpected registration result", "Wrong username: '%s'" % actual_user, None, None)
trace("Successfully registered")
return (OK, "", "", session, r.text)
def login_user(host, user, password, use_client_cert=False):
base_url = f"https://{host}/"
if use_client_cert:
cert=(ADMIN_CERT, ADMIN_KEY)
else:
cert=None
session = requests.Session()
if random.choice([0, 1]):
url = urljoin(base_url, get_url_path('/', use_client_cert))
trace("Going to '%s' and waiting for a redirect to '/login'" % url)
try:
r = session.get(url, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during requesting home page: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during requesting home page: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Unexpected result", "Unexpected HTTP status code when requesting home page without session cookie: '%d'" % r.status_code, None, None)
if r.request.url != urljoin(base_url, get_url_path('/login', use_client_cert)):
return (MUMBLE, "Unexpected result", "Unexpected login url when requesting home page without session cookie: '%s'" % r.request.url, None, None)
headers = {'Referer': urljoin(base_url, '/login')}
else:
headers = {}
trace(f"Trying to login using {user}:{password} (use_client_cert={use_client_cert})")
data = {"username": user, "password": password}
url = urljoin(base_url, get_url_path('/login', use_client_cert))
try:
r = session.post(url, data=data, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during login: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during login: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Can't login", "Unexpected login result: '%d'" % r.status_code, None, None)
try:
parser = etree.HTMLParser()
parser.feed(r.text)
doc = parser.close()
except Exception as e:
return (MUMBLE, "Unexpected login result", "Can't parse result html: '%s'" % e, None, None)
user_element = doc.xpath("//span[@id='username']")
if len(user_element) != 1:
return (MUMBLE, "Unexpected login result", "Can't find username HTML element in '%s'" % r.text, None, None)
actual_user = user_element[0].text.removesuffix(' | ').strip()
if actual_user != user:
return (MUMBLE, "Unexpected login result", "Wrong username: '%s'" % actual_user, None, None)
trace("Successfully logged in")
return (OK, "", "", session, r.text)
def get_contact_id_by_name(home_page_html, name):
try:
parser = etree.HTMLParser()
parser.feed(home_page_html)
doc = parser.close()
except Exception as e:
return (MUMBLE, "Unexpected result", "Can't parse home page html: '%s'" % e, None)
row_element = doc.xpath("//tbody/tr[td/a[contains(text(), '%s')]]" % name)
if len(row_element) == 0:
return (CORRUPT, "Can't find contact", "Can't find contact", None)
if len(row_element) != 1:
return (MUMBLE, "Unexpected result", "Can't find contact '%s' in '%s'" % (name, home_page_html), None)
contact_id = row_element[0].xpath("./td[contains(@class, 'js-name')]/a/@href")[0].rsplit('/', 1)[-1]
return (OK, "", "", contact_id)
def generate_random_contact(comment = None):
data = {}
data["firstName"] = fake.first_name()
data["lastName"] = fake.last_name()
if random.choice([0, 1]):
data["title"] = fake.job()
if random.choice([0, 1]):
data["organization"] = fake.company()
if random.choice([0, 1]):
data["email"] = fake.company_email()
if random.choice([0, 1]):
data["phone"] = fake.phone_number()
if comment:
data["comment"] = comment
else:
if random.choice([0, 1]):
data["comment"] = fake.paragraph(nb_sentences=random.choice([1,2]))
return data
def create_contact(host, session, comment, use_client_cert=False):
base_url = f"https://{host}/"
if use_client_cert:
cert=(ADMIN_CERT, ADMIN_KEY)
else:
cert=None
data = generate_random_contact(comment)
if random.choice([0, 1]):
headers = {'Referer': base_url}
url = urljoin(base_url, get_url_path('/add', use_client_cert))
trace(f"Going to the '{url}' page from home page")
try:
r = session.get(url, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during requesting '/add' page: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during requesting '/add' page: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Unexpected result", "Unexpected HTTP status code when requesting '/add' page: '%d'" % r.status_code, None, None)
headers = {'Referer': urljoin(base_url, '/add')}
else:
headers = {}
try:
url = urljoin(base_url, get_url_path('/add', use_client_cert))
r = session.post(url, data=data, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during creating contact: %s" % e, None, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during creating contact: %s" % e, None, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None, None)
if r.status_code != 200:
return (MUMBLE, "Can't create contact", "Unexpected status code when creating a contact: '%d'" % r.status_code, None, None)
name = "%s %s" % (data["firstName"], data["lastName"])
(status, out, err, contact_id) = get_contact_id_by_name(r.text, name)
if status != OK:
if status == CORRUPT:
status = MUMBLE
return (status, out, err, None, None)
trace("Contact '%s' successfully created" % name)
return (OK, "", "", contact_id, name)
def get_contact_comment(host, session, id, use_client_cert=False):
base_url = f"https://{host}/"
if use_client_cert:
cert=(ADMIN_CERT, ADMIN_KEY)
else:
cert=None
if random.choice([0, 1]):
headers = {'Referer': base_url}
else:
headers = {}
url = urljoin(base_url, get_url_path(f"/edit/{id}", use_client_cert))
try:
r = session.get(url, headers=headers, timeout=TIMEOUT, verify=VERIFY, cert=cert)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during reading a contact: %s" % e, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during reading a contact: %s" % e, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None)
if r.status_code != 200:
return (MUMBLE, "Can't get contact", "Unexpected status code when reading a contact: '%d'" % r.status_code, None)
try:
parser = etree.HTMLParser()
parser.feed(r.text)
doc = parser.close()
except Exception as e:
return (MUMBLE, "Unexpected result", "Can't parse contact html: '%s'" % e, None)
contact_comment = doc.xpath("//textarea[@name='comment']/text()")
if len(contact_comment) != 1:
return (MUMBLE, "Unexpected result", "Can't find comment in '%s'" % r.text, None)
trace("Successfully got contact comment: '%s'" % contact_comment[0].strip())
return (OK, "", "", contact_comment[0].strip())
def switch_dark_mode(host, dark_mode):
base_url = f"https://{host}/"
session = requests.Session()
if not dark_mode is None:
if dark_mode:
trace("Set settings cookie to 'darkMode:1'")
session.cookies.set("settings", "darkMode:1")
else:
trace("Set settings cookie to 'darkMode:0'")
session.cookies.set("settings", "darkMode:0")
url = urljoin(base_url, '/')
trace("Going to '%s' and waiting for a redirect to '/login'" % url)
try:
r = session.get(url, timeout=TIMEOUT, verify=VERIFY)
except (requests.exceptions.ConnectionError, ConnectionRefusedError, http.client.RemoteDisconnected, socket.error) as e:
return (DOWN, "Connection error", "Connection error during requesting home page: %s" % e, None)
except requests.exceptions.Timeout as e:
return (DOWN, "Timeout", "Timeout during requesting home page: %s" % e, None)
if r.status_code == 502 or r.status_code == 504:
return (DOWN, "Connection error", "@andgein forced me to return DOWN for 502 Bad Gateway", None)
if r.status_code != 200:
return (MUMBLE, "Unexpected result", "Unexpected HTTP status code when requesting home page without session cookie: '%d'" % r.status_code, None)
if r.request.url != urljoin(base_url, '/login'):
return (MUMBLE, "Unexpected result", "Unexpected login url when requesting home page without session cookie: '%s'" % r.request.url, None)
return (OK, "", "", r.text)
def get_theme(html):
try:
parser = etree.HTMLParser()
parser.feed(html)
doc = parser.close()
except Exception as e:
return (MUMBLE, "Unexpected result", "Can't parse html: '%s'" % e, None)
theme = doc.xpath("/*/@data-bs-theme")
if len(theme) != 1:
return (MUMBLE, "Unexpected result", "Can't find 'data-bs-theme' attribute in '%s'" % r.text, None)
trace("Successfully got 'data-bs-theme' attribute: '%s'" % theme[0])
return (OK, "", "", theme[0])
return (OK, "", "", "")
def put(args):
if len(args) != 4:
verdict(CHECKER_ERROR, "Checker error", "Wrong args count for put()")
host, flag_id, flag, vuln = args
trace("put(%s, %s, %s, %s)" % (host, flag_id, flag, vuln))
user = get_random_string(5, 15)
password = get_random_string(7, 20)
if random.choice([0, 1]):
use_client_cert = True
else:
use_client_cert = False
(status, out, err, session, home_page_html) = register_user(host, user, password, use_client_cert)
if status != OK:
verdict(status, out, err)
(status, out, err, contact_id, name) = create_contact(host, session, flag, use_client_cert)
if status != OK:
verdict(status, out, err)
flag_id = json.dumps({"public_flag_id": contact_id, "name": name, "user": user, "password": password})
verdict(OK, flag_id)
def get(args):
if len(args) != 4:
verdict(CHECKER_ERROR, "Checker error", "Wrong args count for get()")
host, flag_id, flag_data, vuln = args
trace("get(%s, %s, %s, %s)" % (host, flag_id, flag_data, vuln))
info = json.loads(flag_id)
user = info['user']
password = info['password']
contact_id = info['public_flag_id']
name = info['name']
if random.choice([0, 1]):
user = get_random_string(5, 15)
password = get_random_string(7, 20)
(status, out, err, session, home_page_html) = register_user(host, user, password, use_client_cert=True)
if status != OK:
verdict(status, out, err)
(status, out, err, contact_id) = get_contact_id_by_name(home_page_html, name)
if status != OK:
verdict(status, out, err)
(status, out, err, contact_comment) = get_contact_comment(host, session, contact_id, use_client_cert=True)
if status != OK:
verdict(status, out, err)
else:
(status, out, err, session, home_page_html) = login_user(host, user, password)
if status != OK:
verdict(status, out, err)
(status, out, err, contact_id) = get_contact_id_by_name(home_page_html, name)
if status != OK:
verdict(status, out, err)
(status, out, err, contact_comment) = get_contact_comment(host, session, contact_id)
if status != OK:
verdict(status, out, err)
if contact_comment != flag_data:
verdict(CORRUPT, "Can't find flag in contact", "Can't find flag in contact comment, actual flag: '%s'" % contact_comment)
verdict(OK)
def check(args):
if len(args) != 1:
verdict(CHECKER_ERROR, "Checker error", "Wrong args count for check()")
host = args[0]
trace("check(%s)" % host)
choice = random.choice([0, 1, 2])
if choice == 0:
dark_mode = None
expected = 'light'
elif choice == 1:
dark_mode = True
expected = 'dark'
else:
dark_mode = False
expected = 'light'
(status, out, err, html) = switch_dark_mode(host, dark_mode)
if status != OK:
verdict(status, out, err)
(status, out, err, theme) = get_theme(html)
if status != OK:
verdict(status, out, err)
if theme != expected:
verdict(MUMBLE, 'Unexpected result', f"Unexpected theme '{theme}', expected '{expected}'")
verdict(OK)
def main(args):
if len(args) == 0:
verdict(CHECKER_ERROR, "Checker error", "No args")
try:
if args[0] == "info":
info()
elif args[0] == "check":
check(args[1:])
elif args[0] == "put":
put(args[1:])
elif args[0] == "get":
get(args[1:])
else:
verdict(CHECKER_ERROR, "Checker error", "Wrong action: " + args[0])
except Exception as e:
verdict(CHECKER_ERROR, "Checker error", "Exception: %s" % traceback.format_exc())
if __name__ == "__main__":
try:
main(sys.argv[1:])
verdict(CHECKER_ERROR, "Checker error", "No verdict")
except Exception as e:
verdict(CHECKER_ERROR, "Checker error", "Exception: %s" % e)