diff --git a/slither/__main__.py b/slither/__main__.py index d2c1b1127..e1afba86c 100644 --- a/slither/__main__.py +++ b/slither/__main__.py @@ -16,7 +16,7 @@ from slither.detectors.abstract_detector import (AbstractDetector, from slither.printers.abstract_printer import AbstractPrinter from slither.slither import Slither from slither.utils.colors import red -from slither.utils.command_line import output_to_markdown, output_detectors, output_printers +from slither.utils.command_line import output_to_markdown, output_detectors, output_printers, output_detectors_json logging.basicConfig() logger = logging.getLogger("Slither") @@ -384,6 +384,12 @@ def parse_args(detector_classes, printer_classes): nargs=0, default=False) + parser.add_argument('--list-detectors-json', + help=argparse.SUPPRESS, + action=ListDetectorsJson, + nargs=0, + default=False) + parser.add_argument('--compact-ast', help=argparse.SUPPRESS, action='store_true', @@ -403,6 +409,12 @@ class ListDetectors(argparse.Action): output_detectors(detectors) parser.exit() +class ListDetectorsJson(argparse.Action): + def __call__(self, parser, *args, **kwargs): + detectors, _ = get_detectors_and_printers() + output_detectors_json(detectors) + parser.exit() + class ListPrinters(argparse.Action): def __call__(self, parser, *args, **kwargs): _, printers = get_detectors_and_printers() @@ -416,6 +428,7 @@ class OutputMarkdown(argparse.Action): parser.exit() + def choose_detectors(args, all_detector_classes): # If detectors are specified, run only these ones diff --git a/slither/utils/command_line.py b/slither/utils/command_line.py index 57542901d..23a3a6e68 100644 --- a/slither/utils/command_line.py +++ b/slither/utils/command_line.py @@ -1,3 +1,4 @@ +import json from prettytable import PrettyTable from slither.detectors.abstract_detector import classification_txt @@ -70,6 +71,33 @@ def output_detectors(detector_classes): idx = idx + 1 print(table) +def output_detectors_json(detector_classes): + detectors_list = [] + for detector in detector_classes: + argument = detector.ARGUMENT + # dont show the backdoor example + if argument == 'backdoor': + continue + help_info = detector.HELP + impact = detector.IMPACT + confidence = classification_txt[detector.CONFIDENCE] + wiki = detector.WIKI + detectors_list.append((argument, help_info, impact, confidence, wiki)) + + # Sort by impact, confidence, and name + detectors_list = sorted(detectors_list, key=lambda element: (element[2], element[3], element[0])) + idx = 1 + table = [] + for (argument, help_info, impact, confidence, wiki) in detectors_list: + table.append({'index': idx, + 'check': argument, + 'description': help_info, + 'impact': classification_txt[impact], + 'confidence': confidence, + 'wiki': wiki}) + idx = idx + 1 + print(json.dumps(table)) + def output_printers(printer_classes): printers_list = [] for printer in printer_classes: