-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_headers.py
More file actions
493 lines (429 loc) · 18.2 KB
/
Copy pathscan_headers.py
File metadata and controls
493 lines (429 loc) · 18.2 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import datetime
import os
import re
import requests
import asyncio
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler which logs even debug messages
fh = logging.FileHandler('scan_headers.log')
fh.setLevel(logging.DEBUG)
# create a console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create a formatter and set the formatter for the handlers
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(funcName)s - %(message)s', datefmt='%d-%b-%Y %H:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
API_HEADERS_DOCTOR = "https://api.headers.doctor"
def save_uuid(uuid: str, port:int, temp: str):
"""
Saves a given UUID and port to a file named 'uuids.txt' in the 'temp' directory.
Args:
uuid (str): The UUID to be saved.
port (int): The port associated with the UUID.
Returns:
None
"""
try:
logger = logging.getLogger(__name__)
with open(f"{temp}/uuids.txt", "a") as f:
f.write(f"{uuid}:{port}\n")
except FileNotFoundError as e:
logger.error(f"Error: {e}")
except Exception as e:
logger.error(f"Error: {e}")
def save_not_valid_url(url: str, port: int, temp: str):
"""
Saves a given URL and port to a file named 'not_valid_urls.txt' in the 'temp' directory.
Args:
url (str): The URL to be saved.
port (int): The port associated with the URL.
Returns:
None
"""
try:
logger = logging.getLogger(__name__)
path = f"{temp}/not_valid_urls.txt"
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path, "a") as f:
f.write(f"{url}:{port}\n")
except FileNotFoundError as e:
logger.error(f"Error: {e}")
except Exception as e:
logger.error(f"Error: {e}")
def save_scan_result(response: any, path: str):
"""
Saves a given response to a JSON file in the given path.
Args:
response (any): The response to be saved.
path (str): The path to the directory where the file will be saved.
Returns:
None
"""
try:
logger = logging.getLogger(__name__)
response = response[0]
url = response['url']
port = response['port']
date_time = response['date']
# if not os.path.exists(path):
directory = os.path.join(path, f"{url}_{port}")
os.makedirs(directory, exist_ok=True)
# raise ValueError(f"Path {path} does not exist")
file_path = os.path.join(directory, f"{date_time}.json")
with open(file_path, "w") as f:
import json
json.dump(response, f, indent=4)
except ValueError as e:
logger.error(f"Error: {e}")
except FileNotFoundError as e:
logger.error(f"Error: {e}")
except Exception as e:
logger.error(f"Error: {e}")
def write_response(response: any, temp: str, url: str = '', port: int = 0, path: str = 'scan'):
"""
Writes a response to a file based on the provided parameters.
Args:
response (any): The response to be written.
url (str): The URL associated with the response. Defaults to an empty string.
port (int): The port associated with the URL. Defaults to 0.
path (str): The path to the directory where the file will be saved. Defaults to 'scan'.
Returns:
None
"""
if url == '' and port == 0:
save_scan_result(response, path)
else:
if response is not None:
save_uuid(response['scan_id'], port, temp)
else:
save_not_valid_url(url, port, temp)
def scan_url(url, port):
"""
Scans a given URL and port and returns the result.
Args:
url (str): The URL to be scanned.
port (int): The port associated with the URL.
Returns:
dict: The result of the scan.
"""
try:
logger = logging.getLogger(__name__)
response = requests.post(
f"{API_HEADERS_DOCTOR}/results/scan-hostname",
headers={'Content-Type': 'application/json',},
params={
"hostname": url,
"port": port
},
)
if response.status_code == 200:
logger.info(f"Request queued for scan {url}:{port}. Status code: {response.status_code}")
return response.json()
else:
logger.error(f"It was not possible to queue {url}:{port}. Status code: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"It was not possible to queue {url}:{port}. Request exception: {e}")
return None
except Exception as e:
logger.error(f"It was not possible to queue {url}:{port}. Error: {e}")
return None
def validate_hostname(hostname: str) -> bool:
"""
Validates a given hostname.
Args:
hostname (str): The hostname to be validated.
Returns:
bool: True if the hostname is valid, False otherwise.
"""
try:
logger = logging.getLogger(__name__)
pattern1 = r"(?i)^(?:(?:https?://)?(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7}))?$"
pattern2 = r"(?i)^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,15})$"
if re.match(pattern1, hostname):
return True
if re.match(pattern2, hostname):
return True
return False
except Exception as e:
logger.error(f"(validate_hostname) Error: {e}")
return False
def format_url(url: str, port: int = 443) -> tuple[dict, int] | None:
"""
Formats a given URL and port and returns the result of scanning the URL.
Args:
url (str): The URL to be formatted.
port (int): The port associated with the URL. Defaults to 443.
Returns:
tuple[dict, int] | None: A tuple containing the result of the scan and the port.
If the URL is invalid, returns None.
"""
try:
logger = logging.getLogger(__name__)
if not validate_hostname(url):
logger.error("Error: Invalid URL")
return None
if url.endswith('/'):
url = url[:-1]
if url.startswith("http://"):
port = 80
url = url.replace("http://", "")
if url.startswith("https://"):
port = 443
url = url.replace("https://", "")
return scan_url(url, port), port
except ValueError as e:
logger.error(f"Error: {e}")
async def scan_file(file_path: str, temp:str, port: int = None):
"""
Scans a given file and writes the results to a file or saves them in a temporary file.
Args:
file_path (str): The path to the file to be scanned.
port (int): The port associated with the URL. Defaults to None.
Returns:
None
"""
logger = logging.getLogger(__name__)
logger.info(f"Scanning file {file_path}")
# Almacenar temporalmente los uuids en un archivo
# Almacenar temporalmente las urls que no funcionan en otro archiv
def in_case_no_port(url: str):
"""
Scans a given URL without a port and writes the result to a file or saves it in a temporary file.
Args:
url (str): The URL to be scanned.
Returns:
None
"""
logger.info(f"Scanning {url}")
if ":" in url:
port = int(url.split(":")[1])
url = url.split(":")[0]
response, _ = format_url(url, port)
write_response(response, temp, url, port)
elif url.startswith("http://") or url.startswith("https://"):
response, _port = format_url(url)
write_response(response, temp, url, _port)
else:
port = 443
response, _ = format_url(url, port)
if response is not None:
save_uuid(response['uuid'])
else:
port = 80
response, _ = format_url(url, port)
write_response(response, temp, url, port)
def with_port(url:str, port:int):
"""
Scans a given URL with a port and writes the result to a file or saves it in a temporary file.
Args:
url (str): The URL to be scanned.
port (int): The port associated with the URL.
Returns:
None
"""
response, _ = format_url(url, port)
write_response(response, temp, url, port)
with open(file_path, 'r') as file:
for line in file:
url = line.strip()
if port is None:
in_case_no_port(url)
else:
with_port(url, port)
async def get_result(path: str=None, uuid: str=None, temp: str=None, uuid_file: str=None) -> None:
"""
Gets the result of a scan from the API.
Args:
path (str): The path to the directory where the result will be saved.
uuid (str): The UUID of the scan. If not provided, the function will
get the UUIDs from the file 'temp/uuids.txt' and get the results
for all of them.
save (bool): If True, the result will be saved to a file in the
given path. Defaults to False.
Returns:
None
"""
try:
logger = logging.getLogger(__name__)
response = None
if uuid:
logger.info(f"Getting result for {uuid}")
while response is None:
response = requests.get(
f"{API_HEADERS_DOCTOR}/results/get_result/{uuid}",
headers={"Accept": "application/json"}
)
if response.status_code == 200:
if response.json():
if path:
write_response(response.json(), temp=temp, path=path)
else:
print(response.json())
else:
response = None
elif uuid_file:
with open(uuid_file, "r+") as file:
lines = file.readlines()
for line in lines:
are_results_ready = True
line = line.strip()
uuid = line.split(":")[0]
port = line.split(":")[1]
logger.info(f"Getting result for {uuid}")
while are_results_ready:
response = requests.get(
f"{API_HEADERS_DOCTOR}/results/get_result/{uuid}",
headers={"Accept": "application/json"}
)
if response.status_code == 200:
if response.json():
if path:
write_response(response.json(), temp=temp, path=path)
are_results_ready = False
else:
print(response.json())
are_results_ready = False
else:
are_results_ready = True
else:
are_results_ready = False
else:
with open(f"{temp}/uuids.txt", "r+") as file:
lines = file.readlines()
for line in lines:
are_results_ready = True
line = line.strip()
uuid = line.split(":")[0]
port = line.split(":")[1]
while are_results_ready:
logger.info(f"Getting result for {uuid}")
response = requests.get(
f"{API_HEADERS_DOCTOR}/results/get_result/{uuid}",
headers={"Accept": "application/json"}
)
if response.status_code == 200:
if response.json():
if path:
write_response(response.json(), temp=temp, path=path)
are_results_ready = False
else:
print(response.json())
are_results_ready = False
else:
are_results_ready = True
else:
are_results_ready = False
# while True:
except ValueError as e:
logger.error(f"Error: {e}")
except FileNotFoundError as e:
logger.error(f"Error: {e}")
except requests.exceptions.RequestException as e:
logger.error(f"Error: {e}")
return None
except Exception as e:
logger.error(f"Error: {e}")
return None
def create_temp():
"""
Creates a directory named 'temp' if it does not exist.
Returns:
str: The path to the 'temp' directory.
"""
logger = logging.getLogger(__name__)
try:
logger = logging.getLogger(__name__)
today = datetime.datetime.now()
date_string = today.strftime('%Y_%m_%dT%H_%M_%S')
if not os.path.exists(f"temp_{date_string}"):
os.makedirs(f"temp_{date_string}")
return f"temp_{date_string}"
except Exception as e:
logger.error(f"Error: {e}")
async def main():
"""
This is the main function of the scan_headers script. It is an asynchronous function that
scans a website and saves the results to a file. It uses the argparse library to parse
command line arguments.
The function takes no parameters and returns no value. It uses the following command line
arguments:
- scan_by_url: The URL to scan. This is a required argument.
- port: The port to use when scanning the URL. This is an optional argument with a default
value of 443.
- file: A file containing a list of URLs to scan. This is an optional argument.
- save_response_to_file: If this argument is given, the script will save the results to a file.
- save_temp: If this argument is given, the script will save the temporary files.
- get_result_from_file: If this argument is given, the script will get the results from a file.
- get_result_from_uuid: If this argument is given, the script will get the result from a UUID.
The function prints the results of the scan to the console and saves them to a file if
requested. It also handles exceptions and cleans up temporary files.
"""
import argparse
temp_dir = create_temp()
parser = argparse.ArgumentParser(description='Scan a website and save results to file.')
parser.add_argument('-u', '--scan_by_url', type=str, nargs='?', help='URL to scan.', required=False)
parser.add_argument('-p', '--port', type=int, default=443, help='Port (default 443).', required=False)
parser.add_argument('-f', '--file', type=str, help='File with list of urls to scan.', required=False)
parser.add_argument('-s', '--save_response_to_file', help='if this param is given, scan_headers will save all the results in your_directory/results/url/url_port.json else it only print the results.', required=False)
parser.add_argument('--save_temp', action='store_true', help='if this param is given, scan_headers will save all the temp files.', required=False)
parser.add_argument('--get_result_from_file', help='if this param is given, scan_headers will get all the results from temp/uuids.txt', required=False)
parser.add_argument('--get_result_from_uuid', type=str, help='if this param is given, scan_headers will get the result from the uuid given.', required=False)
try:
args = parser.parse_args()
except Exception as e:
logger.error(f"Error parsing arguments: {e}")
raise e
print(f"""
Scanning:
\tURL: {args.scan_by_url if args.scan_by_url else "False"}
\tPort: {args.port if args.port else "False"}
\tFile: {args.file if args.file else "False"}
\tSave response: {args.save_response_to_file if args.save_response_to_file else "False"}
\tSave temp: {args.save_temp if args.save_temp else "False"}
\tGet result from file: {args.get_result_from_file if args.get_result_from_file else "False"}
\tGet result from uuid: {args.get_result_from_uuid if args.get_result_from_uuid else "False"}
""")
try:
logger = logging.getLogger(__name__)
if args.scan_by_url:
response = format_url(args.scan_by_url, args.port)
if response is not None:
await get_result(uuid=response[0]['scan_id'], path=args.save_response_to_file, temp=temp_dir)
else:
logger.warning("Invalid URL")
elif args.file:
await asyncio.gather(scan_file(args.file, temp_dir, args.port), get_result(path=args.save_response_to_file, temp=temp_dir))
if args.get_result_from_file:
if args.save_response_to_file:
await asyncio.gather(get_result(path=args.save_response_to_file, uuid_file=args.get_result_from_file, temp=temp_dir))
else:
await asyncio.gather(get_result(uuid=args.get_result_from_uuid))
elif args.get_result_from_uuid:
if args.save_response_to_file:
await asyncio.gather(get_result(uuid=args.get_result_from_uuid, path=args.save_response_to_file, temp=temp_dir))
else:
await asyncio.gather(get_result(uuid=args.get_result_from_uuid))
except KeyboardInterrupt:
logger.info("Exiting gracefully")
except Exception as e:
logger.error(f"Error: {e}")
finally:
try:
logger = logging.getLogger(__name__)
if not args.save_temp:
import shutil
shutil.rmtree(temp_dir)
except KeyboardInterrupt:
logger.info("Exiting gracefully")
except Exception as e:
logger.error(f"Error: {e}")
if __name__ == '__main__':
asyncio.run(main())