|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import random |
| 5 | +import sys |
| 6 | + |
| 7 | +if True: |
| 8 | + saved_args = sys.argv.copy() |
| 9 | + |
| 10 | +from ark_lib import * |
| 11 | +from checklib import * # type: ignore |
| 12 | +from pwn import PwnlibException |
| 13 | + |
| 14 | + |
| 15 | +class Checker(BaseChecker): |
| 16 | + vulns: int = 1 |
| 17 | + timeout: int = 20 |
| 18 | + uses_attack_data: bool = True |
| 19 | + |
| 20 | + def __init__(self, *args, **kwargs): |
| 21 | + super(Checker, self).__init__(*args, **kwargs) |
| 22 | + self.mch = CheckMachine(self) |
| 23 | + |
| 24 | + def check(self): |
| 25 | + # Register a user |
| 26 | + username, password = rnd_username(), rnd_password() |
| 27 | + with ( |
| 28 | + self.mch.connect() as r1, |
| 29 | + self.mch.connect() as r2, |
| 30 | + self.mch.connect() as anon_r, |
| 31 | + ): |
| 32 | + def gr(): |
| 33 | + return random.choice([r1, r2]) |
| 34 | + |
| 35 | + def ar(): |
| 36 | + return random.choice([r1, anon_r]) |
| 37 | + |
| 38 | + self.mch.register(gr(), username, password) |
| 39 | + |
| 40 | + self.mch.login(r1, username, password) |
| 41 | + self.mch.login(r2, username, password) |
| 42 | + |
| 43 | + files = [ |
| 44 | + (self.mch.random_filename(), rnd_string(random.randint(10, 50))) |
| 45 | + for _ in range(random.randint(1, 10)) |
| 46 | + ] |
| 47 | + for file, content in files: |
| 48 | + self.mch.save_file(gr(), file, content) |
| 49 | + |
| 50 | + random.shuffle(files) |
| 51 | + for file, content in files: |
| 52 | + got_content = self.mch.cat_file(gr(), file, Status.MUMBLE) |
| 53 | + self.assert_eq(got_content, content, "File content mismatch") |
| 54 | + |
| 55 | + # Test list functionality |
| 56 | + list_output = self.mch.list_user_files(gr(), '') |
| 57 | + for file, _ in files: |
| 58 | + self.assert_in(file, [f.path for f in list_output], |
| 59 | + "File not found in list") |
| 60 | + |
| 61 | + # Test suggest_users |
| 62 | + suggest_output = self.mch.suggest_users(gr()) |
| 63 | + if len(suggest_output) < 30 and not any(u.username == username for u in suggest_output): |
| 64 | + self.cquit(Status.MUMBLE, "Suggest users failed") |
| 65 | + |
| 66 | + # Test list_user_files |
| 67 | + user_files = self.mch.list_user_files(ar(), username) |
| 68 | + for file, content in files: |
| 69 | + self.assert_in(file, [f.path for f in user_files], |
| 70 | + "File not found in user files") |
| 71 | + self.assert_in(len(content), [f.size for f in user_files], |
| 72 | + "File size mismatch") |
| 73 | + |
| 74 | + # Register a second user |
| 75 | + # Test second user can copy first user's files |
| 76 | + # Test first user can cat their own files from the second user's account |
| 77 | + |
| 78 | + username2, password2 = rnd_username(), rnd_password() |
| 79 | + self.mch.register(ar(), username2, password2) |
| 80 | + self.mch.login(r2, username2, password2) |
| 81 | + |
| 82 | + # Test list_user_files |
| 83 | + user_files = self.mch.list_user_files(ar(), username) |
| 84 | + for file, content in files: |
| 85 | + self.assert_in(file, [f.path for f in user_files], |
| 86 | + "File not found in user files") |
| 87 | + self.assert_in(len(content), [f.size for f in user_files], |
| 88 | + "File size mismatch") |
| 89 | + |
| 90 | + file_to_copy = random.choice(files) |
| 91 | + dst_path = self.mch.random_filename() |
| 92 | + self.mch.copy_file(r2, file_to_copy[0], dst_path) |
| 93 | + |
| 94 | + # Test second user can list their own files |
| 95 | + user_files = self.mch.list_user_files(r2, username2) |
| 96 | + self.assert_in(dst_path, [f.path for f in user_files], |
| 97 | + "File not found in user files") |
| 98 | + |
| 99 | + # Test first user can cat their own files from the second user's quota |
| 100 | + content = self.mch.cat_file(r1, dst_path, Status.MUMBLE) |
| 101 | + self.assert_eq(content, file_to_copy[1], "File content mismatch") |
| 102 | + |
| 103 | + self.cquit(Status.OK) |
| 104 | + |
| 105 | + def put(self, flag_id: str, flag: str, vuln: str): |
| 106 | + username1, password1 = rnd_username(), rnd_password() |
| 107 | + username2, password2 = rnd_username(), rnd_password() |
| 108 | + |
| 109 | + with ( |
| 110 | + self.mch.connect() as r1, |
| 111 | + self.mch.connect() as r2, |
| 112 | + ): |
| 113 | + self.mch.register(r1, username1, password1) |
| 114 | + self.mch.register(r2, username2, password2) |
| 115 | + |
| 116 | + self.mch.login(r1, username1, password1) |
| 117 | + self.mch.login(r2, username2, password2) |
| 118 | + |
| 119 | + flag_file1 = self.mch.random_filename() |
| 120 | + flag_file2 = self.mch.random_filename() |
| 121 | + |
| 122 | + self.mch.save_file(r1, flag_file1, flag) |
| 123 | + self.mch.copy_file(r2, flag_file1, flag_file2) |
| 124 | + |
| 125 | + public = f'u1={username1}:u2={username2}' |
| 126 | + |
| 127 | + private = json.dumps({ |
| 128 | + 'username1': username1, |
| 129 | + 'password1': password1, |
| 130 | + 'username2': username2, |
| 131 | + 'password2': password2, |
| 132 | + 'flag_file1': flag_file1, |
| 133 | + 'flag_file2': flag_file2, |
| 134 | + }) |
| 135 | + |
| 136 | + self.cquit(Status.OK, public=public, private=private) |
| 137 | + |
| 138 | + def get(self, flag_id: str, flag: str, vuln: str): |
| 139 | + data = json.loads(flag_id) |
| 140 | + |
| 141 | + with ( |
| 142 | + self.mch.connect() as r1, |
| 143 | + self.mch.connect() as r2, |
| 144 | + self.mch.connect() as anon_r, |
| 145 | + ): |
| 146 | + self.mch.login(r1, data['username1'], |
| 147 | + data['password1'], Status.CORRUPT) |
| 148 | + self.mch.login(r2, data['username2'], |
| 149 | + data['password2'], Status.CORRUPT) |
| 150 | + |
| 151 | + for r in [r1, r2, anon_r]: |
| 152 | + file_list = self.mch.list_user_files(r, data['username1']) |
| 153 | + self.assert_in(data['flag_file1'], [f.path for f in file_list], |
| 154 | + "Flag file not found in user files", Status.CORRUPT) |
| 155 | + |
| 156 | + file_list = self.mch.list_user_files(r, data['username2']) |
| 157 | + self.assert_in(data['flag_file2'], [f.path for f in file_list], |
| 158 | + "Flag file not found in user files", Status.CORRUPT) |
| 159 | + |
| 160 | + content = self.mch.cat_file(r1, data['flag_file1'], Status.CORRUPT) |
| 161 | + self.assert_eq(content, flag, "Flag mismatch", Status.CORRUPT) |
| 162 | + |
| 163 | + content = self.mch.cat_file(r1, data['flag_file2'], Status.CORRUPT) |
| 164 | + self.assert_eq(content, flag, "Flag mismatch", Status.CORRUPT) |
| 165 | + |
| 166 | + self.cquit(Status.OK) |
| 167 | + |
| 168 | + def action(self, action, *args, **kwargs): |
| 169 | + try: |
| 170 | + super(Checker, self).action(action, *args, **kwargs) |
| 171 | + except PwnlibException: |
| 172 | + self.cquit(Status.DOWN, 'Connection error', 'Got pwnlib exception') |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + c = Checker(saved_args[2]) |
| 177 | + |
| 178 | + try: |
| 179 | + c.action(saved_args[1], *saved_args[3:]) |
| 180 | + except c.get_check_finished_exception(): |
| 181 | + cquit(Status(c.status), c.public, c.private) |
0 commit comments