Added an elapsed time calculation for both individual targets and the entire program run.

This commit is contained in:
Tib3rius 2019-09-16 15:03:46 -04:00
parent 4d79a9e340
commit e9c6273144
1 changed files with 41 additions and 59 deletions

View File

@ -12,6 +12,7 @@ import asyncio
from colorama import Fore, Style from colorama import Fore, Style
from concurrent.futures import ProcessPoolExecutor, as_completed, FIRST_COMPLETED from concurrent.futures import ProcessPoolExecutor, as_completed, FIRST_COMPLETED
import ipaddress import ipaddress
import math
import os import os
import re import re
import socket import socket
@ -108,61 +109,46 @@ def fail(*args, sep=' ', end='\n', file=sys.stderr, **kvargs):
cprint(*args, color=Fore.RED, char='!', sep=sep, end=end, file=file, frame_index=2, **kvargs) cprint(*args, color=Fore.RED, char='!', sep=sep, end=end, file=file, frame_index=2, **kvargs)
exit(-1) exit(-1)
def merge_toml(a, b, path=None): def calculate_elapsed_time(start_time):
if path is None: path = [] elapsed_seconds = round(time.time() - start_time)
for key in b:
if key in a: m, s = divmod(elapsed_seconds, 60)
if isinstance(a[key], dict) and isinstance(b[key], dict): h, m = divmod(m, 60)
merge_toml(a[key], b[key], path + [str(key)])
elif a[key] == b[key]: elapsed_time = []
pass if h == 1:
else: elapsed_time.append(str(h) + ' hour')
a[key] = b[key] elif h > 1:
else: elapsed_time.append(str(h) + ' hours')
a[key] = b[key]
return a if m == 1:
elapsed_time.append(str(m) + ' minute')
elif m > 1:
elapsed_time.append(str(m) + ' minutes')
if s == 1:
elapsed_time.append(str(s) + ' second')
elif s > 1:
elapsed_time.append(str(s) + ' seconds')
return ', '.join(elapsed_time)
port_scan_profiles_config_file = 'port-scan-profiles.toml' port_scan_profiles_config_file = 'port-scan-profiles.toml'
try: with open(os.path.join(rootdir, 'config', port_scan_profiles_config_file), 'r') as p:
with open(os.path.join(rootdir, 'config', port_scan_profiles_config_file), 'r') as p: try:
try: port_scan_profiles_config = toml.load(p)
port_scan_profiles_config = toml.load(p)
custom_port_scan_profiles_config_file = os.path.join(rootdir, 'config', 'custom-port-scan-profiles.toml') if len(port_scan_profiles_config) == 0:
if os.path.isfile(custom_port_scan_profiles_config_file): fail('There do not appear to be any port scan profiles configured in the {port_scan_profiles_config_file} config file.')
try:
with open(custom_port_scan_profiles_config_file, 'r') as c:
custom_port_scan_profiles_config = toml.load(c)
merge_toml(port_scan_profiles_config, custom_port_scan_profiles_config)
except IOError:
fail('Error: Couldn\'t load custom-port-scan-profiles.toml config file. Check the file exists and has the correct permissions.')
if len(port_scan_profiles_config) == 0: except toml.decoder.TomlDecodeError as e:
fail('There do not appear to be any port scan profiles configured in the {port_scan_profiles_config_file} config file.') fail('Error: Couldn\'t parse {port_scan_profiles_config_file} config file. Check syntax and duplicate tags.')
except toml.decoder.TomlDecodeError as e: with open(os.path.join(rootdir, 'config', 'service-scans.toml'), 'r') as c:
fail('Error: Couldn\'t parse {port_scan_profiles_config_file} or custom-port-scan-profiles.toml config file. Check syntax and duplicate tags.') try:
except IOError: service_scans_config = toml.load(c)
fail('Error: Couldn\'t load {port_scan_profiles_config_file} config file. Check the file exists and has the correct permissions.') except toml.decoder.TomlDecodeError as e:
fail('Error: Couldn\'t parse service-scans.toml config file. Check syntax and duplicate tags.')
try:
with open(os.path.join(rootdir, 'config', 'service-scans.toml'), 'r') as s:
try:
service_scans_config = toml.load(s)
custom_service_scans_config_file = os.path.join(rootdir, 'config', 'custom-service-scans.toml')
if os.path.isfile(custom_service_scans_config_file):
try:
with open(custom_service_scans_config_file, 'r') as c:
custom_service_scans_config = toml.load(c)
merge_toml(service_scans_config, custom_service_scans_config)
except IOError:
fail('Error: Couldn\'t load custom-service-scans.toml config file. Check the file exists and has the correct permissions.')
except toml.decoder.TomlDecodeError as e:
fail('Error: Couldn\'t parse service-scans.toml or custom-service-scans.toml config file. Check syntax and duplicate tags.')
except IOError:
fail('Error: Couldn\'t load service-scans.toml config file. Check the file exists and has the correct permissions.')
with open(os.path.join(rootdir, 'config', 'global-patterns.toml'), 'r') as p: with open(os.path.join(rootdir, 'config', 'global-patterns.toml'), 'r') as p:
try: try:
@ -602,16 +588,10 @@ def scan_host(target, concurrent_scans):
try: try:
loop.run_until_complete(scan_services(loop, semaphore, target)) loop.run_until_complete(scan_services(loop, semaphore, target))
elapsed_seconds = time.time() - start_time
m, s = divmod(elapsed_seconds, 60) elapsed_time = calculate_elapsed_time(start_time)
h, m = divmod(m, 60)
elapsed_time = '' info('Finished scanning target {byellow}{target.address}{rst} in {elapsed_time}')
if h > 0:
elapsed_time += h + ' hour'
info('Finished scanning target {byellow}{target.address}{rst}')
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(1) sys.exit(1)
@ -757,6 +737,7 @@ if __name__ == '__main__':
sys.exit(1) sys.exit(1)
with ProcessPoolExecutor(max_workers=args.concurrent_targets) as executor: with ProcessPoolExecutor(max_workers=args.concurrent_targets) as executor:
start_time = time.time()
futures = [] futures = []
for address in targets: for address in targets:
@ -772,4 +753,5 @@ if __name__ == '__main__':
executor.shutdown(wait=False) executor.shutdown(wait=False)
sys.exit(1) sys.exit(1)
info('{bgreen}Finished scanning all targets!{rst}') elapsed_time = calculate_elapsed_time(start_time)
info('{bgreen}Finished scanning all targets in {elapsed_time}!{rst}')