-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
339 lines (276 loc) · 11.8 KB
/
scanner.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
import requests
from bs4 import BeautifulSoup
import subprocess
import validators
import bleach
import os
import logging
from Wappalyzer import Wappalyzer, WebPage
from geopy.geocoders import Nominatim
from fpdf import FPDF
FLAGS = {
"Unknown": "🌐",
"AD": "🇦🇩",
"AE": "🇦🇪",
"AF": "🇦🇫",
"TN": "🇹🇳",
}
logging.basicConfig(filename='scan.log', level=logging.INFO)
def is_valid_url(url):
return validators.url(url)
def get_ip_info(url):
try:
ip_info = requests.get(f'https://ipinfo.io/{url}/json').json()
return {
"IP": ip_info['ip'],
"City": ip_info['city'],
"Region": ip_info['region'],
"Country": ip_info['country'],
"Flag": ip_info["country"]
}
except requests.exceptions.RequestException as e:
logging.error("An error occurred while getting IP information: %s", str(e))
return {
"IP": "Unknown",
"City": "Unknown",
"Region": "Unknown",
"Country": "Unknown",
"Flag": "🌐"
}
def get_server_location(url):
ip_info = get_ip_info(url)
if ip_info["IP"] != "Unknown":
geolocator = Nominatim(user_agent="your_app_name") # Replace "your_app_name" with a unique user agent
location = geolocator.geocode(ip_info["IP"])
if location:
return {
"Latitude": location.latitude,
"Longitude": location.longitude,
"Address": location.address
}
return {
"Latitude": "Unknown",
"Longitude": "Unknown",
"Address": "Unknown"
}
def scan_website(url):
if not is_valid_url(url):
logging.error("Invalid URL provided: %s", url)
print("Invalid URL. Please provide a valid URL.")
return []
try:
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
links = []
for anchor in soup.find_all('a'):
link = anchor.get('href')
if link:
sanitized_link = bleach.clean(link, tags=[], attributes={})
links.append(sanitized_link)
return links
else:
logging.error("Failed to retrieve URL: %s, Status code: %s", url, response.status_code)
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
except requests.exceptions.RequestException as e:
logging.error("An error occurred while scanning: %s", str(e))
print(f"An error occurred: {e}")
def scan_for_malware(url):
if not is_valid_url(url):
logging.error("Invalid URL provided: %s", url)
print("Invalid URL. Please provide a valid URL.")
return "Invalid URL. Please provide a valid URL."
try:
response = requests.get(url)
if response.status_code == 200:
content = response.content
with open('temp_file', 'wb') as temp_file:
temp_file.write(content)
result = subprocess.run(['clamscan', 'temp_file'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
if "Infected files: 0" in result.stdout:
logging.info("No malware found on URL: %s", url)
print(f"No malware found on {url}")
return "No malware found"
else:
logging.warning("Malware found on URL: %s:\n%s", url, result.stdout)
print(f"Malware found on {url}:\n{result.stdout}")
return f"Malware found:\n{result.stdout}"
else:
logging.error("Failed to retrieve URL: %s, Status code: %s", url, response.status_code)
print(f"Failed to retrieve {url}. Status code: {response.status_code}")
return f"Failed to retrieve {url}. Status code: {response.status_code}"
except requests.exceptions.RequestException as e:
logging.error("An error occurred while scanning: %s", str(e))
print(f"An error occurred: {e}")
return f"An error occurred: {e}"
def detect_technologies(url):
try:
webpage = WebPage.new_from_url(url)
wappalyzer = Wappalyzer.latest()
technologies = wappalyzer.analyze(webpage)
tech_info = []
for tech in technologies:
tech_info.append({
"name": tech["name"],
"version": tech.get("version", "N/A")
})
return tech_info
except Exception as e:
return f"Error detecting technologies: {str(e)}"
def check_for_config_files(url):
if not is_valid_url(url):
logging.error("Invalid URL provided: %s", url)
print("Invalid URL. Please provide a valid URL.")
return []
common_config_files = [
"robots.txt",
".htaccess",
"wp-config.php",
"config.php",
"web.config",
]
found_config_files = []
for file in common_config_files:
config_url = f"{url}/{file}"
try:
response = requests.get(config_url)
response.raise_for_status()
if response.status_code == 200:
logging.info("Found a potentially sensitive configuration file: %s", config_url)
found_config_files.append(config_url)
except requests.exceptions.RequestException as e:
logging.error("An error occurred while checking for config file: %s", str(e))
return found_config_files
def sql_injection_test(url):
payload = {
"username": "admin' OR '1'='1",
"password": "password"
}
response = requests.post(url, data=payload)
if "Login successful" in response.text:
logging.warning("SQL injection test successful")
print("SQL injection successful")
return "SQL injection successful"
else:
logging.info("SQL injection test failed")
print("SQL injection failed")
return "SQL injection failed"
def get_waf_info(url):
try:
response = requests.get(url)
headers = response.headers
waf_info = headers.get('X-WebApplication-Firewall-Info')
if waf_info:
return waf_info
else:
return "No WAF information found in headers"
except requests.exceptions.RequestException as e:
logging.error("An error occurred while getting WAF information: %s", str(e))
return "Error getting WAF information"
def create_pdf_report(website_url, links, malware_result, config_files_result, sql_injection_result, ip_info,
technologies_result, waf_info, server_location):
class PDF(FPDF):
def header(self):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Website Security Scan Report', align='C', ln=True)
self.set_font('Arial', '', 10)
self.cell(0, 10, f'Website URL: {website_url}', ln=True)
def chapter_title(self, title):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, title, ln=True)
def chapter_body(self, body):
self.set_font('Arial', '', 12)
if body is not None:
self.multi_cell(0, 10, body.encode('latin1', 'ignore').decode('latin1'))
else:
self.multi_cell(0, 10, "No information available")
def chapter_table(self, headers, data):
self.set_font('Arial', 'B', 12)
for header in headers:
self.cell(40, 10, header, 1)
self.ln()
self.set_font('Arial', '', 12)
for row in data:
for col in row:
self.cell(40, 10, col, 1)
self.ln()
def chapter_config_files(self, config_files):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Potentially Sensitive Configuration Files', ln=True)
self.set_font('Arial', '', 12)
for config_file in config_files:
self.cell(0, 10, config_file, ln=True)
def chapter_waf_info(self, waf_info):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Web Application Firewall (WAF) Information', ln=True)
self.set_font('Arial', '', 12)
self.multi_cell(0, 10, waf_info.encode('latin1', 'ignore').decode('latin1'))
def chapter_technologies(self, technologies):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Detected Technologies', ln=True)
self.set_font('Arial', '', 12)
if not technologies or not isinstance(technologies, list):
self.cell(0, 10, 'No technologies detected', ln=True)
else:
for tech in technologies:
self.cell(0, 10, f"Technology: {tech['name']}, Version: {tech['version']}", ln=True)
def chapter_server_location(self, location):
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Server Location Information', ln=True)
self.set_font('Arial', '', 12)
self.chapter_table(["Latitude", "Longitude", "Address"],
[[location["Latitude"], location["Longitude"], location["Address"]]])
pdf = PDF()
pdf.add_page()
pdf.chapter_title('Links Found on the Website')
for link in links:
pdf.chapter_body(link)
pdf.chapter_title('Malware Scan Result')
pdf.chapter_body(malware_result)
pdf.chapter_title('Potentially Sensitive Configuration Files')
if config_files_result is not None:
pdf.chapter_config_files(config_files_result)
else:
pdf.chapter_body("No information available")
pdf.chapter_waf_info(waf_info)
pdf.chapter_technologies(technologies_result)
server_location = get_server_location(website_url)
pdf.chapter_server_location(server_location)
pdf.chapter_title('IP Information')
headers = ["IP", "City", "Region", "Country", "Flag"]
data = [
[ip_info["IP"], ip_info["City"], ip_info["Region"], ip_info["Country"], ip_info["Flag"]]
]
pdf.chapter_table(headers, data)
filename = ''.join(c for c in website_url if c.isalnum() or c in ('-', '_', '.'))
pdf_file_name = f"{filename}_report.pdf"
pdf.output(pdf_file_name)
return pdf_file_name
if __name__ == '__main__':
website_url = input("Enter the website URL to scan: ")
with open('temp_file', 'wb') as temp_file:
temp_file.write(b"")
os.chmod('temp_file', 0o600)
links = scan_website(website_url)
if links:
print(f"Links found on {website_url}:\n")
for link in links:
print(link)
print("\nScanning for malware...\n")
malware_res = scan_for_malware(website_url)
print("\nChecking for common configuration files...\n")
config_files_result = check_for_config_files(website_url)
print("\nPerforming SQL injection test...\n")
sql_injection_result = sql_injection_test(website_url)
ip_info = get_ip_info(website_url)
waf_info = get_waf_info(website_url)
technologies_result = detect_technologies(website_url)
print("\nGenerating PDF report...\n")
server_location = get_server_location(website_url)
pdf_file_name = create_pdf_report(website_url, links, malware_res, config_files_result,
sql_injection_result, ip_info, technologies_result, waf_info, server_location)
print(f"PDF report generated: {pdf_file_name}")
else:
print(f"No links found on {website_url}")
os.remove('temp_file')