Esempio n. 1
0
def process(filename, args, detector_classes, printer_classes):
    """
    The core high-level code for running Slither static analysis.

    Returns:
        list(result), int: Result list and number of contracts analyzed
    """
    slither = Slither(filename, args.solc, args.disable_solc_warnings, args.solc_args)

    for detector_cls in detector_classes:
        slither.register_detector(detector_cls)

    for printer_cls in printer_classes:
        slither.register_printer(printer_cls)

    analyzed_contracts_count = len(slither.contracts)

    results = []

    if printer_classes:
        slither.run_printers()  # Currently printers does not return results

    elif detector_classes:
        detector_results = slither.run_detectors()
        detector_results = [x for x in detector_results if x]  # remove empty results
        detector_results = [item for sublist in detector_results for item in sublist]  # flatten

        results.extend(detector_results)

    return results, analyzed_contracts_count
# Dictionary which counts vulnerabilities found on contracts by type
statistics = {"front-running": 0}

directory = argv[1]
for filename in os.listdir(directory):
    if filename.endswith(".sol"):  # only run procedure on Solidity files
        filePath = os.path.join(directory, filename)
        logger.info("Analyzing file: " + filePath)

        try:  # in case of compiling errors just skip the contract (and don't count it as processed)
            # Load contracts from file
            slither = Slither(filePath)

            # Register detector
            slither.register_detector(FrontRunning)

            # Start detecting
            detector_results = slither.run_detectors()
            detector_results = [x for x in detector_results
                                if x]  # remove empty results

            # Update contract count (here I can safely assume the contract is successfully processed
            contract_processed += len(slither.contracts)
            # Update vulnerabilities found to statistics dict
            for sublist in detector_results:
                for item in sublist:
                    detector = item["check"]
                    logger.info("Found vulnerability to: {}".format(detector))
                    statistics.update({detector: statistics[detector] + 1})