-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.py
342 lines (316 loc) · 10.8 KB
/
graphql.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
import argparse
import sys
import urllib3
import requests
from urllib.parse import urlparse
import json
import time
def logo():
print(f"""
[ Tool for graphql Testing ] - v1.0.1
{"-"*61}
| _ _ _ |
| ___| |_ __ _(_)_ __ __ _| | |
| / __| __/ _` | | '_ \ / _` | | |
| \__ | || (_| | | | | | (_| | | |
| |___/\__\__,_|_|_| |_|\__, |_| |
| |_| |
| |
| By:- Sumeet Darekar |
{"-"*61}
""")
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,epilog="""
EXAMPLE USAGE :
--> Scanning all vulnerabilities in tools.
python3 graphql.py -t <your_target> -a
--> For Introspection query [use -s if you want to see the output]
python3 graphql.py -t <your_target> -i
python3 graphql.py -t <your_target> -i -s [dumps the information]
--> For fuzzing for endpoints. it will use Default worldlist[graphql-wordlist.txt] you can add yours endpoints also.
python3 graphql.py -t <your_target> -f
--> For excuting your custom query. Can change the query from custom-query.txt
python3 graphql.py -t <your_target> -cq
""")
parser.add_argument('-i','--introspection', action='append',nargs='?',help=' run introspection query')
parser.add_argument('-t','--target',help=' specify the target [format : https://example.com/graphql ]')
parser.add_argument('-f','--fuzz', action='append',nargs='?',help=' fuzzing graphql endpoints [wordlist : graphql-wordlist.txt (default)]')
parser.add_argument('-s','--show', action='append',nargs='?',help=' show the output of introspection query')
parser.add_argument('-c','--checkcsrf', action='append',nargs='?',help=' check for CSRF')
parser.add_argument('-q','--query', action='append',nargs='?',help=' check for query based batching')
parser.add_argument('-cq','--custom_query', action='append',nargs='?',help=' send the custom query')
parser.add_argument('-a','--all', action='append',nargs='?',help=' Scan all vulnerabilities[ csrf, batching, introspection query ] present on tool. ')
logo()
args=parser.parse_args()
if args.target is None:
print(f"provide target")
sys.exit()
print("-"*65)
print(f"| :: URL : {args.target} ")
print("-"*65)
def intropection_query():
intro_query = """
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
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
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}"""
inter_query=False
try:
print("Checking if introspection query is allowed...")
if(inter_query==False):
response = requests.post(args.target, json=dict(query=intro_query), verify=False)
json_response = response.json()
#print(json_response['data'])
print("\033[A\033[K", end="")
if(response.json().get("data")):
inter_query = True
print("\n--> introspection query allowed\n")
if(args.show):
print(json_response['data'])
#print(response.request.body)
else:
print("introspection query not allowed")
except:
print("Error while getting the schema...")
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # disable the warning
def fuzzing_graphql():
try:
with open('graphql-wordlist.txt', "r") as f:
words = [line.strip() for line in f.readlines()]
#print(words)
print(f"Scanning the graphql endpoints/directories... \n")
for word in words:
print(f": {word}")
response = requests.get(url=f"{args.target}/{word}", verify=False)
#print(response.url)
#print(response.status_code)
if(response.status_code >= 200 and response.status_code < 304 or response.status_code==400):
print(f"\n --> Endpoint : {word} [ Response code : {response.status_code} ]")
print(f" URL : {response.url} \n\n")
print("\033[A\033[K", end="")
except :
print("Interruption Occur during scanning...")
def csrf():
result = False
try:
try:
print("\nChecking GET based CSRF...")
payload = {"query":"{ a }"}
response = requests.get(args.target, verify=False, params=payload)
json_response = response.json()
#print(response.url)
error_list = response.json()["errors"]
#print(error_list)
#print(response.json()["errors"])
print("\033[A\033[K", end="")
for error in response.json()["errors"]:
#print(error)
if "Cannot query field" in error['message']:
print("-"*80)
print("--> [ GET based CSRF might be present ] ")
print("-"*80)
result = True
words = error_list
sample1 = "undefined"
sample2 = "error"
for word in words:
if(word == sample1 or word == sample2):
print("-"*80)
print("--> [ GET based CSRF might be present ]" )
print("-"*80)
result = True
except:
print("Error during scnning")
try:
print("\nChecking POST based CSRF...")
response1 = requests.post(args.target, data=payload, verify=False)
# print(response1.url)
errors = response1.json().get("errors")
print("\033[A\033[K", end="")
for error in errors:
#print(error)
if "Cannot query field" in error['message']:
print("-"*80)
print("--> [ POST based CSRF might be present ] ")
print("-"*80)
result = True
words = errors
sample1 = "undefined"
sample2 = "error"
for word in words:
if(word == sample1 or word == sample2):
print("-"*80)
print("--> [ POST based CSRF might be present ]")
print("-"*80)
result = True
except:
print("Error during scanning")
print("Checking if x-www-urlencoded is accepted...")
payload = {"query":"{ a }"}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post(args.target, data=payload, headers=headers)
print("\033[A\033[K", end="")
if r.status_code == 200:
print("-"*80)
print("--> [ Server accept x-www-form-urlencoded data might be vulnerable to CSRF ]")
print("-"*80)
result = True
except Exception as e:
print("Error due to : ",e)
if result == False:
print(f"--> CSRF might not be present in [{response1.url}]\n")
def query_batching():
payload = """[
{
"query":"..."
},{
"query":"..."
}
,{
"query":"..."
}
,{
"query":"..."
}
]
"""
try:
print("Scanning query based batching attack...")
time.sleep(2)
headers = {'content-type': 'application/json'}
response = requests.post(args.target, data=payload, headers=headers, verify=False)
errors = response.json()
#print(errors)
#print(response.json()['errors'])
print("\033[A\033[K", end="")
if(len(response.json()['errors'])>1):
print("-"*80)
print("\n--> [ Query based batching vulnerability is present ]\n")
print(f"payload is : {payload}\n")
print("-"*80)
else:
print("\n--> Not vulnerable to query based batching attack \n")
except Exception as e:
print(f"\nSomething went wrong error : [ {e} ] \n")
def customquery():
print("Excuting your query...")
with open('custom-query.txt', "r") as f:
user_input = f.read()
print("\033[A\033[K", end="")
print("Done.\n")
print(f"payload given : {user_input}")
headers = {'content-type': 'application/json'}
print("-"*80)
print("Fetching data...")
try:
response = requests.post(args.target, json=dict(query=user_input), headers=headers,verify=False)
print("\033[A\033[K", end="")
print("Done.\n")
if(response.status_code == 200):
print(f"\n --> {response.json()}\n")
else:
print(f"Something went wrong status code : [ {response.status_code} ]\n")
except Exception as e:
print("Error : {e}")
if(args.introspection and args.checkcsrf):
intropection_query()
csrf()
elif(args.checkcsrf and args.query):
query_batching()
csrf()
elif(args.introspection):
print(intropection_query())
elif(args.fuzz):
fuzzing_graphql()
elif(args.checkcsrf):
csrf()
elif(args.query):
query_batching()
elif(args.custom_query):
customquery()
elif(args.all):
intropection_query()
csrf()
query_batching()