-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from juerkkil/support_for_multiple_urls
scan multiple targets at once
- Loading branch information
Showing
7 changed files
with
130 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
if __name__ == "__main__": | ||
from .securityheaders import main | ||
from .secheaders import main | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import argparse | ||
import asyncio | ||
import json | ||
import sys | ||
|
||
|
||
from . import cmd_utils | ||
from .exceptions import SecurityHeadersException, FailedToFetchHeaders | ||
from .securityheaders import SecurityHeaders | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Scan HTTP security headers', | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('url', metavar='URL', nargs='?', default=None, type=str, help='Target URL') | ||
parser.add_argument('--target-list', dest='target_list', metavar='FILE', default=None, type=str, | ||
help='Read multiple target URLs from a file and scan them all') | ||
parser.add_argument('--max-redirects', dest='max_redirects', metavar='N', default=2, type=int, | ||
help='Max redirects, set 0 to disable') | ||
parser.add_argument('--insecure', dest='insecure', action='store_true', | ||
help='Do not verify TLS certificate chain') | ||
parser.add_argument('--json', dest='json', action='store_true', help='JSON output instead of text') | ||
parser.add_argument('--no-color', dest='no_color', action='store_true', help='Do not output colors in terminal') | ||
parser.add_argument('--verbose', '-v', dest='verbose', action='store_true', | ||
help='Verbose output') | ||
args = parser.parse_args() | ||
|
||
if not args.url and not args.target_list: | ||
print("No target url provided.", file=sys.stderr) | ||
parser.print_usage(sys.stderr) | ||
sys.exit(1) | ||
|
||
if args.url: | ||
try: | ||
res = scan_target(args.url, args) | ||
except SecurityHeadersException as e: | ||
print(e, file=sys.stderr) | ||
sys.exit(1) | ||
if args.json: | ||
print(json.dumps(res)) | ||
else: | ||
print(cmd_utils.output_text(res['target'], res['headers'], res['https'], args.no_color, args.verbose)) | ||
elif args.target_list: | ||
asyncio.run(scan_multiple_targets(args)) | ||
|
||
|
||
def async_scan_done(scan): | ||
res, args = scan.result() | ||
if 'error' in res: | ||
print(f"Scanning target {res['target']}...") | ||
print(f"Error: {res['error']}\n") | ||
else: | ||
print(cmd_utils.output_text(res['target'], res['headers'], res['https'], args.no_color, args.verbose)) | ||
|
||
|
||
def scan_target(url, args): | ||
header_check = SecurityHeaders(url, args.max_redirects, args.insecure) | ||
header_check.fetch_headers() | ||
headers = header_check.check_headers() | ||
|
||
if not headers: | ||
raise FailedToFetchHeaders("Failed to fetch headers") | ||
|
||
https = header_check.test_https() | ||
return {'target': header_check.get_full_url(), 'headers': headers, 'https': https} | ||
|
||
|
||
def scan_target_wrapper(url, args): | ||
try: | ||
# Return the args also for the callback function | ||
return scan_target(url, args), args | ||
except SecurityHeadersException as e: | ||
return {'target': url, 'error': str(e)}, args | ||
|
||
|
||
async def scan_multiple_targets(args): | ||
with open(args.target_list, encoding='utf-8') as file: | ||
targets = [line.rstrip() for line in file] | ||
|
||
targets = list(set(targets)) # Remove possible duplicates | ||
loop = asyncio.get_event_loop() | ||
tasks = [] | ||
for target in targets: | ||
task = loop.run_in_executor(None, scan_target_wrapper, target, args) | ||
if not args.json: | ||
# Output result of each scan immediately | ||
task.add_done_callback(async_scan_done) | ||
tasks.append(task) | ||
|
||
res = [] | ||
for task in tasks: | ||
await task | ||
|
||
# When json output, aggregate the results and output the json dump at the end | ||
if args.json: | ||
for t in tasks: | ||
result, _args = t.result() | ||
res.append(result) | ||
|
||
print(json.dumps(res, indent=2)) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters