-
Notifications
You must be signed in to change notification settings - Fork 979
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
import argparse | ||
from slither import Slither | ||
from slither.utils.colors import red | ||
import logging | ||
from .possible_paths import find_target_paths, resolve_functions, ResolveFunctionException | ||
|
||
logging.basicConfig() | ||
logging.getLogger("Slither").setLevel(logging.INFO) | ||
|
||
def parse_args(): | ||
""" | ||
Parse the underlying arguments for the program. | ||
:return: Returns the arguments for the program. | ||
""" | ||
parser = argparse.ArgumentParser(description='PossiblePaths', | ||
usage='possible_paths.py filename [contract.function targets]') | ||
|
||
parser.add_argument('filename', | ||
help='The filename of the contract or truffle directory to analyze.') | ||
|
||
parser.add_argument('targets', nargs='+') | ||
|
||
parser.add_argument('--solc', help='solc path', default='solc') | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
# ------------------------------ | ||
# PossiblePaths.py | ||
# Usage: python3 possible_paths.py filename targets | ||
# Example: python3 possible_paths.py contract.sol contract1.function1 contract2.function2 contract3.function3 | ||
# ------------------------------ | ||
# Parse all arguments | ||
args = parse_args() | ||
|
||
# Perform slither analysis on the given filename | ||
slither = Slither(args.filename, is_truffle=os.path.isdir(args.filename), solc=args.solc, disable_solc_warnings=True) | ||
|
||
try: | ||
targets = resolve_functions(slither, args.targets) | ||
except ResolveFunctionException as r: | ||
print(red(r)) | ||
exit(-1) | ||
|
||
# Print out all target functions. | ||
print(f"Target functions:") | ||
for target in targets: | ||
print(f"- {target.contract.name}.{target.full_name}") | ||
print("\n") | ||
|
||
# Obtain all paths which reach the target functions. | ||
reaching_paths = find_target_paths(slither, targets) | ||
reaching_functions = set([y for x in reaching_paths for y in x if y not in targets]) | ||
|
||
# Print out all function names which can reach the targets. | ||
print(f"The following functions reach the specified targets:") | ||
for function_desc in sorted([f"{f.contract.name}.{f.full_name}" for f in reaching_functions]): | ||
print(f"- {function_desc}") | ||
print("\n") | ||
|
||
# Format all function paths. | ||
reaching_paths_str = [' -> '.join([f"{f.contract.name}.{f.full_name}" for f in reaching_path]) for reaching_path in reaching_paths] | ||
|
||
# Print a sorted list of all function paths which can reach the targets. | ||
print(f"The following paths reach the specified targets:") | ||
for reaching_path in sorted(reaching_paths_str): | ||
print(f"{reaching_path}\n") | ||
|
||
if __name__ == '__main__': | ||
main() |