generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.py
executable file
·181 lines (140 loc) · 6.6 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
#!/usr/bin/env python3
import json
import random
import sys
if True:
saved_args = sys.argv.copy()
from ark_lib import *
from checklib import * # type: ignore
from pwn import PwnlibException
class Checker(BaseChecker):
vulns: int = 1
timeout: int = 20
uses_attack_data: bool = True
def __init__(self, *args, **kwargs):
super(Checker, self).__init__(*args, **kwargs)
self.mch = CheckMachine(self)
def check(self):
# Register a user
username, password = rnd_username(), rnd_password()
with (
self.mch.connect() as r1,
self.mch.connect() as r2,
self.mch.connect() as anon_r,
):
def gr():
return random.choice([r1, r2])
def ar():
return random.choice([r1, anon_r])
self.mch.register(gr(), username, password)
self.mch.login(r1, username, password)
self.mch.login(r2, username, password)
files = [
(self.mch.random_filename(), rnd_string(random.randint(10, 50)))
for _ in range(random.randint(1, 10))
]
for file, content in files:
self.mch.save_file(gr(), file, content)
random.shuffle(files)
for file, content in files:
got_content = self.mch.cat_file(gr(), file, Status.MUMBLE)
self.assert_eq(got_content, content, "File content mismatch")
# Test list functionality
list_output = self.mch.list_user_files(gr(), '')
for file, _ in files:
self.assert_in(file, [f.path for f in list_output],
"File not found in list")
# Test suggest_users
suggest_output = self.mch.suggest_users(gr())
if len(suggest_output) < 30 and not any(u.username == username for u in suggest_output):
self.cquit(Status.MUMBLE, "Suggest users failed")
# Test list_user_files
user_files = self.mch.list_user_files(ar(), username)
for file, content in files:
self.assert_in(file, [f.path for f in user_files],
"File not found in user files")
self.assert_in(len(content), [f.size for f in user_files],
"File size mismatch")
# Register a second user
# Test second user can copy first user's files
# Test first user can cat their own files from the second user's account
username2, password2 = rnd_username(), rnd_password()
self.mch.register(ar(), username2, password2)
self.mch.login(r2, username2, password2)
# Test list_user_files
user_files = self.mch.list_user_files(ar(), username)
for file, content in files:
self.assert_in(file, [f.path for f in user_files],
"File not found in user files")
self.assert_in(len(content), [f.size for f in user_files],
"File size mismatch")
file_to_copy = random.choice(files)
dst_path = self.mch.random_filename()
self.mch.copy_file(r2, file_to_copy[0], dst_path)
# Test second user can list their own files
user_files = self.mch.list_user_files(r2, username2)
self.assert_in(dst_path, [f.path for f in user_files],
"File not found in user files")
# Test first user can cat their own files from the second user's quota
content = self.mch.cat_file(r1, dst_path, Status.MUMBLE)
self.assert_eq(content, file_to_copy[1], "File content mismatch")
self.cquit(Status.OK)
def put(self, flag_id: str, flag: str, vuln: str):
username1, password1 = rnd_username(), rnd_password()
username2, password2 = rnd_username(), rnd_password()
with (
self.mch.connect() as r1,
self.mch.connect() as r2,
):
self.mch.register(r1, username1, password1)
self.mch.register(r2, username2, password2)
self.mch.login(r1, username1, password1)
self.mch.login(r2, username2, password2)
flag_file1 = self.mch.random_filename()
flag_file2 = self.mch.random_filename()
self.mch.save_file(r1, flag_file1, flag)
self.mch.copy_file(r2, flag_file1, flag_file2)
public = f'u1={username1}:u2={username2}'
private = json.dumps({
'username1': username1,
'password1': password1,
'username2': username2,
'password2': password2,
'flag_file1': flag_file1,
'flag_file2': flag_file2,
})
self.cquit(Status.OK, public=public, private=private)
def get(self, flag_id: str, flag: str, vuln: str):
data = json.loads(flag_id)
with (
self.mch.connect() as r1,
self.mch.connect() as r2,
self.mch.connect() as anon_r,
):
self.mch.login(r1, data['username1'],
data['password1'], Status.CORRUPT)
self.mch.login(r2, data['username2'],
data['password2'], Status.CORRUPT)
for r in [r1, r2, anon_r]:
file_list = self.mch.list_user_files(r, data['username1'])
self.assert_in(data['flag_file1'], [f.path for f in file_list],
"Flag file not found in user files", Status.CORRUPT)
file_list = self.mch.list_user_files(r, data['username2'])
self.assert_in(data['flag_file2'], [f.path for f in file_list],
"Flag file not found in user files", Status.CORRUPT)
content = self.mch.cat_file(r1, data['flag_file1'], Status.CORRUPT)
self.assert_eq(content, flag, "Flag mismatch", Status.CORRUPT)
content = self.mch.cat_file(r1, data['flag_file2'], Status.CORRUPT)
self.assert_eq(content, flag, "Flag mismatch", Status.CORRUPT)
self.cquit(Status.OK)
def action(self, action, *args, **kwargs):
try:
super(Checker, self).action(action, *args, **kwargs)
except PwnlibException:
self.cquit(Status.DOWN, 'Connection error', 'Got pwnlib exception')
if __name__ == '__main__':
c = Checker(saved_args[2])
try:
c.action(saved_args[1], *saved_args[3:])
except c.get_check_finished_exception():
cquit(Status(c.status), c.public, c.private)