|
| 1 | +from argparse import ArgumentParser |
| 2 | +import os.path |
| 3 | + |
| 4 | + |
| 5 | +class InputHelper(object): |
| 6 | + @staticmethod |
| 7 | + def readable_file(parser, arg): |
| 8 | + if not os.path.exists(arg): |
| 9 | + parser.error("The file %s does not exist!" % arg) |
| 10 | + else: |
| 11 | + return open(arg, 'r') # return an open file handle |
| 12 | + |
| 13 | + @staticmethod |
| 14 | + def process_targets(arguments): |
| 15 | + targets = set() |
| 16 | + |
| 17 | + if arguments.target: |
| 18 | + targets.add(arguments.target) |
| 19 | + else: |
| 20 | + for target in arguments.target_list: |
| 21 | + targets.add(target.strip()) |
| 22 | + |
| 23 | + return targets |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def process_commands(arguments): |
| 27 | + commands = set() |
| 28 | + |
| 29 | + if arguments.command: |
| 30 | + commands.add(arguments.target) |
| 31 | + else: |
| 32 | + for command in arguments.command_list: |
| 33 | + commands.add(command.strip()) |
| 34 | + |
| 35 | + return commands |
| 36 | + |
| 37 | +class InputParser(object): |
| 38 | + def __init__(self): |
| 39 | + self._parser = self.setup_parser() |
| 40 | + |
| 41 | + def parse(self, argv): |
| 42 | + return self._parser.parse_args(argv) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def setup_parser(): |
| 46 | + parser = ArgumentParser() |
| 47 | + |
| 48 | + targets = parser.add_mutually_exclusive_group(required=True) |
| 49 | + |
| 50 | + targets.add_argument( |
| 51 | + '-t', dest='target', required=False, |
| 52 | + help='Specify a target or domain name.' |
| 53 | + ) |
| 54 | + |
| 55 | + targets.add_argument( |
| 56 | + '-tL', dest='target_list', required=False, |
| 57 | + help='Specify a list of targets or domain names.', |
| 58 | + metavar="FILE", |
| 59 | + type=lambda x: InputHelper.readable_file(parser, x) |
| 60 | + ) |
| 61 | + |
| 62 | + commands = parser.add_mutually_exclusive_group(required=True) |
| 63 | + commands.add_argument( |
| 64 | + '-c', dest='command', |
| 65 | + help='Specify a single command to execute.' |
| 66 | + ) |
| 67 | + |
| 68 | + commands.add_argument( |
| 69 | + '-cL', dest='command_list', required=False, |
| 70 | + help='Specify a list of commands to execute', |
| 71 | + metavar="FILE", |
| 72 | + type=lambda x: InputHelper.readable_file(parser, x) |
| 73 | + ) |
| 74 | + |
| 75 | + output = parser.add_mutually_exclusive_group() |
| 76 | + output.add_argument( |
| 77 | + '-oN', dest='output_normal', |
| 78 | + help='Normal output printed to a file when the -oN option is ' |
| 79 | + 'specified with a filename argument.' |
| 80 | + ) |
| 81 | + |
| 82 | + output.add_argument( |
| 83 | + '-oJ', dest='output_json', |
| 84 | + help='JSON output printed to a file when the -oJ option is ' |
| 85 | + 'specified with a filename argument.' |
| 86 | + ) |
| 87 | + |
| 88 | + output.add_argument( |
| 89 | + '-oG', dest='output_grepable', |
| 90 | + help='Grepable output printed to a file when the -oG option is ' |
| 91 | + 'specified with a filename argument.' |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | + parser.add_argument( |
| 96 | + '--no-color', dest='nocolor', action='store_true', default=False, |
| 97 | + help='If set then any foreground or background colours will be ' |
| 98 | + 'stripped out.' |
| 99 | + ) |
| 100 | + |
| 101 | + output_types = parser.add_mutually_exclusive_group() |
| 102 | + output_types.add_argument( |
| 103 | + '-v', '--verbose', dest='verbose', action='store_true', default=False, |
| 104 | + help='If set then verbose output will be displayed in the terminal.' |
| 105 | + ) |
| 106 | + output_types.add_argument( |
| 107 | + '--silent', dest='silent', action='store_true', default=False, |
| 108 | + help='If set only findings will be displayed and banners ' |
| 109 | + 'and other information will be redacted.' |
| 110 | + ) |
| 111 | + |
| 112 | + return parser |
0 commit comments