generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecker.py
executable file
·191 lines (146 loc) · 7.86 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
#!/usr/bin/env python3
import sys
import os
import random
import tempfile
from modelrna_lib import ModelrnaLib
from model_helper import ModelHelper, DatasetBuilder
from checklib import status, BaseChecker, get_initialized_session, rnd_username, rnd_password, rnd_string, cquit
import requests
import re
class Checker(BaseChecker):
vulns: int = 1
timeout: int = 15
uses_attack_data: bool = True
# Amazing vaccine names.
vaccines_prefixes = ['Astrakek-', 'Zenekeka-', 'Pfizer-BioKek-', 'Sputnikek-', 'Sinokek-', '', '']
requests_agents = ['python-requests/2.{}.0'.format(x) for x in range(15, 28)]
def __init__(self, *args, **kwargs):
super(Checker, self).__init__(*args, **kwargs)
self.mlib = ModelrnaLib(self)
self.model_helper = ModelHelper()
self.uuid_regexp = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$')
def session_with_requests(self):
sess = get_initialized_session()
if random.randint(0, 1) == 1:
sess.headers['User-Agent'] = random.choice(self.requests_agents)
return sess
def random_vaccine_name(self):
vaccine_name = random.choice(self.vaccines_prefixes) + rnd_string(3)
if len(vaccine_name) < 5:
vaccine_name += rnd_string(3)
return vaccine_name
def build_model(self, model_path, ds_builder, known_row=None, vaccine_name=None, company_name=None):
if known_row is None:
known_row = ds_builder.random_row()
known_prediction, known_prob = None, None
if random.randint(0, 1) == 0:
# Create cb model.
X, y = ds_builder.build()
cbm = self.model_helper.build_cb_model(X, y)
known_prediction = cbm.predict([known_row, ])[0]
known_prob = cbm.predict_proba([known_row, ])[0]
else:
# Create onnx-native model.
pos_index = random.randint(0, len(known_row) - 1)
value = known_row[pos_index] - 1
self.model_helper.build_onnx_model(pos=pos_index, thresh=value, graph_name=vaccine_name,
company_name=company_name)
known_prediction = True
self.model_helper.save(model_path)
return known_prediction, known_prob
def action(self, action, *args, **kwargs):
try:
super(Checker, self).action(action, *args, **kwargs)
except requests.exceptions.ConnectionError:
self.cquit(status.Status.DOWN, 'Connection error', 'Got requests connection error')
def validate_uuid(self, uuid):
self.assert_eq(bool(self.uuid_regexp.fullmatch(uuid)), True, 'Invalid id format')
def check(self):
session = self.session_with_requests()
username, password, vaccine_name = rnd_username(), rnd_password(), self.random_vaccine_name()
user_info = self.mlib.register(session, username, password, vaccine_name)
user_id = user_info['user_id']
self.validate_uuid(user_id)
# Just check that latest_users return valid format.
self.mlib.list_latest_users(session)
user_info = self.mlib.get_user_info(session)
self.assert_eq(user_info['username'], username, 'Invalid data provided on /userinfo')
self.assert_eq(user_info['user_id'], user_id, 'Invalid data provided on /userinfo')
vaccine_info = user_info['vaccine_info']
_, model_path = tempfile.mkstemp(suffix='.onnx')
ds_builder = DatasetBuilder().random_rows(20)
known_row = ds_builder.random_row()
known_prediction = ds_builder.random_prediction()
known_prob = None
ds_builder.add_row(known_row, known_prediction)
known_prediction, known_prob = self.build_model(model_path, ds_builder, known_row=known_row,
vaccine_name=vaccine_name, company_name=username)
model_data = open(model_path, 'rb').read()
# Remove tmp file.
os.unlink(model_path)
self.mlib.upload_model(session, model_data, vaccine_info['vaccine_key'])
session.close()
# Create different user and try to predict value for known row.
predict_sess = self.session_with_requests()
ssn = rnd_string(32)
data_to_send = ds_builder.make_dict(known_row)
data_to_send['ssn'] = ssn
prediction_response = self.mlib.end_to_end_prediction(predict_sess, vaccine_id=vaccine_info['vaccine_id'],
data=data_to_send)
self.assert_eq(prediction_response['prediction'], known_prediction,
'Invalid model prediction on vaccine "{}"'.format(vaccine_info['vaccine_id']))
test_id = prediction_response['test_id']
self.validate_uuid(test_id)
test_response = self.mlib.get_test_result(predict_sess, test_id)
self.assert_eq(ssn, test_response['ssn'], "Invalid ssn returned on /vaccine/test/<test_id>")
self.assert_eq(test_response['prediction'], known_prediction,
'Invalid model prediction on vaccine "{}"'.format(vaccine_info['vaccine_id']))
# Get 'ssn' as vaccine owner.
session = self.get_initialized_session()
self.mlib.login(session, username, password)
tests_data = self.mlib.get_tests_for_vaccine(session)
self.assert_in(ssn, [x['ssn'] for x in tests_data], 'Invalid ssn returned on /vaccine/tests')
self.cquit(status.Status.OK)
def put(self, flag_id: str, flag: str, vuln: str):
session = self.session_with_requests()
username, password, vaccine_name = rnd_username(), rnd_password(), self.random_vaccine_name()
user_info = self.mlib.register(session, username, password, vaccine_name)
user_id = user_info['user_id']
self.validate_uuid(user_id)
user_info = self.mlib.get_user_info(session)
self.assert_eq(user_info['username'], username, 'Invalid data provided on /userinfo')
self.assert_eq(user_info['user_id'], user_id, 'Invalid data provided on /userinfo')
vaccine_info = user_info['vaccine_info']
_, model_path = tempfile.mkstemp(suffix='.onnx')
self.build_model(model_path, DatasetBuilder().random_rows(10), known_row=None,
vaccine_name=vaccine_name, company_name=username)
model_data = open(model_path, 'rb').read()
# Remove tmp file.
os.unlink(model_path)
self.mlib.upload_model(session, model_data, vaccine_info['vaccine_key'])
predict_sess = self.session_with_requests()
data_to_send = DatasetBuilder.make_dict(DatasetBuilder.random_row())
data_to_send['ssn'] = flag
prediction_response = self.mlib.end_to_end_prediction(predict_sess, vaccine_id=vaccine_info['vaccine_id'],
data=data_to_send)
test_id = prediction_response['test_id']
self.validate_uuid(test_id)
self.cquit(status.Status.OK, user_id, f'{username}:{password}:{test_id}')
def get(self, flag_id: str, flag: str, vuln: str):
s = self.session_with_requests()
username, password, test_id = flag_id.split(':')
self.mlib.login(s, username, password)
tests_data = self.mlib.get_tests_for_vaccine(s)
self.assert_in(flag, [x['ssn'] for x in tests_data], 'Invalid ssn returned on /vaccine/tests',
status=status.Status.CORRUPT)
new_session = self.session_with_requests()
test_response = self.mlib.get_test_result(new_session, test_id)
self.assert_eq(flag, test_response['ssn'], "Invalid ssn returned on /vaccine/test/<test_id>")
self.cquit(status.Status.OK)
if __name__ == '__main__':
c = Checker(sys.argv[2])
try:
c.action(sys.argv[1], *sys.argv[3:])
except c.get_check_finished_exception() as e:
cquit(status.Status(c.status), c.public, c.private)