Skip to content

Commit 6ce5654

Browse files
committedAug 1, 2021
Update autorecon.py
Removed several instances of commented out code. Added exception handling for process killing code.
1 parent 2c4467c commit 6ce5654

File tree

1 file changed

+5
-36
lines changed

1 file changed

+5
-36
lines changed
 

‎autorecon.py

+5-36
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ async def execute(self, cmd, blocking=True, outfile=None, errfile=None):
124124

125125
if target.autorecon.config['verbose'] >= 1:
126126
info('Service scan {bblue}' + plugin.name + ' (' + tag + '){rst} is running the following command against {byellow}' + address + '{rst}: ' + cmd)
127-
#info('{blue}[{bright}' + address + ' ' + tag + '{srst}]{rst} Running command: ' + cmd)
128127

129128
if outfile is not None:
130129
outfile = os.path.abspath(os.path.join(target.scandir, e(outfile)))
@@ -384,15 +383,6 @@ def __init__(self):
384383
def add_argument(self, plugin, name, **kwargs):
385384
# TODO: make sure name is simple.
386385
name = '--' + plugin.slug + '.' + slugify(name)
387-
'''if 'action' in kwargs.keys() and kwargs['action'] != 'store':
388-
if kwargs['action'] in ['store_true']
389-
else:
390-
if 'metavar' not in kwargs.keys():
391-
kwargs['metavar'] = 'VALUE'
392-
393-
if 'metavar' not in kwargs.keys() and 'choices' not in kwargs.keys() and ('action' in kwargs.keys() and kwargs['action'] not in ['store_true', 'store_false']):
394-
kwargs['metavar'] = 'VALUE'
395-
'''
396386

397387
if self.argparse_group is None:
398388
self.argparse_group = self.argparse.add_argument_group('plugin arguments', description='These are optional arguments for certain plugins.')
@@ -639,7 +629,10 @@ def cancel_all_tasks(signal, frame):
639629
for target in autorecon.scanning_targets:
640630
for process_list in target.running_tasks.values():
641631
for process_dict in process_list['processes']:
642-
process_dict['process'].kill()
632+
try:
633+
process_dict['process'].kill()
634+
except ProcessLookupError: # Will get raised if the process finishes before we get to killing it.
635+
pass
643636

644637
async def start_heartbeat(target, period=60):
645638
while True:
@@ -712,24 +705,12 @@ async def service_scan(plugin, service):
712705
while True:
713706
if semaphore.locked():
714707
if semaphore != service.target.autorecon.port_scan_semaphore: # This will be true unless user sets max_scans == max_port_scans
715-
# port_scan_task_count = 0
716-
# for t in asyncio.all_tasks():
717-
# frame = inspect.getframeinfo(t.get_stack(limit=1)[0])
718-
# if frame.function == 'port_scan' and 'await plugin.run(target)' in frame.code_context[0]:
719-
# #print(frame)
720-
# port_scan_task_count += 1
721-
# print("Old Port Scan Task Count: " + str(port_scan_task_count))
722708

723709
port_scan_task_count = 0
724710
for targ in service.target.autorecon.scanning_targets:
725711
for process_list in targ.running_tasks.values():
726-
#print('Length: ' + str(len(process_list)))
727-
#for process_dict in process_list['processes']:
728712
if issubclass(process_list['plugin'].__class__, PortScan):
729-
#for process_dict in process_list['processes']:
730-
# print(str(process_dict['process'].returncode) + ': ' + process_dict['cmd'])
731713
port_scan_task_count += 1
732-
#print("New Port Scan Task Count: " + str(new_port_scan_task_count))
733714

734715
if not service.target.autorecon.pending_targets and (service.target.autorecon.config['max_port_scans'] - port_scan_task_count) >= 1: # If no more targets, and we have room, use port scan semaphore.
735716
if service.target.autorecon.port_scan_semaphore.locked():
@@ -892,8 +873,6 @@ async def scan_target(target):
892873
else:
893874
continue
894875

895-
#plugin = task.result()['plugin']
896-
#info('Port scan {bblue}' + plugin.name + ' (' + plugin.slug + '){srst}]{rst} found {bmagenta}' + service.name + '{rst} on {bmagenta}' + service.protocol + '/' + str(service.port) + '{rst} on {byellow}' + target.address + '{rst}')
897876
info('Found {bmagenta}' + service.name + '{rst} on {bmagenta}' + service.protocol + '/' + str(service.port) + '{rst} on {byellow}' + target.address + '{rst}')
898877

899878
service.target = target
@@ -1116,7 +1095,7 @@ async def main():
11161095
for key, val in global_toml.items():
11171096
if key == 'global' and isinstance(val, dict): # Process global plugin options.
11181097
for gkey, gvals in global_toml['global'].items():
1119-
if isinstance(gvals, dict):# and 'help' in gvals:
1098+
if isinstance(gvals, dict):
11201099
options = {'metavar':'VALUE'}
11211100

11221101
if 'default' in gvals:
@@ -1360,8 +1339,6 @@ async def main():
13601339

13611340
start_time = time.time()
13621341

1363-
#sys.exit(0)
1364-
13651342
pending = []
13661343
i = 0
13671344
while autorecon.pending_targets:
@@ -1385,19 +1362,11 @@ async def main():
13851362
if autorecon.pending_targets:
13861363
pending.add(asyncio.create_task(scan_target(Target(autorecon.pending_targets.pop(0), autorecon))))
13871364

1388-
#port_scan_task_count = 0
1389-
#for t in asyncio.all_tasks():
1390-
# if inspect.getframeinfo(t.get_stack(limit=1)[0]).function == 'port_scan':
1391-
# port_scan_task_count += 1
1392-
#print("Old Port Scan Task Count: " + str(port_scan_task_count))
1393-
13941365
port_scan_task_count = 0
13951366
for targ in autorecon.scanning_targets:
13961367
for process_list in targ.running_tasks.values():
13971368
if issubclass(process_list['plugin'].__class__, PortScan):
1398-
#print(process_list)
13991369
port_scan_task_count += 1
1400-
#print("New Port Scan Task Count: " + str(port_scan_task_count))
14011370

14021371
num_new_targets = math.ceil((autorecon.config['max_port_scans'] - port_scan_task_count) / port_scan_plugin_count)
14031372
if num_new_targets > 0:

0 commit comments

Comments
 (0)
Please sign in to comment.