Limit tab completion of scan command to scans with installed components (#74)

* scans without installed requirements dont tab-complete

* added meets_requirements functions to classes

* updated tests

* added check for None case
This commit is contained in:
epi052
2020-06-28 15:33:34 -05:00
committed by GitHub
parent 6ed51a19be
commit 4d1aef2d34
14 changed files with 179 additions and 45 deletions

View File

@@ -1,4 +1,6 @@
import sys
import pickle
import typing
import inspect
import pkgutil
import importlib
@@ -6,6 +8,16 @@ import ipaddress
from pathlib import Path
from collections import defaultdict
from ..recon.config import defaults
def get_tool_state() -> typing.Union[dict, None]:
""" Load current tool state from disk. """
tools = Path(defaults.get("tools-dir")) / ".tool-dict.pkl"
if tools.exists():
return pickle.loads(tools.read_bytes())
def get_scans():
""" Iterates over the recon package and its modules to find all of the classes that end in [Ss]can.
@@ -42,10 +54,19 @@ def get_scans():
if inspect.ismodule(obj) and not name.startswith("_"):
# we're only interested in modules that don't begin with _ i.e. magic methods __len__ etc...
for subname, subobj in inspect.getmembers(obj):
if inspect.isclass(subobj) and subname.lower().endswith("scan"):
# now we only care about classes that end in [Ss]can
scans[subname].append(f"{__package__}.{name}")
for sub_name, sub_obj in inspect.getmembers(obj):
# now we only care about classes that end in [Ss]can
if inspect.isclass(sub_obj) and sub_name.lower().endswith("scan"):
# final check, this ensures that the tools necessary to AT LEAST run this scan are present
# does not consider upstream dependencies
try:
if not sub_obj.meets_requirements():
continue
except AttributeError:
# some scan's haven't implemented meets_requirements yet, silently allow them through
pass
scans[sub_name].append(f"{__package__}.{name}")
return scans