MCPcopy Index your code
hub / github.com/AutoRecon/AutoRecon / port_scan

Function port_scan

autorecon/main.py:269–332  ·  view source on GitHub ↗
(plugin, target)

Source from the content-addressed store, hash-verified

267 return semaphore
268
269async def port_scan(plugin, target):
270 if config['ports']:
271 if config['ports']['tcp'] or config['ports']['udp']:
272 target.ports = {'tcp':None, 'udp':None}
273 if config['ports']['tcp']:
274 target.ports['tcp'] = ','.join(config['ports']['tcp'])
275 if config['ports']['udp']:
276 target.ports['udp'] = ','.join(config['ports']['udp'])
277 if plugin.specific_ports is False:
278 warn('Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} cannot be used to scan specific ports, and --ports was used. Skipping.', verbosity=2)
279 return {'type':'port', 'plugin':plugin, 'result':[]}
280 else:
281 if plugin.type == 'tcp' and not config['ports']['tcp']:
282 warn('Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} is a TCP port scan but no TCP ports were set using --ports. Skipping', verbosity=2)
283 return {'type':'port', 'plugin':plugin, 'result':[]}
284 elif plugin.type == 'udp' and not config['ports']['udp']:
285 warn('Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} is a UDP port scan but no UDP ports were set using --ports. Skipping', verbosity=2)
286 return {'type':'port', 'plugin':plugin, 'result':[]}
287
288 async with target.autorecon.port_scan_semaphore:
289 info('Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} running against {byellow}' + target.address + '{rst}', verbosity=1)
290
291 start_time = time.time()
292
293 async with target.lock:
294 target.running_tasks[plugin.slug] = {'plugin': plugin, 'processes': [], 'start': start_time}
295
296 try:
297 result = await plugin.run(target)
298 except Exception as ex:
299 exc_type, exc_value, exc_tb = sys.exc_info()
300 error_text = ''.join(traceback.format_exception(exc_type, exc_value, exc_tb)[-2:])
301 raise Exception(cprint('Error: Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} running against {byellow}' + target.address + '{rst} produced an exception:\n\n' + error_text, color=Fore.RED, char='!', printmsg=False))
302
303 for process_dict in target.running_tasks[plugin.slug]['processes']:
304 if process_dict['process'].returncode is None:
305 warn('A process was left running after port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} against {byellow}' + target.address + '{rst} finished. Please ensure non-blocking processes are awaited before the run coroutine finishes. Awaiting now.', verbosity=2)
306 await process_dict['process'].wait()
307
308 if process_dict['process'].returncode != 0:
309 errors = []
310 while True:
311 line = await process_dict['stderr'].readline()
312 if line is not None:
313 errors.append(line + '\n')
314 else:
315 break
316 error('Port scan {bblue}' + plugin.name + ' {green}(' + plugin.slug + '){rst} ran a command against {byellow}' + target.address + '{rst} which returned a non-zero exit code (' + str(process_dict['process'].returncode) + '). Check ' + target.scandir + '/_errors.log for more details.', verbosity=2)
317 async with target.lock:
318 with open(os.path.join(target.scandir, '_errors.log'), 'a') as file:
319 file.writelines('[*] Port scan ' + plugin.name + ' (' + plugin.slug + ') ran a command which returned a non-zero exit code (' + str(process_dict['process'].returncode) + ').\n')
320 file.writelines('[-] Command: ' + process_dict['cmd'] + '\n')
321 if errors:
322 file.writelines(['[-] Error Output:\n'] + errors + ['\n'])
323 else:
324 file.writelines('\n')
325
326 elapsed_time = calculate_elapsed_time(start_time)

Callers 1

scan_targetFunction · 0.85

Calls 7

warnFunction · 0.90
infoFunction · 0.90
cprintFunction · 0.90
errorFunction · 0.90
calculate_elapsed_timeFunction · 0.85
readlineMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected