generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.py
executable file
·118 lines (85 loc) · 4.14 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
#!/usr/bin/env python3
import random
import secrets
import sys
import uuid
import json
import grpc
from checklib import *
from oilmarket_lib import CheckMachine
class Checker(BaseChecker):
vulns: int = 1
timeout: int = 15
uses_attack_data: bool = True
def __init__(self, *args, **kwargs):
super(Checker, self).__init__(*args, **kwargs)
self.c = CheckMachine(self)
def action(self, action, *args, **kwargs):
try:
super(Checker, self).action(action, *args, **kwargs)
except self.get_check_finished_exception():
raise
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.UNAVAILABLE:
self.cquit(Status.DOWN, "unavailable", f"grpc error: {e}")
else:
self.cquit(Status.MUMBLE, f"grpc error: {e.code()}", f"grpc error: {e}")
except ConnectionRefusedError:
self.cquit(Status.DOWN, "Connection refused", "Connection refused")
def check(self):
with self.c.connect() as channel, self.c.handle_grpc_error(status=Status.MUMBLE):
stub = self.c.get_stub(channel)
attesters = [rnd_username() for _ in range(random.randint(2, 7))]
attester_api_keys = [(attester, self.c.create_attester(stub, attester)) for attester in attesters]
buyer = rnd_username()
fake_flag = rnd_string(32)
buyer_api_key = self.c.create_buyer(stub, buyer, fake_flag, attesters)
seller = rnd_username()
seller_api_key = self.c.create_seller(stub, seller)
barrel_ids = [self.c.add_barrel(stub, seller_api_key) for _ in range(2, 7)]
random.shuffle(barrel_ids)
for barrel_id in barrel_ids:
attester, attester_api_key = random.choice(attester_api_keys)
request = json.dumps({"barrel_id": barrel_id}).encode()
signature = self.c.sign(stub, attester_api_key, request)
service_flag = self.c.sell(stub, seller_api_key, buyer, attester, request, signature)
self.assert_eq(service_flag, fake_flag, "incorrect data on sell", Status.MUMBLE)
self.cquit(Status.OK)
def put(self, flag_id: str, flag: str, vuln: str):
with self.c.connect() as channel, self.c.handle_grpc_error(status=Status.MUMBLE):
stub = self.c.get_stub(channel)
attester = rnd_username()
attester_api_key = self.c.create_attester(stub, attester)
buyer = rnd_username()
buyer_api_key = self.c.create_buyer(stub, buyer, flag, [attester])
self.cquit(Status.OK,
json.dumps({
"buyer": buyer,
"attester": attester
}),
json.dumps({
"buyer": {"name" : buyer, "api_key": buyer_api_key},
"attester": {"name": attester, "api_key": attester_api_key},
}),
)
def get(self, flag_id: str, flag: str, vuln: str):
with self.c.connect() as channel, self.c.handle_grpc_error(status=Status.CORRUPT):
stub = self.c.get_stub(channel)
flag_data = json.loads(flag_id)
buyer = flag_data["buyer"]["name"]
attester = flag_data["attester"]["name"]
attester_api_key = flag_data["attester"]["api_key"]
seller = rnd_username()
seller_api_key = self.c.create_seller(stub, seller)
barrel_id = self.c.add_barrel(stub, seller_api_key)
request = json.dumps({"barrel_id": barrel_id}).encode("ascii")
signature = self.c.sign(stub, attester_api_key, request)
service_flag = self.c.sell(stub, seller_api_key, buyer, attester, request, signature)
self.assert_eq(service_flag, flag, "incorrect flag", Status.CORRUPT)
self.cquit(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():
cquit(Status(c.status), c.public, c.private)