-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchecker.py
executable file
·266 lines (196 loc) · 6.54 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
import sys
import socket
import hashlib
import random
import time
import json
import traceback
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import requests
OK, CORRUPT, MUMBLE, DOWN, CHECKER_ERROR = 101, 102, 103, 104, 110
PORT = 443
TIMEOUT = 15
ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
def gen_rand_string(l=12):
return "".join(random.choice(ABC) for i in range(l))
def verdict(exit_code, public="", private=""):
if public:
print(public)
if private:
print(private, file=sys.stderr)
sys.exit(exit_code)
def info():
verdict(OK, "vulns: 1\npublic_flag_description: Flag ID is the user's login, flag is somewhere near\n")
def call_api(s, ip, data):
return s.post(f"https://{ip}:{PORT}/api/graphql", json=data, verify=False).json().get("data", {})
def gen_login():
ABC = "abcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*(){}'<>ABCDEFGHIJKLMNOPQRSTUVWXYZ"
name = "".join(random.choice(ABC) for i in range(random.randrange(6, 10)))
if random.random() < 0.01:
return name + ";select id,flag from users"
if random.random() < 0.01:
return name + "' union select id,flag from users -- "
if random.random() < 0.01:
return name + f"; union select get_flag(id, {random.randrange(1, 100)}) -- "
if random.random() < 0.01:
return name + f";select getFlag(id, {random.randrange(1, 100)}) "
return name
def gen_password():
return "".join(random.choice(ABC) for i in range(16))
def gen_flag():
return "".join(random.choice(ABC) for i in range(8))
def create_user(s, host, login, password, flag):
data = {
"variables": {
"data": {
"login": login,
"flag": flag,
"password": password
}
},
"query": CREATE_USER_MUTATION
}
resp = call_api(s, host, data=data)
return resp.get("item", {}).get("label", "")
LIST_USER_QUERY = """query ($where: UserWhereInput, $take: Int!, $skip: Int!, $orderBy: [UserOrderByInput!]) {
items: users(where: $where, take: $take, skip: $skip, orderBy: $orderBy) {
id
login
flag
__typename
}
count: usersCount(where: $where)
}"""
def check(host):
login = gen_login()
password = gen_password()
flag = gen_flag()
s = requests.session()
created_login = create_user(s, host, login, password, flag)
if not created_login:
verdict(MUMBLE, "Failed to register a new user", "Failed to register: %s %s" % (login, password))
data = {
"variables": {
"where": {
"OR": []
},
"take": 50,
"skip": 0,
"orderBy": [{
"createdAt": "desc"
}]
},
"query": LIST_USER_QUERY
}
resp = call_api(s, host, data=data)
items = resp.get("items", [])
logins = [item.get("login", "") for item in items]
if created_login not in logins:
verdict(MUMBLE, "User list is broken", "User list is broken: %s %s" % (login, password))
verdict(OK)
CREATE_USER_MUTATION = """mutation ($data: UserCreateInput!) {
item: createUser(data: $data) {
id
label: login
__typename
}
}"""
def put(host, flag_id, flag, vuln):
login = gen_login()
password = gen_password()
s = requests.session()
created_login = create_user(s, host, login, password, flag)
if created_login != login:
verdict(MUMBLE, "Failed to register a new user", "Failed to register: %s %s" % (login, password))
verdict(OK, json.dumps({"public_flag_id": login, "login": login, "password": password}))
AUTHENTICATE_MUTATION = """mutation ($identity: String!, $secret: String!) {
authenticate: authenticateUserWithPassword(login: $identity, password: $secret) {
... on UserAuthenticationWithPasswordSuccess {
item {
id
__typename
}
__typename
}
... on UserAuthenticationWithPasswordFailure {
message
__typename
}
__typename
}
}"""
ITEM_QUERY = """query ItemPage($id: ID!) {
item: user(where: {id: $id}) {
id
login
flag
password {
isSet
__typename
}
createdAt
__typename
}
}
"""
def get(host, flag_id, flag, vuln):
s = requests.session()
try:
info = json.loads(flag_id)
login = info["login"]
password = info["password"]
except Exception:
verdict(CHECKER_ERROR, "Bad flag id", "Bad flag_id: %s" % traceback.format_exc())
data = {
"variables": {
"identity": login,
"secret": password
}, "query": AUTHENTICATE_MUTATION
}
resp = call_api(s, host, data=data)
user_id = resp.get("authenticate", {}).get("item", {}).get("id", "")
if not user_id:
verdict(CORRUPT, "Failed to login user", "Failed to login: %s %s" % (login, password))
data = {
"operationName": "ItemPage",
"variables": {"id": user_id},
"query": ITEM_QUERY
}
resp = call_api(s, host, data=data)
stored_flag = resp.get("item", {}).get("flag", "")
if stored_flag != flag:
verdict(CORRUPT, "No such flags",
"No such flag %s, stored_flag %d" % (flag, stored_flag))
verdict(OK, flag_id)
def main(args):
CMD_MAPPING = {
"info": (info, 0),
"check": (check, 1),
"put": (put, 4),
"get": (get, 4),
}
if not args:
verdict(CHECKER_ERROR, "No args", "No args")
cmd, args = args[0], args[1:]
if cmd not in CMD_MAPPING:
verdict(CHECKER_ERROR, "Checker error", "Wrong command %s" % cmd)
handler, args_count = CMD_MAPPING[cmd]
if len(args) != args_count:
verdict(CHECKER_ERROR, "Checker error", "Wrong args count for %s" % cmd)
try:
handler(*args)
except ConnectionRefusedError as E:
verdict(DOWN, "Connect refused", "Connect refused: %s" % E)
except ConnectionError as E:
verdict(MUMBLE, "Connection aborted", "Connection aborted: %s" % E)
except AttributeError as E:
verdict(MUMBLE, "Bad data format", "Bad data format: %s" % E)
except OSError as E:
verdict(DOWN, "Connect error", "Connect error: %s" % E)
except Exception as E:
verdict(CHECKER_ERROR, "Checker error", "Checker error: %s" % traceback.format_exc())
verdict(CHECKER_ERROR, "Checker error", "No verdict")
if __name__ == "__main__":
main(args=sys.argv[1:])