-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgraphCrawler.py
478 lines (437 loc) · 15.8 KB
/
graphCrawler.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import json
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from graphql import *
import argparse
import os
import subprocess
import time
import random
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url",
dest="url",
help="The Graphql endpoint URL.",
action='store',
required=True)
parser.add_argument("-o", "--output",
dest="output_path",
help="Saves schema to this filename + the url.",
action='store')
parser.add_argument("-e", "--explore",
dest="explore",
help="Explore mode will explore a domain for subdmomains with GraphQL endpoints",
action='store_true')
parser.add_argument("-i", "--inputFile",
dest="inputFile",
help="Add a file with a list of endpoints to search",
action='store')
parser.add_argument("-s", "--schemaFile",
dest="schemaFile",
help="Add a file containing the Graphql schema")
parser.add_argument("-a", "--headers",
nargs='+',
dest="headers",
help="Adds specified headers to the request",
action='store')
args = parser.parse_args()
mutationLocation = 0
bad_words = ["error", "Invalid token", "INTERNAL_SERVER_ERROR", "Unauthorized"]
sensative_words = ["users", "user", "password", "reset", "edit", "config", "file", "files", "permissions", "products", "role", "register"]
#print ascii art
print("""
██████╗ ██████╗ █████╗ ██████╗ ██╗ ██╗ ██████╗██████╗ █████╗ ██╗ ██╗██╗ ███████╗██████╗
██╔════╝ ██╔══██╗██╔══██╗██╔══██╗██║ ██║██╔════╝██╔══██╗██╔══██╗██║ ██║██║ ██╔════╝██╔══██╗
██║ ███╗██████╔╝███████║██████╔╝███████║██║ ██████╔╝███████║██║ █╗ ██║██║ █████╗ ██████╔╝
██║ ██║██╔══██╗██╔══██║██╔═══╝ ██╔══██║██║ ██╔══██╗██╔══██║██║███╗██║██║ ██╔══╝ ██╔══██╗
╚██████╔╝██║ ██║██║ ██║██║ ██║ ██║╚██████╗██║ ██║██║ ██║╚███╔███╔╝███████╗███████╗██║ ██║
╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚══╝╚══╝ ╚══════╝╚══════╝╚═╝ ╚═╝
github.com/gsmith257-cyber/GraphCrawler
From: S1n1st3r
""")
def clairvoyance(filename):
print("[+] Trying to grab the schema using Clairvoyance (this could take a while)...")
try:
a = os.system("python3 -m clairvoyance -o ./" + filename + " -w ./wordlist/google-10000-english-no-swears.txt " + args.url + " > /dev/null 2>&1")
except subprocess.TimeoutExpired:
print(f'Timeout for clairvoyance expired')
#find endpoints from urls
def graphinder(url):
#get the domain of the url
print("[+] Setting up Graphinder from escape.tech")
try:
a = os.system("docker pull escapetech/graphinder > /dev/null 2>&1")
except:
print("[-] Error pulling Graphinder, ensure you have Docker installed")
exit()
print("[+] Running Graphinder")
#run graphinder
try:
a = os.system("docker run -it --rm escapetech/graphinder -d " + url + " > results 2>&1")
a = None
except:
print("[-] Error running Graphinder")
#check if there is an output file
output = []
if os.path.isfile("results"):
with open('results','r') as file:
with open('results2','w') as f:
for line in file:
if not line.isspace():
f.write(line)
#delete every line with INF or SCS in it
counter = 0
with open("results2", "r") as f:
#skip the first 20 lines
for line in f:
if counter > 20:
if "INF" not in line and "SCS" not in line:
if counter >= 22:
print(line.strip())
output.append(line.strip())
else:
counter += 1
else:
counter += 1
if len(output) > 0:
#replace http:// with https://
output = [x.replace("http://", "https://") for x in output]
print("[+] Graphinder found the following endpoints:")
for line in output:
print(line)
return output
#open the output file
else:
print("[-] Graphinder found no endpoints")
exit()
else:
print("[-] Graphinder encountered an error")
exit()
def main(url, args):
outputFile = args.output_path
headers = args.headers
schemaFile = args.schemaFile
#add headers if needed
if headers:
headers = {header.split(":")[0]:header.split(":")[1] for header in headers}
transport = AIOHTTPTransport(url=url, headers=headers)
else:
transport = AIOHTTPTransport(url=url)
if schemaFile is None:
#get the graphql schema
client = Client(transport=transport, fetch_schema_from_transport=True)
#download the graphql schema
schema = client.schema
# grab the schema from the endpoint
introspectionQuery = gql(
"""
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
types {
...FullType
}
directives {
name
description
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
"""
)
result = None
# Execute the query on the transport
resp = "n"
print("[+] Downloading schema for " + url + " ...")
random_number = random.randint(1000, 9999 - 1)
filename = "output" + "_" + str(random_number) + ".json"
if outputFile:
filename = outputFile + ".json"
try:
result = client.execute(introspectionQuery)
with open(filename, "w") as f:
json.dump(result, f, indent=2)
new_line = '''{\n
"data": '''
with open(filename, 'r+') as file:
content = file.read()
file.seek(0)
file.write(new_line + content)
with open(filename, 'a') as file:
file.write("\n}")
except Exception as e:
print("[-] Error downloading schema, is introspection enabled?")
if "Apollo" in str(e) or "apollo" in str(e):
print("[+] Apollo server detected")
resp = input("Do you want to try to grab the schema using Clairvoyance? [y/n] ")
if resp == "y":
clairvoyance(filename)
print("Sleeping for 15 minutes while it runs...")
time.sleep(900)
print("[+] I'm awake now, let's continue...")
else:
print("[-] Exiting...")
return
else:
print("[-] Exiting...")
return
else:
#schema file supplied
if not os.path.exists(schemaFile):
print(f"[-] Supplied schema file {schemaFile} does not exist. Exiting...")
return
filename = schemaFile
#cleanup query result with json
with open(filename, "r", encoding="utf8") as f:
result = json.load(f)
#[__schema]["mutationType"]
#[__schema]["types"][' "kind": "OBJECT",']["users"]
#see if mutation type is present
mutationEnabled = False
if "mutationType" in result["data"]["__schema"]:
try:
mutation_type = result["data"]["__schema"]["mutationType"]["name"]
print("[+] Mutation enabled! Name: " + mutation_type)
mutationEnabled = True
except:
print("[-] Error getting mutation name, it could be disabled")
else:
print("[-] Mutation disabled")
i = 0
while i < 1000:
try:
if result["data"]["__schema"]["types"][i]["name"] == mutation_type:
print(f"[+] Located mutation in schema : {i}")
mutationLocation = i
print("[+] I'll leave this for you to test, scipts arent gentle with editing data")
break
i += 1
except:
break
#print out the users object from result
#loop through until you find name of Query
i = 0
while i < 1000:
try:
if result["data"]["__schema"]["types"][i]["name"] == "Query":
print(f"[+] Located queries location in schema : {i}")
break
i += 1
except:
break
sensative_locations = []
h = 0
while h < 1000:
try:
if result["data"]["__schema"]["types"][i]["fields"][h]["name"] in sensative_words:
print(f"[+] Located sensative query in schema : {h}")
sensative_locations.append(h)
h += 1
except:
break
print("[+] " + str(len(sensative_locations)) + " sensative queries found")
for j in sensative_locations:
print("[+] " + result["data"]["__schema"]["types"][i]["fields"][j]["name"] + " is a sensative query")
print("[+] Checking authorization...")
easy_queries = []
q = 0
while q < h:
try:
if result["data"]["__schema"]["types"][i]["fields"][q]["args"][0]["name"] == 1:
print("*", end="")
q += 1
except:
print(f"[+] Found easy query in schema : {q}")
easy_queries.append(q)
q += 1
#test the queries
badCount = 0
goodCount = 0
field = ""
for k in easy_queries:
name = result["data"]["__schema"]["types"][i]["fields"][k]["name"]
if "delete" in name or "Delete" in name:
prompter = input("This query, " + str(name) + ", contains 'delete'. Are you sure you want to execute?: (y/n)")
if prompter == "n":
continue
print("[+] Testing easy query : " + name)
#get the type of the query and get fields of the type
try:
type = result["data"]["__schema"]["types"][i]["fields"][k]["type"]["ofType"]["name"]
except:
type = result["data"]["__schema"]["types"][i]["fields"][k]["type"]["name"]
f = 0
while f < 1000:
try:
name2 = result["data"]["__schema"]["types"][f]["name"]
if name2 == type:
try:
field = result["data"]["__schema"]["types"][f]["fields"][0]["name"]
except:
field = None
break
f += 1
except Exception as e:
f += 1
pass
if field is None:
queryText = """
query {{{name}}}
""".format(name=name)
else:
queryText = """
query {{{name}{{{field}}}}}
""".format(name=name, field=field)
try:
query = gql(
queryText
)
except Exception as e:
print("Simple query errored out. Possibly bad read on schema")
try:
result2 = client.execute(query)
result2 = json.loads(json.dumps(result))
good2go = True
for l in bad_words:
if l in result2:
print("[-] " + l + " found in result")
good2go = False
if good2go:
print("[+] query authorized")
goodCount += 1
except Exception as e:
print("[-] Bad word found in result")
badCount += 1
if badCount > 1:
print("[-] Although introspection is enabled, most queries are not authorized it seems")
#set the criticality rating
senseCount = len(sensative_locations)
if senseCount > 4:
senseCount = 4
if len(easy_queries) > 0:
rating = (senseCount * 0.1) + (goodCount / len(easy_queries)) * 0.1
else:
rating = (senseCount * 0.1)
if mutationEnabled:
rating += 0.5
rating = round((rating*10), 2)
print("[+] Criticalilty rating : " + str(rating))
print("Do you want to get the possible paths to access the sensative node?")
print("example: username from the user field")
resp = input("(y/n): ")
if resp == "y":
x = True
while x:
type = input("Type of the sensative node: ")
print("[+] Getting possible paths to " + type)
os.system("./support/graphql-path-enum -i " + filename + " -t " + type)
resp = input("Do you want to get more paths? (y/n): ")
if resp == "n":
x = False
print("Do you want me to list the queries that are available from the schema?")
resp = input("(y/n): ")
if resp == "y":
locations = []
h = 0
while h < 1000:
try:
print(result["data"]["__schema"]["types"][i]["fields"][h]["name"])
h += 1
except:
break
print("[+] " + str(h) + " queries found")
print("Do you want me to list the mutations that are available from the schema?")
resp = input("(y/n): ")
if resp == "y":
locations = []
h = 0
while h < 1000:
try:
print(result["data"]["__schema"]["types"][mutationLocation]["fields"][h]["name"])
h += 1
except:
break
print("[+] " + str(h) + " mutations found")
print("[-] Exiting...")
return
#critical 9+
#high 7+
#medium 5+
#low 1-5
if __name__ == "__main__":
if args.explore:
output = graphinder(args.url)
if output is not None:
for url in output:
main(url, args)
print("[+] Done")
exit()
else:
if args.inputFile is not None:
try:
with open(args.inputFile, 'r') as f:
for line in f:
main(line.strip(), args)
except Exception as e:
print(e)
else:
main(args.url, args)
print("[+] Done")
exit()