|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import random |
| 3 | +import re |
| 4 | +import string |
| 5 | +import sys |
| 6 | + |
| 7 | +import checklib |
| 8 | +import requests |
| 9 | +from checklib import * |
| 10 | +from checklib import status |
| 11 | + |
| 12 | +import docs_lib |
| 13 | + |
| 14 | +LEVEL_1_DOMAINS = [ |
| 15 | + ".AC", ".AD", ".AE", ".AERO", ".AF", ".AG", ".AI", ".AL", ".AM", ".AN", ".AO", ".AQ", ".AR", ".ARPA", ".AS", ".ASIA", |
| 16 | + ".AT", ".AU", ".AW", ".AX", ".AZ", ".BA", ".BB", ".BD", ".BE", ".BF", ".BG", ".BH", ".BI", ".BIZ", ".BJ", ".BL", ".BM", |
| 17 | + ".BN", ".BO", ".BR", ".BS", ".BT", ".BV", ".BW", ".BY", ".BZ", ".CA", ".CAT", ".CC", ".CD", ".CF", ".CG", ".CH", ".CI", |
| 18 | + ".CK", ".CL", ".CM", ".CN", ".CO", ".COM", ".COOP", ".CR", ".CU", ".CV", ".CX", ".CY", ".CZ", ".DE", ".DJ", ".DK", ".DM", |
| 19 | + ".DO", ".DZ", ".EC", ".EDU", ".EE", ".EG", ".EH", ".ER", ".ES", ".ET", ".EU", ".FI", ".FJ", ".FK", ".FM", ".FO", ".FR", |
| 20 | + ".GA", ".GB", ".GD", ".GE", ".GF", ".GG", ".GH", ".GI", ".GL", ".GM", ".GN", ".GOV", ".GP", ".GQ", ".GR", ".GS", ".GT", |
| 21 | + ".GU", ".GW", ".GY", ".HK", ".HM", ".HN", ".HR", ".HT", ".HU", ".ID", ".IE", ".IL", ".IM", ".IN", ".INFO", ".INT", ".IO", |
| 22 | + ".IQ", ".IR", ".IS", ".IT", ".JE", ".JM", ".JO", ".JOBS", ".JP", ".KE", ".KG", ".KH", ".KI", ".KM", ".KN", ".KP", ".KR", |
| 23 | + ".KW", ".KY", ".KZ", ".LA", ".LB", ".LC", ".LI", ".LK", ".LR", ".LS", ".LT", ".LU", ".LV", ".LY", ".MA", ".MC", ".MD", |
| 24 | + ".ME", ".MF", ".MG", ".MH", ".MIL", ".MK", ".ML", ".MM", ".MN", ".MO", ".MOBI", ".MP", ".MQ", ".MR", ".MS", ".MT", ".MU", |
| 25 | + ".MUSEUM", ".MV", ".MW", ".MX", ".MY", ".MZ", ".NA", ".NAME", ".NC", ".NE", ".NET", ".NF", ".NG", ".NI", ".NL", ".NO", |
| 26 | + ".NP", ".NR", ".NU", ".NZ", ".OM", ".ORG", ".PA", ".PE", ".PF", ".PG", ".PH", ".PK", ".PL", ".PM", ".PN", ".PR", ".PRO", |
| 27 | + ".PS", ".PT", ".PW", ".PY", ".QA", ".RE", ".RO", ".RS", ".RU", ".RW", ".SA", ".SB", ".SC", ".SD", ".SE", ".SG", ".SH", |
| 28 | + ".SI", ".SJ", ".SK", ".SL", ".SM", ".SN", ".SO", ".SR", ".ST", ".SU", ".SV", ".SY", ".SZ", ".TC", ".TD", ".TEL", ".TF", |
| 29 | + ".TG", ".TH", ".TJ", ".TK", ".TL", ".TM", ".TN", ".TO", ".TP", ".TR", ".TRAVEL", ".TT", ".TV", ".TW", ".TZ", ".UA", ".UG", |
| 30 | + ".UK", ".UM", ".US", ".UY", ".UZ", ".VA", ".VC", ".VE", ".VG", ".VI", ".VN", ".VU", ".WF", ".WS" |
| 31 | +] |
| 32 | + |
| 33 | + |
| 34 | +class Checker(BaseChecker): |
| 35 | + vulns: int = 1 |
| 36 | + timeout: int = 15 |
| 37 | + uses_attack_data: bool = True |
| 38 | + |
| 39 | + def __init__(self, *args, **kwargs): |
| 40 | + super(Checker, self).__init__(*args, **kwargs) |
| 41 | + self.lib = docs_lib.DocsLib(self) |
| 42 | + self.token_regexp = re.compile(r'^[0-9A-Za-z]{1,80}$') |
| 43 | + |
| 44 | + def get_random_org(self): |
| 45 | + l = rnd_string(10, alphabet=string.ascii_lowercase) |
| 46 | + r = random.choice(LEVEL_1_DOMAINS) |
| 47 | + return f"{l}{r}".lower() |
| 48 | + |
| 49 | + def action(self, action, *args, **kwargs): |
| 50 | + try: |
| 51 | + super(Checker, self).action(action, *args, **kwargs) |
| 52 | + except requests.exceptions.ConnectionError: |
| 53 | + self.cquit(Status.DOWN, 'Connection error', 'Got requests connection error') |
| 54 | + |
| 55 | + def check(self): |
| 56 | + session = checklib.get_initialized_session() |
| 57 | + org = self.get_random_org() |
| 58 | + |
| 59 | + response = self.lib.create_org(session, org) |
| 60 | + token = response.get('token') |
| 61 | + org_id = response.get('id') |
| 62 | + |
| 63 | + self.assert_eq(bool(self.token_regexp.fullmatch(token)), True, 'Invalid token format') |
| 64 | + |
| 65 | + u, p = rnd_username(), rnd_password() |
| 66 | + u1, p1 = rnd_username(), rnd_password() |
| 67 | + self.lib.create_user(session, u, p, token) |
| 68 | + u = f'{u}@{org}' |
| 69 | + |
| 70 | + session = self.lib.login(session, u, p) |
| 71 | + |
| 72 | + title = rnd_string(10) |
| 73 | + content = rnd_string(10) |
| 74 | + |
| 75 | + got_doc = self.lib.create_doc(session, title, content) |
| 76 | + got_doc = self.lib.get_doc(session, got_doc.get('id')) |
| 77 | + |
| 78 | + self.lib.create_user(session, u1, p1, token) |
| 79 | + u1 = f'{u1}@{org}' |
| 80 | + |
| 81 | + session_alter = checklib.get_initialized_session() |
| 82 | + self.lib.login(session_alter, u1, p1) |
| 83 | + |
| 84 | + got_alter_doc = self.lib.get_doc(session_alter, got_doc.get('id')) |
| 85 | + self.assert_eq(got_alter_doc.get('title'), title, 'Failed to get document') |
| 86 | + self.assert_eq(got_alter_doc.get('content'), content, 'Failed to get document') |
| 87 | + |
| 88 | + new_title = rnd_string(10) |
| 89 | + self.lib.update_doc(session, got_doc.get('id'), title=new_title) |
| 90 | + |
| 91 | + got_updated_doc = self.lib.get_doc(session, got_doc.get('id')) |
| 92 | + self.assert_eq(got_updated_doc.get('title'), new_title, 'Failed to update document') |
| 93 | + self.assert_eq(got_updated_doc.get('content'), content, 'Failed to update document') |
| 94 | + |
| 95 | + search_results = self.lib.search(session_alter, new_title) |
| 96 | + self.assert_in(got_updated_doc.get('id'), [x.get('id') for x in search_results], 'Failed to search document') |
| 97 | + self.assert_in(got_updated_doc.get('title'), [x.get('title') for x in search_results], |
| 98 | + 'Failed to search document') |
| 99 | + self.assert_in(got_updated_doc.get('content'), [x.get('content') for x in search_results], |
| 100 | + 'Failed to search document') |
| 101 | + |
| 102 | + self.cquit(Status.OK) |
| 103 | + |
| 104 | + def put(self, flag_id: str, flag: str, vuln: str): |
| 105 | + session = checklib.get_initialized_session() |
| 106 | + org_id = self.get_random_org() |
| 107 | + |
| 108 | + response = self.lib.create_org(session, org_id) |
| 109 | + token = response.get('token') |
| 110 | + |
| 111 | + self.assert_eq(bool(self.token_regexp.fullmatch(token)), True, 'Invalid token format') |
| 112 | + |
| 113 | + u, p = rnd_username(), rnd_password() |
| 114 | + self.lib.create_user(session, u, p, token) |
| 115 | + |
| 116 | + sess = checklib.get_initialized_session() |
| 117 | + u = f'{u}@{org_id}' |
| 118 | + self.lib.login(sess, u, p) |
| 119 | + title = checklib.rnd_string(10) |
| 120 | + created_doc = self.lib.create_doc(sess, title, flag) |
| 121 | + |
| 122 | + doc_id = created_doc.get('id') |
| 123 | + self.assert_eq(bool(self.token_regexp.fullmatch(doc_id)), True, 'Invalid docid format') |
| 124 | + |
| 125 | + self.cquit(Status.OK, doc_id, f"{token}:{u}:{p}:{doc_id}") |
| 126 | + |
| 127 | + def get(self, flag_id: str, flag: str, vuln: str): |
| 128 | + token, u, p, doc_id = flag_id.split(':') |
| 129 | + sess = checklib.get_initialized_session() |
| 130 | + self.lib.login(sess, u, p, status=status.Status.CORRUPT) |
| 131 | + doc = self.lib.get_doc(sess, doc_id, status=status.Status.CORRUPT) |
| 132 | + self.assert_eq(doc.get('content'), flag, 'Invalid content', status=status.Status.CORRUPT) |
| 133 | + |
| 134 | + sess = checklib.get_initialized_session() |
| 135 | + u1, p1 = rnd_username(), rnd_password() |
| 136 | + created_user = self.lib.create_user(sess, u1, p1, token) |
| 137 | + |
| 138 | + sess = checklib.get_initialized_session() |
| 139 | + self.lib.login(sess, created_user.get('email'), created_user.get('password')) |
| 140 | + |
| 141 | + self.lib.search(sess, '', status=status.Status.CORRUPT) |
| 142 | + |
| 143 | + self.cquit(Status.OK) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + c = Checker(sys.argv[2]) |
| 148 | + |
| 149 | + try: |
| 150 | + c.action(sys.argv[1], *sys.argv[3:]) |
| 151 | + except c.get_check_finished_exception() as e: |
| 152 | + cquit(status.Status(c.status), c.public, c.private) |
0 commit comments